มอดูล:encodings

จาก วิกิพจนานุกรม พจนานุกรมเสรี
local export = {}

local encoders = {}

encoders["ISO 8859-1"] = function(text)
	local ret = {}
	
	for cp in mw.ustring.gcodepoint(text) do
		if cp >= 256 then
			error("Invalid ISO 8859-1 character \"" .. mw.ustring.char(cp) .. "\".")
		end
		
		table.insert(ret, string.char(cp))
	end
	
	return table.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