Module:Letters

Frae Wikipedia, the free beuk o knawledge
local export = {}

local m_parameters = require("Module:parameters")
local m_languages = require("Module:languages")
local m_links = require("Module:links")
local m_scripts = require("Module:scripts")
local m_script_utils = require("Module:script utilities")
local m_IPA = require("Module:IPA")
local m_IPAdata = mw.loadData('Module:IPA/data') 

-- Copied from Module:links/templates (function: export.l_term_t), and adapted.
local function link(term, lang, sc, face)
	-- Forward the information to full_link
	return m_links.full_link( { term = term, lang = lang, sc = sc }, face)
end

local function linkNoTag(term, lang)
	return m_links.language_link{ term = term, lang = lang }
end

local function tag(term, lang, sc)
	return m_script_utils.tag_text(term, lang, sc)
end

--The main function, that returns the formatted list of letters.
function export.letters(frame)
	local args = frame:getParent().args
	
	local namespace = mw.title.getCurrentTitle().nsText
	if namespace ~= "Appendix" then
		return export.letters2(frame)
	end

	-- The variable "result" is going to contain the end text.
	local result = {}

	-- l = the language code, such as "en" for English
	local l = args[1]
	local langObject = m_languages.getByCode(l) or m_languages.err(l, 1)
	local langName = langObject:getCanonicalName()

	-- sc = the script code, such as "Latn" for Latin
	local sc = args["sc"] or nil
	local scObject = nil
	local scName = nil

	-- The first categories.
	table.insert(result, "[[Category:Wt/sco/Letter appendices|" .. langName .. "]]")
	table.insert(result, "[[Category:Wt/sco/" .. langName .. " appendices|Letters]]")

	-- The beginning of the result text, a link to the script appendix between parentheses.
	if sc then
		scObject = m_scripts.getByCode(sc)
		scName = scObject:getCanonicalName()

		table.insert(result, "(''[[Appendix:" .. scName .. " script|" .. scName .. " script]]'') ")
		table.insert(result, "[[Category:Wt/sco/" .. scName .. " script appendices|" .. langName .. "]]")
	else
		table.insert(result, "(''? script'') ")
	end

	-- colonArgs and numberedArgs are going to be filled below.
	local colonArgs = {}
	local numberedArgs = {}

	-- k = key, it is the key to "v"
	-- v = value, each argument given in Template:letters
	for k, v in pairs(args) do

		-- It does not contain "sc=", which is a named argument.
		-- The argument "1" is not added in numberedArgs. It is the language code.
		if mw.ustring.find(v, ":", 1, true) then
			table.insert(colonArgs, args[k])
		elseif tonumber(k) ~= nil and k ~= 1 then
			table.insert(numberedArgs, args[k])
		end
	end

	-- kk = key, it is the key to "vv"
	-- vv = value, each numbered argument, like "Aa", "Bb", "Cc"
	for kk, vv in pairs(numberedArgs) do
		if kk ~= 1 then
			table.insert(result, ", ")
		end

		-- currentLetter = a single value like "Aa"
		local currentLetter = numberedArgs[kk]

		-- About "letterCases":
		-- If currentLetter contains "[" (as in: [[A]][[a]]), letterCases is a table with only the currentLetter.
		-- Otherwise, letterCases is a table with each letter, unformatted.
		local letterCases = {}
		if mw.ustring.find(currentLetter, "[", 1, true) or mw.ustring.find(currentLetter, "<", 1, true) then
			letterCases[1] = currentLetter
		else
			for i=1, mw.ustring.len(currentLetter) do
				letterCases[i] = mw.ustring.sub(currentLetter, i, i)
			end
		end

		-- kkk = key, it is the key to "vvv"
		-- vvv = value, each letter in letterCases
		for kkk, vvv in pairs(letterCases) do

			-- Formats with "link" each letter in letterCases.
			-- The result is added in the "result" text.
			table.insert(result, link(letterCases[kkk], langObject, scObject))

		end

		-- Adds the letter category, like [[Category:Wt/sco/Dd]].
		table.insert(result, "[[Category:Wt/sco/" .. currentLetter .. "|" .. langName .. "]]")
	end

	-- kkkk = key, it is the key to "vvvv"
	-- vvvv = value, it is the value, as in "Aa:⠁"
	for kkkk, vvvv in pairs(colonArgs) do

		if kkkk ~= 1 then
			table.insert(result, ", ")
		end

		local equalSignSplit = mw.text.split (vvvv, ": ", true)
		local partOne = equalSignSplit[1]
		local partTwo = equalSignSplit[2]

		-- The result is added in the "result" text.
		table.insert(result, "'''" .. partOne .. "'''" .. ": " .. link(partTwo, langObject, scObject))

		-- Adds the letter category, like [[Category:Wt/sco/Dd]].
		table.insert(result, "[[Category:Wt/sco/" .. partOne .. ": " .. partTwo .. "|" .. langName .. "]]")

	end

	return table.concat(result)
end

--Returns the list of letter names.
function export.letterNames(frame)
	local namespace = mw.title.getCurrentTitle().nsText
	if namespace ~= "Appendix" then
		return export.letterNames2(frame)
	end
	
	local args = frame:getParent().args
	local colonArgs = {}

	-- The variable "result" is going to contain the end text.
	local result = {}

	-- sc = the script code, such as "Latn" for Latin
	local sc = args["sc"] or nil

	-- l = the language code, such as "en" for English
	local l = args[1]
	local langObject = m_languages.getByCode(l) or m_languages.err(l, 1)

	for k, v in pairs(args) do
		if mw.ustring.find(v, ":", 1, true) then
			table.insert(colonArgs, args[k])
		end
	end

	-- k = key, it is the key to "v"
	-- v = value, it is the value, as in "Bb: bee"
	for k, v in pairs(colonArgs) do

		if k ~= 1 then
			table.insert(result, ", ")
		end

		local equalSignSplit = mw.text.split (v, ": ", true)
		local partOne = equalSignSplit[1]
		local partTwo = equalSignSplit[2]

		-- The result is added in the "result" text.
		table.insert(result, "'''" .. partOne .. "'''" .. ": " .. link(partTwo, langObject, scObject))

	end

	return table.concat(result)
end

local function qualifier(content)
	if content then
		return 
		'<span class="ib-brac qualifier-brac">(</span>'
		.. '<span class="ib-content qualifier-content">'
		.. content .. '</span>'
		.. '<span class="ib-brac qualifier-brac">)</span> '
	end
end
	

function export.letters2(frame)
	local output, list, categories = {}, {}, {}
	
	local namespace = mw.title.getCurrentTitle().nsText
	
	local params = {
		[1] = { list = true, required = true },
		["native name"] = {},
		["lang"] = { required = true },
		["sc"] = {},
		["qual"] = {},
	}
	
	local args = m_parameters.process(frame:getParent().args, params)
	
	local lang = m_languages.getByCode(args.lang) or m_languages.err(args.lang, "lang")
	local langName = lang:getCanonicalName()
	local sc = m_scripts.getByCode(args.sc) or m_scripts.findBestScript(text, lang) or error("Script could not be identified.")
	local scName = sc:getCanonicalName()
	
	local qualifierContent = args.qual or '[[Appendix:' .. scName .. ' script|' .. scName .. '-script]] letters'
	table.insert(
		output,
		qualifier(qualifierContent)
		)
	
	local native_name =  args["native name"]
	if native_name then
		native_name = link(native_name, lang, sc, "bold")
		table.insert(output, native_name .. "; ")
	end
	
	for i, letterParameter in ipairs(args[1]) do
		if sc:getCode() == "Latn" then
			--[[	If script is Latin, no transliteration is needed, so language span
					is applied to whole list, and individual list items are linked and not language-tagged. ]]
			local open_par, uppercase, lowercase, close_par
			
			if mw.ustring.find(letterParameter, "/") then
				open_par, uppercase, lowercase, close_par = mw.ustring.match(letterParameter, "(%(?)([^/]+)/([^%)]+)(%)?)")
			elseif mw.ustring.find(letterParameter, "%u.*%l") then
				open_par, uppercase, lowercase, close_par = mw.ustring.match(letterParameter, "^(%(?)(%u)(%l)(%)?)$")
			else
				open_par, letters, close_par = mw.ustring.match(letterParameter, "^(%(?)([^%(%)]+)(%)?)$")
			end
			
			if open_par and close_par then
				if uppercase and lowercase then	
					uppercase, lowercase = linkNoTag(uppercase, lang, sc), linkNoTag(lowercase, lang, sc)
					
					table.insert(list, open_par .. uppercase .. "&nbsp;" .. lowercase .. close_par)
				elseif letters then
					letters = linkNoTag(letters, lang, sc)
					
					table.insert(list, open_par .. letters .. close_par)
				end
			else
				letterParameter = linkNoTag(letterParameter, lang, sc)
				
				table.insert(list, letterParameter)
			end
		else
			--[[	If script is non-Latin, add transliteration to
					and tag each item on the list individually.		]]
			local letters = {}
			for letter in mw.ustring.gmatch(letterParameter, ".") do
				 table.insert(letters, "[[" .. letter .. "]]")
			end
			
			letters = table.concat(letters, "&nbsp;")
			local linkedLetters = link(letters, lang, sc)
			
			table.insert(list, linkedLetters)
		end
		
		-- For some reason, args[1].maxindex is nil.
		if args[1][i + 1] then
			table.insert(list, ", ")
		end
	end
	
	if namespace == "Template" then
		table.insert(categories, "[[Category:Wt/sco/" .. langName .. " leet templates|letters]]")
		table.insert(categories, "[[Category:Wt/sco/Chairacter leet templates|" .. langName .. "]]")
	end

	list = table.concat(list)
	--[[	Add language tagging to whole list for Latin script,
			because individual letters were not tagged.			]]
	if sc:getCode() == "Latn" then
		list = tag(list, lang, sc)
	end
	
	table.insert(output, list)
	
	return table.concat(output) .. table.concat(categories)
end

function export.letterNames2(frame)
	local output, list = {}, {}
	
	local namespace = mw.title.getCurrentTitle().nsText
	
	local params = {
		[1] = { list = true, required = true },
		["native name"] = {},
		["lang"] = { required = true },
		["sc"] = {},
		["qual"] = {},
	}
	
	local output = {}
	
	local args = m_parameters.process(frame:getParent().args, params)
	
	local lang = m_languages.getByCode(args.lang) or m_languages.err(args.lang, "lang")
	local sc = args.sc or "Latn"
	sc = m_scripts.getByCode(sc)
	
	local qualifierContent = args.qual or sc:getCanonicalName() .. "-script letter names"
	table.insert(
		output,
		qualifier(qualifierContent)
		)
	
	local native_name =  args["native name"]
	if native_name then
		native_name = link(native_name, lang, sc, "bold")
		table.insert(output, native_name .. "; ")
	end
	
	if sc:getCode() == "Latn" then
		for i, letter_name in ipairs(args[1]) do
			alternative_names = mw.text.split(letter_name, "%s*/%s*")
			
			local letterNames = {}
			for i, letter_name in ipairs(alternative_names) do
				table.insert(letterNames, linkNoTag(letter_name, lang, sc) )
				if alternative_names[i + 1] then
					table.insert(letterNames, " / ")
				end
			end
			
			table.insert(list, table.concat(letterNames))
		end
	end
	
	list = table.concat(list, ", ")
	
	table.insert(output, list)
	
	return table.concat(output)
end

function export.definition(frame)
	local parameters = {
		[1] = { list = true },
		["alphabet name"] = {},
		["name"] = {},
		["trans"] = {},
		[""] = {},
	}
	
	local args = m_parameters.process(frame:getParent().args, parameters)
	
	local types = {
		letter = {
			text = table.concat {
				'<span class="use-with-mention">',
				'The {{{ordinal}}} [[letter]] of the {{{canonical name}}} [[alphabet]]{{{alphabet name}}}, ',
				'called {{{letter names list}}} ',
				'and written in the {{{Latin script}}}.'
				},
			},
		name = {
			text = table.concat {
				'<span class="use-with-mention">',
				'The name of the {{{Latin-script}}} letter ',
				'{{{uppercase}}}{{{separator}}}{{{lowercase}}}.</span>',
				},
			},
		diacritic = {},
		ordinal = {},
		syllable = {},
	}
	
	local lang = m_languages.getByCode(args[1][1]) or m_languages.err(args[1][1], 1)
	local type = args[1][2]
	
	local alphabet_name = args["alphabet name"]
	if alphabet_name then
		alphabet_name = " (" .. alphabet_name .. ")"
	else
		alphabet_name = ""
	end
	
	local data
	
	if type then
		if types[type] then
			data = types[type]
		else
			error('The type of definition "' .. tostring(type) .. '" is not recognized.')
		end
	else
		error("Select a type of definition using parameter 2: letter, letter name, diacritic, ordinal, or syllable.")
	end
	
	local text = { data.text }
	
	if #text > 0 then
		text.add = function(self, code, item)
			if self and self[1] then
				self[1] = mw.ustring.gsub(self[1], "{{{" .. code .. "}}}", item)
			end
		end
		
		text:add("canonical name", lang:getCanonicalName() )
		text:add("Latin script", "[[Appendix:Laitin script|Laitin script]]")
		text:add("Latin-script", "[[Appendix:Laitin script|Laitin-script]]")
		text:add("alphabet name", alphabet_name)
	else
		error('The type "' .. type .. '" does not have text defined.')
	end
	
	return text[1]
end

return export