Module:also

Revision as of 02:16, 6 April 2024 by TheNightAvl (talk | contribs)

{{also}}


local export = {}

local m_links = require("Module:links")
local getArgs = require('Module:Arguments').getArgs

function export.codify(text) --removes diacritics and non-alphanumeric characters, converts to upper case
	text = mw.ustring.toNFC(text)
	text = mw.ustring.gsub(text,"[ÁÀÂÄǍĂĀÃÅĄ]","A")
	text = mw.ustring.gsub(text,"[ÆǢǼ]","Ae")
	text = mw.ustring.gsub(text,"[ĆĊĈČÇ]","C")
	text = mw.ustring.gsub(text,"[ĎĐḌÐḐ]","D")
	text = mw.ustring.gsub(text,"[ÉÈĖÊËĚĔĒẼĘẸƐƎƏỀỂỄẾỆ]","E")
	text = mw.ustring.gsub(text,"[ĠĜĞĢ]","G")
	text = mw.ustring.gsub(text,"[ĤĦḤ]","H")
	text = mw.ustring.gsub(text,"[İÍÌÎÏǏĬĪĨĮỊ]","I")
	text = mw.ustring.gsub(text,"[Ĵ]","J")
	text = mw.ustring.gsub(text,"[Ķ]","K")
	text = mw.ustring.gsub(text,"[ĹĿĽĻŁḶḸ]","L")
	text = mw.ustring.gsub(text,"[Ṃ]","M")
	text = mw.ustring.gsub(text,"[ŃŇÑŅṆŊ]","N")
	text = mw.ustring.gsub(text,"[ÓÒÔÖǑŎŌÕǪỌŐØƆ]","O")
	text = mw.ustring.gsub(text,"[Œ]","Oe")
	text = mw.ustring.gsub(text,"[ŔŘŖṚṜꝚⱤɌƦȐȒṘ]","R")
	text = mw.ustring.gsub(text,"[ŚŜŠŞȘṢ]","S")
	text = mw.ustring.gsub(text,"[ŤŢȚṬ]","T")
	text = mw.ustring.gsub(text,"[Þ]","Th")
	text = mw.ustring.gsub(text,"[ÚÙÛÜǓŬŪŨŮŲỤŰǗǛǙǕ]","U")
	text = mw.ustring.gsub(text,"[Ŵ]","W")
	text = mw.ustring.gsub(text,"[ÝŶŸỸȲ]","Y")
	text = mw.ustring.gsub(text,"[ŹŻŽ]","Z")
	text = mw.ustring.gsub(text,"[áàâäǎăāãåąắăằắẳẵặâầẩẫấậả]","a")
	text = mw.ustring.gsub(text,"[æǣǽ]","ae")
	text = mw.ustring.gsub(text,"[ćċĉčç]","c")
	text = mw.ustring.gsub(text,"[ďđḍðḑ]","d")
	text = mw.ustring.gsub(text,"[éèėêëěĕēẽęẹɛǝəềểễếệ]","e")
	text = mw.ustring.gsub(text,"[ġĝğģ]","g")
	text = mw.ustring.gsub(text,"[ĥħḥḩ]","h")
	text = mw.ustring.gsub(text,"[ıíìîïǐĭīĩįị]","i")
	text = mw.ustring.gsub(text,"[ĵ]","j")
	text = mw.ustring.gsub(text,"[ķ]","k")
	text = mw.ustring.gsub(text,"[ĺŀľļłḷḹ]","l")
	text = mw.ustring.gsub(text,"[ṃ]","m")
	text = mw.ustring.gsub(text,"[ńňñņṇŋ]","n")
	text = mw.ustring.gsub(text,"[óòôöǒŏōõǫọőøɔơồ]","o")
	text = mw.ustring.gsub(text,"[œ]","oe")
	text = mw.ustring.gsub(text,"[ŕřŗṛṝꝛɽɍʀȑȓṙ]","r")
	text = mw.ustring.gsub(text,"[śŝšşșṣ]","s")
	text = mw.ustring.gsub(text,"[ß]","ss")
	text = mw.ustring.gsub(text,"[ťţțṭ]","t")
	text = mw.ustring.gsub(text,"[þ]","th")
	text = mw.ustring.gsub(text,"[úùûüǔŭūũůųụűǘǜǚǖưứừ]","u")
	text = mw.ustring.gsub(text,"[ŵ]","w")
	text = mw.ustring.gsub(text,"[ýŷÿỹȳ]","y")
	text = mw.ustring.gsub(text,"[źżž]","z")
	text = mw.ustring.upper(text)
	text = mw.ustring.gsub(text,"%W","")
	return text
end

function export.show(frame)
	local args = getArgs(frame)
	local links = {}
	
	if args[1] then -- manual mode
		local i = 1
		while args[i] do
			table.insert(links, "'''" .. m_links.full_link{term = args[i]} .. "'''")
			i = i + 1
		end
	else -- auto mode
		local here = args["test"] or mw.title.getCurrentTitle().subpageText
		local data = mw.loadData("Module:also/data")
		
		local here_key = ""
		
		for key, pages in pairs(data) do
			if here_key == "" then
				for _, page in ipairs(pages) do
					if page == here then
						here_key = key
						mw.log("key found: " .. here_key)
						break
					end
				end
			else
				break
			end
		end
		
		if here_key == "" then
			mw.log("no key match")
			local toAdd = false
			local check_key = export.codify(here)
			
			for key, _ in pairs(data) do
				if check_key == key then
					toAdd = true
					break
				end
			end
			
			if toAdd then
				return "possibility found: " .. check_key
			else
				return ""
			end
		end
		
		for _, page in ipairs(data[here_key]) do
			if page ~= here then
				table.insert(links, "'''" .. m_links.full_link{term = page} .. "'''")	
			end
		end
	end
	
	if links[1] then
		table.sort(links, function(a,b) return a < b end)
		return "<dt><dd>''See also:'' " .. table.concat(links, ", ") .. "</dd></dt>"
	else
		return ""
	end
end

return export