Module:columns: Difference between revisions

no edit summary
(Undo revision 225322 by Sware (talk))
Tag: Undo
No edit summary
Line 3: Line 3:
local m_links = require("Module:links")
local m_links = require("Module:links")
local m_languages = require("Module:languages")
local m_languages = require("Module:languages")
local m_table = require("Module:table")




local function format_list_items(items, lang)
local function format_list_items(items, lang, sc)
local result = {}
local result = {}
for i, item in ipairs(items) do
for _, item in ipairs(items) do
if lang and not string.find(item, "<span") then
if type(item) == "table" then
item = m_links.full_link({lang = lang, term = item})
local link = m_links.full_link(item.term)
if item.q then
link = require("Module:qualifier").format_qualifier(item.q) .. " " .. link
end
item = link
elseif lang and not string.find(item, "<span") then
item = m_links.full_link {lang = lang, term = item, sc = sc}  
end
end
result[i] = '\n* ' .. item
table.insert(result, '\n* ' .. item)
end
end
Line 55: Line 62:
     if args.alphabetize then
     if args.alphabetize then
require("Module:Collation").sort(args.content, args.lang)
    local function keyfunc(item)
    if type(item) == "table" then
    item = item.term.term
    end
    -- Remove all HTML so mixtures of raw terms and {{l|...}} sort
    -- correctly.
    item = item:gsub("<.->", "")
    return item
    end
require("Module:collation").sort(args.content, args.lang, keyfunc)
end
end
table.insert(output, format_list_items(args.content, args.lang))
table.insert(output, format_list_items(args.content, args.lang, args.sc))
table.insert(output, '</div>')
table.insert(output, '</div>')
Line 83: Line 99:
end
end


local param_mods = {"t", "alt", "tr", "ts", "pos", "lit", "id", "sc", "g", "q"}
local param_mod_set = m_table.listToSet(param_mods)


function export.display(frame)
function export.display(frame)
Line 122: Line 141:
["collapse"] = {type = "boolean"},
["collapse"] = {type = "boolean"},
["sort"] = {type = "boolean"},
["sort"] = {type = "boolean"},
["sc"] = {},
}
}
Line 127: Line 147:
local lang = frame_args["lang"] or args[lang_param]
local lang = frame_args["lang"] or args[lang_param]
lang = m_languages.getByCode(lang) or m_languages.err(lang, lang_param)
lang = m_languages.getByCode(lang, lang_param)
local sc = args["sc"]
if sc then
sc = require "Module:scripts".getByCode(sc)
or error("|sc= does not contain a valid script code")
end
local sort = frame_args["sort"]
local sort = frame_args["sort"]
Line 137: Line 163:
collapse = args["collapse"]
collapse = args["collapse"]
end
end
 
local iut
for i, item in ipairs(args[first_content_param]) do
-- 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:find("<") and not item:find("^[^<]*<[a-z]*[^a-z:]") then
if not iut then
iut = require("Module:inflection utilities")
end
local run = iut.parse_balanced_segment_run(item, "<", ">")
local orig_param = first_content_param + i - 1
local function parse_err(msg)
error(msg .. ": " .. orig_param .. "= " .. table.concat(run))
end
local termobj = {term = {}}
termobj.term.lang = lang
termobj.term.term = run[1]
 
for j = 2, #run - 1, 2 do
if run[j + 1] ~= "" then
parse_err("Extraneous text '" .. run[j + 1] .. "' after modifier")
end
local modtext = run[j]:match("^<(.*)>$")
if not modtext then
parse_err("Internal error: Modifier '" .. modtext .. "' isn't surrounded by angle brackets")
end
local prefix, arg = modtext:match("^([a-z]+):(.*)$")
if not prefix then
parse_err("Modifier " .. run[j] .. " lacks a prefix, should begin with one of '" ..
table.concat(param_mods, ":', '") .. ":'")
end
if param_mod_set[prefix] then
local obj_to_set
if prefix == "q" then
obj_to_set = termobj
else
obj_to_set = termobj.term
end
if obj_to_set[prefix] then
parse_err("Modifier '" .. prefix .. "' occurs twice, second occurrence " .. run[j])
end
if prefix == "t" then
termobj.term.gloss = arg
elseif prefix == "g" then
termobj.term.genders = mw.text.split(arg, ",")
elseif prefix == "sc" then
termobj.term.sc = require("Module:scripts").getByCode(arg, pname)
else
obj_to_set[prefix] = arg
end
else
parse_err("Unrecognized prefix '" .. prefix .. "' in modifier " .. run[j])
end
end
args[first_content_param][i] = termobj
end
end
 
return export.create_list { column_count = frame_args["columns"] or args[columns_param],
return export.create_list { column_count = frame_args["columns"] or args[columns_param],
content = args[first_content_param],
content = args[first_content_param],
Line 144: Line 231:
collapse = collapse,
collapse = collapse,
toggle_category = frame_args["toggle_category"],
toggle_category = frame_args["toggle_category"],
class = frame_args["class"], lang = lang, format_header = true }
class = frame_args["class"], lang = lang, sc = sc, format_header = true }
end
end




return export
return export