Jump to content

Module:egy-hieroglyphs

From Wiktionary, the free dictionary

Used by {{egy-h}} to display hieroglyphs. See there for details.

Data for this module is stored in Module:egy-hieroglyphs/data.


local export = {}

local m_data = mw.loadData("Module:egy-hieroglyphs/data")

local mdc_complex_group_normalization_map = m_data.mdc_complex_group_normalization_map
local mdc_normalization_map = m_data.mdc_normalization_map
local mdc_phonogram_map = m_data.mdc_phonogram_map
local hiero_map = m_data.hiero_map
local mdc_symbol_map = m_data.mdc_symbol_map
local wikihiero_bracket_fix_map = m_data.wikihiero_bracket_fix_map
local wikihiero_word_set = m_data.wikihiero_word_set
local unavailable_glyph_image_height_map = m_data.unavailable_glyph_image_height_map

local lang = require("Module:languages").getByCode("egy")
local trackfn = require("Module:debug").track

function export.remove_comments(input)
	return mw.ustring.gsub(input, '<!%-%-(.-)%-%->', '')
end

function export.mdc_to_unicode(frame)
	local mdc = frame.args[1]
	local unicode = export.remove_comments(mdc)

	unicode = mw.ustring.gsub(unicode, ":%*", ":")
	unicode = mw.ustring.gsub(unicode, "%*%*", "%*")
	unicode = mw.ustring.gsub(unicode, " ", "-")

	unicode = mw.ustring.gsub(unicode, "x&r&t&Y1", "x:r*t:Y1")

	for pat, repl in pairs(mdc_complex_group_normalization_map) do
		unicode = unicode:gsub("%f[%w_<>]" .. pat .. "%f[^%w_<>]", repl)
	end

	for pat, repl in pairs(mdc_normalization_map) do
		unicode = unicode:gsub("%f[%w_<>]" .. pat .. "%f[^%w_<>]", repl)
	end

	unicode = mw.ustring.gsub(unicode, "%f[%w_<>](.-)%f[^%w_<>]", "<span title=\"%1\">%1</span>")
	unicode = mw.ustring.gsub(unicode, "\"<", "\"&lt;")
	unicode = mw.ustring.gsub(unicode, ">\"", "&gt;\"")

	for pat, repl in pairs(mdc_phonogram_map) do
		unicode = unicode:gsub("\"([%w_<>]+)\">" .. pat .. "<", "\"" .. repl .. " %[%1%]\">" .. repl .. "<")
	end

	for pat, repl in pairs(hiero_map) do
		unicode = unicode:gsub(">" .. pat .. "<", ">" .. repl .. "<")
	end

	unicode = mw.ustring.gsub(unicode, "%.%.", "𓑁")
	for pat, repl in pairs(mdc_symbol_map) do
		unicode = unicode:gsub(pat, repl)
	end

	unicode = "<span class=\"Egyp\" lang=\"egy\">" .. unicode .. "</span>"

	return unicode
end

function export.mdc_to_wikihiero(frame)
	local mdc = frame.args[1]
	local wikihiero = export.remove_comments(mdc)

	for pat, repl in pairs(wikihiero_bracket_fix_map) do
		wikihiero = wikihiero:gsub(pat, repl)
	end

	for glyph in string.gmatch(wikihiero, "%f[%w_%*:~]%u[%w_]+%f[^%w_%*:]") do
		if (wikihiero_word_set[glyph] == nil and mdc_phonogram_map[glyph] == nil) then
			local glyph_image_height = 93
			if unavailable_glyph_image_height_map[glyph] ~= nil then
				glyph_image_height = unavailable_glyph_image_height_map[glyph]
			end
			local glyph_height = math.min(38, math.floor(glyph_image_height * 0.41))
			local glyph_quadrat =
				"<table class=\"mw-hiero-table mw-hiero-outer\" dir=\"ltr\"><tr><td><table class=\"mw-hiero-table\"><tr><td height=\"" ..
				(glyph_height + 2) ..
				"\"><span style=\"margin: 1px;\">[[File:Abydos-Bold-hieroglyph-" ..
				glyph ..
				".png|" ..
				glyph ..
				"|x" ..
				glyph_height ..
				"px|link=|class=skin-invert]]</span></td></tr></table></td></tr></table>" -- not yet supported: mirroring, multi-glyph quadrats
			wikihiero = mw.ustring.gsub(wikihiero, "%f[%w_%*:~]" .. glyph .. "%f[^%w_%*:%.%|]",
				frame:preprocess("</hiero>" .. glyph_quadrat .. "<hiero>"))
		end
	end

	wikihiero = mw.ustring.gsub(wikihiero, "%*", "@")
	for unsupported_quadrat in string.gmatch(wikihiero, "~[%w_%@:]+%f[^%w_%@:]") do
		local quadrat = mw.ustring.gsub(unsupported_quadrat, "~", "")
		quadrat = mw.ustring.gsub(quadrat, ":", "::")
		quadrat = mw.ustring.gsub(quadrat, "@", " ")
		local quadrat_start =
			"<table class=\"mw-hiero-table mw-hiero-outer\" dir=\"ltr\"><tr><td><table class=\"mw-hiero-table\"><tr><td height=\"40\">"
		local quadrat_end = "</td></tr></table></td></tr></table>"

		for glyph in string.gmatch(quadrat, "%f[%w_][%w_]+%f[^%w_]") do
			if (wikihiero_word_set[glyph] == nil and mdc_phonogram_map[glyph] == nil) then
				local glyph_image_height = 70
				if unavailable_glyph_image_height_map[glyph] ~= nil then
					glyph_image_height = unavailable_glyph_image_height_map[glyph]
				end
				local glyph_height = math.min(29, math.floor(glyph_image_height * 0.41))
				local processed_glyph = "<span style=\"margin: 1px;\">[[File:Abydos-Bold-hieroglyph-" ..
					glyph .. ".png|" .. glyph .. "|x" .. glyph_height .. "px|link=|class=skin-invert]]</span>"
				quadrat = mw.ustring.gsub(quadrat, "%f[%w_]" .. glyph .. "%f[^%w_%.%|]", processed_glyph)
			else
				quadrat = mw.ustring.gsub(quadrat, "%f[%w_]" .. glyph .. "%f[^%w]",
					frame:preprocess("<hiero>" .. glyph .. "</hiero>"))
			end
		end

		quadrat = mw.ustring.gsub(quadrat, "::", "<br>")
		quadrat = quadrat_start .. quadrat .. quadrat_end
		wikihiero = mw.ustring.gsub(wikihiero, unsupported_quadrat, frame:preprocess("</hiero>" .. quadrat .. "<hiero>"))
	end
	wikihiero = mw.ustring.gsub(wikihiero, "@", "*")

	wikihiero = mw.ustring.gsub(wikihiero, "!", frame:preprocess("</hiero><br><hiero>"))

	-- Prevents empty hiero tables to be created.
	wikihiero = mw.ustring.gsub("<hiero>" .. wikihiero .. "</hiero>", "<hiero>%-?</hiero>", "")

	return frame:preprocess(wikihiero)
end

function track(page)
	trackfn(lang:getCode() .. "-hieroglyphs/" .. page)
	return true
end

function export.parameter_to_hiero(frame)
	local param = frame.args[1]
	if mw.ustring.find(param, "hiero") or mw.ustring.find(param, "egy%-glyph") then
		-- [[Special:WhatLinksHere/Wiktionary:Tracking/egy-hieroglyphs/wikihiero to cleanup]]
		track("wikihiero to cleanup")
		return param
	else
		return export.mdc_to_wikihiero(frame)
	end
end

return export