Jump to content

Module:Morse/sandbox

From Wiktionary, the free dictionary


local export = {}

local sc = require("Module:scripts").getByCode("Morse")

local charList = {
	["A"] = ".-",
	["B"] = "-...",
	["C"] = "-.-.",
	["D"] = "-..",
	["E"] = ".",
	["F"] = "..-.",
	["G"] = "--.",
	["H"] = "....",
	["I"] = "..",
	["J"] = ".---",
	["K"] = "-.-",
	["L"] = ".-..",
	["M"] = "--",
	["N"] = "-.",
	["O"] = "---",
	["P"] = ".--.",
	["Q"] = "--.-",
	["R"] = ".-.",
	["S"] = "...",
	["T"] = "-",
	["U"] = "..-",
	["V"] = "...-",
	["W"] = ".--",
	["X"] = "-..-",
	["Y"] = "-.--",
	["Z"] = "--..",
}

local headwordMap = {
	["."] = "[[File:Morse code dot.svg|class=skin-invert-image|link=]]",
	["-"] = "[[File:Morse code dash.svg|class=skin-invert-image|link=]]",
	[" "] = "[[File:60x15transparent spacer.svg|27px|link=]]",
}

local inlineMap = {
	["."] = "[[File:Morse code dot.svg|x5px|class=skin-invert-image|link=]]",
	["-"] = "[[File:Morse code dash.svg|x5px|class=skin-invert-image|link=]]",
	[" "] = "[[File:60x15transparent spacer.svg|7px|link=]]",
}

function export.convertToMorse(text)
	local result = mw.ustring.gsub(text, ".", charList)

	return result
end

function export.textToHeadword(text)
	return mw.ustring.gsub(text, "[-. ]", headwordMap)
end

function export.textToInline(text)
	return mw.ustring.gsub(text, "[-. ]", inlineMap)
end

function export.link(frame)
	local text = frame:getParent().args[1]
	local textUpper = mw.ustring.upper(text)
	local morseValues = export.convertToMorse(textUpper)
	local morseFinal = export.textToInline(morseValues)

	local result = morseFinal
	return result
end

local types = {
	["letter"] = "letters",
	["number"] = "numbers",
	["punctuation mark"] = "punctuation marks",
	["symbol"] = "symbols",
	["interjection"] = "interjections",
}

function export.headword(frame)

	local m_head = require("Module:headword")

	local type = frame.args["1"] ~= "" and frame.args["1"]
	local head = frame:getParent().args["head"]
	if not head or head == "" then
		head = mw.title.getCurrentTitle().subpageText
	end
	local langCode = frame.args["lang"] or "mul"
	local lang = require("Module:languages").getByCode(langCode)

	local display = '<span style="display:inline-block;vertical-align:middle">' .. export.textToHeadword(head) .. '</span>'
	
	local data = {lang = lang, sc = sc, categories = {}, sort_key = head, heads = {display}, translits = {"-"}}
	
	if types[type] then
		data.pos_category = types[type]
	end

	return m_head.full_headword(data)

end

return export