Module:Asbox: Difference between revisions

From Zoophilia Wiki
Jump to navigationJump to search
meta>Codehydro
(comments and some code simplification)
meta>Codehydro
(rewrote p.main as to not need buffer; disabled demo var in article space; more detailed comments)
Line 5: Line 5:
local args, stubCats
local args, stubCats


function demo(text, alt)
--[[
return args.demo and text or alt
Formats category links. Stores them until called with cat.done=true
end
Takes multiple or single categories in the form of 'cat'
 
or a table of strings and/or tables containing parts. (See below)
--Formats category links. Stores them until called with cat.done=true
]]function category(cat)
--Takes multiple or single categories in the form of 'cat' or {'cat1', 'cat2', {'cat3', 'sort3'}}
local attention, link = 'Stub message templates needing attention'
--If it finds a category name of length 1, assumes it is a sort key.
for _, v in ipairs((type(cat) == type'' or cat.t) and {cat} or cat) do--[[
--Such keys must always be paired in a table {{'cat', 'sort'}}
If v is a table:
local attention = 'Stub message templates needing attention'
[1] = full category name; defaults to local attention if blank
function category(cat)
k = Category sort key. Prefix before v.t
for _, v in ipairs(type(cat) == type'' and {cat} or cat) do
t = page.text or args.tempsort#; appended after k or in its place if omitted;t is required if v is not a string
p.key = string.format(
Basically the same as v = (v[1] or attention) .. ' | ' .. (v.k or '') .. v.t
]] link = string.format(
'[[%sCategory:%s]]',
'[[%sCategory:%s]]',
demo(':', ''),
p.demo and ':' or '',
--All user-inputed cats are always paired with a sort key
not v.t and v or string.format(
--Since all non-user cat are longer than 2 chars, we can
p.demo and '%s|Category:%s|%s' or '%s|%s%s',
--compare length instead of checking type
v[1] or attention,
#v > 2 and v or string.format(
p.demo and (v[1] or attention) or v.k or '',
demo('%s|Category:%s|%s', '%s|%s%s'),
p.demo and ((v.k or '') .. v.t) or v.t
#v[1] < 2 and attention or v[1],
#v[1] < 2 and attention or v[1],
#v[1] < 2 and table.concat(v) or v[2]
)
)
)
)
if not p.cats.inv[p.key] then
if not p.cats.inv[link] then
table.insert(p.cats, p.key)
table.insert(p.cats, link)
p.cats.inv[p.key] = #p.cats
p.cats.inv[link] = #p.cats
end
end
end
end
return cat.done and table.concat(p.cats, demo(' | ', '')) or ''
return cat.done and table.concat(p.cats, p.demo and ' | ' or '') or ''
end
end


--Makes an ombox warning; will return an empty string instead
--Makes an ombox warning; will return an empty string instead
--if optional v.ifNot is true
--Takes table {ifNot = Boolean, text, {cat. sort key, cat. sort name}}
function ombox(v)--{text, {subcat, page}}
function ombox(v)
if v.ifNot then
if v.ifNot then
return ''
return ''
Line 51: Line 49:
end
end


 
function catStub(page, pageDoc)
function catStub(page, pageDoc)--p.templatepage passes page; p.main does not
stubCats = {missing = {}, v = {}}
stubCats = {
missing = {},
S = {},
v = {}
}
--Find category parameters and store the number (main cat = '')
for k, _ in pairs(args) do
for k, _ in pairs(args) do
--Find category parameters and store the number (main cat = '')
table.insert(stubCats, string.match(k, '^category(%d*)$'))
table.insert(stubCats, string.match(k, '^category(%d*)$'))
end
end
table.sort(stubCats)
table.sort(stubCats)
local cat, tsort
for k, v in ipairs(stubCats) do
for k, v in ipairs(stubCats) do
--Get category names and, if called by p.templatepage, the optional sort key
--Get category names and, if called by p.templatepage, the optional sort key
cat = args['category' .. v]
local cat, tsort = args['category' .. v], args['tempsort' .. v]
tsort = args['tempsort' .. v]
--Do not place in main category if |tempsort = 'no'; For example, if stub template merely
--Does not place in main category if |tempsort = 'no'
--adds |qualifier= to a meta stub and |category#= to a sub-category of meta's main category
--For example, if stub template simply adds |qualifier= to a meta stub
--and includes a |category#= to a sub-category of the main category
if v ~= '' or tsort ~= 'no' then
if v ~= '' or tsort ~= 'no' then
table.insert(stubCats.v, {cat, page and (tsort or ('*' .. page.text))})
--p.templatepage passes page; p.main does not, i.e. articles are categorized without sort keys.
table.insert(stubCats.v, page and
(tsort and {cat, t = tsort} or {cat, k = '*', t = page.text})
or cat
)
end
end
--Check category existance only if on the template page (i.e. stub documentation)
if page then
if page then
--Check category existance
--This check is only performed on the template page
--since it uses an expensive parser function
if not mw.title.new('Category:' .. cat).exists then
if not mw.title.new('Category:' .. cat).exists then
table.insert(stubCats.missing, '<code>category' .. v .. '</code>')
table.insert(stubCats.missing, '<code>category' .. v .. '</code>')
end
end
table.insert(stubCats.v,
--[[
--Only check for docs if main |category= is set and flags if present.
Checks non-demo stub template for documentation and flags if doc is present.
--Main categories with no docs and all numbered categories
All stub cats names are checked and flagged if it does not match 'Category: [] stub'.
--are checked for proper naming and flagged if not
The main stub cat is exempt from the name check if the stub template has its own doc
v == '' and pageDoc.exists and
(presumably, this doc would have an explanation as to why the main stub cat is non-conforming).
]] table.insert(stubCats.v,
v == '' and not p.demo and pageDoc.exists and
'Stub message templates with documentation subpages'
'Stub message templates with documentation subpages'
or not string.match(cat, ' stubs$') and {'S', page.text}
or not string.match(cat, ' stubs$') and {k = 'S', t = page.text}
or nil
or nil
)
)
end
end
end
end
--Add category names after loop is completed
--Add category names after loop is completed
category(stubCats.v)
category(stubCats.v)
--Out params set to non-categories with <code> tags to ombox
return #stubCats.missing > 0 and ombox{
return stubCats.missing and ombox{
--Changed, original msg:
--Changed, original msg: One or more of the stub categories defined in this template do not seem to exist! Please double-check the parameters {{para|category}}, {{para|category1}} and {{para|category2}}.
--One or more of the stub categories defined in this template do not seem to exist! Please double-check the parameters {{para|category}}, {{para|category1}} and {{para|category2}}.
'The following parameter' .. (#stubCats.missing == 1 and ' defines a stub category that does' or 's define stub categories that do') .. ' not exist: ' .. mw.text.listToText(stubCats.missing),
'The following parameter'
{'N', page.text}
.. (#stubCats.missing == 1 and ' defines a stub category that does' or 's define stub categories that do')
.. ' not exist: ' .. mw.text.listToText(stubCats.missing),
{k = 'N', t = page.text}
} or ''
} or ''
end
end


--Shows population of categories found by catStub()
--Shows population of categories found by catStub(). Outputs demo values if none
--Outputs demo values if there are none
function population()
function population()
local wikitext, base = '', '* [[:Category:%s]] (population: (%s)\n'
local wikitext, base = '', '* [[:Category:%s]] (population: (%s)\n'
if #stubCats == 0 then
if not args.category and stubCats[1] ~= false then
return string.format(base, '{{{category}}}', 0)
table.insert(stubCats, 1, false)
end
end
for k, v in ipairs(stubCats) do
for k, v in ipairs(stubCats) do
wikitext = wikitext .. string.format(base, args['category' .. v], mw.site.stats.pagesInCategory(args['category' .. v], 'all'))
wikitext = wikitext .. string.format(
base,
v and args['category' .. v] or '{{{category}}}',
v and mw.site.stats.pagesInCategory(args['category' .. v], 'all') or 0
)
end
end
return wikitext
return wikitext
end
end


--Flags bad stub templates and creates standard doc page
--Includes standard stub documention and flags stub templates with bad parameter values.
function p.templatepage(frame, page)
function p.templatepage(frame, page)
local tStubDoc = mw.title.new('Template:Stub documentation')
local pageDoc = mw.title.new(page.fullText .. '/doc')
page = page or mw.title.getCurrentTitle()
args = args or require('Module:Arguments').getArgs(frame, {
args = args or require('Module:Arguments').getArgs(frame, {
wrappers = WRAPPER_TEMPLATE
wrappers = WRAPPER_TEMPLATE
})
})
category{
page = page or mw.title.getCurrentTitle()
p.demo = p.demo or page.namespace ~= 0 and args.demo or nil
local tStubDoc = mw.title.new'Template:Stub documentation'
local pageDoc = mw.title.new(page.fullText .. '/doc')
--Reorganization note: Original Asbox alternates between outputting categories and checking on params |category#=.
--Rather than checking multiple times and switching tasks, all stub category param operations have been rolled into catStub()
return ombox{--Show ombox warnings for missing args. Ombox for args with non-existing categories is in catStub()
ifNot = args.category,
'The <code>|category</code> parameter is not set. Please add an appropriate stub category.',
{k = 'C', t = page.text}
}
.. ombox{
ifNot = args.subject or args.article or args.qualifier,
'This stub template contains no description! At least one of the parameters <code>|subject</code>, <code>|article</code> or <code>|qualifier</code> must be defined.',
{k = 'D', t = page.text}
}
.. catStub(page, pageDoc)
.. category{
done = p.demo ~= 'doc',
'Stub message templates',
'Stub message templates',
'Exclude in print',
'Exclude in print',
--Changed. Original Asbox does its first of 3 checks on params |category#= here.
--Rather than checking multiple times, all operations involving those params have been rolled into catStub()
(args.icon and
(args.icon and
'Stub message templates using icon parameter'
'Stub message templates using icon parameter'
Line 133: Line 145:
(mw.title.new('Media:' .. mw.text.split(args.image, '|')[1]).exists and
(mw.title.new('Media:' .. mw.text.split(args.image, '|')[1]).exists and
nil
nil
or {'B', page.text}
or {k = 'B', t = page.text}
)
)
or 'Stub message templates without images'
or 'Stub message templates without images'
)
)
),
),
args.imagealt and {'I', page.text} or nil
args.imagealt and {k = 'I', t = page.text} or nil
}
}
--Show ombox warnings and check for bat stub categories
--Add standard stub template documentation; demo calls will not call Module:Documentation unless p.demo = 'doc'
return ombox{
.. (p.demo and p.demo ~= 'doc' and '' or require('Module:Documentation').main{
ifNot = args.category,
'The <code>|category</code> parameter is not set. Please add an appropriate stub category.',
{'C', page.text}
}
.. ombox{
ifNot = args.subject or args.article or args.qualifier,
'This stub template contains no description! At least one of the parameters <code>|subject</code>, <code>|article</code> or <code>|qualifier</code> must be defined.',
{'D', page.text}
}
.. catStub(page, pageDoc)
--Add standard stub template documentation
.. ((args.demo and args.demo ~= 'doc' and '') or (require('Module:Documentation').main{
content =
content =
(page.text ~= 'Stub' and --This comparison performed in {{Asbox/stubtree}} before it invokes Module:Asbox stubtree
(page.text ~= 'Stub' and--This comparison performed in {{Asbox/stubtree}} before it invokes Module:Asbox stubtree
require('Module:Asbox stubtree').subtree{args = {pagename = page.text}}
require('Module:Asbox stubtree').subtree{args = {pagename = page.text}}
or ''
or ''
Line 168: Line 168:
.. '}}</code> produces the message shown at the beginning, and adds the article to the following categor' .. (#stubCats > 1 and 'ies' or 'y') .. ':\n'
.. '}}</code> produces the message shown at the beginning, and adds the article to the following categor' .. (#stubCats > 1 and 'ies' or 'y') .. ':\n'
.. population()
.. population()
.. (pageDoc.exists and --transclusion of /doc if it exists
.. (pageDoc.exists and--transclusion of /doc if it exists
frame:expandTemplate{title = pageDoc.text}
frame:expandTemplate{title = pageDoc.text}
or ''
or ''
Line 183: Line 183:
.. (page.protectionLevels.edit[1] == 'sysop' and
.. (page.protectionLevels.edit[1] == 'sysop' and
" <br/>This template is [[WP:PROTECT|fully protected]] and any [[WP:CAT|categories]] should be added to the template's ["
" <br/>This template is [[WP:PROTECT|fully protected]] and any [[WP:CAT|categories]] should be added to the template's ["
.. pageDoc:fullURL('action=edit&preload=Template:Category_interwiki/preload', 'relative')
.. pageDoc:fullURL('action=edit&preload=Template:Category_interwiki/preload', 'relative')
.. '| /doc] subpage, which is not protected.'
.. '| /doc] subpage, which is not protected.'
or ''
or ''
)
)
}
})
--Output categories when done.
.. (args.demo ~= 'doc' and category{done = true} or '')))
end
end


--The template as it appears on articles.
function p.main(frame, page)
function p.main(frame, page)
page = page or mw.title.getCurrentTitle()
args = args or require('Module:Arguments').getArgs(frame, {
args = args or require('Module:Arguments').getArgs(frame, {
wrappers = WRAPPER_TEMPLATE
wrappers = WRAPPER_TEMPLATE
})
})
page = page or mw.title.getCurrentTitle()
--Ensures demo parameter will never affect article space output. Necessary because demo now affects category()
p.demo = p.demo or page.namespace ~= 0 and args.demo or nil
local output = mw.html.create()
local output = mw.html.create()
local asbox = output:tag'table'
:tag'table'
:addClass('metadata plainlinks stub')
:addClass('metadata plainlinks stub')
:css('background', 'transparent')
:css('background', 'transparent')
:attr('role', 'presentation')
:attr('role', 'presentation')
if args.icon or args.image then
:node((args.icon or args.image) and
asbox:tag('td'):wikitext(args.icon or string.format(
mw.html.create'td'
'[[File:%s|%spx|alt=%s]]',
:wikitext(args.icon or string.format(
args.image,
'[[File:%s|%spx|alt=%s]]',
args.pix or '40x30',
args.image,
args.imagealt or 'Stub icon'
args.pix or '40x30',
))
args.imagealt or 'Stub icon'
end
))
local buffer = asbox:tag('td')
)
buffer:tag('i'):wikitext(string.format(
:node(
'This %s %s %s is a [[Wikipedia:stub|stub]]. You can help Wikipedia by [%s expanding it].',
mw.html.create'td'
args.subject or '',
:tag'i'
args.article or 'article',
:wikitext(string.format(
args.qualifier or '',
'This %s %s %s is a [[Wikipedia:stub|stub]]. You can help Wikipedia by [%s expanding it].',
page:fullUrl('action=edit', 'relative')
args.subject or '',
))
args.article or 'article',
if args.name then
args.qualifier or '',
buffer:wikitext(require('Module:Navbar')._navbar{
page:fullUrl('action=edit', 'relative')
args.name,
))
mini = 'yes',
:done()
style = 'position: absolute; right: 15px; display: none;'
:wikitext(args.name and
})
require('Module:Navbar')._navbar{
end
args.name,
if args.note then
mini = 'yes',
buffer:tag('br')
style = 'position: absolute; right: 15px; display: none;'
buffer:tag('span')
}
:css('font-style', 'normal')
or nil
:css('font-size', 'smaller')
)
:wikitext(args.note)
:node(args.note and
mw.html.create()
:tag'br':done()
:tag'span'
:css('font-style', 'normal')
:css('font-size', 'smaller')
:wikitext(args.note)
:done()
)
)
:done()
--[[
Stub categories for templates include a sort key (Otherwise all will be indexed under the letter 'T' for 'Template:[] stubs')
Articles using the template do not need a sort key since they have unique names.
When p.demo equals 'doc', the demo stub categories will appear as those for a stub template.
Otherwise, any non-nil p.demo will emulate article space categories (plus any error cats unless set to 'art')
]] if p.demo ~= 'doc' then
catStub()
end
end
if page.namespace == 0 then -- Main namespace
if page.namespace == 0 then -- Main namespace
category'All stub articles'
category'All stub articles'
catStub()
elseif p.demo then
elseif args.demo then
--Unless p.demo is set to 'art', it will also include error categories normally only shown on
--Preview categories; not in original template, but shouldn't hurt
--the template but not in the article. The elseif after namespace == 0 means demo cats will never show in article space.
p.templatepage(frame, page)
p.demodoc = p.demo ~= 'art' and p.templatepage(frame, page) or nil
output:wikitext(string.format('<small>Demo categories: %s</small>', category{done = true}))
output:wikitext(string.format('<small>Demo categories: %s</small>', category{done = true}))
else
else
--Checks for valid name; emulates original template's check using {{FULLPAGENAME:{{{name|}}}}}
local normalizedName = mw.title.new(args.name or '')
local normalizedName = mw.title.new(args.name or '')
if normalizedName and normalizedName.fullText == page.fullText then
if normalizedName and normalizedName.fullText == page.fullText then
output:wikitext(p.templatepage(frame, page))
output:wikitext(p.templatepage(frame, page))
elseif not page.isSubpage and page.namespace == 10 then -- Template namespace and not a subpage
elseif not page.isSubpage and page.namespace == 10 then-- Template namespace and not a subpage
category{{args.name and 'E' or 'W', page.text}}
category{{args.name and 'E' or 'W', page.text}}
end
end
Line 250: Line 267:
return output
return output
:wikitext(
:wikitext(
category{done = not args.demo},
category{done = not p.demo},
args.demo == 'doc' and p.templatepage(frame, page) or nil
p.demo == 'doc' and p.demodoc or nil
)
)
end
end


return p
return p

Revision as of 21:06, 6 January 2015

Documentation for this module may be created at Module:Asbox/doc

local p = {
	cats = {inv = {}}
}
local WRAPPER_TEMPLATE = 'Template:Asbox'
local args, stubCats

--[[
Formats category links. Stores them until called with cat.done=true
Takes multiple or single categories in the form of 'cat'
or a table of strings and/or tables containing parts. (See below)
]]function category(cat)
	local attention, link = 'Stub message templates needing attention'
	for _, v in ipairs((type(cat) == type'' or cat.t) and {cat} or cat) do--[[
If v is a table:
	[1] = full category name; defaults to local attention if blank
	k = Category sort key. Prefix before v.t
	t = page.text or args.tempsort#; appended after k or in its place if omitted;t is required if v is not a string
Basically the same as v = (v[1] or attention) .. ' | ' .. (v.k or '') .. v.t
]]	link = string.format(
			'[[%sCategory:%s]]',
			p.demo and ':' or '',
			not v.t and v or string.format(
				p.demo and '%s|Category:%s|%s' or '%s|%s%s',
				v[1] or attention,
				p.demo and (v[1] or attention) or v.k or '',
				p.demo and ((v.k or '') .. v.t) or v.t
			)
		)
		if not p.cats.inv[link] then
			table.insert(p.cats, link)
			p.cats.inv[link] = #p.cats
		end
	end
	return cat.done and table.concat(p.cats, p.demo and ' | ' or '') or ''
end

--Makes an ombox warning; will return an empty string instead
--Takes table {ifNot = Boolean, text, {cat. sort key, cat. sort name}}
function ombox(v)
	if v.ifNot then
		return ''
	end
	p.ombox = p.ombox or require('Module:Message box').ombox
	category{v[2]}
	return p.ombox{
		type = 'content',
		text = v[1]
	}
end

function catStub(page, pageDoc)
	stubCats = {missing = {}, v = {}}
	for k, _ in pairs(args) do
--Find category parameters and store the number (main cat = '')
		table.insert(stubCats, string.match(k, '^category(%d*)$'))
	end
	table.sort(stubCats)
	for k, v in ipairs(stubCats) do
--Get category names and, if called by p.templatepage, the optional sort key
		local cat, tsort = args['category' .. v], args['tempsort' .. v]
--Do not place in main category if |tempsort = 'no'; For example, if stub template merely
--adds |qualifier= to a meta stub and |category#= to a sub-category of meta's main category
		if v ~= '' or tsort ~= 'no' then
--p.templatepage passes page; p.main does not, i.e. articles are categorized without sort keys.
			table.insert(stubCats.v, page and
				(tsort and {cat, t = tsort} or {cat, k = '*', t = page.text})
				or cat
			)
		end
--Check category existance only if on the template page (i.e. stub documentation)
		if page then
			if not mw.title.new('Category:' .. cat).exists then
				table.insert(stubCats.missing, '<code>category' .. v .. '</code>')
			end
--[[
Checks non-demo stub template for documentation and flags if doc is present.
All stub cats names are checked and flagged if it does not match 'Category: [] stub'.
The main stub cat is exempt from the name check if the stub template has its own doc
(presumably, this doc would have an explanation as to why the main stub cat is non-conforming).
]]			table.insert(stubCats.v, 
				v == '' and not p.demo and pageDoc.exists and
					'Stub message templates with documentation subpages'
					or not string.match(cat, ' stubs$') and {k = 'S', t = page.text}
					or nil
			)
		end
	end
--Add category names after loop is completed
	category(stubCats.v)
	return #stubCats.missing > 0 and ombox{
--Changed, original msg:
--One or more of the stub categories defined in this template do not seem to exist! Please double-check the parameters {{para|category}}, {{para|category1}} and {{para|category2}}.
		'The following parameter'
			.. (#stubCats.missing == 1 and ' defines a stub category that does' or 's define stub categories that do')
			.. ' not exist: ' .. mw.text.listToText(stubCats.missing),
		{k = 'N', t = page.text}
	} or ''
end

--Shows population of categories found by catStub(). Outputs demo values if none
function population()
	local wikitext, base = '', '* [[:Category:%s]] (population: (%s)\n'
	if not args.category and stubCats[1] ~= false then
		table.insert(stubCats, 1, false)
	end
	for k, v in ipairs(stubCats) do
		wikitext = wikitext .. string.format(
			base,
			v and args['category' .. v] or '{{{category}}}',
			v and mw.site.stats.pagesInCategory(args['category' .. v], 'all') or 0
	)
	end
	return wikitext
end

--Includes standard stub documention and flags stub templates with bad parameter values.
function p.templatepage(frame, page)
	args = args or require('Module:Arguments').getArgs(frame, {
		wrappers = WRAPPER_TEMPLATE
	})
	page = page or mw.title.getCurrentTitle()
	p.demo = p.demo or page.namespace ~= 0 and args.demo or nil
	local tStubDoc = mw.title.new'Template:Stub documentation'
	local pageDoc = mw.title.new(page.fullText .. '/doc')
--Reorganization note: Original Asbox alternates between outputting categories and checking on params |category#=.
--Rather than checking multiple times and switching tasks, all stub category param operations have been rolled into catStub()
	return ombox{--Show ombox warnings for missing args. Ombox for args with non-existing categories is in catStub()
		ifNot = args.category,
		'The <code>|category</code> parameter is not set. Please add an appropriate stub category.',
		{k = 'C', t = page.text}
	}
	.. ombox{
		ifNot = args.subject or args.article or args.qualifier,
		'This stub template contains no description! At least one of the parameters <code>|subject</code>, <code>|article</code> or <code>|qualifier</code> must be defined.',
		{k = 'D', t = page.text}
	}
	.. catStub(page, pageDoc)
	.. category{
		done = p.demo ~= 'doc',
		'Stub message templates',
		'Exclude in print',
		(args.icon and
			'Stub message templates using icon parameter'
			or (args.image and 
				(mw.title.new('Media:' .. mw.text.split(args.image, '|')[1]).exists and
					nil
					or {k = 'B', t = page.text}
				)
				or 'Stub message templates without images'
			)
		),
		args.imagealt and {k = 'I', t = page.text} or nil
	}
--Add standard stub template documentation; demo calls will not call Module:Documentation unless p.demo = 'doc'
	.. (p.demo and p.demo ~= 'doc' and '' or require('Module:Documentation').main{
		content =
			(page.text ~= 'Stub' and--This comparison performed in {{Asbox/stubtree}} before it invokes Module:Asbox stubtree
				require('Module:Asbox stubtree').subtree{args = {pagename = page.text}}
				or ''
			)
			.. '\n== About this template ==\nThis template is used to identify a '
			.. string.format('%s stub%s.', args.subject or '', args.qualifier and (' ' .. args.qualifier) or '')
			.. ' It uses {{[[Template:Asbox|asbox]]}}, which is a meta-template designed to ease the process of creating and maintaining stub templates.\n=== Usage ===\nTyping <code>{{'
			.. (page.text == 'Stub' and
				'stub'
				or page.text
			)
			.. '}}</code> produces the message shown at the beginning, and adds the article to the following categor' .. (#stubCats > 1 and 'ies' or 'y') .. ':\n'
			.. population()
			.. (pageDoc.exists and--transclusion of /doc if it exists
				frame:expandTemplate{title = pageDoc.text}
				or ''
			)
			.. '\n== General information ==\n'--Generic documentation
			.. frame:expandTemplate{title = tStubDoc.text} .. '\n\n'
		,
		['link box'] = 
			string.format(
				'This documentation is automatically generated by [[Template:Asbox/templatepage]].<br>The general information is transcluded from [[Template:Stub documentation]]. <span style="font-size:smaller;font-style:normal;line-height:130%%">([%s edit] | [%s history])</span>',
				tStubDoc:fullUrl('action=edit', 'relative'),
				tStubDoc:fullUrl('action=history', 'relative')
			)
			.. (page.protectionLevels.edit[1] == 'sysop' and
					" <br/>This template is [[WP:PROTECT|fully protected]] and any [[WP:CAT|categories]] should be added to the template's ["
						.. pageDoc:fullURL('action=edit&preload=Template:Category_interwiki/preload', 'relative')
						.. '| /doc] subpage, which is not protected.'
				or ''
			)
	})
end

function p.main(frame, page)
	args = args or require('Module:Arguments').getArgs(frame, {
		wrappers = WRAPPER_TEMPLATE
	})
	page = page or mw.title.getCurrentTitle()
--Ensures demo parameter will never affect article space output. Necessary because demo now affects category()
	p.demo = p.demo or page.namespace ~= 0 and args.demo or nil
	local output = mw.html.create()
		:tag'table'
			:addClass('metadata plainlinks stub')
			:css('background', 'transparent')
			:attr('role', 'presentation')
			:node((args.icon or args.image) and
				mw.html.create'td'
					:wikitext(args.icon or string.format(
						'[[File:%s|%spx|alt=%s]]',
						args.image,
						args.pix or '40x30',
						args.imagealt or 'Stub icon'
					))
			)
			:node(
				mw.html.create'td'
					:tag'i'
						:wikitext(string.format(
							'This %s %s %s is a [[Wikipedia:stub|stub]]. You can help Wikipedia by [%s expanding it].',
							args.subject or '',
							args.article or 'article',
							args.qualifier or '',
							page:fullUrl('action=edit', 'relative')
						))
						:done()
					:wikitext(args.name and
						require('Module:Navbar')._navbar{
							args.name,
							mini = 'yes',
							style = 'position: absolute; right: 15px; display: none;'
						}
						or nil
					)
					:node(args.note and
						mw.html.create()
							:tag'br':done()
							:tag'span'
								:css('font-style', 'normal')
								:css('font-size', 'smaller')
								:wikitext(args.note)
								:done()
					)
			)
			:done()
--[[
Stub categories for templates include a sort key (Otherwise all will be indexed under the letter 'T' for 'Template:[] stubs')
Articles using the template do not need a sort key since they have unique names.
When p.demo equals 'doc', the demo stub categories will appear as those for a stub template.
Otherwise, any non-nil p.demo will emulate article space categories (plus any error cats unless set to 'art')
]]	if p.demo ~= 'doc' then
		catStub()
	end
	if page.namespace == 0 then -- Main namespace
		category'All stub articles'
	elseif p.demo then
--Unless p.demo is set to 'art', it will also include error categories normally only shown on
--the template but not in the article. The elseif after namespace == 0 means demo cats will never show in article space.
		p.demodoc = p.demo ~= 'art' and p.templatepage(frame, page) or nil
		output:wikitext(string.format('<small>Demo categories: %s</small>', category{done = true}))
	else
--Checks for valid name; emulates original template's check using {{FULLPAGENAME:{{{name|}}}}}
		local normalizedName = mw.title.new(args.name or '')
		if normalizedName and normalizedName.fullText == page.fullText then
			output:wikitext(p.templatepage(frame, page))
		elseif not page.isSubpage and page.namespace == 10 then-- Template namespace and not a subpage
			category{{args.name and 'E' or 'W', page.text}}
		end
	end
	return output
		:wikitext(
			category{done = not p.demo},
			p.demo == 'doc' and p.demodoc or nil
		)
end

return p