Jump to content

Module:Template category auto topic

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Andrybak (talk | contribs) at 14:13, 19 September 2020 (add suffix ' stub templates'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local getArgs = require('Module:Arguments').getArgs

p = {}

local function main(frame)
	local args = getArgs(frame)
	local title = mw.title.getCurrentTitle()
	local pageName = args.demopage or title.text
	local formatStr = args[1]
	local defaultText = args[2]

	local suffixes = {
		' sidebar templates',
		' stub templates',
		' templates',
		' navboxes',
		' navigational boxes'
	}
	-- possible article name
	local s = pageName
	for _, suffix in pairs(suffixes) do
		if s == pageName then
			s = string.gsub(pageName, suffix, '', 1)
		else
			break
		end
	end
	if s == pageName then
		-- unknown category naming convention
		return defaultText
	end

	local article = mw.title.makeTitle('', s)
	if article.exists then
		return string.format(formatStr, '[[' .. s .. ']]')
	end
	return defaultText
end
p.main = main

return p