Module:usex/templates: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 1: Line 1:
local export = {}
local export = {}


local m_languages = require("Module:languages")
local table_module = "Module:table"
local usex_module = "Module:usex"
local yesno_module = "Module:yesno"
 
local insert = table.insert
local rfind = mw.ustring.find
local rsplit = mw.text.split
local rsplit = mw.text.split
local rfind = mw.ustring.find


function export.usex_t(frame)
function export.usex_t(frame)
local params = {
local alias_of_3 = {alias_of = 3}
[1] = {required = true},
local boolean = {type = "boolean"}
[2] = {},
local list = {list = true}
[3] = {},
local language = {type = "language"}
local script = {type = "script"}
["inline"] = {type = "boolean"},
["noenum"] = {type = "boolean"},
-- Invocation arguments (passed in the template #invoke call).
["ref"] = {},
local iargs = require("Module:parameters").process(frame.args, {
["lit"] = {},
["quote"] = true,
["q"] = {list = true},
["inline"] = true,
["sc"] = {},
["nocat"] = boolean,
["source"] = {},
["class"] = true,
["footer"] = {},
})
["subst"] = {},
 
["t"] = {alias_of = 3},
local parent_args = frame:getParent().args
["translation"] = {alias_of = 3},
 
["tr"] = {},
-- Template (parent) arguments.
local args = require("Module:parameters").process(parent_args, {
-- Usex/quotation text parameters
[1] = {required = true, type = "language"},
[2] = true,
["termlang"] = language,
["tr"] = true,
["transliteration"] = {alias_of = "tr"},
["transliteration"] = {alias_of = "tr"},
["ts"] = {},
["ts"] = true,
["transcription"] = {alias_of = "ts"},
["transcription"] = {alias_of = "ts"},
["nocat"] = {type = "boolean"},
["sc"] = script,
["brackets"] = {type = "boolean"},
["norm"] = true,
["sort"] = {},
["normalization"] = {alias_of = "norm"},
}
["normsc"] = script,
["subst"] = true,
local quote = (frame.args["quote"] or "") ~= ""
["q"] = list,
local compat = (frame.args["compat"] or "") ~= ""
["qualifier"] = {alias_of = "q"},
local template_inline = (frame.args["inline"] or "") ~= ""
["qq"] = list,
local template_nocat = (frame.args["nocat"] or "") ~= ""
["ref"] = true,
local class = frame.args["class"]
-- Usex/quotation audio parameters,
if compat then
["audio"] = true,
params["lang"] = {required = true}
 
params["t"].alias_of = 2
-- Translation of usex text
params["translation"].alias_of = 2
[3] = true,
table.remove(params, 1)
["t"] = alias_of_3,
["translation"] = alias_of_3,
["lit"] = true,
 
-- Original text, if the usex/quotation is a translation
["orig"] = true,
["origlang"] = language,
["origtr"] = true,
["origts"] = true,
["origsc"] = script,
["orignorm"] = true,
["orignormsc"] = script,
["origsubst"] = true,
["origq"] = list,
["origqq"] = list,
["origref"] = true,
 
-- Citation-related parameters; for anything more complex, usex {{quote-*}}
["source"] = true,
["footer"] = true,
 
-- Formatting parameters
["inline"] = true,
["brackets"] = boolean,
 
-- Categorization parameters
["nocat"] = boolean,
["sort"] = true,
})
 
local lang = args[1]
local sc = args.sc
local normsc = args.normsc
if normsc and not args.norm then
error("Cannot specify normsc= without norm=")
end
end
local args = require("Module:parameters").process(frame:getParent().args, params)
local lang = args[compat and "lang" or 1] or "und"
local sc = args["sc"]
local data = {
lang = m_languages.getByCode(lang, compat and "lang" or 1),
sc = (sc and require("Module:scripts").getByCode(sc, true) or nil),
usex = args[compat and 1 or 2],
translation = args[compat and 2 or 3],
transliteration = args["tr"],
transcription = args["ts"],
noenum = args["noenum"],
inline = args["inline"] or template_inline,
ref = args["ref"],
quote = quote,
lit = args["lit"],
substs = args["subst"],
qualifiers = args["q"],
source = args["source"],
footer = args["footer"],
nocat = args["nocat"] or template_nocat,
brackets = args["brackets"],
sortkey = args["sort"],
class = class,
}
return require("Module:usex").format_usex(data)
end


-- Given a comma-separated list of language codes, return the English equivalent.
local termlang = args.termlang
function export.format_langs(frame)
if termlang then
local langcodes = rsplit(frame.args[1], ",")
insert(args.qq, 1, "in " .. lang:getCanonicalName())
local langnames = {}
for _, langcode in ipairs(langcodes) do
local lang = m_languages.getByCode(langcode) or m_languages.err(langcode, 1)
table.insert(langnames, lang:getCanonicalName())
end
end
if #langnames == 1 then
 
return langnames[1]
local origlang, origsc, orignormsc
elseif #langnames == 2 then
if args.orig then
return langnames[1] .. " and " .. langnames[2]
origlang = args.origlang
insert(args.origqq, 1, "in " .. origlang:getCanonicalName())
origsc = args.origsc
orignormsc = args.orignormsc
if orignormsc and not args.orignorm then
error("Cannot specify orignormsc= without orignorm=")
end
else
else
local retval = {}
for _, noparam in ipairs { "origlang", "origtr", "origts", "origsc", "orignorm", "orignormsc", "origsubst",
for i, langname in ipairs(langnames) do
"origref" } do
table.insert(retval, langname)
if args[noparam] then
if i <= #langnames - 2 then
error(("Cannot specify %s= without orig="):format(noparam))
table.insert(retval, ", ")
elseif i == #langnames - 1 then
table.insert(retval, "<span class=\"serial-comma\">,</span><span class=\"serial-and\"> and</span> ")
end
end
end
end
return table.concat(retval, "")
if #args.origq > 0 then
error("Cannot specify origq= without orig=")
end
if #args.origqq > 0 then
error("Cannot specify origqq= without orig=")
end
end
 
local inline = args.inline or iargs.inline
if inline ~= "auto" then
inline = require(yesno_module)(inline)
end
end
end


-- Given a comma-separated list of language codes, return the first one.
local data = {
function export.first_lang(frame)
lang = lang,
local langcodes = rsplit(frame.args[1], ",")
termlang = termlang,
return langcodes[1]
sc = sc,
normsc = normsc,
usex = args[2],
translation = args[3],
transliteration = args.tr,
transcription = args.ts,
normalization = args.norm,
inline = inline,
ref = args.ref,
quote = iargs.quote,
lit = args.lit,
subst = args.subst,
-- FIXME, change to left and right qualifiers
qq = #args.qq > 0 and args.qq or args.q,
audio = args.audio,
source = args.source,
footer = args.footer,
nocat = args.nocat or iargs.nocat,
brackets = args.brackets,
sortkey = args.sort,
class = iargs.class,
 
-- Original text, if the usex/quotation is a translation
orig = args.orig,
origlang = origlang,
origtr = args.origtr,
origts = args.origts,
origsc = origsc,
orignorm = args.orignorm,
orignormsc = orignormsc,
origsubst = args.origsubst,
origq = args.origq,
origqq = args.origqq,
origref = args.origref,
}
 
return require(usex_module).format_usex(data)
end
end


Navigation menu