A modult a Modul:nyms/doc lapon tudod dokumentálni
local export = {}
local m_languages = require("Module:languages")
local m_links = require("Module:links")
local rsplit = mw.text.split
local function wrap_span(text, lang, sc)
return '<span class="' .. sc .. '" lang="' .. lang .. '">' .. text .. '</span>'
end
local param_mods = {"t", "alt", "tr", "ts", "pos", "lit", "id", "sc", "g", "q", "qq"}
-- Do m_table.listToSet(param_mods) inline, maybe saving memory?
local param_mod_set = {}
for _, param_mod in ipairs(param_mods) do
param_mod_set = true
end
local function get_thesaurus_text(lang, args, maxindex)
local thesaurus
local thesaurus_links = {}
while args and args:find("^Thesaurus:") do
for _, param_mod in ipairs(param_mods) do
if args then
error("You cannot use named parameters with Thesaurus links, but saw " .. param_mod .. maxindex .. "=")
end
end
local link
local term = args:sub(11) -- remove Thesaurus: from beginning
local sc = require("Module:scripts").findBestScript(term, lang):getCode()
local fragment = term:find("#")
if fragment then
link = " .. "|Thesaurus:" .. wrap_span(term:sub(1, fragment-1), lang:getCode(), sc) .. "]]"
else
link = " .. "|Thesaurus:" .. wrap_span(term, lang:getCode(), sc) .. "]]"
end
table.insert(thesaurus_links, 1, link)
maxindex = maxindex - 1
end
if #thesaurus_links > 0 then
thesaurus = (maxindex == 0 and "''see'' " or "; ''see also'' ")
.. table.concat(thesaurus_links, ", ")
end
return thesaurus or "", maxindex
end
function export.nyms(frame)
local list_with_holes = {list = true, allow_holes = true}
local params = {
= {required = true, default = "und"},
= {list = true, allow_holes = true, required = true},
}
for _, param_mod in ipairs(param_mods) do
params = list_with_holes
end
local args = require("Module:parameters").process(frame:getParent().args, params)
local nym_type = frame.args
local nym_type_class = string.gsub(nym_type, "%s", "-")
local lang = m_languages.getByCode(args, 1)
local maxindex = math.max(args.maxindex, args.maxindex, args.maxindex)
local thesaurus, link_maxindex = get_thesaurus_text(lang, args, maxindex)
local items = {}
local iut
local use_semicolon = false
local syn = 0
for i = 1, link_maxindex do
local item = args
if item and item:find("^Thesaurus:") then
error("A link to Thesaurus must be the last in the list")
end
if item ~= ";" then
syn = syn + 1
local termobj = {
joiner = i > 1 and (args == ";" and "; " or ", ") or "",
q = args,
qq = args,
term = {
lang = lang, term = item, id = args,
sc = args and require("Module:scripts").getByCode(args, "sc" .. syn) or nil,
alt = args, tr = args, ts = args,
gloss = args, lit = args, pos = args,
genders = args and rsplit(args, ",") or nil,
},
}
-- Check for new-style argument, e.g. מרים<tr:Miryem>. But exclude HTML entry with <span ...>,
-- <i ...>, <br/> or similar in it, caused by wrapping an argument in {{l|...}}, {{af|...}} or similar.
-- Basically, all tags of the sort we parse here should consist of a less-than sign, plus letters,
-- plus a colon, e.g. <tr:...>, so if we see a tag on the outer level that isn't in this format,
-- we don't try to parse it. The restriction to the outer level is to allow generated HTML inside
-- of e.g. qualifier tags, such as foo<q:similar to {{m|fr|bar}}>.
if item and item:find("<") and not item:find("^*<*") then
if not iut then
iut = require("Module:inflection utilities")
end
local run = iut.parse_balanced_segment_run(item, "<", ">")
local function parse_err(msg)
error(msg .. ": " .. (i + 1) .. "=" .. table.concat(run))
end
termobj.term.term = run
for j = 2, #run - 1, 2 do
if run ~= "" then
parse_err("Extraneous text '" .. run .. "' after modifier")
end
local modtext = run:match("^<(.*)>$")
if not modtext then
parse_err("Internal error: Modifier '" .. modtext .. "' isn't surrounded by angle brackets")
end
local prefix, arg = modtext:match("^(+):(.*)$")
if not prefix then
parse_err("Modifier " .. run .. " lacks a prefix, should begin with one of '" ..
table.concat(param_mods, ":', '") .. ":'")
end
if param_mod_set then
local obj_to_set
if prefix == "q" or prefix == "qq" then
obj_to_set = termobj
else
obj_to_set = termobj.term
end
if prefix == "t" then
prefix = "gloss"
elseif prefix == "g" then
prefix = "genders"
arg = rsplit(arg, ",")
elseif prefix == "sc" then
arg = require("Module:scripts").getByCode(arg, "" .. (i + 1) .. ":sc")
end
if obj_to_set then
parse_err("Modifier '" .. prefix .. "' occurs twice, second occurrence " .. run)
end
obj_to_set = arg
else
parse_err("Unrecognized prefix '" .. prefix .. "' in modifier " .. run)
end
end
elseif item and item:find(",", 1, true) then
-- FIXME: Why is this here and when is it used?
use_semicolon = true
end
table.insert(items, termobj)
end
end
if use_semicolon then
for i, item in ipairs(items) do
if i > 1 then
item.joiner = "; "
end
end
end
for i, item in ipairs(items) do
items = item.joiner .. (item.q and require("Module:qualifier").format_qualifier({item.q}) .. " " or "")
.. m_links.full_link(item.term) .. (item.qq and " " .. require("Module:qualifier").format_qualifier({item.qq}) or "")
end
return "<span class=\"nyms " .. nym_type_class .. "\"><span class=\"defdate\">" ..
mw.getContentLanguage():ucfirst(nym_type) .. ((#items > 1 or thesaurus ~= "") and "" or "") ..
":</span> " .. table.concat(items) .. thesaurus .. "</span>"
end
return export