Module:Asbox: Difference between revisions

From Zoophilia Wiki
Jump to navigationJump to search
meta>Codehydro
m irrelevant class name from template:param
meta>Codehydro
comments and some code simplification
Line 9: Line 9:
end
end


--Formats category links. Stores them until called with cat.done=true
--Takes multiple or single categories in the form of 'cat' or {'cat1', 'cat2', {'cat3', 'sort3'}}
--If it finds a category name of length 1, assumes it is a sort key.
--Such keys must always be paired in a table {{'cat', 'sort'}}
local attention = 'Stub message templates needing attention'
local attention = 'Stub message templates needing attention'
function category(cat)--{{'cat', 'sort'}, 'cat2'} or 'cat'
function category(cat)
for _, v in ipairs(type(cat) == type'' and {cat} or cat) do
for _, v in ipairs(type(cat) == type'' and {cat} or cat) do
p.key = string.format(
p.key = string.format(
'[[%sCategory:%s]]',
'[[%sCategory:%s]]',
demo(':', ''),
demo(':', ''),
--All user-inputed cats are always paired with a sort key
--Since all non-user cat are longer than 2 chars, we can
--compare length instead of checking type
#v > 2 and v or string.format(
#v > 2 and v or string.format(
demo('%s|Category:%s|%s', '%s|%s%s'),
demo('%s|Category:%s|%s', '%s|%s%s'),
Line 30: Line 37:
end
end


--Makes an ombox warning; will return an empty string instead
--if optional v.ifNot is true
function ombox(v)--{text, {subcat, page}}
function ombox(v)--{text, {subcat, page}}
if v.ifNot then
if v.ifNot then
Line 41: Line 50:
}
}
end
end


function catStub(page, pageDoc)--p.templatepage passes page; p.main does not
function catStub(page, pageDoc)--p.templatepage passes page; p.main does not
Line 48: Line 58:
v = {}
v = {}
}
}
--Find category parameters and store the number (main cat = '')
for k, _ in pairs(args) do
for k, _ in pairs(args) do
table.insert(stubCats, string.match(k, '^category(%d*)$'))
table.insert(stubCats, string.match(k, '^category(%d*)$'))
Line 54: Line 65:
local cat, tsort
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
cat = args['category' .. v]
cat = args['category' .. v]
tsort = args['tempsort' .. v]
tsort = args['tempsort' .. v]
--Does not place in main category if |tempsort = 'no'
--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))})
table.insert(stubCats.v, {cat, page and (tsort or ('*' .. page.text))})
end
end
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, 'category' .. v)
table.insert(stubCats.missing, '<code>category' .. v .. '</code>')
end
end
table.insert(stubCats.v,  
table.insert(stubCats.v,  
--Only check for docs if main |category= is set and flags if present.
--Main categories with no docs and all numbered categories
--are checked for proper naming and flagged if not
v == '' and pageDoc.exists and
v == '' and pageDoc.exists and
'Stub message templates with documentation subpages'
'Stub message templates with documentation subpages'
Line 71: Line 92:
end
end
end
end
if #stubCats.missing > 0 then
--Add category names after loop is completed
stubCats.missed = {}
for k, meowFeedMe in ipairs(stubCats.missing) do
table.insert(stubCats.missed, '<code>|' .. meowFeedMe .. '</code>')
end
end
category(stubCats.v)
category(stubCats.v)
return stubCats.missed and ombox{
--Out params set to non-categories with <code> tags to ombox
return stubCats.missing 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}}.
--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.missed),
'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),
{'N', page.text}
{'N', page.text}
} or ''
} or ''
end
end


--Shows population of categories found by catStub()
--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'
Line 96: Line 115:
end
end


function p.templatepage(frame, page)--flags bad stub templates and creates standard doc page
--Flags bad stub templates and creates standard doc page
function p.templatepage(frame, page)
local tStubDoc = mw.title.new('Template:Stub documentation')
local tStubDoc = mw.title.new('Template:Stub documentation')
local pageDoc = mw.title.new(page.fullText .. '/doc')
local pageDoc = mw.title.new(page.fullText .. '/doc')
Line 103: Line 123:
wrappers = WRAPPER_TEMPLATE
wrappers = WRAPPER_TEMPLATE
})
})
return category{
category{
'Stub message templates',
'Stub message templates',
'Exclude in print',
'Exclude in print',
Line 120: Line 140:
args.imagealt and {'I', page.text} or nil
args.imagealt and {'I', page.text} or nil
}
}
.. ombox{
--Show ombox warnings and check for bat stub categories
return ombox{
ifNot = args.category,
ifNot = args.category,
'The <code>|category</code> parameter is not set. Please add an appropriate stub category.',
'The <code>|category</code> parameter is not set. Please add an appropriate stub category.',
Line 131: Line 152:
}
}
.. catStub(page, pageDoc)
.. catStub(page, pageDoc)
--Add standard stub template documentation
.. ((args.demo and args.demo ~= 'doc' and '') or (require('Module:Documentation').main{
.. ((args.demo and args.demo ~= 'doc' and '') or (require('Module:Documentation').main{
content =
content =
Line 166: Line 188:
)
)
}
}
--Output categories when done.
.. (args.demo ~= 'doc' and category{done = true} or '')))
.. (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()
page = page or mw.title.getCurrentTitle()
Line 215: Line 239:
--Preview categories; not in original template, but shouldn't hurt
--Preview categories; not in original template, but shouldn't hurt
p.templatepage(frame, page)
p.templatepage(frame, page)
table.sort(p.cats)
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

Revision as of 03:20, 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

function demo(text, alt)
	return args.demo and text or alt
end

--Formats category links. Stores them until called with cat.done=true
--Takes multiple or single categories in the form of 'cat' or {'cat1', 'cat2', {'cat3', 'sort3'}}
--If it finds a category name of length 1, assumes it is a sort key.
--Such keys must always be paired in a table {{'cat', 'sort'}}
local attention = 'Stub message templates needing attention'
function category(cat)
	for _, v in ipairs(type(cat) == type'' and {cat} or cat) do
		p.key = string.format(
			'[[%sCategory:%s]]',
			demo(':', ''),
			--All user-inputed cats are always paired with a sort key
			--Since all non-user cat are longer than 2 chars, we can
			--compare length instead of checking type
			#v > 2 and v or string.format(
				demo('%s|Category:%s|%s', '%s|%s%s'),
				#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
			table.insert(p.cats, p.key)
			p.cats.inv[p.key] = #p.cats
		end
	end
	return cat.done and table.concat(p.cats, demo(' | ', '')) or ''
end

--Makes an ombox warning; will return an empty string instead
--if optional v.ifNot is true
function ombox(v)--{text, {subcat, page}}
	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)--p.templatepage passes page; p.main does not
	stubCats = {
		missing = {},
		S = {},
		v = {}
	}
	--Find category parameters and store the number (main cat = '')
	for k, _ in pairs(args) do
		table.insert(stubCats, string.match(k, '^category(%d*)$'))
	end
	table.sort(stubCats)
	local cat, tsort
	for k, v in ipairs(stubCats) do
		--Get category names and, if called by p.templatepage, the optional sort key
		cat = args['category' .. v]
		tsort = args['tempsort' .. v]
		--Does not place in main category if |tempsort = 'no'
		--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
			table.insert(stubCats.v, {cat, page and (tsort or ('*' .. page.text))})
		end
		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
				table.insert(stubCats.missing, '<code>category' .. v .. '</code>')
			end
			table.insert(stubCats.v, 
				--Only check for docs if main |category= is set and flags if present.
				--Main categories with no docs and all numbered categories
				--are checked for proper naming and flagged if not
				v == '' and pageDoc.exists and
					'Stub message templates with documentation subpages'
					or not string.match(cat, ' stubs$') and {'S', page.text}
					or nil
			)
		end
	end
	--Add category names after loop is completed
	category(stubCats.v)
	--Out params set to non-categories with <code> tags to ombox
	return stubCats.missing 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),
		{'N', page.text}
	} or ''
end

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

--Flags bad stub templates and creates standard doc 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, {
		wrappers = WRAPPER_TEMPLATE
	})
	category{
		'Stub message templates',
		'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
			'Stub message templates using icon parameter'
			or (args.image and 
				(mw.title.new('Media:' .. mw.text.split(args.image, '|')[1]).exists and
					nil
					or {'B', page.text}
				)
				or 'Stub message templates without images'
			)
		),
		args.imagealt and {'I', page.text} or nil
	}
	--Show ombox warnings and check for bat stub categories
	return ombox{
		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 =
			(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 ''
			)
	}
	--Output categories when done.
	.. (args.demo ~= 'doc' and category{done = true} or '')))
end

--The template as it appears on articles.
function p.main(frame, page)
	page = page or mw.title.getCurrentTitle()
	args = args or require('Module:Arguments').getArgs(frame, {
		wrappers = WRAPPER_TEMPLATE
	})
	local output = mw.html.create()
	local asbox = output:tag'table'
		:addClass('metadata plainlinks stub')
		:css('background', 'transparent')
		:attr('role', 'presentation')
	if args.icon or args.image then
		asbox:tag('td'):wikitext(args.icon or string.format(
			'[[File:%s|%spx|alt=%s]]',
			args.image,
			args.pix or '40x30',
			args.imagealt or 'Stub icon'
		))
	end
	local buffer = asbox:tag('td')
	buffer: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')
	))
	if args.name then
		buffer:wikitext(require('Module:Navbar')._navbar{
			args.name,
			mini = 'yes',
			style = 'position: absolute; right: 15px; display: none;'
		})
	end
	if args.note then
		buffer:tag('br')
		buffer:tag('span')
			:css('font-style', 'normal')
			:css('font-size', 'smaller')
			:wikitext(args.note)
	end
	if page.namespace == 0 then -- Main namespace
		category'All stub articles'
		catStub()
	elseif args.demo then
		--Preview categories; not in original template, but shouldn't hurt
		p.templatepage(frame, page)
		output:wikitext(string.format('<small>Demo categories: %s</small>', category{done = true}))
	else
		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 args.demo},
			args.demo == 'doc' and p.templatepage(frame, page) or nil
		)
end

return p