Module:Asbox: Difference between revisions
From Zoophilia Wiki
Jump to navigationJump to search
meta>Mr. Stradivarius check the type of args keys before doing string matching on them - positional parameters have numeric keys, and attempts to do string matching on them will result in an error |
meta>Codehydro Added Asbox/templatepage |
||
Line 1: | Line 1: | ||
local p = {} | |||
local WRAPPER_TEMPLATE = 'Template:Asbox' | local WRAPPER_TEMPLATE = 'Template:Asbox' | ||
local p = {} | local args, msgBox | ||
function category(cat, name) | |||
local result = '' | |||
if type(cat) == type'' then | |||
cat = {cat} | |||
end | |||
for _, v in ipairs(cat) do | |||
v = (#v > 1 and '' or 'Stub message templates needing attention|') .. v | |||
result = result .. ('[[Category:' .. v .. (name or '') .. ']]') | |||
end | |||
return result | |||
end | |||
function ombox(catSort, text, name) | |||
msgBox = msgBox or require('Module:Message box') | |||
return msgBox.ombox{ | |||
type = 'content', | |||
text = mw.getCurrentFrame():preprocess(text) | |||
} | |||
.. category(catSort, name) | |||
end | |||
function stubCats(page, tempsort) | |||
local wikitext, missingCats, hasDoc = '', 0 | |||
for k, v in pairs(args) do | |||
if type(k) == 'string' then | |||
p.catNum = string.match(k, '^category(%d*)$') | |||
wikitext = wikitext .. ( | |||
tempsort == 'no' and p.catNum == '' and '' | |||
or (p.catNum and | |||
category{v .. (page and --Note: p.templatepage passes page; p.main does not | |||
('|' .. (args['tempsort' .. p.catNum] | |||
or ('*' .. page.text) | |||
)) | |||
or '' | |||
)} | |||
) | |||
or '' | |||
) | |||
if page and p.catNum then | |||
missingCats = missingCats + mw.getCurrentFrame():callParserFunction{ | |||
name = '#ifexist', | |||
args = {'Category:' .. v, 0, 1} | |||
} | |||
hasDoc = false | |||
if p.catNum == '' then | |||
hasDoc = mw.getCurrentFrame():callParserFunction{ | |||
name = '#ifexist', | |||
args = {page.prefixedText .. '/doc', true, false} | |||
} | |||
wikitext = wikitext .. (hasDoc and '[[Category:Stub message templates with documentation subpages]]' or '') | |||
end | |||
if not hasDoc and not string.match(v, ' stubs$') then | |||
wikitext = wikitext .. category('S', page.text) | |||
end | |||
end | |||
end | |||
end | |||
if missingCats > 0 then | |||
wikitext = wikitext .. ombox( | |||
'N', | |||
'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}}.' | |||
) | |||
end | |||
return wikitext | |||
end | |||
function p.templatepage(frame, page) | |||
page = page or mw.title.getCurrentTitle() | |||
args = args or require('Module:Arguments').getArgs(frame, { | |||
wrappers = WRAPPER_TEMPLATE | |||
}) | |||
return category{ | |||
'Stub message templates', | |||
'Exclude in print' | |||
} | |||
.. stubCats(page, args.tempsort) | |||
.. ((args.icon and | |||
'[[Category:Stub message templates using icon parameter]]') | |||
or (args.image and | |||
frame:callParserFunction{ | |||
name = '#ifexist', | |||
args = { | |||
'Media:' .. mw.text.split(args.image, '|')[1], | |||
'', | |||
category('B', page.text) | |||
} | |||
} | |||
or '[[Category:Stub message templates without images]]' | |||
) | |||
) | |||
.. (args.imagealt and | |||
category('I', page.text) | |||
or '' | |||
) | |||
.. (args.category and | |||
'' | |||
or ombox( | |||
'C', | |||
'The {{para|category}} parameter is not set. Please add an appropriate stub category.', | |||
page.text | |||
) | |||
) | |||
.. ((args.subject or args.article or args.qualifier) and | |||
'' | |||
or ombox( | |||
'D', | |||
'This stub template contains no description! At least one of the parameters {{para|subject}}, {{para|article}} or {{para|qualifier}} must be defined.', | |||
page.text | |||
) | |||
) | |||
-- Note: Original Asbox/templatepage checks for valid category names and whether they exist here. | |||
-- Module has already done these at the first call of stubCats, avoiding a repeat checking if category# args are set. | |||
end | |||
function p.main(frame, page) | function p.main(frame, page) | ||
page = page or mw.title.getCurrentTitle() | page = page or mw.title.getCurrentTitle() | ||
args = args or require('Module:Arguments').getArgs(frame, { | |||
wrappers = WRAPPER_TEMPLATE | wrappers = WRAPPER_TEMPLATE | ||
}) | }) | ||
local output = mw.html.create( | local output = mw.html.create() | ||
output | local asbox = output: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 | 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 | end | ||
local buffer = | local buffer = asbox:tag('td') | ||
buffer | 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 | if args.name then | ||
buffer | buffer:wikitext(require('Module:Navbar')._navbar{ | ||
args.name, | |||
mini = 'yes', | |||
style = 'position: absolute; right: 15px; display: none;' | |||
}) | |||
end | end | ||
if args.note then | if args.note then | ||
buffer:tag('br') | buffer:tag('br') | ||
buffer | buffer:tag('span') | ||
:css('font-style', 'normal') | |||
:css('font-size', 'smaller') | |||
:wikitext(args.note) | |||
end | end | ||
if page.namespace == 0 then -- Main namespace | if page.namespace == 0 then -- Main namespace | ||
output | output | ||
:wikitext'[[Category:All stub articles]]' | |||
:wikitext(stubCats()) | |||
elseif 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|}}}}} | --mw.title.new('') is always nil. This is intentional and matches the results of original {{FULLPAGENAME:{{{name|}}}}} | ||
if mw.title.new(args.name or '') == page then | if mw.title.new(args.name or '') == page then | ||
output:wikitext(p.templatepage()) | |||
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 | ||
output | output:wikitext(string.format( | ||
'[[Category:Stub message templates needing attention|%s]]', | '[[Category:Stub message templates needing attention|%s]]', | ||
(args.name and 'E' or 'W') .. page.text | (args.name and 'E' or 'W') .. page.text | ||
) | )) | ||
end | end | ||
end | end |
Revision as of 17:08, 4 January 2015
Documentation for this module may be created at Module:Asbox/doc
local p = {}
local WRAPPER_TEMPLATE = 'Template:Asbox'
local args, msgBox
function category(cat, name)
local result = ''
if type(cat) == type'' then
cat = {cat}
end
for _, v in ipairs(cat) do
v = (#v > 1 and '' or 'Stub message templates needing attention|') .. v
result = result .. ('[[Category:' .. v .. (name or '') .. ']]')
end
return result
end
function ombox(catSort, text, name)
msgBox = msgBox or require('Module:Message box')
return msgBox.ombox{
type = 'content',
text = mw.getCurrentFrame():preprocess(text)
}
.. category(catSort, name)
end
function stubCats(page, tempsort)
local wikitext, missingCats, hasDoc = '', 0
for k, v in pairs(args) do
if type(k) == 'string' then
p.catNum = string.match(k, '^category(%d*)$')
wikitext = wikitext .. (
tempsort == 'no' and p.catNum == '' and ''
or (p.catNum and
category{v .. (page and --Note: p.templatepage passes page; p.main does not
('|' .. (args['tempsort' .. p.catNum]
or ('*' .. page.text)
))
or ''
)}
)
or ''
)
if page and p.catNum then
missingCats = missingCats + mw.getCurrentFrame():callParserFunction{
name = '#ifexist',
args = {'Category:' .. v, 0, 1}
}
hasDoc = false
if p.catNum == '' then
hasDoc = mw.getCurrentFrame():callParserFunction{
name = '#ifexist',
args = {page.prefixedText .. '/doc', true, false}
}
wikitext = wikitext .. (hasDoc and '[[Category:Stub message templates with documentation subpages]]' or '')
end
if not hasDoc and not string.match(v, ' stubs$') then
wikitext = wikitext .. category('S', page.text)
end
end
end
end
if missingCats > 0 then
wikitext = wikitext .. ombox(
'N',
'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}}.'
)
end
return wikitext
end
function p.templatepage(frame, page)
page = page or mw.title.getCurrentTitle()
args = args or require('Module:Arguments').getArgs(frame, {
wrappers = WRAPPER_TEMPLATE
})
return category{
'Stub message templates',
'Exclude in print'
}
.. stubCats(page, args.tempsort)
.. ((args.icon and
'[[Category:Stub message templates using icon parameter]]')
or (args.image and
frame:callParserFunction{
name = '#ifexist',
args = {
'Media:' .. mw.text.split(args.image, '|')[1],
'',
category('B', page.text)
}
}
or '[[Category:Stub message templates without images]]'
)
)
.. (args.imagealt and
category('I', page.text)
or ''
)
.. (args.category and
''
or ombox(
'C',
'The {{para|category}} parameter is not set. Please add an appropriate stub category.',
page.text
)
)
.. ((args.subject or args.article or args.qualifier) and
''
or ombox(
'D',
'This stub template contains no description! At least one of the parameters {{para|subject}}, {{para|article}} or {{para|qualifier}} must be defined.',
page.text
)
)
-- Note: Original Asbox/templatepage checks for valid category names and whether they exist here.
-- Module has already done these at the first call of stubCats, avoiding a repeat checking if category# args are set.
end
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
output
:wikitext'[[Category:All stub articles]]'
:wikitext(stubCats())
elseif 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|}}}}}
if mw.title.new(args.name or '') == page then
output:wikitext(p.templatepage())
elseif not page.isSubpage and page.namespace == 10 then -- Template namespace and not a subpage
output:wikitext(string.format(
'[[Category:Stub message templates needing attention|%s]]',
(args.name and 'E' or 'W') .. page.text
))
end
end
return output
end
return p