Module:Vital article/sandbox
Appearance
| This is the module sandbox page for Module:Vital article (diff). See also the companion subpage for test cases (run). |
local p = {}
local getLink = function(page)
local subpage = mw.ustring.upper(mw.ustring.sub(page, 1, 1)) -- get first letter of article name
local codepoint = mw.ustring.codepoint(page, 1, 1)
if codepoint<65 or codepoint>90 then --first letter is not between A-Z
subpage = 'others'
end
local data_page = 'Wikipedia:Vital articles/data/' .. subpage .. '.json'
local data = mw.loadJsonData(data_page)[page]
if data then
local level = tostring(data.level)
if level then
local trimmed_link = data.trimmed_link
local link
if trimmed_link then
link = 'Wikipedia:Vital articles' .. trimmed_link
else
link = 'Wikipedia:Vital articles/Level/' .. level
if (level=='4' or level=='5') and data.topic then
link = link .. '/' .. data.topic
end
if data.sublist then
link = link .. '/' .. data.sublist
end
--if data.section then -- not used until we have stable section anchors to link to
-- link = link .. '#' .. data.section
--end
end
return link, level
end
end
end
p.link = function(frame)
local page = frame.args.page~='' and frame.args.page or mw.title.getCurrentTitle().subjectPageTitle.text
local link, level = getLink(page)
return '[[' .. page .. ']]' .. (link and ' [[File:Círculos Concéntricos.svg|11px]] [[' .. link .. '|' .. level .. ']]' or '')
end
local top = function(container, frame)
return (container and '' or frame:expandTemplate{title = 'Possibly empty category'})
.. frame:expandTemplate{title = 'Wikipedia category', args = {container = container}}
.. frame:expandTemplate{title = 'CatAutoTOC'}
end
local parse = function(page, pattern, pos1, pos2, cat1, cat2, container)
local regex = '^' .. pattern:gsub('_CLASS', '([%a-]+)'):gsub('_LEVEL', '(%w+)'):gsub('_TOPIC', '([%a ]+)') .. '$'
local match1, match2 = page:match(regex)
if match1 then
local t = {
[pos1] = match1,
[pos2] = pos2 and match2
}
t['_TOPIC'] = t['_TOPIC']=='an unknown topic' and 'Unknown' or t['_TOPIC']
return 'Output:[[Category:' .. cat1:gsub(pos1, t[pos1]):gsub(pos2, t[pos2]) .. ']]'
.. (cat2 and '[[Category:' .. cat2:gsub(pos1, t[pos1]):gsub(pos2, t[pos2]) .. ']]' or '')
end
end
p.header = function(frame)
local cat = frame.args.category~='' and frame.args.category or mw.title.getCurrentTitle().subjectPageTitle.text
local patterns = {
{'Wikipedia vital articles in _TOPIC by class', '_TOPIC', nil, 'Wikipedia vital articles in _TOPIC|Class', 'Wikipedia vital articles by topic by class|_TOPIC'},
{'_CLASS vital articles by topic', '_CLASS', nil, '_CLASS vital articles|Topic]]', 'Wikipedia vital articles by class by topic|_CLASS'},
{'Wikipedia vital articles in _TOPIC by level', '_TOPIC', nil, 'Wikipedia vital articles in _TOPIC|Level', 'Wikipedia vital articles by topic by level|_TOPIC'},
{'Wikipedia level_LEVEL vital articles by topic', '_LEVEL', nil, 'Wikipedia level-_LEVEL vital articles|Topic', 'Wikipedia vital articles by level by topic|_LEVEL'},
{'Wikipedia _CLASS vital articles by level', '_CLASS', nil, '_CLASS vital articles|Level]]', '[[Wikipedia vital articles by class by level|_CLASS'},
{'Wikipedia level_LEVEL vital articles by class', '_LEVEL', nil, 'Wikipedia level-_LEVEL vital articles|Class', 'Wikipedia vital articles by level by class|_LEVEL'},
{'Wikipedia vital articles in _TOPIC', '_TOPIC', 'Wikipedia vital articles by topic|_TOPIC'},
{'Wikipedia level_LEVEL vital articles', '_LEVEL', nil, 'Wikipedia vital articles by level|_LEVEL'},
{'_CLASS level_LEVEL vital articles', '_CLASS', '_LEVEL', 'Wikipedia _CLASS vital articles by level|_LEVEL', 'Wikipedia level-_LEVEL vital articles by class|_CLASS'},
{'_CLASS vital articles', '_CLASS', nil, '_CLASS articles', 'Vital articles by quality|_CLASS'},
{'Wikipedia level_LEVEL vital articles in _TOPIC', '_LEVEL', '_TOPIC', 'Wikipedia vital articles in _TOPIC by level|_LEVEL', 'Wikipedia level-_LEVEL vital articles by topic|_TOPIC'},
{'_CLASS vital articles in _TOPIC', '_CLASS', '_TOPIC', 'Wikipedia vital articles in _TOPIC by class|_CLASS', '_CLASS vital articles by topic|_TOPIC'}
}
end
return p