Module:Sandbox/Erutuon/charinsert names

This is an old revision of this page, as edited by Erutuon (talk | contribs) at 20:34, 2 February 2019 (highlight). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

local function get_all_codepoints(str)
	local codepoint_set = {}
	for codepoint in mw.ustring.gcodepoint(str) do
		codepoint_set[codepoint] = true
	end
	return require "Module:table".keysToList(codepoint_set)
end

function p.JSON_character_names(str)
	local codepoint_list = get_all_codepoints(str)
	
	local Unicode = require "Module:Unicode data"
	
	local JSON = {}
	local len = 0
	for i, codepoint in ipairs(codepoint_list) do
		local key_and_value = '"' .. codepoint .. '":"' .. Unicode.lookup_name(codepoint) .. '"'
		if len + #key_and_value > 79 then
			key_and_value = '\n' .. key_and_value
			len = 0
		end
		
		len = len + #key_and_value
		
		table.insert(JSON, key_and_value)
	end
	
	return "{" .. table.concat(JSON, ",") .. "}"
end

-- copied from [[wikt:Module:debug]]
function p.highlight(content, options)
	if type(content) == "table" then
		options = content
		options = {
			lang = options.lang or "lua",
			inline = options.inline and true
		}
		return function(content)
			return mw.getCurrentFrame():extensionTag{
				name = "syntaxhighlight",
				content = content,
				args = options
			}
		end
	else
		return mw.getCurrentFrame():extensionTag{
			name = "syntaxhighlight",
			content = content,
			args = {
				lang = options and options.lang or "lua",
				inline = options and options.inline and true or nil
			}
		}
	end
end

function p.charinsert_char_names()
	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
	
	return p.highlight(p.JSON_character_names(charinsert))
end

return p