Module:gloss

From Laenkea
Revision as of 20:57, 17 April 2025 by TheNightAvl (talk | contribs)
Jump to navigation Jump to search

Underlies {{gloss}}, as well as Module:gloss grid. Uses Module:form of/data.


local export = {}
local getArgs = require('Module:Arguments').getArgs

local m_data = mw.loadData("Module:form of/data")

local delimiters = "%s%p"

function export.show(frame)
	local args = getArgs(frame)
	local gloss = args[1]
	local gloss_keys = {}
	for glosslet in gloss:gmatch("[^" .. delimiters .. "]+") do -- split the string into chunks by delimiters
		if glosslet:match("^[%d%u]+$") then --[[ match chunks which are valid glosses (i.e. only contain capital letters and numbers)
			This method means that you can use capital letters in semantic glosses, such as when you need to gloss proper nouns,
			without the capitalisation being mistaken for grammatical glossing. Be wary of glossing acronyms, as “USA”, for example,
			will still be parsed as a grammatical gloss on its own.
			]]--
			local gloss_code = glosslet:lower() -- decapitalise grammatical glosses for correct display under small caps
			local glosslet_format = gloss_code:gsub("(%d+)", "<small>%1</small>") -- make numbers small too, as they are unaffected by small caps
			local l_gloss = m_data.aliases[gloss_code]
			if l_gloss then -- check valid gloss (does not necessarily check for standardised glossing practices, just whatever is in form of/data)
				local l = m_data.labels[l_gloss]
				if l then l_gloss = type(l.glossary) == "string" and l.glossary or l.label end -- check if label has glossary text and update
				glosslet_format = frame:expandTemplate{title = "hover", args = {glosslet_format, l_gloss, noline = args.noline or (args.style == nil and l and args.nolinks == nil), style = args.style}} --[[ format hover text (I spent ages agonising over the logic in excel for the conditions of this system, so here it is)
					LINKS ENABLED?	IN GLOSSARY? (L)	NOLINK=1	STYLE	NOLINE=1	LINE DISPLAYED?	FINAL BOOLEAN FOR NO LINE
					✓				✓					×			×		×			×				TRUE
					✓				×					×			×		×			✓				FALSE
					×				✓					✓			×		×			✓				FALSE
					×				×					✓			×		×			✓				FALSE
					✓				✓					×			✓		×			✓				FALSE
					✓				×					×			✓		×			✓				FALSE
					×				✓					✓			✓		×			✓				FALSE
					×				×					✓			✓		×			✓				FALSE
					✓				✓					×			×		✓			×				TRUE
					✓				×					×			×		✓			×				TRUE
					×				✓					✓			×		✓			×				TRUE
					×				×					✓			×		✓			×				TRUE
					-]]
				if l and args.nolinks == nil then glosslet_format = "[[Appendix:Glossary#" .. l_gloss .. "|" .. glosslet_format .. "]]" end
				-- format links (hover and links MUST be done in this order for the gloss' hover texts to override the links')
			end
			gloss_keys[glosslet] = '<span style="font-variant: small-caps">' .. glosslet_format .. '</span>'
		end
	end
	for search, replace in pairs(gloss_keys) do gloss = gloss:gsub(search, replace)	end
	
	return gloss
end

return export

--[[
=p.show(mw.getCurrentFrame():newChild{title="whatever",args={"sheep-NOM.SG 3S.ANIM-DAT;SG wool-GEN.SG NEG.PST-DISJ=SG Radestria.DAT | "}})
]]--