45,581
edits
No edit summary |
No edit summary |
||
Line 8: | Line 8: | ||
local function format_list_items(items, lang, sc) | local function format_list_items(items, lang, sc) | ||
local result = {} | local result = {} | ||
local function term_already_linked(term) | |||
-- FIXME: "<span" is an ugly hack to prevent double-linking of terms already run through {{l|...}}: | |||
-- [[Thread:User talk:CodeCat/MewBot adding lang to column templates]] | |||
return term:find("<span") | |||
end | |||
for _, item in ipairs(items) do | for _, item in ipairs(items) do | ||
if type(item) == "table" then | if type(item) == "table" then | ||
local link = m_links.full_link(item.term) | local link = term_already_linked(item.term.term) and item.term.term or m_links.full_link(item.term) | ||
if item.q then | if item.q then | ||
link = require("Module:qualifier").format_qualifier(item.q) .. " " .. link | link = require("Module:qualifier").format_qualifier(item.q) .. " " .. link | ||
end | |||
if item.qq then | |||
link = link .. " " .. require("Module:qualifier").format_qualifier(item.qq) | |||
end | end | ||
item = link | item = link | ||
elseif lang and not | elseif lang and not term_already_linked(item) then | ||
item = m_links.full_link {lang = lang, term = item, sc = sc} | item = m_links.full_link {lang = lang, term = item, sc = sc} | ||
end | end | ||
table.insert(result, '\n* ' .. item) | table.insert(result, '\n* ' .. item) | ||
end | end | ||
return table.concat(result) | return table.concat(result) | ||
end | end | ||
Line 43: | Line 51: | ||
error("expected table, got " .. type(args)) | error("expected table, got " .. type(args)) | ||
end | end | ||
args.class = args.class or "derivedterms" | args.class = args.class or "derivedterms" | ||
args.column_count = args.column_count or 1 | args.column_count = args.column_count or 1 | ||
args.toggle_category = args.toggle_category or "derived terms" | args.toggle_category = args.toggle_category or "derived terms" | ||
local output = {} | local output = {} | ||
if args.header then | if args.header then | ||
if args.format_header then | if args.format_header then | ||
Line 60: | Line 68: | ||
end | end | ||
table.insert(output, (column_header:gsub('{{{(.-)}}}', args))) | table.insert(output, (column_header:gsub('{{{(.-)}}}', args))) | ||
if args.alphabetize then | if args.alphabetize then | ||
local function keyfunc(item) | local function keyfunc(item) | ||
Line 66: | Line 74: | ||
item = item.term.term | item = item.term.term | ||
end | end | ||
return item | return item | ||
end | end | ||
Line 74: | Line 79: | ||
end | end | ||
table.insert(output, format_list_items(args.content, args.lang, args.sc)) | table.insert(output, format_list_items(args.content, args.lang, args.sc)) | ||
table.insert(output, '</div>') | table.insert(output, '</div>') | ||
if args.collapse then | if args.collapse then | ||
table.insert(output, button .. '</div>') | table.insert(output, button .. '</div>') | ||
end | end | ||
return table.concat(output) | return table.concat(output) | ||
end | end | ||
Line 93: | Line 98: | ||
args.collapse, args.class, args.header, args.column_width, | args.collapse, args.class, args.header, args.column_width, | ||
args.line_start, args.lang = ... | args.line_start, args.lang = ... | ||
args.format_header = true | args.format_header = true | ||
return export.create_list(args) | return export.create_list(args) | ||
end | end | ||
local param_mods = {"t", "alt", "tr", "ts", "pos", "lit", "id", "sc", "g", "q"} | local param_mods = {"t", "alt", "tr", "ts", "pos", "lit", "id", "sc", "g", "q", "qq"} | ||
local param_mod_set = m_table.listToSet(param_mods) | local param_mod_set = m_table.listToSet(param_mods) | ||
function export. | function export.display_from(column_args, list_args) | ||
local iparams = { | local iparams = { | ||
["class"] = {}, | ["class"] = {}, | ||
Line 124: | Line 129: | ||
["toggle_category"] = {}, | ["toggle_category"] = {}, | ||
} | } | ||
local frame_args = require("Module:parameters").process( | local frame_args = require("Module:parameters").process(column_args, iparams, nil, "columns", "display_from") | ||
local compat = frame_args["lang"] or list_args["lang"] | |||
local compat = frame_args["lang"] or | |||
local lang_param = compat and "lang" or 1 | local lang_param = compat and "lang" or 1 | ||
local columns_param = compat and 1 or 2 | local columns_param = compat and 1 or 2 | ||
Line 137: | Line 141: | ||
[columns_param] = not frame_args["columns"] and {required = true, default = 2} or nil, | [columns_param] = not frame_args["columns"] and {required = true, default = 2} or nil, | ||
[first_content_param] = {list = true}, | [first_content_param] = {list = true}, | ||
["title"] = {}, | ["title"] = {}, | ||
["collapse"] = {type = "boolean"}, | ["collapse"] = {type = "boolean"}, | ||
Line 143: | Line 147: | ||
["sc"] = {}, | ["sc"] = {}, | ||
} | } | ||
local args = require("Module:parameters").process( | local args = require("Module:parameters").process(list_args, params, nil, "columns", "display_from") | ||
local lang = frame_args["lang"] or args[lang_param] | local lang = frame_args["lang"] or args[lang_param] | ||
lang = m_languages.getByCode(lang, lang_param) | lang = m_languages.getByCode(lang, lang_param) | ||
local sc = args["sc"] | local sc = args["sc"] and require("Module:scripts").getByCode(sc, "sc") or nil | ||
local sort = frame_args["sort"] | local sort = frame_args["sort"] | ||
if args["sort"] ~= nil then | if args["sort"] ~= nil then | ||
Line 164: | Line 164: | ||
end | end | ||
local | local put | ||
for i, item in ipairs(args[first_content_param]) do | for i, item in ipairs(args[first_content_param]) do | ||
-- Check for | -- Parse off an initial language code (e.g. 'la:minūtia' or 'grc:[[σκῶρ|σκατός]]'). Don't parse if there's a spac | ||
-- after the colon (happens e.g. if the user uses {{desc|...}} inside of {{col}}, grrr ...). | |||
local termlang, actual_term = item:match("^([A-Za-z._-]+):([^ ].*)$") | |||
-- Make sure that only real language codes are handled as language links, so as to not catch interwiki | |||
-- or namespaces links. | |||
if termlang and ( | |||
mw.loadData("Module:languages/code to canonical name")[termlang] or | |||
mw.loadData("Module:etymology languages/code to canonical name")[termlang] | |||
) then | |||
-- -1 since i is one-based | |||
termlang = m_languages.getByCode(termlang, first_content_param + i - 1, "allow etym") | |||
item = actual_term | |||
else | |||
termlang = lang | |||
end | |||
local termobj = {term = {lang = termlang, sc = sc}} | |||
-- Check for inline modifier, 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:find("<") and not item:find("^[^<]*<[a-z]*[^a-z:]") then | if item:find("<") and not item:find("^[^<]*<[a-z]*[^a-z:]") then | ||
if not | if not put then | ||
put = require("Module:parse utilities") | |||
end | end | ||
local run = | local run = put.parse_balanced_segment_run(item, "<", ">") | ||
local orig_param = first_content_param + i - 1 | local orig_param = first_content_param + i - 1 | ||
local function parse_err(msg) | local function parse_err(msg) | ||
error(msg .. ": " .. orig_param .. "= " .. table.concat(run)) | error(msg .. ": " .. orig_param .. "= " .. table.concat(run)) | ||
end | end | ||
termobj.term.term = run[1] | termobj.term.term = run[1] | ||
Line 200: | Line 214: | ||
if param_mod_set[prefix] then | if param_mod_set[prefix] then | ||
local obj_to_set | local obj_to_set | ||
if prefix == "q" then | if prefix == "q" or prefix == "qq" then | ||
obj_to_set = termobj | obj_to_set = termobj | ||
else | else | ||
obj_to_set = termobj.term | obj_to_set = termobj.term | ||
end | end | ||
if prefix == "t" then | if prefix == "t" then | ||
prefix = "gloss" | |||
elseif prefix == "g" then | elseif prefix == "g" then | ||
prefix = "genders" | |||
arg = mw.text.split(arg, ",") | |||
elseif prefix == "sc" then | elseif prefix == "sc" then | ||
arg = require("Module:scripts").getByCode(arg, orig_param .. ":sc") | |||
end | |||
if obj_to_set[prefix] then | |||
parse_err("Modifier '" .. prefix .. "' occurs twice, second occurrence " .. run[j]) | |||
end | end | ||
obj_to_set[prefix] = arg | |||
else | else | ||
parse_err("Unrecognized prefix '" .. prefix .. "' in modifier " .. run[j]) | parse_err("Unrecognized prefix '" .. prefix .. "' in modifier " .. run[j]) | ||
end | end | ||
end | end | ||
else | |||
termobj.term.term = item | |||
end | end | ||
args[first_content_param][i] = termobj | |||
end | end | ||
Line 234: | Line 250: | ||
end | end | ||
function export.display(frame) | |||
return export.display_from(frame.args, frame:getParent().args) | |||
end | |||
-- A version of col which substs any automatically generated forms in order to save memory (e.g. for Chinese). | |||
function export.generated_forms(frame) | |||
local column_args, list_args = frame.args, frame:getParent().args | |||
local iparams = { | |||
["columns"] = {type = "number"}, | |||
["name"] = {required = true}, | |||
["toggle_category"] = {}, | |||
} | |||
local frame_args = require("Module:parameters").process(column_args, iparams, nil, "columns", "generated_forms") | |||
local first_content_param = 2 + (frame_args["columns"] and 0 or 1) | |||
frame_args["name"] = frame_args["columns"] and frame_args["name"] .. frame_args["columns"] or frame_args["name"] | |||
local params = { | |||
[1] = {required = true, default = "und"}, | |||
[2] = not frame_args["columns"] and {required = true, default = 2} or nil, | |||
[first_content_param] = {list = true}, | |||
["title"] = {}, | |||
["collapse"] = {type = "boolean"}, | |||
["sort"] = {type = "boolean"}, | |||
["sc"] = {}, | |||
} | |||
local args = require("Module:parameters").process(list_args, params, nil, "columns", "generated_forms") | |||
local lang = args[1] | |||
lang = m_languages.getByCode(lang, lang_param) | |||
args["sc"] = args["sc"] or "" | |||
if sc and sc ~= "" then | |||
sc = require "Module:scripts".getByCode(sc) | |||
or error("|sc= does not contain a valid script code") | |||
end | |||
args[2] = args[2] or "" | |||
args["title"] = args["title"] or "" | |||
args["collapse"] = args["collapse"] or "" | |||
args["sort"] = args["sort"] or "" | |||
local items = {} | |||
for i, item in ipairs(args[first_content_param]) do | |||
if item:find("<") and not item:find("^[^<]*<[a-z]*[^a-z:]") then | |||
item = item:gsub("^([^<]*)(<[a-z]*[a-z:])", function(m1, m2) | |||
return table.concat(lang:generateForms(m1), "//") | |||
end) | |||
else | |||
item = table.concat(lang:generateForms(item), "//") | |||
end | |||
table.insert(items, item) | |||
end | |||
for k, arg in pairs(args) do | |||
if type(arg) == "string" then | |||
if arg ~= "" then | |||
if type(k) == "string" then | |||
args[k] = k .. "=" .. args[k] .. "|" | |||
else | |||
args[k] = args[k] .. "|" | |||
end | |||
end | |||
elseif type(arg) == "boolean" then | |||
args[k] = k .. "=1|" | |||
end | |||
end | |||
local prefix = "{{" .. frame_args["name"] .. "|" .. args[1] .. args["sc"] | |||
prefix = not frame_args["columns"] and prefix .. args[2] or prefix | |||
return prefix .. args["title"] .. args["collapse"] .. args["sort"] .. table.concat(items, "|") .. "}}" | |||
end | |||
return export | return export |