Module:Labels
Appearance
local m_labeldata = mw.loadData("Module:labels/data")
local m_languages = mw.loadData("Module:languages/alldata")
local m_utilities = require("Module:utilities")
local export = {}
-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
local args = frame:getParent().args
NAMESPACE = mw.title.getCurrentTitle().nsText
local compat = (frame.args["compat"] or "") ~= ""
-- Gather parameters
local lang = args[(compat and "lang" or 1)] or (NAMESPACE == "Template" and "und") or ""; if compat and lang == "" then lang = "en" end --(NAMESPACE == "Template" and "und") or error("Language code has not been specified. Please pass parameter \"lang=\" to the template.")
local nocat = args["nocat"]; if nocat == "" then nocat = nil end
local script = args["script"]; if script == "" then script = nil end
local script2 = args["script2"]; if script2 == "" then script2 = nil end
local sort_key = args["sort"]; if sort_key == "" then sort_key = nil end
local sort_key2 = args["sort2"]; if sort_key2 == "" then sort_key2 = nil end
-- Gather the labels
local labels = {}
local i = (compat and 1 or 2)
local label = args[i]; if label == "" then label = nil end
while label do
table.insert(labels, label)
i = i + 1
label = args[i]; if label == "" then label = nil end
end
if #labels < 1 then
if NAMESPACE == "Template" then
labels = {"example"}
else
error("You must specify at least one label.")
end
end
-- Show the labels
local omit_comma = false
for i, label in ipairs(labels) do
if label == "_" then
labels[i] = ''
omit_comma = true
elseif label == "or" or label == "and" then
labels[i] = ' ' .. label
omit_comma = true
else
-- Is this label really an alias of another label?
-- If so, then just "rename" the current label to the one it points to.
if m_labeldata.aliases[label] then
label = m_labeldata.aliases[label]
end
local data = export.get_data(label)
label = data.display or label
if i > 1 then
label = (not omit_comma and "<span class=\"ib-comma\">,</span>" or "") .. " " .. label
end
labels[i] = label .. (nocat and "" or show_categories(data, lang, script, sort_key, script2, sort_key2))
omit_comma = data.omit_comma
end
end
return
"<span class=\"ib-brac\">(</span><span class=\"ib-content\">" ..
table.concat(labels, "") ..
"</span><span class=\"ib-brac\">)</span>"
end
function show_categories(data, lang, script, sort_key, script2, sort_key2)
local langinfo = m_languages[lang] or error("The language code \"" .. lang .. "\" is not valid.")
local categories = {}
local categories2 = {}
for i, cat in ipairs(data.topical_categories or {}) do
table.insert(categories, lang .. ":" .. cat)
if script then
table.insert(categories, lang .. ":" .. cat .. " in " .. script .. " script")
end
if script2 then
table.insert(categories2, lang .. ":" .. cat .. " in " .. script2 .. " script")
end
end
for i, cat in ipairs(data.pos_categories or {}) do
table.insert(categories, langinfo.names[1] .. " " .. cat)
if script then
table.insert(categories, langinfo.names[1] .. " " .. cat .. " in " .. script .. " script")
end
if script2 then
table.insert(categories2, langinfo.names[1] .. " " .. cat .. " in " .. script2 .. " script")
end
end
for i, cat in ipairs(data.regional_categories or {}) do
table.insert(categories, cat .. " " .. langinfo.names[1])
if script then
table.insert(categories, cat .. " " .. langinfo.names[1] .. " in " .. script .. " script")
end
if script2 then
table.insert(categories2, cat .. " " .. langinfo.names[1] .. " in " .. script2 .. " script")
end
end
for i, cat in ipairs(data.plain_categories or {}) do
table.insert(categories, cat)
if script then
table.insert(categories, cat .. " in " .. script .. " script")
end
if script2 then
table.insert(categories2, cat .. " in " .. script2 .. " script")
end
end
return m_utilities.format_categories(categories, lang, sort_key) .. m_utilities.format_categories(categories2, lang, sort_key2)
end
-- Get the data for a label, if any is available.
function export.get_data(label)
local data = {}
-- Is this label a known/recognised label?
if m_labeldata.labels[label] then
data = m_labeldata.labels[label]
else
-- Does a valid context label template exist for this label?
local label_template = mw.title.new("Template:" .. label)
local frame = mw.getCurrentFrame()
if label_template and label_template.exists and frame:expandTemplate{ title = label, args = { ["sub"] = "test" } } == "valid context label" then
data.display = frame:expandTemplate{ title = label, args = {["sub"] = "helper", [1] = "label"} }; if data.display == "" then data.display = nil end
data.omit_comma = frame:expandTemplate{ title = label, args = {["sub"] = "helper", [1] = "next"} }; if data.omit_comma == "" then data.omit_comma = nil end
data.topical_categories = frame:expandTemplate{ title = label, args = {["sub"] = "helper", [1] = "topcat"} }; if data.topical_categories == "" then data.topical_categories = nil end
data.pos_categories = frame:expandTemplate{ title = label, args = {["sub"] = "helper", [1] = "poscat"} }; if data.pos_categories == "" then data.pos_categories = nil end
data.regional_categories = frame:expandTemplate{ title = label, args = {["sub"] = "helper", [1] = "regcat"} }; if data.regional_categories == "" then data.regional_categories = nil end
data.plain_categories = frame:expandTemplate{ title = label, args = {["sub"] = "helper", [1] = "cat"} }; if data.plain_categories == "" then data.plain_categories = nil end
data.topical_categories = data.topical_categories and {data.topical_categories} or nil
data.pos_categories = data.pos_categories and {data.pos_categories} or nil
data.regional_categories = data.regional_categories and {data.regional_categories} or nil
data.plain_categories = data.plain_categories and {data.plain_categories} or nil
end
end
return data
end
return export