Module:labels2
Documentation for this module may be created at Module:labels2/doc
-- This was copied and only slightly adapted from [[Module:labels]]. It is hoped the two modules will be merged in the future.
local m_labeldata = mw.loadData("Module:labels2/data")
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)]; if lang == "" then lang = nil end
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
if not lang then
if NAMESPACE == "Template" then
lang = "und"
elseif compat then
lang = "en"
else
error("Language code has not been specified. Please provide it to the template using the first parameter.")
end
end
lang = require("Module:languages").getByCode(lang)
-- 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_preComma = false
local omit_postComma = false
for i, label in ipairs(labels) do
if omit_postComma then
omit_preComma = true
omit_postComma = false
end
if m_labeldata.aliases[label] then
label = m_labeldata.aliases[label]
end
local data = export.get_data(label)
label = data.display or label
local omit_comma = omit_preComma or data.omit_preComma
omit_preComma = false
omit_postComma = data.omit_postComma
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))
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 categories = {}
local categories2 = {}
for i, cat in ipairs(data.topical_categories or {}) do
table.insert(categories, lang:getCode() .. ":" .. cat)
if script then
table.insert(categories, lang:getCode() .. ":" .. cat .. " in " .. script .. " script")
end
if script2 then
table.insert(categories2, lang:getCode() .. ":" .. cat .. " in " .. script2 .. " script")
end
end
for i, cat in ipairs(data.pos_categories or {}) do
table.insert(categories, lang:getCanonicalName() .. " " .. cat)
if script then
table.insert(categories, lang:getCanonicalName() .. " " .. cat .. " in " .. script .. " script")
end
if script2 then
table.insert(categories2, lang:getCanonicalName() .. " " .. cat .. " in " .. script2 .. " script")
end
end
for i, cat in ipairs(data.regional_categories or {}) do
table.insert(categories, cat .. " " .. lang:getCanonicalName())
if script then
table.insert(categories, cat .. " " .. lang:getCanonicalName() .. " in " .. script .. " script")
end
if script2 then
table.insert(categories2, cat .. " " .. lang:getCanonicalName() .. " 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_postComma = frame:expandTemplate{ title = label, args = {["sub"] = "helper", [1] = "next"} }; if data.omit_postComma == "" then data.omit_postComma = 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