Module:Asbox: Difference between revisions
From Zoophilia Wiki
Jump to navigationJump to search
meta>Codehydro mNo edit summary |
meta>Codehydro (replace |link box= value with Lua; streamlined code to) |
||
Line 3: | Line 3: | ||
local args, msgBox | local args, msgBox | ||
local stubCats | local stubCats | ||
function category(cat, name) | function category(cat, name) | ||
Line 21: | Line 20: | ||
return msgBox.ombox{ | return msgBox.ombox{ | ||
type = 'content', | type = 'content', | ||
text = | text = text | ||
} | } | ||
.. category(catSort, name) | .. category(catSort, name) | ||
end | end | ||
function catStub(page, | function catStub(page, pageDoc) | ||
local wikitext, missingCats, hasDoc = '', | local wikitext, missingCats, hasDoc = '', {} | ||
stubCats = {} | stubCats = {} | ||
for k, | for k, _ in pairs(args) do | ||
if type(k) == 'string' then | if type(k) == 'string' then | ||
table.insert(stubCats, string.match(k, '^category(%d*)$')) | table.insert(stubCats, string.match(k, '^category(%d*)$')) | ||
Line 54: | Line 53: | ||
) | ) | ||
if page then | if page then | ||
if not mw.title.new('Category:' .. args[key.cat]).exists then | |||
table.insert(missingCats, key.cat) | |||
end | |||
hasDoc = false | hasDoc = false | ||
if v == '' then | if v == '' then | ||
hasDoc = | hasDoc = pageDoc.exists | ||
wikitext = wikitext .. (hasDoc and '[[Category:Stub message templates with documentation subpages]]' or '') | wikitext = wikitext .. (hasDoc and '[[Category:Stub message templates with documentation subpages]]' or '') | ||
end | end | ||
Line 69: | Line 64: | ||
wikitext = wikitext .. category('S', page.text) | wikitext = wikitext .. category('S', page.text) | ||
end | end | ||
stubCats[k] = {args[key.cat], key.cat} -- | stubCats[k] = {args[key.cat], key.cat}--values to be used in population() | ||
end | end | ||
end | end | ||
if missingCats > 0 then | if #missingCats > 0 then | ||
missingCats.text = '' | |||
for k, meowFeedMe in ipairs(missingCats) do | |||
missingCats.text = missingCats.text | |||
.. (k == #missingCats and #missingCats > 1 and 'and ' or '') | |||
.. '<code class="nowrap">|' .. meowFeedMe .. '</code>' | |||
.. (k == #missingCats and '.' or (#missingCats < 3 and ' ' or ', ')) | |||
end | |||
wikitext = wikitext .. ombox( | wikitext = wikitext .. ombox( | ||
'N', | 'N', | ||
--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' | |||
.. (#missingCats == 1 and | |||
' defines a stub category that does' | |||
or 's define stub categories that do' | |||
) | |||
.. ' not exist: ' .. missingCats.text, | |||
page.text | |||
) | ) | ||
end | end | ||
Line 93: | Line 102: | ||
function p.templatepage(frame, 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() | page = page or mw.title.getCurrentTitle() | ||
args = args or require('Module:Arguments').getArgs(frame, { | args = args or require('Module:Arguments').getArgs(frame, { | ||
Line 101: | Line 112: | ||
'Exclude in print' | 'Exclude in print' | ||
} | } | ||
.. catStub(page, | .. catStub(page, pageDoc) | ||
.. | .. (args.icon and | ||
'[[Category:Stub message templates using icon parameter]]' | '[[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 | |||
'' | |||
or category('B', page.text) | |||
) | |||
or '[[Category:Stub message templates without images]]' | or '[[Category:Stub message templates without images]]' | ||
) | ) | ||
Line 124: | Line 131: | ||
or ombox( | or ombox( | ||
'C', | 'C', | ||
'The | 'The <code class="nowrap">|category</code> parameter is not set. Please add an appropriate stub category.', | ||
page.text | page.text | ||
) | ) | ||
Line 132: | Line 139: | ||
or ombox( | or ombox( | ||
'D', | 'D', | ||
'This stub template contains no description! At least one of the parameters | '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.', | ||
page.text | page.text | ||
) | ) | ||
) | ) | ||
-- | --Changed from original: Original Asbox/templatepage checks categories for valid names and whether they exist here. | ||
--Module has already done so when it called of catStub, avoiding a repeat checking if category# args are set. | --Module has already done so when it called of catStub, avoiding a repeat checking if category# args are set. | ||
.. require('Module:Documentation').main{ | .. require('Module:Documentation').main{ | ||
content = ( | content = ( | ||
(page.text ~= 'Stub' and --This comparison in {{Asbox/stubtree}} before invoking the Module:Asbox stubtree | |||
require('Module:Asbox stubtree').subtree{args = {pagename = page.text}} | require('Module:Asbox stubtree').subtree{args = {pagename = page.text}} | ||
or '' | 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 '') | .. 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. | .. ' 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 | .. (page.text == 'Stub' and | ||
'stub' | 'stub' | ||
Line 153: | Line 160: | ||
.. '}}</code> produces the message shown at the beginning, and adds the article to the following categor' .. (#stubCats > 1 and 'ies' or 'y') .. ':\n' | .. '}}</code> produces the message shown at the beginning, and adds the article to the following categor' .. (#stubCats > 1 and 'ies' or 'y') .. ':\n' | ||
.. population() | .. population() | ||
.. | .. (pageDoc.exists and --transclusion of /doc if it exists | ||
frame:expandTemplate{title = pageDoc.text} | |||
frame:expandTemplate{title = | |||
or '' | or '' | ||
) | ) | ||
.. ' | .. '\n== General information ==\n'--Generic documentation | ||
.. frame:expandTemplate{title = | .. frame:expandTemplate{title = tStubDoc.text} .. '\n\n' | ||
), | ), | ||
['link box'] = | ['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 '' | |||
) | |||
) | |||
} | } | ||
end | end | ||
Line 211: | Line 234: | ||
elseif args.demo then | elseif args.demo then | ||
--not in original Template:Asbox, but shouldn't hurt | --not in original Template:Asbox, but shouldn't hurt | ||
output:wikitext('<small>Demo categories | output:wikitext('<small>Demo categories' .. string.gsub(catStub(), '%[%[', ' | [[:') .. '</small>') | ||
else | |||
local normalizedName = mw.title.new(args.name or '') | |||
if | if normalizedName and normalizedName.fullText == page.fullText then | ||
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 | ||
Line 223: | Line 246: | ||
end | end | ||
end | end | ||
--Uncomment below to see templatepage when previewing Template:Asbox/testcases | |||
--if page.fullText == 'Template:Asbox/testcases' then return output:wikitext(p.templatepage(frame, page)) end | |||
return output | return output | ||
end | end | ||
return p | return p |
Revision as of 00:48, 5 January 2015
Documentation for this module may be created at Module:Asbox/doc
local p = {} local WRAPPER_TEMPLATE = 'Template:Asbox' local args, msgBox local stubCats 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 = text } .. category(catSort, name) end function catStub(page, pageDoc) local wikitext, missingCats, hasDoc = '', {} stubCats = {} for k, _ in pairs(args) do if type(k) == 'string' then table.insert(stubCats, string.match(k, '^category(%d*)$')) end end table.sort(stubCats) local key for k, v in ipairs(stubCats) do key = { cat = 'category' .. v, ts = 'tempsort' .. v } wikitext = wikitext .. (v == '' and args[key.ts] == 'no' and '' or category{args[key.cat] .. (page and--True if catStub called by p.templatepage ('|' .. ( args[key.ts] or ('*' .. page.text) )) or '' ) } ) if page then if not mw.title.new('Category:' .. args[key.cat]).exists then table.insert(missingCats, key.cat) end hasDoc = false if v == '' then hasDoc = pageDoc.exists wikitext = wikitext .. (hasDoc and '[[Category:Stub message templates with documentation subpages]]' or '') end if not hasDoc and not string.match(args[key.cat], ' stubs$') then wikitext = wikitext .. category('S', page.text) end stubCats[k] = {args[key.cat], key.cat}--values to be used in population() end end if #missingCats > 0 then missingCats.text = '' for k, meowFeedMe in ipairs(missingCats) do missingCats.text = missingCats.text .. (k == #missingCats and #missingCats > 1 and 'and ' or '') .. '<code class="nowrap">|' .. meowFeedMe .. '</code>' .. (k == #missingCats and '.' or (#missingCats < 3 and ' ' or ', ')) end wikitext = wikitext .. ombox( 'N', --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' .. (#missingCats == 1 and ' defines a stub category that does' or 's define stub categories that do' ) .. ' not exist: ' .. missingCats.text, page.text ) end return wikitext end function population() local wikitext, base = '', '* [[:Category:%s]] (population: (%s)\n' for k, v in ipairs(stubCats) do wikitext = wikitext .. string.format(base, v[1], mw.site.stats.pagesInCategory(v[1], 'all')) end if wikitext == '' then wikitext = string.format(base, '{{{category}}}', 0) end return wikitext end 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 }) return category{ 'Stub message templates', 'Exclude in print' } .. catStub(page, pageDoc) .. (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 '' ) .. (args.category and '' or ombox( 'C', 'The <code class="nowrap">|category</code> 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 <code class="nowrap">|subject</code>, <code class="nowrap">|article</code> or <code class="nowrap">|qualifier</code> must be defined.', page.text ) ) --Changed from original: Original Asbox/templatepage checks categories for valid names and whether they exist here. --Module has already done so when it called of catStub, avoiding a repeat checking if category# args are set. .. require('Module:Documentation').main{ content = ( (page.text ~= 'Stub' and --This comparison in {{Asbox/stubtree}} before invoking the 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 '' ) ) } 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(catStub()) elseif args.demo then --not in original Template:Asbox, but shouldn't hurt output:wikitext('<small>Demo categories' .. string.gsub(catStub(), '%[%[', ' | [[:') .. '</small>') 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 output:wikitext(string.format( '[[Category:Stub message templates needing attention|%s]]', (args.name and 'E' or 'W') .. page.text )) end end --Uncomment below to see templatepage when previewing Template:Asbox/testcases --if page.fullText == 'Template:Asbox/testcases' then return output:wikitext(p.templatepage(frame, page)) end return output end return p