Usage

edit

This module implements the {{Asbox}} template. Please see the template page for usage instructions.

Tracking category

edit

local WRAPPER_TEMPLATE, args = 'Template:Asbox'
local templatestyles = 'Asbox/styles.css'
local p, Buffer, stubCats = {
	cats = setmetatable({}, {__newindex = function(t, i, v)
		if not rawget(t, i) then
			rawset(t, i, v)
			table.insert(t, i)
		end
	end}),
	init = function(self, frame, page)
		args, page = args or require('Module:Arguments').getArgs(frame, {
			wrappers = WRAPPER_TEMPLATE
		}), page or mw.title.getCurrentTitle()
		self.demo = self.demo or page.namespace ~= 0 and args.demo
		return args, page
	end
}, require('Module:Buffer')

local attention, catTag, catKey = Buffer'Stub message templates needing attention', '[[Category:%s]]', '%s|%s%s'
local function category(cat)
	for _, v in ipairs((tostring(cat) == cat or cat.t) and {cat} or cat) do
		if v and v ~= true then
			p.cats[catTag:format(tostring(v) == v and
				v
				or (v[1] and Buffer(v[1]) or attention):_in(v.k):_(v.t):_str(2, nil, nil, '|')
			)] = true
		end
	end
	return cat.done and table.concat(p.cats, p.demo and ' | ' or nil) or ''
end

local 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

local function catStub(page, pageDoc)
	stubCats = {missing = {}, v = {}}
	local zwj = '\226\128\141'  -- U+200D, E2 80 8D
	local zwnj = '\226\128\140' -- U+200C, E2 80 8C
	local disallowedUnicodeChars = '[^%w%p%s' .. zwj .. zwnj .. ']' 
	local code
	for k, _ in pairs(args) do
		table.insert(stubCats, string.match(k, '^category(%d*)$'))
	end
	table.sort(stubCats)
	for k, v in ipairs(stubCats) do
		local tsort, cat = args['tempsort' .. v], mw.ustring.gsub(args['category' .. v], disallowedUnicodeChars, '')
		table.insert(stubCats.v,
			 page and (
				v=='' and tsort == 'no'
				or tsort and {cat, k = ' ', t = tsort}
				or {cat, k = ' *', t = page.text}
			)
			or cat
		)
		if page then
			if not mw.title.new('Category:' .. cat).exists then
				code = code or mw.html.create'code':wikitext'|category'
				table.insert(stubCats.missing, tostring(mw.clone(code):wikitext(v)))
			end
			table.insert(stubCats.v, v == '' and not p.demo and pageDoc.exists and
				'Stub message templates with documentation subpages'
				or not cat:match' stubs$' and {k = 'S', t = page.text}
			)
		end
	end
	category(stubCats.v)
	return #stubCats.missing > 0 and ombox{
		'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}
	}
end

local 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 _, v in ipairs(stubCats) do
		table.insert(wikitext, base:format(
			v and args['category' .. v] or '{{{category}}}',
			v and mw.site.stats.pagesInCategory(args['category' .. v], 'all') or 0
		))
	end
	return table.concat(wikitext)
end

function p.templatepage(frame, page)
	args, page = p:init(frame, page)
	local tStubDoc = mw.title.new'Template:Stub documentation'
	local pageDoc = page:subPageTitle('doc')
	return Buffer(
		ombox{
			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>|resource</code> or <code>|qualifier</code> must be defined.',
			{k = 'D', t = page.text}
		})
		:_(catStub(page, pageDoc))
		:_(category{
			done = p.demo ~= 'doc',
			'Stub message templates',
			args.icon and
				'Stub message templates using icon parameter'
				or args.image and (
					mw.title.new('Media:' .. mw.text.split(args.image, '|')[1]).exists
					or {k = 'B', t = page.text}
				)
				or 'Stub message templates without images',
			args.imagealt and {k = 'I', t = page.text},
		})
		:_((not p.demo or p.demo == 'doc') and
			require('Module:Documentation').main{
				content = Buffer(page.text ~= 'Stub' and
						require('Module:Asbox stubtree').subtree{args = {pagename = page.text}}
					)
					:_in'\n== About this template ==\nThis template is used to identify a':_(args.subject):_'stub':_(args.qualifier):_out' '
					:_'. It uses {{[[Template:Asbox|asbox]]}}, which is a meta-template designed to ease the process of creating and maintaining stub templates.\n=== Usage ===\nTyping '
					:_(mw.html.create'code'
						:wikitext('{{', page.text == 'Stub' and 'stub' or page.text, '}}')
					)
					:_' produces the message shown at the beginning, and adds the resource 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}
					)
					:_'\n== General information ==\n'
					:_(frame:expandTemplate{title = tStubDoc.text})
					:_'\n\n'(),
				['link box'] = Buffer'This documentation is automatically generated by [[Module:Asbox]].'
					:_in'The general information is transcluded from [[Template:Stub documentation]]. '
						:_(mw.html.create'span'
							:cssText'font-size:smaller;font-style:normal;line-height:130%'
							:node(('([%s edit] | [%s history])'):format(
								tStubDoc:fullUrl('action=edit', 'relative'),
								tStubDoc:fullUrl('action=history', 'relative')
							))
						)
						:_out()
					:_(page.protectionLevels.edit and page.protectionLevels.edit[1] == 'sysop' and
						"This template is [[WV:PROTECT|fully protected]] and any [[WV: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.'
					)' <br/>'
			}
		)()
end

function p.main(frame, page)
	args, page = p:init(frame, page)
	local output = mw.html.create'div'
		:attr{role = 'note'}
		:addClass'metadata plainlinks asbox stub'
		:tag'table'
			:attr{role = 'presentation'}
			:tag'tr'
				:addClass'noresize'
				:node((args.icon or args.image) and
					mw.html.create'td'
						:wikitext(args.icon or ('[[File:%s|%spx|alt=%s]]'):format(
							args.image or '',
							args.pix or '40x30',
							args.imagealt or 'Stub icon'
						))
				)
				:tag'td'
					:tag'p'
						:addClass'asbox-body'
						:wikitext(
							Buffer'This':_(args.subject):_(args.article or 'resource'):_(args.qualifier)' ',
							' is a [[WV:stub|stub]]. You can help Wikiversity by [',
							page:fullUrl('action=edit', 'relative'),
							' expanding it].'
						)
					:done()
					:node(args.note and
						mw.html.create()
							:tag'p'
								:addClass'asbox-note'
								:wikitext(args.note)
							:done()
					)
		:allDone()
		:node(args.name and
			require'Module:Navbar'._navbar{
				args.name,
				mini = 'yes',
			}
			)
	if page.namespace == 0 then -- Main namespace
		category'All stub resources'
		catStub()
	elseif p.demo then
		if p.demo ~= 'doc' then catStub() end
		p.demodoc = p.demo ~= 'art' and p.templatepage(frame, page)
		output = mw.html.create()
			:node(output)
			:tag'small':wikitext(
				'Demo categories: ',
				(category{done = true}:gsub('(%[%[)(Category:)([^|%]]-)(%|)', '%1%2%3|%2%3%4'):gsub('(%[%[)(Category:)', '%1:%2'))
			):done()
			:wikitext(p.demo == 'doc' and p.demodoc or nil)
	else
		local normalizedName = mw.title.new(args.name or '')
		if normalizedName and normalizedName.fullText == page.fullText then
			output = mw.html.create():node(output):wikitext(p.templatepage(frame, page))
		elseif not page.isSubpage and page.namespace == 10 then
			category{{k = args.name and 'E' or 'W', t = page.text}}
		end
	end
	return frame:extensionTag{ name = 'templatestyles', args = { src = templatestyles} } .. tostring(output:wikitext(not p.demo and category{done = true} or nil))
end

return p