Module:Dochead: Difference between revisions

From Xenharmonic Wiki
Jump to navigation Jump to search
Ganaram inukshuk (talk | contribs)
No edit summary
Ganaram inukshuk (talk | contribs)
No edit summary
Line 31: Line 31:
function p.categorize(namespace, pagename, is_lua_based_template)
function p.categorize(namespace, pagename, is_lua_based_template)
local is_lua_based_template = is_lua_based_template or false
local is_lua_based_template = is_lua_based_template or false
local cats = ""
 
if pagename:sub(-4) == "/doc" then
if pagename:sub(-4) == "/doc" then
-- Page is a documentation subpage
-- Documentation subpages
if namespace == "Template" then
if namespace == "Template" then
return "[[Category:Template documentation]]"
cats = "[[Category:Template documentation]]"
elseif namespace == "Module" then
elseif namespace == "Module" then
return "[[Category:Module documentation]]"
cats = "[[Category:Module documentation]]"
end
end
else
else
-- Page is either a template or module page
-- Main pages
if namespace == "Template" then
if namespace == "Template" then
return "[[Category:Templates]]" .. (is_lua_based_template and [[Category:Lua-based templates]] or "")
cats = "[[Category:Templates]]"
if is_lua_based_template then
cats = cats .. " [[Category:Lua-based templates]]"
end
elseif namespace == "Module" then
elseif namespace == "Module" then
return "[[Category:Lua modules]]"
cats = "[[Category:Lua modules]]"
end
end
end
end
 
return ""
return cats
end
end


function p._dochead(args)
function p._dochead(args)
local namespace = args["namespace"]
local namespace = args["namespace"]
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"] or "Template:" .. pagename
local corr_template = args["temp"] or ("Template:" .. pagename)
local corr_module  = args["mod" ] or "Module:"  .. pagename
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 "" .. p.categorize(namespace, pagename) end
if header == "none" then
return p.categorize(namespace, pagename)
end
 
local result = ""
local result = ""
if namespace == "Module" then
local has_template = p.page_exists(corr_template)
local has_module  = p.page_exists(corr_module)
if p.page_exists(corr_template) then
 
if header == "library" or header == "metamodule" then header = "" end
-- Handle each header type first
if header == "dualuse" then
if header == "dualuse" then
if namespace == "Module" 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)
if has_template then
elseif header == "metatemplate" then
result = string.format("This module may be invoked by templates using its corresponding [[%s|template]], or used directly from other modules.", corr_template)
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
else
result = table.concat({header, string.format("This module implements [[%s]].", corr_template)}, " ")
result = header
end
end
else
else
if header == "dualuse" or header == "metatemplate" or header == "noinvoke" then header = "" end
result = "This template or its options is in the wrong namespace. It should be used within the Module namespace."
end
if header == "library" or header == "metamodule" then
 
result = string.format("This module primarily serves as a library for other modules.")
elseif header == "metatemplate" then
if namespace == "Module" then
if has_template then
result = string.format("This module implements a metatemplate, and may be invoked by templates using its corresponding [[%s|template]], or used directly from other modules.", corr_template)
else
result = "This module implements a metatemplate."
end
elseif namespace == "Template" then
if has_module then
result = string.format("This template is a metatemplate. It is used to build other templates and should not be used standalone, except for testing or simple usage. This template is implemented by the Lua module [[%s]].", corr_module)
else
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."
end
else
result = "This template or its options is in the wrong namespace. It should be within the Template or Module namespaces."
end
 
elseif header == "noinvoke" then
if namespace == "Module" then
if has_template then
result = string.format("This module should not be invoked directly; use its corresponding [[%s|template]] instead.", corr_template)
else
else
result = header
result = header
end
end
else
result = "This template or its options is in the wrong namespace. It should be used within the Module namespace."
end
elseif header == "library" or header == "metamodule" then
if namespace == "Module" then
result = "This module primarily serves as a library for other modules."
else
result = "This template or its options is in the wrong namespace. It should be used within the Module namespace."
end
end
 
result = result .. p.categorize(namespace, pagename, p.page_exists(corr_template))
else
if namespace == "Module" then
elseif namespace == "Template" then
if has_template then
if header == "noinvoke" then header = "" end
result = string.format("%s This module implements [[%s]].", header, corr_template)
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
else
result = table.concat({header, string.format("This template is implemented by the Lua module [[%s]].")}, " ")
result = header
end
end
else
elseif namespace == "Template" then
if header == "metatemplate" then
if has_module 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."
result = string.format("%s This template is implemented by the Lua module [[%s]].", header, corr_module)
else
else
result = header
result = header
end
end
else
result = "This template or its options is in the wrong namespace. It should be used within the Template or Module namespaces."
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
end
 
return string.format(":''%s''", result)
-- Append categorization info
local has_link = (namespace == "Module" and has_template)
or (namespace == "Template" and has_module)
return string.format(":%s %s", result, p.categorize(namespace, pagename, has_link))
end
end


function p.dochead(frame)
function p.dochead(frame)
local args = getArgs(frame) or {}
local args = getArgs(frame, { removeBlanks = true }) or {}
-- Get current title
local title = mw.title.getCurrentTitle()
local title = mw.title.getCurrentTitle()
 
args["namespace"] = args["namespace"] or title.nsText
args["namespace"] = args["namespace"] or title.nsText
args["pagename" ] = args["pagename" ] or title.text
args["pagename"] = args["pagename"] or title.text
args["header"   ] = args["header"   ] or "noinvoke"
args["header"]   = args["header"]   or args["namespace"] == "Module" and "noinvoke" or ""
args["temp"] = args["temp"] or args["pagename"]
args["temp"]     = args["temp"]     or ("Template:" .. args["pagename"])
args["mod" ] = args["mod" ] or args["pagename"]
args["mod"]       = args["mod"]       or ("Module:" .. args["pagename"])
return p._dochead(args)
return p._dochead(args)
--return args["pagename"]
end
end


return p
return p

Revision as of 07:19, 28 October 2025

Module documentation[view] [edit] [history] [purge]
This module should not be invoked directly; use its corresponding template instead.
Module:Dochead is ready for use. This message indicates that a module is ready for use, or has recently been repaired. This message may be removed once this module has been used on several pages or once it is verified to work as intended.

Details: Functionally complete. Edge-case observations still ongoing. Links can now be viewed on /doc pages.

Introspection summary for Module:Dochead 
Functions provided (4)
Line Function Params
21 page_exists (fullpagename)
31 categorize (namespace, pagename, is_lua_based_template)
57 _dochead (main) (args)
144 dochead (invokable) (frame)
Lua modules required (1)
Variable Module Functions used
getArgs Module:Arguments getArgs

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
	local cats = ""

	if pagename:sub(-4) == "/doc" then
		-- Documentation subpages
		if namespace == "Template" then
			cats = "[[Category:Template documentation]]"
		elseif namespace == "Module" then
			cats = "[[Category:Module documentation]]"
		end
	else
		-- Main pages
		if namespace == "Template" then
			cats = "[[Category:Templates]]"
			if is_lua_based_template then
				cats = cats .. " [[Category:Lua-based templates]]"
			end
		elseif namespace == "Module" then
			cats = "[[Category:Lua modules]]"
		end
	end

	return cats
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 = ""
	local has_template = p.page_exists(corr_template)
	local has_module   = p.page_exists(corr_module)

	-- Handle each header type first
	if header == "dualuse" then
		if namespace == "Module" then
			if has_template then
				result = string.format("This module may be invoked by templates using its corresponding [[%s|template]], or used directly from other modules.", corr_template)
			else
				result = header
			end
		else
			result = "This template or its options is in the wrong namespace. It should be used within the Module namespace."
		end

	elseif header == "metatemplate" then
		if namespace == "Module" then
			if has_template then
				result = string.format("This module implements a metatemplate, and may be invoked by templates using its corresponding [[%s|template]], or used directly from other modules.", corr_template)
			else
				result = "This module implements a metatemplate."
			end
		elseif namespace == "Template" then
			if has_module then
				result = string.format("This template is a metatemplate. It is used to build other templates and should not be used standalone, except for testing or simple usage. This template is implemented by the Lua module [[%s]].", corr_module)
			else
				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."
			end
		else
			result = "This template or its options is in the wrong namespace. It should be within the Template or Module namespaces."
		end

	elseif header == "noinvoke" then
		if namespace == "Module" then
			if has_template then
				result = string.format("This module should not be invoked directly; use its corresponding [[%s|template]] instead.", corr_template)
			else
				result = header
			end
		else
			result = "This template or its options is in the wrong namespace. It should be used within the Module namespace."
		end

	elseif header == "library" or header == "metamodule" then
		if namespace == "Module" then
			result = "This module primarily serves as a library for other modules."
		else
			result = "This template or its options is in the wrong namespace. It should be used within the Module namespace."
		end

	else
		if namespace == "Module" then
			if has_template then
				result = string.format("%s This module implements [[%s]].", header, corr_template)
			else
				result = header
			end
		elseif namespace == "Template" then
			if has_module then
				result = string.format("%s This template is implemented by the Lua module [[%s]].", header, corr_module)
			else
				result = header
			end
		else
			result = "This template or its options is in the wrong namespace. It should be used within the Template or Module namespaces."
		end
	end

	-- Append categorization info
	local has_link = (namespace == "Module" and has_template)
		or (namespace == "Template" and has_module)
	return string.format(":%s %s", result, p.categorize(namespace, pagename, has_link))
end

function p.dochead(frame)
	local args = getArgs(frame, { removeBlanks = true }) or {}
	local title = mw.title.getCurrentTitle()

	args["namespace"] = args["namespace"] or title.nsText
	args["pagename"]  = args["pagename"]  or title.text
	args["header"]    = args["header"]    or args["namespace"] == "Module" and "noinvoke" or ""
	args["temp"]      = args["temp"]      or ("Template:" .. args["pagename"])
	args["mod"]       = args["mod"]       or ("Module:" .. args["pagename"])
	

	return p._dochead(args)
end

return p