Module:Uses Chart
Appearance
| This module depends on the following other modules: |
Implements {{Uses Chart}}.
A couple test cases
| ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
One style page
Multiple style pages
Sandbox version of style page exists
No style pages specified
Unicode
|
--Based on [[Module:Uses TemplateStyles]]
local yesno = require('Module:Yesno')
local mList = require('Module:List')
local mTableTools = require('Module:TableTools')
local mMessageBox = require('Module:Message box')
local TNT = require('Module:TNT')
local p = {}
local function format(msg, ...)
return TNT.format('I18n/Uses Chart', msg, ...)
end
local function getConfig()
return mw.loadData('Module:Uses Chart/config')
end
local function renderBox(cSources)
local boxArgs = {
type = 'notice',
small = true,
image = string.format('[[File:Chart_extension_logo.svg|32px|alt=%s]]', format('logo-alt'))
}
if #cSources < 1 then
boxArgs.text = format(mw.title.getCurrentTitle():inNamespaces(828,829) and 'emptylist-module' or 'emptylist-template')
else
local cfg = getConfig()
local cSourcesLinks = {}
for i, cs in ipairs(cSources) do
cs = mw.ustring.gsub(mw.ustring.gsub(cs, "^:?[Cc]ommons:", ""), "^[Dd]ata:", "")
local csTitle = mw.title.new(cs)
cs = csTitle.fullText
local link
if csTitle.namespace ~= 828 then
cs = "Data:" .. cs
if not (mw.ustring.find(cs, "%.chart$") or mw.ustring.find(cs, "%.tab$")) then
link = string.format('[[:commons:%s.chart|%s.chart]] ([[:commons:%s.tab|.tab]])', cs, cs, cs)
end
end
link = link or string.format('[[:commons:%s|%s]]', cs, cs)
cSourcesLinks[i] = link
end
local cSourcesList = mList.makeList('bulleted', cSourcesLinks)
boxArgs.text = format(
mw.title.getCurrentTitle():inNamespaces(828,829) and 'header-module' or 'header-template') ..
'\n' .. cSourcesList
end
return mMessageBox.main('mbox', boxArgs)
end
local function renderTrackingCategories(args, cSources, titleObj)
if yesno(args.nocat) then
return ''
end
local cfg = getConfig()
local cats = {}
-- Error category
if #cSources < 1 and cfg['error_category'] then
cats[#cats + 1] = cfg['error_category']
end
-- TemplateStyles category
titleObj = titleObj or mw.title.getCurrentTitle()
if (titleObj.namespace == 10 or titleObj.namespace == 828)
and not cfg['subpage_blacklist'][titleObj.subpageText]
then
local category = args.category or cfg['default_category']
if category then
cats[#cats + 1] = category
end
end
for i, cat in ipairs(cats) do
cats[i] = string.format('[[Category:%s]]', cat)
end
return table.concat(cats)
end
function p._main(args)
local cfg = getConfig()
local cSources = mTableTools.compressSparseArray(args)
local box = renderBox(cSources)
local trackingCategories = renderTrackingCategories(args, cSources)
return box .. trackingCategories
end
function p.main(frame)
local origArgs = frame:getParent().args
local args = {}
for k, v in pairs(origArgs) do
v = v:match('^%s*(.-)%s*$')
if v ~= '' then
args[k] = v
end
end
return p._main(args)
end
return p