Module:documentation: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 38: Line 38:
}
}
end
end
local function make_accel_documentation(title, cats)
local doc_content
local lang_code = title.subpageText
local lang = lang_cache[lang_code]
if lang then
cats:insert(lang:getCanonicalName() .. " modules|accel")
cats:insert(("Accel submodules|%s"):format(lang:getCanonicalName()))
return ("This module contains new entry creation rules for %s; see [[WT:ACCEL]] for an overview, and [[Module:accel]] for information on creating new rules.")
:format(lang:makeCategoryLink())
end
return doc_content
end


local function make_Unicode_data_documentation(title, cats)
local function make_Unicode_data_documentation(title, cats)
Line 70: Line 56:
-- This provides categories and documentation for various data modules, so that
-- This provides categories and documentation for various data modules, so that
-- [[Category:Uncategorized modules]] isn’t unnecessarily cluttered.
-- [[Category:Uncategorized modules]] isn’t unnecessarily cluttered.
-- It is a list of tables, each of which have three possible fields:
-- It is a list of tables, each of which have the following possible fields:


-- The “regex” field is required. It is a Lua pattern to match the module’s
-- `regex` (required): A Lua pattern to match the module's title. If it matches, the data in this entry will be used.
-- title. If it matches, the data will be used. If the pattern contains one
-- Any captures in the pattern can by referenced in the `cat` field using %1 for the first capture, %2 for the
-- capture, and a category has been specified, the capture will be used as a
-- second, etc. (often used for creating the sortkey for the category). In addition, the captures are passed to the
-- sortkey.
-- `process` function as the third and subsequent parameters.


-- The “doc” field is required. This may be a function or a
-- `process` (optional): This may be a function or a string. If it is a function, it is called as follows:
-- string. If it is a function, it receives a title and an array (see
--    process(TITLE, CATS, CAPTURE1, CAPTURE2, ...)
-- [[Module:array]]) of categories that will be added to the documentation as
-- where:
-- its first and second arguments, and may return a string, which will be used
--    * TITLE is a title object describing the module's title; see [https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Title_objects].
-- as documentation. If it is a string, it is the name of a submodule under
--    * CATS is an array object (see [[Module:array]]) of categories that the module will be added to.
-- “Module:documentation/functions/which returns a function, of the same type
--    * CAPTURE1, CAPTURE2, ... contain any captures in the `regex` field.
-- as described above.
-- The return value of `process` should be a string and will be used as the module's documentation.
-- If `process` is a string, it is the name of a submodule under 'Module:documentation/functions/' which returns a
-- function, of the same type as described above. If `process` is omitted entirely, the module will have no
-- documentation.


-- The “cat” field is optional. It is a category in which the module should be
-- `cat` (optional): A string naming the category into which the module should be placed. Captures specified in `regex`
-- placed. The inner part of the category link is generated as follows:
-- may be referenced in this string using %1 for the first capture, %2 for the second, etc. Currently, in order to put
-- mw.ustring.gsub(full_title, cat_field)
-- the module into multiple categories, you need to write a `process` function that adds categories to the CATS
-- So if the “cat” field is present, the “regex” field should match the whole
-- parameter passed in.
-- title, and if there is “|%1” at the end of the “cat” field, for a sortkey,
-- there should be one capture in the “regex” field.
local module_regex = {
local module_regex = {
{
{
regex = "^Module:languages/.+$",
regex = "^Module:languages/.+$",
cat = "Modules dealing with languages and scripts",
cat = "Language and script modules",
},
},
{
{
regex = "^Module:scripts/.+$",
regex = "^Module:scripts/.+$",
cat = "Modules dealing with languages and scripts",
cat = "Language and script modules",
},
},
{
{
Line 111: Line 98:
regex = "^Module:zh/data/dial%-pron/.+$",
regex = "^Module:zh/data/dial%-pron/.+$",
cat = "Chinese dialectal pronunciation data modules",
cat = "Chinese dialectal pronunciation data modules",
doc = "zh dial or syn",
process = "zh dial or syn",
},
},
{
{
regex = "^Module:zh/data/dial%-syn/.+$",
regex = "^Module:zh/data/dial%-syn/.+$",
cat = "Chinese dialectal synonyms data modules",
cat = "Chinese dialectal synonyms data modules",
doc = "zh dial or syn",
process = "zh dial or syn",
},
},
{
{
regex = "^Module:zh/data/glyph%-data/.+$",
regex = "^Module:zh/data/glyph%-data/.+$",
cat = "Chinese historical character forms data modules",
cat = "Chinese historical character forms data modules",
doc = function(title, cats)
process = function(title, cats)
local character = title.fullText:match("^Module:zh/data/glyph%-data/(.+)")
local character = title.fullText:match("^Module:zh/data/glyph%-data/(.+)")
if character then
if character then
Line 132: Line 119:
regex = "^Module:zh/data/ltc%-pron/(.+)$",
regex = "^Module:zh/data/ltc%-pron/(.+)$",
cat = "Middle Chinese pronunciation data modules|%1",
cat = "Middle Chinese pronunciation data modules|%1",
doc = "zh data",
process = "zh data",
},
},
{
{
regex = "^Module:zh/data/och%-pron%-BS/(.+)$",
regex = "^Module:zh/data/och%-pron%-BS/(.+)$",
cat = "Old Chinese (Baxter-Sagart) pronunciation data modules|%1",
cat = "Old Chinese (Baxter-Sagart) pronunciation data modules|%1",
doc = "zh data",
process = "zh data",
},
},
{
{
regex = "^Module:zh/data/och%-pron%-ZS/(.+)$",
regex = "^Module:zh/data/och%-pron%-ZS/(.+)$",
cat = "Old Chinese (Zhengzhang) pronunciation data modules|%1",
cat = "Old Chinese (Zhengzhang) pronunciation data modules|%1",
doc = "zh data",
process = "zh data",
},
},
-- capture rest of zh/data submodules
{
{
-- capture rest of zh/data submodules
regex = "^Module:zh/data/(.+)$",
regex = "^Module:zh/data/(.+)$",
cat = "Chinese data modules|%1",
cat = "Chinese data modules|%1",
Line 151: Line 138:
{
{
regex = "^Module:mul/guoxue%-data/cjk%-?(.*)$",
regex = "^Module:mul/guoxue%-data/cjk%-?(.*)$",
cat = nil,
process = "guoxue-data",
doc = "guoxue-data",
},
},
{
{
regex = "^Module:Unicode data/(.+)$",
regex = "^Module:Unicode data/(.+)$",
cat = "Unicode data modules|%1",
cat = "Unicode data modules|%1",
doc = make_Unicode_data_documentation,
process = make_Unicode_data_documentation,
},
},
{
{
regex = "^Module:number list/data/(.+)$",
regex = "^Module:number list/data/(.+)$",
cat = nil, -- category added by doc function
process = "number list",
doc = "number list",
},
{
regex = "^Module:es%-conj/data/(.+)$",
cat = "Spanish data modules|%1"
},
},
{
{
regex = "^Module:accel/(.+)$",
regex = "^Module:accel/(.+)$",
cat = nil,
process = function(title, cats)
doc = make_accel_documentation,
local lang_code = title.subpageText
local lang = lang_cache[lang_code]
if lang then
cats:insert(lang:getCanonicalName() .. " modules|accel")
cats:insert(("Accel submodules|%s"):format(lang:getCanonicalName()))
return ("This module contains new entry creation rules for %s; see [[WT:ACCEL]] for an overview, and [[Module:accel]] for information on creating new rules.")
:format(lang:makeCategoryLink())
end
end,
},
},
{
{
regex = "^Module:inc%-ash/dial/data/(.+)$",
regex = "^Module:inc%-ash/dial/data/(.+)$",
cat = "Ashokan Prakrit modules|%1",
cat = "Ashokan Prakrit modules|%1",
doc = function (title, cats)
process = function(title, cats)
local word = title.fullText:match("^Module:inc%-ash/dial/data/(.+)$")
local word = title.fullText:match("^Module:inc%-ash/dial/data/(.+)$")
if word then
if word then
Line 188: Line 177:
{
{
regex = "^Module:[%l-]+:Dialects$",
regex = "^Module:[%l-]+:Dialects$",
cat = nil,
process = function(title, cats)
doc = function (title, cats)
local content = title:getContent()
local content = title:getContent()
local has_aliases = content:find("aliases") ~= nil
local has_aliases = content:find("aliases") ~= nil
Line 200: Line 188:
{
{
regex = "^.+%-translit$",
regex = "^.+%-translit$",
cat = nil,
process = "translit",
doc = "translit",
},
{
regex = "^Module:category tree/poscatboiler/data/lang%-specific/(.+)$",
process = function(title, cats, lang_code)
local lang = lang_cache[lang_code]
if lang then
local langname = lang:getCanonicalName()
cats:insert("Category tree data modules/poscatboiler|" .. langname)
cats:insert(langname .. " modules")
cats:insert(langname .. " data modules")
return "This module handles generating the descriptions and categorization for " .. langname .. " category pages "
.. "of the format \"" .. langname .. " LABEL\" where LABEL can be any text. Examples are "
.. "[[:Category:Bulgarian conjugation 2.1 verbs]] and [[:Category:Russian velar-stem neuter-form nouns]]. "
.. "This module is part of the poscatboiler system, which is a general framework for generating the "
.. "descriptions and categorization of category pages.\n\n"
.. "For more information, see [[Module:category tree/poscatboiler/data/lang-specific/documentation]]."
end
end
},
{
regex = "^Module:category tree/poscatboiler/data/(.+)$",
cat = "Category tree data modules/poscatboiler|%1",
},
{
regex = "^Module:ja/data/(.+)$",
cat = "Japanese data modules|%1",
},
{
regex = "^Module:Swadesh/data/(.+)$",
cat = "Swadesh modules|%1",
},
{
regex = "^Module:typing%-aids",
process = function(title, cats)
local data_suffix = title.fullText:match("^Module:typing%-aids/data/(.+)$")
if data_suffix then
cats:insert("Data modules")
if data_suffix:find "^[%l-]+$" then
local lang = require "Module:languages".getByCode(data_suffix)
if lang then
cats:insert(lang:getCanonicalName() .. " modules")
end
elseif data_suffix:find "^%u%l%l%l$" then
local script = require "Module:scripts".getByCode(data_suffix)
if script then
cats:insert(script:getCategoryName())
end
end
end
end,
},
{
regex = "^Module:R:([a-z%-]+):(.+)$",
process = function(title, cats, lang_code, refname)
local lang = lang_cache[lang_code]
if lang then
cats:insert(lang:getCanonicalName() .. " modules|" .. refname)
cats:insert(("Reference modules|%s"):format(lang:getCanonicalName()))
return mw.getCurrentFrame():preprocess("This module implements the reference template {{temp|R:" ..
lang_code .. ":" .. refname .. "}}.")
end
end,
},
},
}
}
Line 228: Line 277:
or mw.title.getCurrentTitle()
or mw.title.getCurrentTitle()
local doc_title = mw.title.new((args['from'] ~= "") and args['from']
local doc_title = mw.title.new((args['from'] ~= "") and args['from']
or (title.fullText .. '/doc'))
or (title.fullText .. '/documentation'))
local contentModel = title.contentModel
local contentModel = title.contentModel
Line 281: Line 330:
output:insert(
output:insert(
"<dd><i style=\"font-size: larger;\">The following " ..
"<dd><i style=\"font-size: larger;\">The following " ..
"[[wikt:Help:Documenting templates and modules|documentation]] is located at [[" ..
"[[Help:Documenting templates and modules|documentation]] is located at [[" ..
doc_title.fullText .. "]]. " ..
doc_title.fullText .. "]]. " ..
"<sup>[[" .. doc_title:fullUrl { action = 'edit' } .. " edit]]</sup> </i></dd>")
"<sup>[[" .. doc_title:fullUrl { action = 'edit' } .. " edit]]</sup> </i></dd>")
else
else
if contentModel == "Scribunto" then
if contentModel == "Scribunto" then
local automatic_cat
local automatic_cats = nil
if user_name then
if user_name then
fallback_docs = 'documentation/fallback/user module'
fallback_docs = "documentation/fallback/user module"
automatic_cat = "User sandbox modules"
automatic_cats = {"User sandbox modules"}
else
else
for _, data in ipairs(module_regex) do
for _, data in ipairs(module_regex) do
if mw.ustring.find(title.fullText, data.regex) then
        local captures = {mw.ustring.match(title.fullText, data.regex)}
        if #captures > 0 then
local cat
local cat
local doc_function
local process_function
if type(data.doc) == "function" then
if type(data.process) == "function" then
doc_function = data.doc
process_function = data.process
elseif type(data.doc) == "string" then
elseif type(data.process) == "string" then
doc_content_module = "Module:documentation/functions/" .. data.doc
doc_content_module = "Module:documentation/functions/" .. data.process
doc_function = require(doc_content_module)
process_function = require(doc_content_module)
end
end
if doc_function then
if process_function then
doc_content = doc_function(title, cats)
doc_content = process_function(title, cats, unpack(captures))
end
end
cat = data.cat
cat = data.cat
if cat then
if cat then
automatic_cat = mw.ustring.gsub(title.fullText, data.regex, cat)
if type(cat) == "string" then
-- If the documentation function (cat.doc) manages to
cat = {cat}
-- generate documentation, it should add a category.
end
elseif doc_content then
for _, c in ipairs(cat) do
automatic_cat = nil
-- gsub() and Lua :gsub() return two arguments, which causes all sorts of problems.
-- Terrible design, there should have been a separate two-argument function.
local gsub_sucks = mw.ustring.gsub(title.fullText, data.regex, c)
table.insert(cats, gsub_sucks)
end
end
end
break
break
Line 318: Line 372:
end
end
end
end
 
if not automatic_cats then
if not automatic_cat then
local success, auto_cats =
local success, auto_cats =
pcall(require("Module:module categorization").categorize, frame, true)
pcall(require("Module:module categorization").categorize, frame, true)
Line 329: Line 382:
end
end
end
end
 
if title.subpageText == "templates" then
if title.subpageText == "templates" then
cats:insert("Template interface modules")
cats:insert("Template interface modules")
end
end
 
cats:insert(automatic_cat)
if automatic_cats then
for _, c in ipairs(automatic_cats) do
cats:insert(c)
end
end
-- meaning module is not in user’s sandbox or one of many datamodule boring series
-- meaning module is not in user’s sandbox or one of many datamodule boring series
needs_doc = not (automatic_cat or doc_content or fallback_docs)
needs_doc = not (automatic_cats or doc_content or fallback_docs)
elseif title.nsText == "Template" then
elseif title.nsText == "Template" then
--cats:insert("Uncategorized templates")
--cats:insert("Uncategorized templates")
Line 353: Line 410:
output:insert(
output:insert(
"<dd><i style=\"font-size: larger;\">The following " ..
"<dd><i style=\"font-size: larger;\">The following " ..
"[[wikt:Help:Documenting templates and modules|documentation]] is " ..
"[[Help:Documenting templates and modules|documentation]] is " ..
"generated by [[" .. (doc_content_module or "Module:documentation") .. "]]. <sup>[[" ..
"generated by [[" .. (doc_content_module or "Module:documentation") .. "]]. <sup>[[" ..
mw.title.new("Module:documentation"):fullUrl { action = 'edit' } ..
mw.title.new("Module:documentation"):fullUrl { action = 'edit' } ..
Line 360: Line 417:
output:insert(
output:insert(
"<dd><i style=\"font-size: larger;\">This " .. pagetype ..
"<dd><i style=\"font-size: larger;\">This " .. pagetype ..
" lacks a [[wikt:Help:Documenting templates and modules|documentation subpage]]. " ..
" lacks a [[Help:Documenting templates and modules|documentation subpage]]. " ..
(fallback_docs and "You may " or "Please ") ..
(fallback_docs and "You may " or "Please ") ..
"[" .. doc_title:fullUrl { action = 'edit', preload = preload }
"[" .. doc_title:fullUrl { action = 'edit', preload = preload }
Line 494: Line 551:
if page_exists(sandbox_of) then
if page_exists(sandbox_of) then
diff = " (" .. compare_pages(title.fullText, sandbox_of, "diff") .. ")"
diff = " (" .. compare_pages(title.fullText, sandbox_of, "diff") .. ")"
else
require("Module:debug").track("documentation/no sandbox of")
end
end
Line 525: Line 584:
if title.nsText == "Template" then
if title.nsText == "Template" then
-- Error search: all(any namespace), hastemplate (show pages using the template), insource (show source code), incategory (any/specific error) -- [[mw:Help:CirrusSearch]], [[w:Help:Searching/Regex]]
-- apparently same with/without: &profile=advanced&fulltext=1
local errorq = 'searchengineselect=mediawiki&search=all: hastemplate:\"'..title.rootText..'\" insource:\"'..title.rootText..'\" incategory:'
local eincategory = "Pages_with_module_errors|ParserFunction_errors|DisplayTitle_errors|Pages_with_ISBN_errors|Pages_with_ISSN_errors|Pages_with_reference_errors|Pages_with_syntax_highlighting_errors|Pages_with_TemplateStyles_errors"
links:insert(
'[' .. tostring(mw.uri.fullUrl('Special:Search', errorq..eincategory )) .. ' errors]'
.. ' (' ..
'[' .. tostring(mw.uri.fullUrl('Special:Search', errorq..'ParserFunction_errors' )) .. ' parser]'
.. '/' ..
'[' .. tostring(mw.uri.fullUrl('Special:Search', errorq..'Pages_with_module_errors' )) .. ' module]'
.. ')'
)
if title.isSubpage and title.text:find("/sandbox%d*%f[/%z]") then -- This is a sandbox template.
if title.isSubpage and title.text:find("/sandbox%d*%f[/%z]") then -- This is a sandbox template.
-- At the moment there are no user sandbox templates with subpage
-- At the moment there are no user sandbox templates with subpage
Line 539: Line 612:
if page_exists(sandbox_of) then
if page_exists(sandbox_of) then
diff = " (" .. compare_pages(title.fullText, sandbox_of, "diff") .. ")"
diff = " (" .. compare_pages(title.fullText, sandbox_of, "diff") .. ")"
else
require("Module:debug").track("documentation/no sandbox of")
end
end
Line 559: Line 634:
output:insert("</dl>\n")
output:insert("</dl>\n")
-- Show error from [[Module:category tree/topic cat/data]] on its submodules'
-- documentation to, for instance, warn about duplicate labels.
if title.fullText:find("Module:category tree/topic cat/data", 1, true) == 1 then
local ok, err = pcall(require, "Module:category tree/topic cat/data")
if not ok then
output:insert('<span class="error">' .. err .. '</span>\n\n')
end
end
if doc_title.exists then
if doc_title.exists then