Module:encodings

local export = {}

local m_str_utils = require("Module:string utilities")

local char = string.char local concat = table.concat local gcodepoint = m_str_utils.gcodepoint local insert = table.insert local u = m_str_utils.char

local encoders = {}

encoders["ISO 8859-1"] = function(text) local ret = {} for cp in gcodepoint(text) do		if cp >= 256 then error("Invalid ISO 8859-1 character \"" .. u(cp) .. "\".") end insert(ret, char(cp)) end return concat(ret) end

encoders["cp1251"] = function(text)	-- cp1251 local ret = {} local range -- 0 1 2 3 4 5 6 7 local diff2 = {[25]=57, [26]=98, [27]=98, [28]=58, [29]=40, [30]=52, [31]=56, [32]=69, [33]=95, [34]=94, [35]=93, [36]=95, [38]=77, [39]=96} local diff4 = {[5]=21, [6]=62, [7]=76, [8]=22, [9]=19, [10]=31, [11]=20, [12]=24, [13]=59, [14]=58, [15]=57, [16]=59, [18]=56, [19]=60, [68]=103, [69]=89} local diff6 = {[11]=61, [12]=61, [16]=71, [17]=71, [18]=88, [20]=73, [21]=73, [22]=90, [24]=90, [25]=90, [26]=77, [30]=97, [40]=103, [49]=110, [50]=95, [164]=228, [270]=285, [282]=329} for cp in gcodepoint(text) do		range = (1910 then cp = 63 -- '?' end insert(ret, char(cp)) end return concat(ret) end

function export.encode(text, encoding) if type(text) == "table" then local params = { [1] = {required = true, allow_empty = true}, [2] = {required = true}, }		local args = require("Module:parameters").process(text.args, params) text = args[1] encoding = args[2] end local encoder = encoders[encoding] if not encoder then error("No encoder exists for the encoding \"" .. encoding .. "\".") end return mw.uri.encode(encoder(text)) end

return export