Module:affixusex/templates: Difference between revisions

From Linguifex
Jump to navigation Jump to search
(Created page with "local export = {} local m_languages = require("Module:languages") local rfind = mw.ustring.find local rsubn = mw.ustring.gsub -- helper functions -- version of rsubn() tha...")
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
local export = {}
local export = {}


local m_languages = require("Module:languages")
local require = require


local rfind = mw.ustring.find
local affixusex_module = "Module:affixusex"
local rsubn = mw.ustring.gsub
local parameter_utilities_module = "Module:parameter utilities"


-- helper functions
local concat = table.concat
local find = string.find
local gsub = string.gsub
local insert = table.insert
local match = string.match
local sort = table.sort
local sub = string.sub


-- version of rsubn() that discards all but the first return value
function export.affixusex_t(frame)
local function rsub(term, foo, bar)
local parent_args = frame:getParent().args
local retval = rsubn(term, foo, bar)
return retval
end


function export.affixusex_t(frame)
local params = {
local params = {
[1] = {required = true, default="und"},
[1] = {required = true, type = "language", default = "und"},
[2] = {list = true, allow_holes = true},
[2] = {list = true, allow_holes = true},
 
["t"] = {list = true, allow_holes = true, require_index = true},
["gloss"] = {list = true, allow_holes = true, require_index = true, alias_of = "t"},
["tr"] = {list = true, allow_holes = true, require_index = true},
["ts"] = {list = true, allow_holes = true, require_index = true},
["g"] = {list = true, allow_holes = true, require_index = true},
["id"] = {list = true, allow_holes = true, require_index = true},
["altaff"] = {},
["altaff"] = {},
["alt"] = {list = true, allow_holes = true, require_index = true},
["q"] = {list = true, allow_holes = true, require_index = true},
["lit"] = {list = true, allow_holes = true, require_index = true},
["pos"] = {list = true, allow_holes = true, require_index = true},
["sc"] = {},
["nointerp"] = {type = "boolean"},
["nointerp"] = {type = "boolean"},
["lang"] = {list = true, allow_holes = true, require_index = true},
["pagename"] = {},
-- Note, sc1=, sc2=, ... are different from sc=; the former apply to
-- individual arguments when lang1=, lang2=, ... is specified, while
-- the latter applies to all arguments where langN=... isn't specified
["langsc"] = {list = "sc", allow_holes = true, require_index = true},
["arrow"] = {list = true, allow_holes = true, require_index = true, type = "boolean"},
["joiner"] = {list = true, allow_holes = true, require_index = true},
["fulljoiner"] = {list = true, allow_holes = true, require_index = true},
["accel"] = {list = true, allow_holes = true, require_index = true},
}
}


local aftype = frame.args["type"]
local aftype = frame.args.type
if aftype == "" or not aftype then
if aftype == "" or not aftype then
aftype = "affix"
aftype = "affix"
Line 49: Line 32:


if aftype == "prefix" then
if aftype == "prefix" then
params["altpref"] = {alias_of = "altaff"}
params.altpref = {alias_of = "altaff"}
elseif aftype == "suffix" then
elseif aftype == "suffix" then
params["altsuf"] = {alias_of = "altaff"}
params.altsuf = {alias_of = "altaff"}
end
end


local args = require("Module:parameters").process(frame:getParent().args, params)
    local m_param_utils = require(parameter_utilities_module)
local param_mods = m_param_utils.construct_param_mods {
-- We want to require an index for all params. Some of the params generated below have separate_no_index, which
-- overrides require_index (and also requires an index for the param corresponding to the first item).
{default = true, require_index = true},
{group = {"link", "ref", "lang", "q", "l"}},
{param = "lit", separate_no_index = true},
{param = "arrow", type = "boolean"},
{param = {"joiner", "fulljoiner"}},
}
 
local has_exclamation_point = {}
local items, args = m_param_utils.parse_list_with_inline_modifiers_and_separate_params {
params = params,
param_mods = param_mods,
raw_args = parent_args,
process_args_before_parsing = function(args)
-- Remember and remove an exclamation point from the beginning of a term. We need to do this *before*
-- parsing inline modifiers because the exclamation point goes before a language prefix, which is split off
-- as part of parsing inline modifiers.
for i, term in ipairs(args[2]) do
if sub(term, 1, 1) == "!" then
has_exclamation_point[i] = true
args[2][i] = gsub(term, "^!", "")
end
end
end,
termarg = 2,
parse_lang_prefix = true,
track_module = "affixusex",
}
 
local lang = args[1]
local lang = args[1]
lang = m_languages.getByCode(lang) or m_languages.err(lang, 1)
local sc = args["sc"]
sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)


-- Find the maximum index among any of the list parameters.
local data = {
local maxmaxindex = 0
items = items,
for k, v in pairs(params) do
lang = lang,
if v.list and v.allow_holes and not v.alias_of and args[k].maxindex > maxmaxindex then
sc = args.sc.default,
maxmaxindex = args[k].maxindex
l = args.l.default,
end
ll = args.ll.default,
end
q = args.q.default,
qq = args.qq.default,
lit = args.lit.default,
}
 
local pagename = args.pagename or mw.loadData("Module:headword/data").pagename


-- Determine whether the terms in the numbered params contain a prefix or suffix.
-- Determine whether the terms in the numbered params contain a prefix or suffix. If not, we may insert one before
-- If not, we may insert one before the last term (for suffixes) or the first
-- the last term (for suffixes) or the first term (for prefixes).
-- term (for prefixes).
local affix_in_items = false
local affix_in_parts = false
for i, item in ipairs(items) do
local SUBPAGE = mw.title.getCurrentTitle().subpageText
if item.term then
local is_affix = {}
-- Careful here, a prefix beginning with ! should be treated as a normal term.
for i=1,maxmaxindex do
if has_exclamation_point[item.orig_index] or ((item.lang or lang):makeEntryName(item.term)) == pagename then
if args[2][i] then
affix_in_items = true
-- Careful here, a prefix beginning with ! should be treated as a
if not item.alt then
-- normal term.
item.alt = item.term
if rfind(args[2][i], "^!") or lang:makeEntryName(args[2][i]) == SUBPAGE then
item.term = nil
affix_in_parts = true
end
is_affix[i] = true
end
end
end
end
end
end


local insertable_aff = args["altaff"] or SUBPAGE
-- Determine affix to check for prefixness/suffixness.
-- Insert suffix derived from page title or altaff=/altsuf= before the last
local insertable_aff = args.altaff or pagename
-- component if
-- (a) nointerp= isn't present, and
-- (b) no suffix is present among the parts (where "suffix" means a part that
--    matches the subpage name after diacritics have been removed, or a part
--    prefixed by !), and either
--    (i) {{suffixusex}}/{{sufex}} was used;
--    (ii) {{affixusex}}/{{afex}} was used and altaff= is given, and its value
--        looks like a suffix (begins with -, doesn't end in -; an infix is
--        not a suffix)
--    (iii) {{affixusex}}/{{afex}} was used and altaff= is not given and the
--          subpage title looks like a suffix (same conditions as for altaff=)
local insert_suffix = not args["nointerp"] and not affix_in_parts and (aftype == "suffix" or (
aftype == "affix" and rfind(insertable_aff, "^%-") and not rfind(insertable_aff, "%-$")))
-- Insert prefix derived from page title or altaff=/altpref= before the first
-- component using similar logic as preceding.
local insert_prefix = not args["nointerp"] and not affix_in_parts and (aftype == "prefix" or (
aftype == "affix" and rfind(insertable_aff, "%-$") and not rfind(insertable_aff, "^%-")))
 
-- Build up the per-term objects.
local parts = {}
for i=1,maxmaxindex do
-- If we're {{suffixusex}} and about to append the last term, or {{prefixusex}}
-- and about to append the first term, and no affix appeared among the terms, and
-- nointerp= isn't set, insert the affix (which comes either from altaff=/altpref=/altsuf=
-- or from the subpage name).
if i == maxmaxindex and insert_suffix or i == 1 and insert_prefix then
local affix = args["altaff"]
affix = affix or SUBPAGE
table.insert(parts, {alt = affix})
end


local part = {}
-- Determine affix to interpolate if needed.
if is_affix[i] and not args["alt"][i] then
local affix = args.altaff
part.alt = rsub(args[2][i], "^!", "")
if not affix then
if lang:hasType("reconstructed") then
affix = "*" .. pagename
else
else
part.term = args[2][i]
affix = pagename
part.alt = args["alt"][i]
end
end
end


local langn = args["lang"][i]
-- Insert suffix derived from page title or altaff=/altsuf= before the last component if
if langn then
-- (a) nointerp= isn't present, and
langn =
-- (b) no suffix is present among the parts (where "suffix" means a part that matches the subpage name after
m_languages.getByCode(langn) or
--    diacritics have been removed, or a part prefixed by !), and either
require("Module:etymology languages").getByCode(langn) or
--    (i) {{suffixusex}}/{{sufex}} was used;
m_languages.err(langn, "lang" .. i)
--    (ii) {{affixusex}}/{{afex}} was used and altaff= is given, and its value looks like a suffix (begins with -,
--        doesn't end in -; an infix is not a suffix)
--    (iii) {{affixusex}}/{{afex}} was used and altaff= is not given and the subpage title looks like a suffix
--          (same conditions as for altaff=)
-- Insert prefix derived from page title or altaff=/altpref= before the first component using similar logic as
-- preceding.
if not args.nointerp and not affix_in_items then
if aftype == "prefix" or (
aftype == "affix" and
sub(insertable_aff, -1) == "-" and
sub(insertable_aff, 1, 1) ~= "-"
) then
insert(items, 1, {alt = affix})
elseif aftype == "suffix" or (
aftype == "affix" and
sub(insertable_aff, 1, 1) == "-" and
sub(insertable_aff, -1) ~= "-"
) then
insert(items, #items, {alt = affix})
end
end
local langsc = args["langsc"][i]
if langsc then
langsc = require("Module:scripts").getByCode(langsc) or error("The script code \"" .. langsc .. "\" is not valid.")
end
part.t = args["t"][i]
part.tr = args["tr"][i]
part.ts = args["ts"][i]
part.g = args["g"][i]
part.id = args["id"][i]
part.q = args["q"][i]
part.lit = args["lit"][i]
part.pos = args["pos"][i]
part.lang = langn
part.sc = langsc
part.arrow = args["arrow"][i]
part.joiner = args["joiner"][i]
part.fulljoiner = args["fulljoiner"][i]
part.accel = args["accel"][i] and string.gsub(args["accel"], "_", "|"),  -- To allow use of | in templates
table.insert(parts, part)
end
end


return require("Module:affixusex").format_affixusex(lang, sc, parts, aftype)
return "<span class=\"affixusex\">" .. require(affixusex_module).format_affixusex(data) .. "</span>"
end
end


return export
return export

Latest revision as of 14:03, 1 July 2025

Documentation for this module may be created at Module:affixusex/templates/doc

local export = {}

local require = require

local affixusex_module = "Module:affixusex"
local parameter_utilities_module = "Module:parameter utilities"

local concat = table.concat
local find = string.find
local gsub = string.gsub
local insert = table.insert
local match = string.match
local sort = table.sort
local sub = string.sub

function export.affixusex_t(frame)
	local parent_args = frame:getParent().args

	local params = {
		[1] = {required = true, type = "language", default = "und"},
		[2] = {list = true, allow_holes = true},

		["altaff"] = {},
		["nointerp"] = {type = "boolean"},
		["pagename"] = {},
	}

	local aftype = frame.args.type
	if aftype == "" or not aftype then
		aftype = "affix"
	end

	if aftype == "prefix" then
		params.altpref = {alias_of = "altaff"}
	elseif aftype == "suffix" then
		params.altsuf = {alias_of = "altaff"}
	end

    local m_param_utils = require(parameter_utilities_module)
	local param_mods = m_param_utils.construct_param_mods {
		-- We want to require an index for all params. Some of the params generated below have separate_no_index, which
		-- overrides require_index (and also requires an index for the param corresponding to the first item).
		{default = true, require_index = true},
		{group = {"link", "ref", "lang", "q", "l"}},
		{param = "lit", separate_no_index = true},
		{param = "arrow", type = "boolean"},
		{param = {"joiner", "fulljoiner"}},
	}

	local has_exclamation_point = {}
	local items, args = m_param_utils.parse_list_with_inline_modifiers_and_separate_params {
		params = params,
		param_mods = param_mods,
		raw_args = parent_args,
		process_args_before_parsing = function(args)
			-- Remember and remove an exclamation point from the beginning of a term. We need to do this *before*
			-- parsing inline modifiers because the exclamation point goes before a language prefix, which is split off
			-- as part of parsing inline modifiers.
			for i, term in ipairs(args[2]) do
				if sub(term, 1, 1) == "!" then
					has_exclamation_point[i] = true
					args[2][i] = gsub(term, "^!", "")
				end
			end
		end,
		termarg = 2,
		parse_lang_prefix = true,
		track_module = "affixusex",
	}

	local lang = args[1]

	local data = {
		items = items,
		lang = lang,
		sc = args.sc.default,
		l = args.l.default,
		ll = args.ll.default,
		q = args.q.default,
		qq = args.qq.default,
		lit = args.lit.default,
	}

	local pagename = args.pagename or mw.loadData("Module:headword/data").pagename

	-- Determine whether the terms in the numbered params contain a prefix or suffix. If not, we may insert one before
	-- the last term (for suffixes) or the first term (for prefixes).
	local affix_in_items = false
	for i, item in ipairs(items) do
		if item.term then
			-- Careful here, a prefix beginning with ! should be treated as a normal term.
			if has_exclamation_point[item.orig_index] or ((item.lang or lang):makeEntryName(item.term)) == pagename then
				affix_in_items = true
				if not item.alt then
					item.alt = item.term
					item.term = nil
				end
			end
		end
	end

	-- Determine affix to check for prefixness/suffixness.
	local insertable_aff = args.altaff or pagename

	-- Determine affix to interpolate if needed.
	local affix = args.altaff
	if not affix then
		if lang:hasType("reconstructed") then
			affix = "*" .. pagename
		else
			affix = pagename
		end
	end

	-- Insert suffix derived from page title or altaff=/altsuf= before the last component if
	-- (a) nointerp= isn't present, and
	-- (b) no suffix is present among the parts (where "suffix" means a part that matches the subpage name after
	--     diacritics have been removed, or a part prefixed by !), and either
	--    (i) {{suffixusex}}/{{sufex}} was used;
	--    (ii) {{affixusex}}/{{afex}} was used and altaff= is given, and its value looks like a suffix (begins with -,
	--         doesn't end in -; an infix is not a suffix)
	--    (iii) {{affixusex}}/{{afex}} was used and altaff= is not given and the subpage title looks like a suffix
	--          (same conditions as for altaff=)
	-- Insert prefix derived from page title or altaff=/altpref= before the first component using similar logic as
	-- preceding.
	if not args.nointerp and not affix_in_items then
		if aftype == "prefix" or (
			aftype == "affix" and
			sub(insertable_aff, -1) == "-" and
			sub(insertable_aff, 1, 1) ~= "-"
		) then
			insert(items, 1, {alt = affix})
		elseif aftype == "suffix" or (
			aftype == "affix" and
			sub(insertable_aff, 1, 1) == "-" and
			sub(insertable_aff, -1) ~= "-"
		) then
			insert(items, #items, {alt = affix})
		end
	end

	return "<span class=\"affixusex\">" .. require(affixusex_module).format_affixusex(data) .. "</span>"
end

return export