Module:Asbox and Module:Asbox/sandbox: Difference between pages

(Difference between pages)
Jump to navigation Jump to search
Page 1
Page 2
(Namespace isn't included in default sort key, update comment appropriately)
 
 
Line 11: Line 11:
 
Both templates had significant contributions from numerous others listed in the revision history tab of their respective pages.
 
Both templates had significant contributions from numerous others listed in the revision history tab of their respective pages.
 
--]]
 
--]]
local WRAPPER_TEMPLATE, args = 'Template:Asbox'
+
local WRAPPER_TEMPLATE = 'Template:Asbox'
local p, Buffer, stubCats = {
+
local args, stubCats
  +
local p = {}
--Prevents dupli-cats... get it? Maybe not?
 
  +
local Buffer = require('Module:Buffer')
cats = setmetatable({}, {__newindex = function(t, i, v)
 
  +
--initializes variables required by both p.main and p.templatepage
if not rawget(t, i) then
 
  +
function p:init(frame, page)
rawset(t, i, v)
 
  +
args = args or require('Module:Arguments').getArgs(frame, {
table.insert(t, i)
 
  +
wrappers = WRAPPER_TEMPLATE
end
 
end}),
+
})
  +
page = page or mw.title.getCurrentTitle()
--initializes variables required by both p.main and p.templatepage
 
  +
--Ensures demo parameter will never affect category() output for articles
init = function(self, frame, page)
 
  +
self.demo = self.demo or page.namespace ~= 0 and args.demo or nil
args, page = args or require('Module:Arguments').getArgs(frame, {
 
  +
return args, page
wrappers = WRAPPER_TEMPLATE
 
  +
end
}), page or mw.title.getCurrentTitle()
 
--Ensures demo parameter will never affect category() output for articles
 
self.demo = self.demo or page.namespace ~= 0 and args.demo
 
return args, page
 
end
 
}, require('Module:Buffer')
 
   
  +
--Prevents dupli-cats... get it? Maybe not?
  +
p.cats = setmetatable({}, {__newindex = function(t, i, v)
  +
if not rawget(t, i) then
  +
rawset(t, i, v)
  +
table.insert(t, i)
  +
end
  +
end})
 
--[[
 
--[[
 
Formats category links. Stores them until called with cat.done=true
 
Formats category links. Stores them until called with cat.done=true
Line 36: Line 38:
 
or a table of strings and/or tables containing parts. (See below)
 
or a table of strings and/or tables containing parts. (See below)
 
]]
 
]]
local attention, catTag, catKey = Buffer'Stub message templates needing attention', '[[Category:%s]]', '%s|%s%s'
 
 
local function category(cat)
 
local function category(cat)
  +
local attention, link = 'Stub message templates needing attention'
for _, v in ipairs((tostring(cat) == cat or cat.t) and {cat} or cat) do
 
  +
for _, v in ipairs((type(cat) == type'' or cat.t) and {cat} or cat) do
 
--[[
 
--[[
 
If v is a table:
 
If v is a table:
Line 47: Line 49:
 
]]
 
]]
 
if v and v ~= true then--reject v = nil, false, or true
 
if v and v ~= true then--reject v = nil, false, or true
p.cats[catTag:format(tostring(v) == v and
+
link = ('[[%sCategory:%s]]'):format(
  +
p.demo and ':' or '',
v
 
  +
type(v) == type'' and
or (v[1] and Buffer(v[1]) or attention):_in(v.k):_(v.t):_str(2, nil, nil, '|')
 
)] = true
+
v
  +
or (p.demo and '%s|Category:%s|%s' or '%s|%s%s'):format(
  +
v[1] or attention,
  +
p.demo and (v[1] or attention) or v.k or '',
  +
p.demo and ((v.k or '') .. v.t) or v.t
  +
)
  +
)
  +
p.cats[link] = true
 
end
 
end
 
end
 
end
Line 86: Line 95:
 
--Get category names and, if called by p.templatepage, the optional sort key
 
--Get category names and, if called by p.templatepage, the optional sort key
 
local tsort, cat = args['tempsort' .. v], mw.ustring.gsub(args['category' .. v], '[^%w%p%s]', '')--remove all hidden unicode chars
 
local tsort, cat = args['tempsort' .. v], mw.ustring.gsub(args['category' .. v], '[^%w%p%s]', '')--remove all hidden unicode chars
--Do not place template in main category if |tempsort = 'no'. However, DO place articles of that template in the main category.
+
--Do not place in main category if |tempsort = 'no'; For example, if stub template merely
  +
--adds |qualifier= to a meta stub and |category#= to a sub-category of meta's main category
table.insert(stubCats.v,
 
  +
if tonumber(v) or tsort ~= 'no' then
page and (--p.templatepage passes page; p.main does not, i.e. articles are categorized without sort keys.
 
  +
--p.templatepage passes page; p.main does not, i.e. articles are categorized without sort keys.
v=='' and tsort == 'no'--if true, inserts 'true' in table, which category() will reject
 
  +
table.insert(stubCats.v, page and
or tsort and {cat, k = ' ', t = tsort}
 
or {cat, k = ' *', t = page.text}--note space in front of sort key
+
(tsort and {cat, k = ' ', t = tsort} or {cat, k = ' *', t = page.text})--space
  +
or cat
 
)
 
)
or cat
+
end
)
 
 
--Check category existance only if on the template page (i.e. stub documentation)
 
--Check category existance only if on the template page (i.e. stub documentation)
 
if page then
 
if page then
Line 203: Line 212:
 
)
 
)
 
:_out()
 
:_out()
:_(page.protectionLevels.edit and page.protectionLevels.edit[1] == 'sysop' and
+
:_(page.protectionLevels.edit[1] == 'sysop' and
 
"This template is [[WP:PROTECT|fully protected]] and any [[WP:CAT|categories]] should be added to the template's ["
 
"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')
 
.. pageDoc:fullUrl('action=edit&preload=Template:Category_interwiki/preload', 'relative')
Line 214: Line 223:
 
function p.main(frame, page)
 
function p.main(frame, page)
 
args, page = p:init(frame, page)
 
args, page = p:init(frame, page)
local output = mw.html.create'table'
+
local output = mw.html.create()
  +
:tag'table'
:addClass'metadata plainlinks stub'
 
  +
:addClass'metadata plainlinks stub'
:css{background = 'transparent'}
 
:attr{role = 'presentation'}
+
:css{background = 'transparent'}
  +
:attr{role = 'presentation'}
:tag'tr'
 
  +
:tag'tr'
:node((args.icon or args.image) and
 
  +
:node((args.icon or args.image) and
mw.html.create'td'
 
  +
mw.html.create'td'
:wikitext(args.icon or ('[[File:%s|%spx|alt=%s]]'):format(
 
args.image or '',
+
:wikitext(args.icon or ('[[File:%s|%spx|alt=%s]]'):format(
args.pix or '40x30',
+
args.image or '',
args.imagealt or 'Stub icon'
+
args.pix or '40x30',
  +
args.imagealt or 'Stub icon'
))
 
)
+
))
:tag'td'
 
:tag'i'
 
:wikitext(
 
Buffer'This':_(args.subject):_(args.article or 'article'):_(args.qualifier)' ',--space
 
' is a [[Wikipedia:stub|stub]]. You can help Wikipedia by [',
 
page:fullUrl('action=edit', 'relative'),
 
' expanding it].'
 
)
 
:done()
 
:node(args.name and
 
require'Module:Navbar'._navbar{
 
args.name,
 
mini = 'yes',
 
style = 'position: absolute; right: 15px; display: none;'
 
}
 
 
)
 
)
:node(args.note and
+
:tag'td'
mw.html.create()
+
:tag'i'
:tag'br':done()
+
:wikitext(
  +
Buffer'This':_(args.subject):_(args.article or 'article'):_(args.qualifier)' ',--space
:tag'span'
 
  +
' is a [[Wikipedia:stub|stub]]. You can help Wikipedia by [',
  +
page:fullUrl('action=edit', 'relative'),
  +
' expanding it].'
  +
)
  +
:done()
  +
:node(args.name and
  +
require'Module:Navbar'._navbar{
  +
args.name,
  +
mini = 'yes',
  +
style = 'position: absolute; right: 15px; display: none;'
  +
}
  +
)
  +
:node(args.note and
  +
mw.html.create'span'
 
:css{['font-style'] = 'normal', ['font-size'] = 'smaller'}
 
:css{['font-style'] = 'normal', ['font-size'] = 'smaller'}
  +
:tag'br':done()
 
:wikitext(args.note)
 
:wikitext(args.note)
:done()
+
)
)
 
 
:allDone()
 
:allDone()
 
--[[
 
--[[
Stub categories for templates include a sort key; this ensures that all stub tags appear at the beginning of their respective categories.
+
Stub categories for templates include a sort key (Otherwise all will be indexed under the letter 'T' for 'Template:[] stubs')
 
Articles using the template do not need a sort key since they have unique names.
 
Articles using the template do not need a sort key since they have unique names.
 
When p.demo equals 'doc', the demo stub categories will appear as those for a stub template.
 
When p.demo equals 'doc', the demo stub categories will appear as those for a stub template.
Line 266: Line 274:
 
--the template but not in the article. The elseif after namespace == 0 means demo cats will never show in article space.
 
--the template but not in the article. The elseif after namespace == 0 means demo cats will never show in article space.
 
p.demodoc = p.demo ~= 'art' and p.templatepage(frame, page)
 
p.demodoc = p.demo ~= 'art' and p.templatepage(frame, page)
output = mw.html.create()
+
output:wikitext(
  +
string.format('<small>Demo categories: %s</small>', category{done = true}),
:node(output)
 
  +
p.demo == 'doc' and p.demodoc or nil
: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
 
else
 
--Checks for valid name; emulates original template's check using {{FULLPAGENAME:{{{name|}}}}}
 
--Checks for valid name; emulates original template's check using {{FULLPAGENAME:{{{name|}}}}}
 
local normalizedName = mw.title.new(args.name or '')
 
local normalizedName = mw.title.new(args.name or '')
 
if normalizedName and normalizedName.fullText == page.fullText then
 
if normalizedName and normalizedName.fullText == page.fullText then
output = mw.html.create():node(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{{k = args.name and 'E' or 'W', t = page.text}}
 
category{{k = args.name and 'E' or 'W', t = page.text}}
 
end
 
end
 
end
 
end
return output:wikitext(not p.demo and category{done = true} or nil)
+
return output:wikitext(category{done = not p.demo})
 
end
 
end