Module:headword/templates: Difference between revisions

From Linguifex
Jump to navigation Jump to search
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
insert = table.insert
local export = {}
local process_params = require("Module:parameters").process


local export = {}
local headword_module = "Module:headword"
local parameters_module = "Module:parameters"
local string_utilities_module = "Module:string utilities"
 
local insert = table.insert
local require = require
local tostring = tostring
 
local function process_params(...)
process_params = require(parameters_module).process
return process_params(...)
end


local function get_args(frame)
local function get_args(frame)
Line 9: Line 19:
local list_allow_holes = {list = true, allow_holes = true}
local list_allow_holes = {list = true, allow_holes = true}
return process_params(frame:getParent().args, {
return process_params(frame:getParent().args, {
[1] = {required = true, type = "language", default = "und"},
[1] = {required = true, type = "language", template_default = "und"},
["sc"] = {type = "script"},
sc = {type = "script"},
["sort"] = true,
sort = true,
 
[2] = {required = true, template_default = "nouns"},
sccat = boolean,
noposcat = boolean,
nomultiwordcat = boolean,
nogendercat = boolean,
nopalindromecat = boolean,
nolinkhead = boolean,
autotrinfl = boolean,
altform = boolean, -- EXPERIMENTAL: see [[Wiktionary:Beer parlour/2024/June#Decluttering the altform mess]]
cat2 = true,
cat3 = true,
cat4 = true,
pagename = true,
 
head = list_allow_holes,
image = true,
id = true,
tr = list_allow_holes,
ts = list_allow_holes,
gloss = true,
g = {list = true, type = "genders", flatten = true},
["g\1qual"] = {list = true, allow_holes = true, replaced_by = false, instead = "use <q:...> or <l:...> inline modifier on a gender"},


[2] = {required = true, default = "nouns"},
["sccat"] = boolean,
["noposcat"] = boolean,
["nomultiwordcat"] = boolean,
["nogendercat"] = boolean,
["nopalindromecat"] = boolean,
["nolinkhead"] = boolean,
["autotrinfl"] = boolean,
["altform"] = boolean, -- EXPERIMENTAL: see [[Wiktionary:Beer parlour/2024/June#Decluttering the altform mess]]
["cat2"] = true,
["cat3"] = true,
["cat4"] = true,
["head"] = list_allow_holes,
["id"] = true,
["tr"] = list_allow_holes,
["ts"] = list_allow_holes,
["gloss"] = true,
["g"] = {list = true},
["g\1qual"] = list_allow_holes,
["affix"] = {list = true},
[3] = list_allow_holes,
[3] = list_allow_holes,
 
["f\1accel-form"] = list_allow_holes,
["f\1accel-form"] = list_allow_holes,
["f\1accel-translit"] = list_allow_holes,
["f\1accel-translit"] = list_allow_holes,
Line 50: Line 61:
["f\1tr"] = list_allow_holes,
["f\1tr"] = list_allow_holes,
["f\1ts"] = list_allow_holes,
["f\1ts"] = list_allow_holes,
["f\1g"] = list_allow_holes,
["f\1t"] = list_allow_holes,
["f\1qual"] = list_allow_holes,
["f\1lit"] = list_allow_holes,
["f\1pos"] = list_allow_holes,
["f\1ng"] = list_allow_holes,
["f\1g"] = {list = true, allow_holes = true, type = "genders"},
["f\1q"] = {list = true, allow_holes = true, type = "qualifier"},
["f\1qq"] = {list = true, allow_holes = true, type = "qualifier"},
["f\1qual"] = {list = true, allow_holes = true, replaced_by = false, instead = "use fNq= or fNl="},
["f\1l"] = {list = true, allow_holes = true, type = "labels"},
["f\1ll"] = {list = true, allow_holes = true, type = "labels"},
["f\1ref"] = {list = true, allow_holes = true, type = "references"},
["f\1autotr"] = boolean_list_allow_holes,
["f\1autotr"] = boolean_list_allow_holes,
["f\1nolink"] = boolean_list_allow_holes,
["f\1nolink"] = boolean_list_allow_holes,
Line 58: Line 78:


function export.head_t(frame)
function export.head_t(frame)
local m_headword = require("Module:headword")
local m_headword = require(headword_module)


local args = get_args(frame)
local args = get_args(frame)
Line 65: Line 85:
local data = {}
local data = {}
data.lang = args[1]
data.lang = args[1]
data.sc = args["sc"]
data.sc = args.sc
data.sccat = args["sccat"]
data.sccat = args.sccat
data.sort_key = args["sort"]
data.sort_key = args.sort
data.heads = args["head"]
data.heads = args.head
data.id = args["id"]
data.image = args.image
data.translits = args["tr"]
data.pagename = args.pagename
data.transcriptions = args["ts"]
data.gloss = args["gloss"]
data.genders = args["g"]
data.affix = args["affix"]


for k, v in pairs(args["gqual"]) do
data.id = args.id
if k ~= "maxindex" then
data.translits = args.tr
if data.genders[k] then
data.transcriptions = args.ts
data.genders[k] = {spec = data.genders[k], qualifiers = {v}}
data.gloss = args.gloss
else
data.genders = args.g
k = k == 1 and "" or tostring(k)
 
error(("g%squal= specified without g%s="):format(k, k))
-- TODO should throw an error if data.heads gets overwritten
end
if data.image then
end
data.heads = {"[[File:" .. data.image .. "|class=skin-invert-image]]"}
end
end


-- EXPERIMENTAL: see [[Wiktionary:Beer parlour/2024/June#Decluttering the altform mess]]
-- EXPERIMENTAL: see [[Wiktionary:Beer parlour/2024/June#Decluttering the altform mess]]
data.altform = args["altform"]
data.altform = args.altform
 
-- Part-of-speech category
-- Part-of-speech category
local pos_category = args[2]
local pos_category = args[2]
data.noposcat = args["noposcat"]
data.noposcat = args.noposcat
 
-- Check for headword aliases and then pluralize if the POS term does not have an invariable plural.
-- Check for headword aliases and then pluralize if the POS term does not have an invariable plural.
data.pos_category = m_headword.canonicalize_pos(pos_category)
data.pos_category = m_headword.canonicalize_pos(pos_category)


-- Additional categories.
-- Additional categories.
data.categories = {}
local categories = {}
data.whole_page_categories = {}
data.whole_page_categories = {}
data.nomultiwordcat = args["nomultiwordcat"]
data.nomultiwordcat = args.nomultiwordcat
data.nogendercat = args["nogendercat"]
data.nogendercat = args.nogendercat
data.nopalindromecat = args["nopalindromecat"]
data.nopalindromecat = args.nopalindromecat
table.insert(data.categories, "Contionary")


if args["cat2"] then
-- FIXME: add a minimum_index spec to [[Module:parameters]] list specs, so
insert(data.categories, data.lang:getFullName() .. " " .. args["cat2"])
-- that `cat` can be changed to a list parameter starting at index 2.
if args.cat2 then
insert(categories, data.lang:getFullName() .. " " .. args.cat2)
end
end
 
if args.cat3 then
if args["cat3"] then
insert(categories, data.lang:getFullName() .. " " .. args.cat3)
insert(data.categories, data.lang:getFullName() .. " " .. args["cat3"])
end
if args.cat4 then
insert(categories, data.lang:getFullName() .. " " .. args.cat4)
end
end


if args["cat4"] then
data.categories = categories
insert(data.categories, data.lang:getFullName() .. " " .. args["cat4"])
end


-- Headword linking
-- Headword linking
data.nolinkhead = args["nolinkhead"]
data.nolinkhead = args.nolinkhead


-- Inflected forms
-- Inflected forms
data.inflections = {enable_auto_translit = args["autotrinfl"]}
data.inflections = {enable_auto_translit = args.autotrinfl}


for i = 1, math.ceil(args[3].maxindex / 2) do
local forms = args[3]
local n = forms.maxindex / 2
for i = 1, n + n % 1 do
local infl_part = {
local infl_part = {
label    = args[3][i * 2 - 1],
label    = forms[i * 2 - 1],
accel    = args["faccel-form"][i] and {
accel    = args["faccel-form"][i] and {
form      = args["faccel-form"][i],
form      = args["faccel-form"][i],
Line 134: Line 153:
nostore  = args["faccel-nostore"][i],
nostore  = args["faccel-nostore"][i],
} or nil,
} or nil,
request  = args["frequest"][i],
request  = args.frequest[i],
enable_auto_translit = args["fautotr"][i],
enable_auto_translit = args.fautotr[i],
}
}
 
local form = {
local form = {
term          =  args[3][i * 2],
term          =  forms[i * 2],
alt          =  args["falt"][i],
alt          =  args.falt[i],
genders      =  args["fg"][i] and mw.text.split(args["fg"][i], ",") or {},
genders      =  args.fg[i],
id            =  args["fid"][i],
id            =  args.fid[i],
lang          =  args["flang"][i],
lang          =  args.flang[i],
nolinkinfl    =  args["fnolink"][i],
nolinkinfl    =  args.fnolink[i],
q             = {args["fqual"][i]},
q            =  args.fq[i],
sc            =  args["fsc"][i],
qq            =  args.fqq[i],
translit      =  args["ftr"][i],
l             = args.fl[i],
transcription =  args["fts"][i],
ll            =  args.fll[i],
refs          =  args.fref[i],
sc            =  args.fsc[i],
tr            =  args.ftr[i],
ts            =  args.fts[i],
gloss        =  args.ft[i],
lit          =  args.flit[i],
pos          =  args.fpos[i],
ng          =  args.fng[i],
}
}
 
-- If no term or alt is given, then the label is shown alone.
-- If no term or alt is given, then the label is shown alone.
if form.term or form.alt then
if form.term or form.alt then
insert(infl_part, form)
insert(infl_part, form)
else
            table.insert(data.categories, "Contionary stubs")
end
end
 
if infl_part.label == "or" then
if infl_part.label == "or" then
-- Append to the previous inflection part, if one exists
-- Append to the previous inflection part, if one exists
Line 168: Line 193:
end
end
end
end
 
return m_headword.full_headword(data)
return m_headword.full_headword(data)
end
end


return export
return export

Latest revision as of 11:42, 21 November 2025



local export = {}

local headword_module = "Module:headword"
local parameters_module = "Module:parameters"
local string_utilities_module = "Module:string utilities"

local insert = table.insert
local require = require
local tostring = tostring

local function process_params(...)
	process_params = require(parameters_module).process
	return process_params(...)
end

local function get_args(frame)
	local boolean = {type = "boolean"}
	local boolean_list_allow_holes = {type = "boolean", list = true, allow_holes = true}
	local list_allow_holes = {list = true, allow_holes = true}
	return process_params(frame:getParent().args, {
		[1] = {required = true, type = "language", template_default = "und"},
		sc = {type = "script"},
		sort = true,

		[2] = {required = true, template_default = "nouns"},
		sccat = boolean,
		noposcat = boolean,
		nomultiwordcat = boolean,
		nogendercat = boolean,
		nopalindromecat = boolean,
		nolinkhead = boolean,
		autotrinfl = boolean,
		altform = boolean, -- EXPERIMENTAL: see [[Wiktionary:Beer parlour/2024/June#Decluttering the altform mess]]
		cat2 = true,
		cat3 = true,
		cat4 = true,
		pagename = true,

		head = list_allow_holes,
		image = true,
		id = true,
		tr = list_allow_holes,
		ts = list_allow_holes,
		gloss = true,
		g = {list = true, type = "genders", flatten = true},
		["g\1qual"] = {list = true, allow_holes = true, replaced_by = false, instead = "use <q:...> or <l:...> inline modifier on a gender"},

		[3] = list_allow_holes,

		["f\1accel-form"] = list_allow_holes,
		["f\1accel-translit"] = list_allow_holes,
		["f\1accel-lemma"] = list_allow_holes,
		["f\1accel-lemma-translit"] = list_allow_holes,
		["f\1accel-gender"] = list_allow_holes,
		["f\1accel-nostore"] = boolean_list_allow_holes,
		["f\1request"] = list_allow_holes,
		["f\1alt"] = list_allow_holes,
		["f\1lang"] = {list = true, allow_holes = true, type = "language"},
		["f\1sc"] = {list = true, allow_holes = true, type = "script"},
		["f\1id"] = list_allow_holes,
		["f\1tr"] = list_allow_holes,
		["f\1ts"] = list_allow_holes,
		["f\1t"] = list_allow_holes,
		["f\1lit"] = list_allow_holes,
		["f\1pos"] = list_allow_holes,
		["f\1ng"] = list_allow_holes,
		["f\1g"] = {list = true, allow_holes = true, type = "genders"},
		["f\1q"] = {list = true, allow_holes = true, type = "qualifier"},
		["f\1qq"] = {list = true, allow_holes = true, type = "qualifier"},
		["f\1qual"] = {list = true, allow_holes = true, replaced_by = false, instead = "use fNq= or fNl="},
		["f\1l"] = {list = true, allow_holes = true, type = "labels"},
		["f\1ll"] = {list = true, allow_holes = true, type = "labels"},
		["f\1ref"] = {list = true, allow_holes = true, type = "references"},
		["f\1autotr"] = boolean_list_allow_holes,
		["f\1nolink"] = boolean_list_allow_holes,
	})
end

function export.head_t(frame)
	local m_headword = require(headword_module)

	local args = get_args(frame)

	-- Get language and script information
	local data = {}
	data.lang = args[1]
	data.sc = args.sc
	data.sccat = args.sccat
	data.sort_key = args.sort
	data.heads = args.head
	data.image = args.image
	data.pagename = args.pagename

	data.id = args.id
	data.translits = args.tr
	data.transcriptions = args.ts
	data.gloss = args.gloss
	data.genders = args.g

	-- TODO should throw an error if data.heads gets overwritten
	if data.image then
		data.heads = {"[[File:" .. data.image .. "|class=skin-invert-image]]"}
	end

	-- EXPERIMENTAL: see [[Wiktionary:Beer parlour/2024/June#Decluttering the altform mess]]
	data.altform = args.altform

	-- Part-of-speech category
	local pos_category = args[2]
	data.noposcat = args.noposcat

	-- Check for headword aliases and then pluralize if the POS term does not have an invariable plural.
	data.pos_category = m_headword.canonicalize_pos(pos_category)

	-- Additional categories.
	local categories = {}
	data.whole_page_categories = {}
	data.nomultiwordcat = args.nomultiwordcat
	data.nogendercat = args.nogendercat
	data.nopalindromecat = args.nopalindromecat

	-- FIXME: add a minimum_index spec to [[Module:parameters]] list specs, so
	-- that `cat` can be changed to a list parameter starting at index 2.
	if args.cat2 then
		insert(categories, data.lang:getFullName() .. " " .. args.cat2)
	end
	if args.cat3 then
		insert(categories, data.lang:getFullName() .. " " .. args.cat3)
	end
	if args.cat4 then
		insert(categories, data.lang:getFullName() .. " " .. args.cat4)
	end

	data.categories = categories

	-- Headword linking
	data.nolinkhead = args.nolinkhead

	-- Inflected forms
	data.inflections = {enable_auto_translit = args.autotrinfl}

	local forms = args[3]
	local n = forms.maxindex / 2
	for i = 1, n + n % 1 do
		local infl_part = {
			label    = forms[i * 2 - 1],
			accel    = args["faccel-form"][i] and {
				form      = args["faccel-form"][i],
				translit  = args["faccel-translit"][i],
				lemma     = args["faccel-lemma"][i],
				lemma_translit = args["faccel-lemma-translit"][i],
				gender    = args["faccel-gender"][i],
				nostore   = args["faccel-nostore"][i],
			} or nil,
			request  = args.frequest[i],
			enable_auto_translit = args.fautotr[i],
		}

		local form = {
			term          =  forms[i * 2],
			alt           =  args.falt[i],
			genders       =  args.fg[i],
			id            =  args.fid[i],
			lang          =  args.flang[i],
			nolinkinfl    =  args.fnolink[i],
			q             =  args.fq[i],
			qq            =  args.fqq[i],
			l             =  args.fl[i],
			ll            =  args.fll[i],
			refs          =  args.fref[i],
			sc            =  args.fsc[i],
			tr            =  args.ftr[i],
			ts            =  args.fts[i],
			gloss         =  args.ft[i],
			lit           =  args.flit[i],
			pos           =  args.fpos[i],
			ng           =  args.fng[i],
		}

		-- If no term or alt is given, then the label is shown alone.
		if form.term or form.alt then
			insert(infl_part, form)
		end

		if infl_part.label == "or" then
			-- Append to the previous inflection part, if one exists
			if #infl_part > 0 and data.inflections[1] then
				insert(data.inflections[#data.inflections], form)
			end
		elseif infl_part.label then
			-- Add a new inflection part
			insert(data.inflections, infl_part)
		end
	end

	return m_headword.full_headword(data)
end

return export