Module:Sandbox/Erutuon/charinsert names

This is an old revision of this page, as edited by Erutuon (talk | contribs) at 03:30, 2 February 2019 (for User:Erutuon/scripts/charinsert-core.js). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

local p = {}

function p.show()
	local content = mw.title.new("MediaWiki:Gadget-charinsert-core.js"):getContent()
	
	local charinsert = content:match("charinsert: (%b{})")
	if not charinsert then return "Could not find charinsert" end
	
	local charinsert_table = mw.text.jsonDecode(charinsert)
	if not charinsert_table then return "charinsert object is not valid JSON" end
	
	local codepoint_set = {}
	for menu_name, menu in pairs(charinsert_table) do
		for codepoint in mw.ustring.gcodepoint(menu) do
			if codepoint > 0x7F then
				codepoint_set[codepoint] = true
			end
		end
	end
	
	local codepoint_list = require "Module:table".setToList(codepoint_set)
	
	local Unicode = require "Module:Unicode data"
	
	local JSON = {}
	local len = 0
	for i, codepoint in ipairs(codepoints) do
		local key_and_value = '"' .. codepoint .. '":"' .. Unicode.lookup_name(codepoint) .. '"'
		if len + #key_and_value > 80 then
			key_and_value = '\n' .. key_and_value
			len = #key_and_value
		else
			len = len + #key_and_value
		end
		
		table.insert(JSON, key_and_value)
	end
	
	return "{" .. table.concat(JSON, ",") .. "}"
end

return p