45,646
edits
No edit summary |
Tag: Undo |
||
Line 5: | Line 5: | ||
local neededhassubpage = data.neededhassubpage | local neededhassubpage = data.neededhassubpage | ||
-- A helper function to escape magic characters in a string | -- A helper function to escape magic characters in a string | ||
-- Magic characters: ^$()%.[]*+-? | -- Magic characters: ^$()%.[]*+-? | ||
function export.pattern_escape(text) | function export.pattern_escape(text) | ||
Line 11: | Line 11: | ||
text = text.args[1] | text = text.args[1] | ||
end | end | ||
text = mw.ustring.gsub(text, "([%^$()%%.%[%]*+%-?])", "%%%1") | |||
return text | return text | ||
end | end | ||
Line 86: | Line 30: | ||
} | } | ||
local args = require("Module:parameters").process(frame.args, params | local args = require("Module:parameters").process(frame.args, params) | ||
text = args[1] | text = args[1] | ||
Line 106: | Line 50: | ||
pattern = export.pattern_escape(pattern) | pattern = export.pattern_escape(pattern) | ||
if invoked then | if invoked then | ||
text = mw.ustring.gsub(text, pattern, replacement) | |||
return text | |||
else | else | ||
return gsub(text, pattern, replacement) | return mw.ustring.gsub(text, pattern, replacement) | ||
end | end | ||
end | end | ||
Line 135: | Line 79: | ||
]] | ]] | ||
function export.format_categories(categories, lang, sort_key, sort_base, force_output, sc) | function export.format_categories(categories, lang, sort_key, sort_base, force_output, sc) | ||
local NAMESPACE = mw.title.getCurrentTitle().nsText | |||
if type(lang) == "table" and not lang.getCode then | if type(lang) == "table" and not lang.getCode then | ||
error("The second argument to format_categories should be a language object.") | error("The second argument to format_categories should be a language object.") | ||
end | end | ||
if force_output or data.allowedNamespaces[NAMESPACE] then | |||
local PAGENAME = mw.title.getCurrentTitle().text | |||
local SUBPAGENAME = mw.title.getCurrentTitle().subpageText | |||
if force_output or allowedNamespaces[ | |||
local PAGENAME = | |||
local SUBPAGENAME = | |||
if not lang then | if not lang then | ||
Line 152: | Line 93: | ||
end | end | ||
-- Generate a default sort key | -- Generate a default sort key | ||
sort_base = lang:makeSortKey(sort_base or SUBPAGENAME, sc) | |||
if sort_key and sort_key ~= "" then | |||
-- Gather some statistics regarding sort keys | |||
if mw.ustring.upper(sort_key) == sort_base then | |||
table.insert(categories, "Sort key tracking/redundant") | |||
end | end | ||
else | else | ||
sort_key = | sort_key = sort_base | ||
end | |||
-- If the sortkey is empty, remove it. | |||
-- Leave the sortkey if it is equal to PAGENAME, because it still | |||
-- might be different from DEFAULTSORT and therefore have an effect; see | |||
-- [[Wiktionary:Grease pit/2020/April#Module:utilities#format categories]]. | |||
if sort_key == "" then | |||
sort_key = nil | |||
end | end | ||
Line 197: | Line 124: | ||
end | end | ||
function export. | -- Used by {{categorize}} | ||
function export.template_categorize(frame) | |||
local NAMESPACE = 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 not lang then | ||
return | 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 | 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 | |||
function export.catfix(lang, sc) | |||
local canonicalName = lang:getCanonicalName() or error('The first argument to the function "catfix" should be a language object from Module:languages.') | local canonicalName = lang:getCanonicalName() or error('The first argument to the function "catfix" should be a language object from Module:languages.') | ||
Line 229: | Line 196: | ||
} | } | ||
local args = require("Module:parameters").process(frame:getParent().args, params | local args = require("Module:parameters").process(frame:getParent().args, params) | ||
local lang = require("Module:languages").getByCode(args[1]) or require("Module:languages").err(args[1], 1) | local lang = require("Module:languages").getByCode(args[1]) or require("Module:languages").err(args[1], 1) | ||
Line 275: | Line 242: | ||
} | } | ||
local args = require("Module:parameters").process(frame:getParent().args, params | local args = require("Module:parameters").process(frame:getParent().args, params) | ||
local langCode = args[1] | local langCode = args[1] | ||
Line 292: | Line 259: | ||
end | end | ||
local | local canonicalName = lang:getCanonicalName() | ||
str = mw.uri.encode(str, "WIKI") | |||
local id = canonicalName .. "-" .. str | |||
if invoked then | if invoked then | ||
Line 299: | Line 270: | ||
return id | return id | ||
end | end | ||
end | end | ||
return export | return export |