This module generates content for the etymology template {{doublet}}
.
local export = {}
local require_when_needed = require("Module:require when needed")
local concat = table.concat
local format_categories = require_when_needed("Module:utilities", "format_categories")
local insert = table.insert
local process_params = require_when_needed("Module:parameters", "process")
local serial_comma_join = require_when_needed("Module:table", "serialCommaJoin")
local rsplit = mw.text.split
local rsubn = mw.ustring.gsub
local ulower = string.ulower
-- version of rsubn() that discards all but the first return value
local function rsub(term, foo, bar)
return (rsubn(term, foo, bar))
end
local function get_parsed_part(template, lang, args, terms, i)
local term = terms
local alt = args
local id = args
local sc = args
local tr = args
local ts = args
local gloss = args
local pos = args
local lit = args
local g = args
if not (term or alt or tr or ts) then
require("Module:debug/track")(template .. "/no term or alt or tr")
return nil
else
return require("Module:links").full_link(
{ term = term, alt = alt, id = id, lang = lang, sc = sc, tr = tr,
ts = ts, gloss = gloss, pos = pos, lit = lit,
genders = g and rsplit(g, ",") or {}
}, "term", true)
end
end
local function get_parsed_parts(template, lang, args, terms)
local parts = {}
-- Find the maximum index among any of the list parameters.
local maxmaxindex = 0
for _, v in pairs(args) do
if type(v) == "table" and v.maxindex and v.maxindex > maxmaxindex then
maxmaxindex = v.maxindex
end
end
for index = 1, maxmaxindex do
insert(parts, get_parsed_part(template, lang, args, terms, index))
end
return parts
end
local function get_args(frame)
local boolean = {type = "boolean"}
local list = {list = true, allow_holes = true, require_index = true}
return process_params(frame:getParent().args, {
= {
required = true,
type = "language",
default = "und"
},
= {list = true, allow_holes = true},
= list,
= {
list = true,
allow_holes = true,
require_index = true,
alias_of = "t"
},
= list,
= list,
= list,
= list,
= list,
= list,
= list,
= {
type = "script",
list = true,
allow_holes = true,
require_index = true
},
= boolean, -- should be processed in the template itself
= boolean,
= boolean,
= {},
})
end
-- Implementation of miscellaneous templates such as {{doublet}} that can take
-- multiple terms. Doesn't handle {{blend}} or {{univerbation}}, which display
-- + signs between elements and use compound_like in ].
function export.misc_variant_multiple_terms(frame)
local args = get_args(frame)
local lang = args
local parts = {}
if not args then
insert(parts, frame.args)
end
if #args > 0 or #args > 0 then
if not args then
insert(parts, " ")
insert(parts, frame.args or "of")
insert(parts, " ")
end
local formatted_terms = get_parsed_parts(ulower(
-- Remove link and convert uppercase to lowercase to get an
-- approximation of the original template name.
rsub(rsub(frame.args, "^%%]$", "")),
lang, args, args)
insert(parts, serial_comma_join(formatted_terms))
end
if not args and frame.args then
local categories = {}
insert(categories, lang:getFullName() .. " " .. frame.args)
insert(parts, format_categories(categories, lang, args))
end
return concat(parts)
end
return export