Module:User:Erutuon/10

From Wiktionary, the free dictionary
Jump to navigation Jump to search
local export = {}

local Array = require "Module:array"

local function superscript_edit_link(title)
	return "<sup>&lbrack;[[Special:Edit/" .. title .. "|edit]]&rbrack;</sup>"
end

local function template_to_wikitext(name, parameters)
    local printed = { name }
    local i = 0
    for _, param in ipairs(parameters) do
        local k, v = unpack(param)
        local k_number = tonumber(k)
        if k_number and k_number == i + 1 then
            table.insert(printed, v)
            i = k_number
        else
            table.insert(printed, k .. "=" .. v)
        end
    end
    return "{{" .. table.concat(printed, "|") .. "}}"
end

local function format_bad_characters(bad_characters)
	local bad_character_text = Array()
	if type(bad_characters) == "string" then
		for character in mw.ustring.gmatch(bad_characters, ".") do
			local code_point = mw.ustring.codepoint(character)
			local text = "<code>" .. mw.text.nowiki(character) .. "</code>"
			if not (0x21 <= code_point and code_point <= 0x7E) then
				text = text .. " ("
				.. require "Module:Unicode data".lookup_name(code_point)
				.. ")"
			end
			bad_character_text:insert(text)
		end
	else
		for _, str in ipairs(bad_characters) do
			if str:find("[^\33-\126]") then
				for character in mw.ustring.gmatch(str, ".") do
					local code_point = mw.ustring.codepoint(character)
					local text = "<code>" .. mw.text.nowiki(character) .. "</code>"
					if not (0x21 <= code_point and code_point <= 0x7E) then
						text = text .. " ("
						.. require "Module:Unicode data".lookup_name(code_point)
						.. ")"
					end
					bad_character_text:insert(text)
				end
			else
				bad_character_text:insert("<code>" .. mw.text.nowiki(str) .. "</code>")
			end
		end
	end
	return bad_character_text:concat ", "
end

function export.show(frame)
	local content = frame.args[1] and mw.text.trim(mw.text.unstripNoWiki(frame.args[1]))
		or mw.title.new(mw.title.getCurrentTitle().fullText .. "/data")
			:getContent():match("^%s*<source.->%s*(.-)%s*</source>%s*$")
	
	local output = Array()
	local script_tag_link = require "Module:script tag link".tag_link
	local titles = Array()
	for line in mw.text.gsplit(content, "\n") do
		local ok, data = pcall(mw.text.jsonDecode, line)
		if not (ok and data) then
			mw.log("The line " .. tostring(line) .. " couldn't be decoded or did not yield a table.")
			break
		end
		titles:insert(data.title)
		output:insert("; " .. script_tag_link(data.title) .. " " .. superscript_edit_link(data.title) .. "\n")
		if data.templates then
			for _, template in ipairs(data.templates) do
				-- [ "{{m|en|term}}" ]
				if type(template) == "string" then
					output:insert(frame:extensionTag("pre", mw.text.nowiki(template)))
				-- [ { "template": [ ["1", "en"], ["2", "term"] ], link: { ... }, ... } ]
				elseif type(template) == "table" then
					local original_name, redirected_name, parameters = unpack(template.template)
					local wikitext = template_to_wikitext(original_name, parameters)
					local line = '* <code data-source="' .. mw.text.nowiki(mw.text.jsonEncode(template)) .. '">' .. mw.text.nowiki(wikitext) .. '</code>'
					if template.bad_characters then
						line = line .. ": " .. format_bad_characters(template.bad_characters)
					end
					output:insert(line)
				end
			end
		elseif data.templates_and_bad_characters then
			for i, item in ipairs(data.templates_and_bad_characters) do
				local bad_character_text = format_bad_characters(item.bad_characters)
				output:insert(
					"* <code>" .. mw.text.nowiki(item.template) .. "</code>: "
					.. bad_character_text
				)
			end
		end
	end
	return '<div class="pages-with-templates">\n' .. output:concat "\n" .. "\n</div>\n; All titles:\n<pre>" .. titles:concat "\n" .. "</pre>"
end

return export