Module:Dochead: Difference between revisions
bugfix removing /doc |
autocategorize metatemplates |
||
| (8 intermediate revisions by 2 users not shown) | |||
| Line 6: | Line 6: | ||
local p = {} | local p = {} | ||
-- TODO: | -- TODO: (Low-ish priority): rewrite to eliminate redundant code | ||
-- Produces a hatnote that is placed on the top of a documentation page (either | -- Produces a hatnote that is placed on the top of a documentation page (either | ||
| Line 18: | Line 16: | ||
-- - whether a template has an accompanying module (overridable/toggleable) | -- - whether a template has an accompanying module (overridable/toggleable) | ||
-- - what functions from which modules are invoked | -- - what functions from which modules are invoked | ||
-- Helper function: categorize modules | -- Helper function: categorize modules | ||
| Line 92: | Line 59: | ||
-- other modules. Such modules generally don't have a corresponding | -- other modules. Such modules generally don't have a corresponding | ||
-- template. | -- template. | ||
-- - data/datamodule indicates a module whose purpose is to provide data | |||
-- values to other modules. Like library modules, these don't have a | |||
-- corresponding template. | |||
-- Does the module have a template? | -- Does the module have a template? | ||
-- - dualuse, metatemplate, and noinvoke: YES (REQUIRED!!) | -- - dualuse, metatemplate, and noinvoke: YES (REQUIRED!!) | ||
-- - metamodule/library: GENERALLY NO | -- - metamodule/library and data: GENERALLY NO | ||
local result = "" | local result = "" | ||
if header == "dualuse" then | if header == "dualuse" then | ||
| Line 104: | Line 74: | ||
else | else | ||
result = string.format( | result = string.format( | ||
"This module has a | "This module has a corresponding template that is currently missing or does not use this module. ([[Special:EditPage/Template:%s|edit template]])", | ||
corr_template:gsub("/doc$", "") | corr_template:gsub("/doc$", "") | ||
) | ) | ||
| Line 122: | Line 92: | ||
if has_template then | if has_template then | ||
result = string.format( | result = string.format( | ||
"This module should not be invoked directly; use its corresponding instead: [[Template:%s]].", | "This module should not be invoked directly; use its corresponding template instead: [[Template:%s]].", | ||
corr_template:gsub("/doc$", "") | corr_template:gsub("/doc$", "") | ||
) | ) | ||
| Line 134: | Line 104: | ||
elseif header == "library" or header == "metamodule" then | elseif header == "library" or header == "metamodule" then | ||
result = "This module primarily serves as a library for other modules and has no corresponding template." | result = "This module primarily serves as a library for other modules and has no corresponding template." | ||
elseif header == "data" or pagename:gsub("/doc$", ""):match("/data$") then | |||
result = "This module primarily serves to provide data values for other modules and has no corresponding template." | |||
elseif header == "none" then | |||
result = "" | |||
else | else | ||
if has_template and header ~= "" then | if has_template and header ~= "" then | ||
| Line 152: | Line 128: | ||
return "" .. cats | return "" .. cats | ||
else | else | ||
return string.format(":''%s''%s\n", result, cats) | return string.format(": ''%s''%s\n", result, cats) | ||
end | end | ||
end | end | ||
-- Helper function: categorize template | -- Helper function: categorize template | ||
local function categorize_template(pagename, has_invoke) | local function categorize_template(pagename, has_invoke, is_metatemplate) | ||
local cats = "" | local cats = "" | ||
if pagename:match("/doc$") then | if pagename:match("/doc$") then | ||
| Line 165: | Line 141: | ||
if has_invoke then | if has_invoke then | ||
cats = cats .. " [[Category:Lua-based templates]]" | cats = cats .. " [[Category:Lua-based templates]]" | ||
end | |||
if is_metatemplate then | |||
cats = cats .. " [[Category:Metatemplates]]" | |||
end | end | ||
end | end | ||
| Line 216: | Line 195: | ||
-- Does the template have a module? | -- Does the template have a module? | ||
-- - dualuse and metatemplate: YES (REQUIRED!!) | -- - dualuse and metatemplate: YES (REQUIRED!!) | ||
-- - noinvoke | -- - noinvoke, metamodule/library, and data/datamodule: option's don't apply | ||
-- to templates. | |||
-- RATIONALE FOR NOINVOKE NOT APPLYING: a template may invoke functions from | -- RATIONALE FOR NOINVOKE NOT APPLYING: a template may invoke functions from | ||
-- more than one module, but one of them must be the "main" module. Since | -- more than one module, but one of them must be the "main" module. Since | ||
| Line 255: | Line 235: | ||
end | end | ||
elseif header == "noinvoke" or header == "library" or header == "metamodule" then | elseif header == "noinvoke" or header == "library" or header == "metamodule" or header == "data" or header == "datamodule" then | ||
result = "This template has a header option in the wrong namespace. It should be used within the Module namespace." | result = "This template has a header option in the wrong namespace. It should be used within the Module namespace." | ||
| Line 290: | Line 270: | ||
-- Categorize | -- Categorize | ||
local cats = categorize_template(pagename, #invokes > 0) | local cats = categorize_template(pagename, #invokes > 0, header == "metatemplate") | ||
if result == "" and invocation_hatnote == "" then | if header == "none" then | ||
return cats | |||
elseif result == "" and invocation_hatnote == "" then | |||
return "" | return "" | ||
elseif result == "" and invocation_hatnote ~= "" then | elseif result == "" and invocation_hatnote ~= "" then | ||
return string.format(":''%s''%s\n", invocation_hatnote, cats) | return string.format(": ''%s''%s\n", invocation_hatnote, cats) | ||
elseif result ~= "" and invocation_hatnote == "" then | elseif result ~= "" and invocation_hatnote == "" then | ||
return string.format(":''%s''%s\n", result, cats) | return string.format(": ''%s''%s\n", result, cats) | ||
else | else | ||
return string.format(":''%s''\n:''%s''%s\n", result, invocation_hatnote, cats) | return string.format(": ''%s''\n: ''%s'' %s\n", result, invocation_hatnote, cats) | ||
end | end | ||
end | end | ||
| Line 315: | Line 297: | ||
-- If header is none, skip everything | -- If header is none, skip everything | ||
if header == "none" then | if header == "none" then | ||
return categorize(namespace, pagename) | --return categorize(namespace, pagename) | ||
end | end | ||
| Line 327: | Line 309: | ||
result = p.make_template_hatnote(header, pagename, corr_module, detect_corr_page) | result = p.make_template_hatnote(header, pagename, corr_module, detect_corr_page) | ||
else | else | ||
result = ":''This documentation template is in the wrong namespace. It should be within the Template or Module namespaces.''\n" | result = ": ''This documentation template is in the wrong namespace. It should be within the Template or Module namespaces.''\n" | ||
end | end | ||
| Line 357: | Line 339: | ||
-- Option to detect corresponding page (default true) | -- Option to detect corresponding page (default true) | ||
args["detect_corr_page"] = args["detect_corr_page"] == nil and true or yesno(args["detect_corr_page"]) | args["detect_corr_page"] = args["detect_corr_page"] == nil and true or yesno(args["detect_corr_page"]) | ||
local result = p._dochead(args) | |||
-- Debugger option to show generated WikiText | |||
local wtext = yesno(frame.args["wtext"] or args["wtext"]) | |||
if wtext == true then | |||
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>" | |||
end | |||
return | return frame:preprocess(result) | ||
end | end | ||