Module:Asbox: Difference between revisions
From Zoophilia Wiki
Jump to navigationJump to search
meta>Codehydro mNo edit summary |
meta>Codehydro We can now safely include bad templates in Template:Asbox/testcases |
||
Line 1: | Line 1: | ||
local p = {} | local p = { | ||
flags = {}, | |||
cats = {} | |||
} | |||
local WRAPPER_TEMPLATE = 'Template:Asbox' | local WRAPPER_TEMPLATE = 'Template:Asbox' | ||
local args, msgBox | local args, msgBox, stubCats | ||
function category(cat, | function demo(text, alt) | ||
local | return args.demo and text or alt | ||
end | |||
function category(cat)--{{'cat', 'sort'}, 'cat2'} or 'cat' | |||
local attention = 'Stub message templates needing attention' | |||
for _, v in ipairs(type(cat) == type'' and {cat} or cat) do | |||
table.insert(p.cats, '[[' .. demo(':', '') .. 'Category:' .. (#v > 2 and | |||
v | |||
or #v[1] > 1 and v[1] | |||
or (attention .. '|' .. demo(attention .. mw.text.nowiki('|'), '') .. v[1] .. v[2]) | |||
) .. ']]' | |||
) | |||
end | end | ||
return | return cat.done and (demo('Demo categories: ', '') .. table.concat(p.cats, demo(' | ', ''))) or '' | ||
end | end | ||
function ombox( | function ombox(v)--{text, {subcat, page}} | ||
if v.ifNot then | |||
return '' | |||
end | |||
msgBox = msgBox or require('Module:Message box') | msgBox = msgBox or require('Module:Message box') | ||
return msgBox.ombox{ | return msgBox.ombox{ | ||
type = 'content', | type = 'content', | ||
text = | text = v[1] | ||
} | } | ||
.. category | .. category{v[2]} | ||
end | end | ||
function catStub(page, pageDoc) | function catStub(page, pageDoc)--p.templatepage passes page; p.main does not | ||
stubCats = { | |||
hasDoc, | |||
missing = {}, | |||
text = '', | |||
v = {} | |||
} | |||
for k, _ in pairs(args) do | for k, _ in pairs(args) do | ||
table.insert(stubCats, string.match(k, '^category(%d*)$')) | |||
end | end | ||
table.sort(stubCats) | table.sort(stubCats) | ||
local | local cat, tsort | ||
for k, v in ipairs(stubCats) do | for k, v in ipairs(stubCats) do | ||
cat = args['category' .. v] | |||
tsort = args['tempsort' .. v] | |||
if v ~= '' or tsort ~= 'no' then | |||
table.insert(stubCats.v, cat .. (page and | |||
if v ~= '' or | ('|' .. (tsort or ('*' .. page.text))) | ||
or '' | |||
)) | |||
end | end | ||
if page then | if page then | ||
if not mw.title.new('Category:' . | if not mw.title.new('Category:' .. cat).exists then | ||
table.insert( | table.insert(stubCats.missing, 'category' .. v) | ||
end | end | ||
hasDoc = v == '' and pageDoc.exists or false | hasDoc = v == '' and pageDoc.exists or false | ||
stubCats.text = stubCats.text | |||
.. (hasDoc and | .. (hasDoc and | ||
' | category'Stub message templates with documentation subpages' | ||
or (not string.match( | or (not string.match(cat, ' stubs$') and table.insert(stubCats.v, {'S', page.text})) | ||
or '' | or '' | ||
) | ) | ||
end | end | ||
end | end | ||
if # | if #stubCats.missing > 0 then | ||
stubCats.mtext = '' | |||
for k, meowFeedMe in ipairs( | for k, meowFeedMe in ipairs(stubCats.missing) do | ||
stubCats.mtext = stubCats.mtext | |||
.. (k == # | .. (k == #stubCats.missing and #stubCats.missing > 1 and 'and ' or '') | ||
.. '<code class="nowrap">|' .. meowFeedMe .. '</code>' | .. '<code class="nowrap">|' .. meowFeedMe .. '</code>' | ||
.. (k == # | .. (k == #stubCats.missing and '.' or (#stubCats.missing < 3 and ' ' or ', ')) | ||
end | end | ||
stubCats.text = stubCats.text .. ombox{ | |||
--Changed from original: 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 from original: 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' | 'The following parameter' .. (#stubCats.missing == 1 and ' defines a stub category that does' or 's define stub categories that do') .. ' not exist: ' .. stubCats.mtext, | ||
{'N', page.text} | |||
} | |||
page.text | |||
end | end | ||
return | return stubCats.text-- .. category(stubCats.v) | ||
end | end | ||
function population() | function population() | ||
local wikitext, base = '', '* [[:Category:%s]] (population: (%s)\n' | 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 | for k, v in ipairs(stubCats) do | ||
wikitext = wikitext .. string.format(base, v | wikitext = wikitext .. string.format(base, args['category' .. v], mw.site.stats.pagesInCategory(args['category' .. v], 'all')) | ||
end | end | ||
return wikitext | return wikitext | ||
end | end | ||
function p.templatepage(frame, page) | function p.templatepage(frame, page)--flags bad stub templates and creates standard doc 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 110: | Line 108: | ||
'Exclude in print' | 'Exclude in print' | ||
} | } | ||
. | --Changed from original: Original Asbox/templatepage checks categories for valid names and whether they exist here. | ||
--This has been moved to the end of this function | |||
.. (args.icon and | .. (args.icon and | ||
' | category'Stub message templates using icon parameter' | ||
or (args.image and | or (args.image and | ||
(mw.title.new('Media:' .. mw.text.split(args.image, '|')[1]).exists and | (mw.title.new('Media:' .. mw.text.split(args.image, '|')[1]).exists and | ||
'' | '' | ||
or category | or category{{'B', page.text}} | ||
) | ) | ||
or ' | or category'Stub message templates without images' | ||
) | ) | ||
) | ) | ||
.. (args.imagealt and | .. (args.imagealt and | ||
category | category{{'I', page.text}} | ||
or '' | or '' | ||
) | ) | ||
.. | .. ombox{ | ||
ifNot = args.category, | |||
'The <code class="nowrap">|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 class="nowrap">|subject</code>, <code class="nowrap">|article</code> or <code class="nowrap">|qualifier</code> must be defined.', | |||
.. | {'D', page.text} | ||
} | |||
.. catStub(page, pageDoc) | |||
.. ((args.demo and args.demo ~= 'doc' and '') or require('Module:Documentation').main{ | |||
) | |||
content = ( | content = ( | ||
(page.text ~= 'Stub' and --This comparison in {{Asbox/stubtree}} before | (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 183: | Line 176: | ||
) | ) | ||
) | ) | ||
} | }) | ||
.. category{done = true} | |||
end | end | ||
Line 191: | Line 185: | ||
wrappers = WRAPPER_TEMPLATE | wrappers = WRAPPER_TEMPLATE | ||
}) | }) | ||
args.test_cats = 1 | |||
local output = mw.html.create() | local output = mw.html.create() | ||
local asbox = output:tag'table' | local asbox = output:tag'table' | ||
Line 227: | Line 222: | ||
end | end | ||
if page.namespace == 0 then -- Main namespace | if page.namespace == 0 then -- Main namespace | ||
category'All stub articles' | |||
catStub() | |||
elseif args.demo then | elseif args.demo then | ||
--not in original | --Preview categories; not in original template, but shouldn't hurt | ||
p.templatepage(frame, page) | |||
table.sort(p.cats) | |||
else | else | ||
local normalizedName = mw.title.new(args.name or '') | local normalizedName = mw.title.new(args.name or '') | ||
Line 238: | Line 233: | ||
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}} | |||
end | end | ||
end | end | ||
output:wikitext(string.format(args.demo and '<small>%s</small>' or '%s', category{done = true})) | |||
if args.demo == 'doc' then | |||
output:wikitext(p.templatepage(frame, page)) | |||
end | |||
return output | return output | ||
end | end | ||
return p | return p |
Revision as of 18:44, 5 January 2015
Documentation for this module may be created at Module:Asbox/doc
local p = {
flags = {},
cats = {}
}
local WRAPPER_TEMPLATE = 'Template:Asbox'
local args, msgBox, stubCats
function demo(text, alt)
return args.demo and text or alt
end
function category(cat)--{{'cat', 'sort'}, 'cat2'} or 'cat'
local attention = 'Stub message templates needing attention'
for _, v in ipairs(type(cat) == type'' and {cat} or cat) do
table.insert(p.cats, '[[' .. demo(':', '') .. 'Category:' .. (#v > 2 and
v
or #v[1] > 1 and v[1]
or (attention .. '|' .. demo(attention .. mw.text.nowiki('|'), '') .. v[1] .. v[2])
) .. ']]'
)
end
return cat.done and (demo('Demo categories: ', '') .. table.concat(p.cats, demo(' | ', ''))) or ''
end
function ombox(v)--{text, {subcat, page}}
if v.ifNot then
return ''
end
msgBox = msgBox or require('Module:Message box')
return msgBox.ombox{
type = 'content',
text = v[1]
}
.. category{v[2]}
end
function catStub(page, pageDoc)--p.templatepage passes page; p.main does not
stubCats = {
hasDoc,
missing = {},
text = '',
v = {}
}
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
cat = args['category' .. v]
tsort = args['tempsort' .. v]
if v ~= '' or tsort ~= 'no' then
table.insert(stubCats.v, cat .. (page and
('|' .. (tsort or ('*' .. page.text)))
or ''
))
end
if page then
if not mw.title.new('Category:' .. cat).exists then
table.insert(stubCats.missing, 'category' .. v)
end
hasDoc = v == '' and pageDoc.exists or false
stubCats.text = stubCats.text
.. (hasDoc and
category'Stub message templates with documentation subpages'
or (not string.match(cat, ' stubs$') and table.insert(stubCats.v, {'S', page.text}))
or ''
)
end
end
if #stubCats.missing > 0 then
stubCats.mtext = ''
for k, meowFeedMe in ipairs(stubCats.missing) do
stubCats.mtext = stubCats.mtext
.. (k == #stubCats.missing and #stubCats.missing > 1 and 'and ' or '')
.. '<code class="nowrap">|' .. meowFeedMe .. '</code>'
.. (k == #stubCats.missing and '.' or (#stubCats.missing < 3 and ' ' or ', '))
end
stubCats.text = stubCats.text .. ombox{
--Changed from original: 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: ' .. stubCats.mtext,
{'N', page.text}
}
end
return stubCats.text-- .. category(stubCats.v)
end
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
function p.templatepage(frame, page)--flags bad stub templates and creates standard doc 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
})
return category{
'Stub message templates',
'Exclude in print'
}
--Changed from original: Original Asbox/templatepage checks categories for valid names and whether they exist here.
--This has been moved to the end of this function
.. (args.icon and
category'Stub message templates using icon parameter'
or (args.image and
(mw.title.new('Media:' .. mw.text.split(args.image, '|')[1]).exists and
''
or category{{'B', page.text}}
)
or category'Stub message templates without images'
)
)
.. (args.imagealt and
category{{'I', page.text}}
or ''
)
.. ombox{
ifNot = args.category,
'The <code class="nowrap">|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 class="nowrap">|subject</code>, <code class="nowrap">|article</code> or <code class="nowrap">|qualifier</code> must be defined.',
{'D', page.text}
}
.. catStub(page, pageDoc)
.. ((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('%sstub%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'] = (
'This documentation is automatically generated by [[Template:Asbox/templatepage]]. <br/>The general information is transcluded from [[Template:Stub documentation]]. '
.. tostring(mw.html.create'span'
:cssText('font-size:smaller;font-style:normal;line-height:130%')
:wikitext(string.format(
'([%s edit] | [%s history])',
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 ''
)
)
})
.. category{done = true}
end
function p.main(frame, page)
page = page or mw.title.getCurrentTitle()
args = args or require('Module:Arguments').getArgs(frame, {
wrappers = WRAPPER_TEMPLATE
})
args.test_cats = 1
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)
table.sort(p.cats)
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
output:wikitext(string.format(args.demo and '<small>%s</small>' or '%s', category{done = true}))
if args.demo == 'doc' then
output:wikitext(p.templatepage(frame, page))
end
return output
end
return p