Module:also: Difference between revisions

From Laenkea
Jump to navigation Jump to search
mNo edit summary
Tags: Mobile edit Mobile web edit Advanced mobile edit
mNo edit summary
Tags: Mobile edit Mobile web edit Advanced mobile edit
Line 6: Line 6:
function export.codify(text) --removes diacritics and non-alphanumeric characters, converts to upper case
function export.codify(text) --removes diacritics and non-alphanumeric characters, converts to upper case
text = mw.ustring.toNFC(text)
text = mw.ustring.toNFC(text)
text = mw.ustring.gsub(text,"[ÁÀÂÄǍĂĀÃÅĄ]","A")
text = mw.ustring.lower(text)
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,"[áàâäǎăāãåąắăằắẳẵặâầẩẫấậả]","a")
text = mw.ustring.gsub(text,"[æǣǽ]","ae")
text = mw.ustring.gsub(text,"[æǣǽ]","ae")

Revision as of 23:10, 10 May 2024

{{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.lower(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,"[ß]","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 termlist = mw.loadData("Module:also/data")
		
		local data = {}
		
		for _, term in ipairs(termlist) do -- sorts data into "codes"
			local code = export.codify(term)
			if data[code] == nil then data[code] = {} end
			table.insert(data[code], term)
		end
		]]--
		
		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
						break
					end
				end
			else
				break
			end
		end
		
		if here_key == "" then
			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
				mw.log("possibility found: " .. check_key)
				return "<div class=\"noprint maintenance-box maintenance-box-yellow\" style=\"background:#FFFFDD; width:{{{width|90}}}%; margin: 0.75em auto; border:1px dashed #888822; padding: 0.25em; \">\n{|\n| rowspan=\"2\" | [[File:Nuvola apps important yellow.svg|50px]]\n! style=\"text-align: left;\" | Possible <code>{{[[Template:also|also]]}}</code> group detected!\n|-\n| Please add this term to the [[Module:also/data|data set]].\n|}</div>"
			else
				return nil
			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 nil
	end
end

return export