మాడ్యూల్:utilities
Documentation for this module may be created at మాడ్యూల్:utilities/doc
local export = {}
local notneeded = {
["und"] = true,
["cmn"] = true,
["ja"] = true,
["zu"] = true,
["nan"] = true,
["yue"] = true,
["ko"] = true,
}
local neededhassubpage = {
["ga"] = true,
["gv"] = true,
["nv"] = true,
["roa-jer"] = true,
["fr"] = true,
["rm"] = true,
["prg"] = true,
["gd"] = true,
["twf"] = true,
["en"] = true,
["ro"] = true,
["egl"] = true,
["roa-tar"] = true,
["gl"] = true,
["ast"] = true,
["br"] = true,
}
-- A helper function to escape magic characters in a string
-- Magic characters: ^$()%.[]*+-?
function export.pattern_escape(text)
text = (type(text) == "table" and text.args[1] or text)
text = mw.ustring.gsub(text, "([%^$()%%.%[%]*+%-?|])", "%%%1")
return text
end
-- Format the categories with the appropriate sort key
function export.format_categories(categories, lang, sort_key, sort_base)
NAMESPACE = NAMESPACE or mw.title.getCurrentTitle().nsText
if NAMESPACE == "" or NAMESPACE == "Appendix" or NAMESPACE == "Reconstruction" then
PAGENAME = PAGENAME or mw.title.getCurrentTitle().text
SUBPAGENAME = SUBPAGENAME or mw.title.getCurrentTitle().subpageText
if not lang then
lang = require("Module:languages").getByCode("und")
end
-- Generate a default sort key
sort_base = lang:makeSortKey(sort_base or SUBPAGENAME)
if sort_key then
-- Gather some statistics regarding sort keys
if mw.ustring.lower(sort_key) == sort_base then
table.insert(categories, "Sort key tracking/redundant")
end
else
sort_key = sort_base
end
-- If the resulting key is the same as the wiki software's default, remove it
if sort_key == PAGENAME then
sort_key = nil
end
for key, cat in ipairs(categories) do
categories[key] = "[[Category:" .. cat .. (sort_key and "|" .. sort_key or "") .. "]]"
end
return table.concat(categories, "")
else
return ""
end
end
-- Used by {{categorize}}
function export.template_categorize(frame)
NAMESPACE = NAMESPACE or mw.title.getCurrentTitle().nsText
local format = frame.args["format"]
local args = frame:getParent().args
local langcode = args[1]; if langcode == "" then langcode = nil end
local sort_key = args["sort"]; if sort_key == "" then sort_key = nil end
local categories = {}
if not langcode then
if NAMESPACE == "Template" then return "" end
error("Language code has not been specified. Please pass parameter 1 to the template.")
end
local lang = require("Module:languages").getByCode(langcode)
if not lang then
if NAMESPACE == "Template" then return "" end
error("The language code \"" .. langcode .. "\" is not valid.")
end
local prefix = ""
if format == "pos" then
prefix = lang:getCanonicalName() .. " "
elseif format == "topic" then
prefix = lang:getCode() .. ":"
end
local i = 2
local cat = args[i]
while cat do
if cat ~= "" then
table.insert(categories, prefix .. cat)
end
i = i + 1
cat = args[i]
end
return export.format_categories(categories, lang, sort_key)
end
return export