Module:Asbox: Difference between revisions

From Zoophilia Wiki
Jump to navigationJump to search
meta>Jackmcbarn
(one more note)
meta>Codehydro
(partial implementation of suggestions; more to come)
Line 1: Line 1:
local WRAPPER_TEMPLATE = 'Template:Asbox'
local WRAPPER_TEMPLATE = 'Template:Asbox'
local p = {}
local p = {}


Line 6: Line 5:
local output = mw.html.create('table')
local output = mw.html.create('table')
local page = mw.title.getCurrentTitle()
local page = mw.title.getCurrentTitle()
-- Instead of creating buffer here, you should do it below. (I mention where.)
local buffer = mw.html.create('td')
output
output
:addClass('metadata plainlinks stub')
:addClass('metadata plainlinks stub')
Line 21: Line 18:
))
))
end
end
-- Here's where you should create buffer. You should do it as
local buffer = output:tag('td')
-- output:tag('td') instead of mw.html.create('td') so that you don't have
-- to add it later with node.
buffer
buffer
:tag('i')
:tag('i')
Line 42: Line 37:
end
end
if args.note then
if args.note then
-- mw.html knows that br is always self-closing, so you don't need to specify that.
buffer:tag('br')
buffer:tag('br', {selfClosing = true})
buffer
buffer
:tag('span')
:tag('span')
Line 50: Line 44:
:wikitext(args.note)
:wikitext(args.note)
end
end
-- When you change buffer as I described above, you won't need this anymore.
output
:node(buffer)
output = tostring(output)
output = tostring(output)
if page.namespace == 0 then -- Main namespace
if page.namespace == 0 then -- Main namespace
output = output .. '[[Category:All stub articles]]'
output = output .. '[[Category:All stub articles]]'
for k,v in pairs(args) do
for k,v in pairs(args) do
-- You should use * instead of ? here, so it will match things like category10.
if string.match(k, 'category%d*') then
if string.match(k, 'category%d?') then
output = output .. string.format('[[Category:%s]]', v)
output = output .. string.format('[[Category:%s]]', v)
end
end
Line 65: Line 55:
-- Instead of the title check you do here, see if the parent frame's title is the same as the current page's title.
-- Instead of the title check you do here, see if the parent frame's title is the same as the current page's title.
if not args.demo and page.basePageTitle ~= WRAPPER_TEMPLATE then
if not args.demo and page.basePageTitle ~= WRAPPER_TEMPLATE then
-- mw.title.new('') is always nil.
-- mw.title.new('') is always nil. This is intentional and matches the results of original {{FULLPAGENAME:{{{name|}}}}}
local nameTitle = mw.title.new(args.name or '')
local nameTitle = mw.title.new(args.name or '')
if nameTitle == page then
if nameTitle == page then

Revision as of 18:21, 24 December 2014

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

local WRAPPER_TEMPLATE = 'Template:Asbox'
local p = {}

function p._main(args, frame)
	local output = mw.html.create('table')
	local page = mw.title.getCurrentTitle()
	output
		:addClass('metadata plainlinks stub')
		:css('background','transparent')
		:attr('role','presentation')
	if args.icon or args.image then
		output
			: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 = output: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
	output = tostring(output)
	if page.namespace == 0 then -- Main namespace
		output = output .. '[[Category:All stub articles]]'
		for k,v in pairs(args) do
			if string.match(k, 'category%d*') then
				output = output .. string.format('[[Category:%s]]', v)
			end
		end
	end
	-- Instead of the title check you do here, see if the parent frame's title is the same as the current page's title.
	if not args.demo and page.basePageTitle ~= WRAPPER_TEMPLATE then
		-- mw.title.new('') is always nil. This is intentional and matches the results of original {{FULLPAGENAME:{{{name|}}}}}
		local nameTitle = mw.title.new(args.name or '')
		if nameTitle == page then
			-- This can go away once you un-split main and _main, as I describe below.
			frame = frame or mw.getCurrentFrame()
			-- You should probably convert Template:Asbox/templatepage to Lua too.
			-- Do it in this module, in a function called templatepage.
			output = output .. frame:expandTemplate{
				title = 'Asbox/templatepage',
				args = args
			}
		-- If nameTitle is nil, trying to check nameTitle.isSubpage causes an error.
		elseif not nameTitle.isSubpage and nameTitle.namespace == 10 then -- Template namespace and not a subpage
			output = output .. string.format(
				'[[Category:Stub message templates needing attention|%s]]',
				(args.name and 'E' or 'W') .. page.text
			)
		end
	end
	return output
end

-- Having main and _main split is really only beneficial for modules that get called from other modules a lot, which this module wouldn't be.
function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = WRAPPER_TEMPLATE
	})
	return p._main(args, frame)
end

return p