Module:Dochead: Difference between revisions
Jump to navigation
Jump to search
implement headers; no categorizing code yet |
No edit summary |
||
| Line 1: | Line 1: | ||
-- This module follows [[User:Ganaram inukshuk/Provisional style guide for Lua]] | -- This module follows [[User:Ganaram inukshuk/Provisional style guide for Lua]] | ||
local getArgs = require("Module:Arguments").getArgs | local getArgs = require("Module:Arguments").getArgs | ||
local p = {} | local p = {} | ||
| Line 22: | Line 20: | ||
-- Detect whether a page exists, where pagename is "Namespace:Title" | -- Detect whether a page exists, where pagename is "Namespace:Title" | ||
function p.page_exists(fullpagename) | function p.page_exists(fullpagename) | ||
local title = mw.title.new(fullpagename) | |||
local title = mw.title.new( | |||
if title and title.exists then | if title and title.exists then | ||
| Line 30: | Line 27: | ||
return false | return false | ||
end | end | ||
end | |||
function p.categorize(namespace, pagename, is_lua_based_template) | |||
local is_lua_based_template = is_lua_based_template or false | |||
if pagename:sub(-4) == "/doc" then | |||
-- Page is a documentation subpage | |||
if namespace == "Template" then | |||
return "[[Category:Template documentation]]" | |||
elseif namespace == "Module" then | |||
return "[[Category:Module documentation]]" | |||
end | |||
else | |||
-- Page is either a template or module page | |||
if namespace == "Template" then | |||
return "[[Category:Templates]]" .. (is_lua_based_template and [[Category:Lua-based templates]] or "") | |||
elseif namespace == "Module" then | |||
return "[[Category:Lua modules]]" | |||
end | |||
end | |||
return "" | |||
end | end | ||
| Line 36: | Line 55: | ||
local pagename = args["pagename" ] | local pagename = args["pagename" ] | ||
local header = args["header" ] or "none" | local header = args["header" ] or "none" | ||
local corr_template = args["temp" | local corr_template = args["temp"] or "Template:" .. pagename | ||
local corr_module = args["mod" | local corr_module = args["mod" ] or "Module:" .. pagename | ||
-- If header is none, don't bother. | -- If header is none, don't bother. | ||
if header == "none" then return "" end | if header == "none" then return "" .. p.categorize(namespace, pagename) end | ||
local result = "" | local result = "" | ||
| Line 46: | Line 65: | ||
if p.page_exists(corr_template) then | if p.page_exists(corr_template) then | ||
if header == "library" or header == "metamodule" then header = "" end | |||
if header == "dualuse" then | if header == "dualuse" then | ||
result = string.format("This module may be invoked by templates by using its corresponding [[%s | template]], or used directly from other modules.", corr_template) | result = string.format("This module may be invoked by templates by using its corresponding [[%s | template]], or used directly from other modules.", corr_template) | ||
| Line 56: | Line 77: | ||
end | end | ||
else | else | ||
if header == "dualuse" or header == "metatemplate" or header == "noinvoke" then header = "" end | |||
if header == "library" or header == "metamodule" then | if header == "library" or header == "metamodule" then | ||
result = string.format("This module primarily serves as a library for other modules.") | result = string.format("This module primarily serves as a library for other modules.") | ||
| Line 62: | Line 85: | ||
end | end | ||
end | end | ||
result = result .. p.categorize(namespace, pagename, p.page_exists(corr_template)) | |||
elseif namespace == "Template" then | elseif namespace == "Template" then | ||
| Line 79: | Line 104: | ||
end | end | ||
end | end | ||
result = result .. p.categorize(namespace, pagename, p.page_exists(corr_module)) | |||
else | else | ||
| Line 94: | Line 121: | ||
args["namespace"] = args["namespace"] or title.nsText | args["namespace"] = args["namespace"] or title.nsText | ||
args["pagename"] | args["pagename" ] = args["pagename" ] or title.text | ||
args["header"] = args["header"] or "noinvoke" | args["header" ] = args["header" ] or "noinvoke" | ||
args["temp"] = args["temp"] or args["pagename"] | |||
args["mod" ] = args["mod" ] or args["pagename"] | |||
return p._dochead(args) | |||
return args[" | --return args["pagename"] | ||
end | end | ||
return p | return p | ||
Revision as of 06:54, 28 October 2025
- '
| Introspection summary for Module:Dochead | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||||||||||||||
No function descriptions were provided. The Lua code may have further information.
-- This module follows [[User:Ganaram inukshuk/Provisional style guide for Lua]]
local getArgs = require("Module:Arguments").getArgs
local p = {}
-- Produces a hatnote that is placed on the top of a documentation page (either
-- template or module documentation) and can autodetect and categorize:
-- - FOR MODULES:
-- - whether a module has an accompanying template (overridable)
-- - whether a module is meant as a library for other modules
-- - FOR TEMPLATES:
-- - whether a template has an accompanying module (overridable)
-- - what functions from which modules are invoked
-- Options for specific types of templates and modules include:
-- - (TEMPLATES) whether it's a metatemplate
-- - (MODULES) whether it's a metamodule (used the same way as a metatemplate,
-- but direct use of module code is allowed)
-- Detect whether a page exists, where pagename is "Namespace:Title"
function p.page_exists(fullpagename)
local title = mw.title.new(fullpagename)
if title and title.exists then
return true
else
return false
end
end
function p.categorize(namespace, pagename, is_lua_based_template)
local is_lua_based_template = is_lua_based_template or false
if pagename:sub(-4) == "/doc" then
-- Page is a documentation subpage
if namespace == "Template" then
return "[[Category:Template documentation]]"
elseif namespace == "Module" then
return "[[Category:Module documentation]]"
end
else
-- Page is either a template or module page
if namespace == "Template" then
return "[[Category:Templates]]" .. (is_lua_based_template and [[Category:Lua-based templates]] or "")
elseif namespace == "Module" then
return "[[Category:Lua modules]]"
end
end
return ""
end
function p._dochead(args)
local namespace = args["namespace"]
local pagename = args["pagename" ]
local header = args["header" ] or "none"
local corr_template = args["temp"] or "Template:" .. pagename
local corr_module = args["mod" ] or "Module:" .. pagename
-- If header is none, don't bother.
if header == "none" then return "" .. p.categorize(namespace, pagename) end
local result = ""
if namespace == "Module" then
if p.page_exists(corr_template) then
if header == "library" or header == "metamodule" then header = "" end
if header == "dualuse" then
result = string.format("This module may be invoked by templates by using its corresponding [[%s | template]], or used directly from other modules.", corr_template)
elseif header == "metatemplate" then
result = string.format("This module implements a metatemplate, and may be invoked by templates by using its corresponding [[%s | template]], or used directly from other modules.", corr_template)
elseif header == "noinvoke" then
result = string.format("This module should not be invoked directly; use its corresponding [[%s | template]] instead.", corr_template)
else
result = table.concat({header, string.format("This module implements [[%s]].", corr_template)}, " ")
end
else
if header == "dualuse" or header == "metatemplate" or header == "noinvoke" then header = "" end
if header == "library" or header == "metamodule" then
result = string.format("This module primarily serves as a library for other modules.")
else
result = header
end
end
result = result .. p.categorize(namespace, pagename, p.page_exists(corr_template))
elseif namespace == "Template" then
if header == "noinvoke" then header = "" end
if p.page_exists(corr_module) then
if header == "metatemplate" then
result = "This template is a metatemplate. It is used to build other templates and should not be used standalone, except for testing or simple usage." .. " " .. string.format("This template is implemented by the Lua module [[%s]].")
else
result = table.concat({header, string.format("This template is implemented by the Lua module [[%s]].")}, " ")
end
else
if header == "metatemplate" then
result = "This template is a metatemplate. It is used to build other templates and should not be used standalone, except for testing or simple usage."
else
result = header
end
end
result = result .. p.categorize(namespace, pagename, p.page_exists(corr_module))
else
result = "This template is in the wrong namespace. It should be used within the Template or Module namespaces."
end
return string.format(":''%s''", result)
end
function p.dochead(frame)
local args = getArgs(frame) or {}
-- Get current title
local title = mw.title.getCurrentTitle()
args["namespace"] = args["namespace"] or title.nsText
args["pagename" ] = args["pagename" ] or title.text
args["header" ] = args["header" ] or "noinvoke"
args["temp"] = args["temp"] or args["pagename"]
args["mod" ] = args["mod" ] or args["pagename"]
return p._dochead(args)
--return args["pagename"]
end
return p