Module:Image categories
Documentation for this module may be created at Module:Image categories/doc
-- <pre>
-- TODO:
-- add event categories
local p = {}
local category_map = {
-- Items
['Items'] = 'Detailed item imagesss',
--
--
--
--
--
-- Misc
}
local directCategories = {
-- errors
'Licenses with a non-existent subject'
}
local formats = {
list = '* [[:Category:%s|%s]]',
names = '[[:Category:%s|%s]] ',
default = '[[Category:%s]]'
}
local nilpage = 'random text that wont be a page'
function p.main(frame)
-- arguments
-- may need to rework later
local args = frame:getParent().args
local ttl = mw.title.new(args[1])
if not ttl then
return '[[Category:Licenses with a non-existent subject]]'
end
if ttl.isRedirect then
ttl = frame:preprocess([==[{{safesubst:#dpl:
|noresultsheader=[[:Category:Licenses with a non-existent subject|ERROR]]
|namespace=
|linksfrom=]==]..
(args[1] or nilpage)..
[==[
|ignorecase=true
|count=1
|format=,%TITLE%
}}]==])
else
ttl = args[1] or nilpage
end
-- get text of the categories to add
local txt = frame:preprocess([==[{{safesubst:#dpl:
|noresultsheader=[[:Category:Licenses with a non-existent subject|ERROR]]
|namespace=
|title=]==]..
ttl..
[==[
|ignorecase=true
|addcategories=true
|count=1
|format=,%CATLIST%
}}]==])
-- get desired format from user
-- default to adding categories
local frm = mw.text.trim(string.lower(args.format or ''))
frm = formats[frm] or formats.default
-- table of categories to add
local tbl = {}
-- for every category listed, find it and add to overall table
for v in string.gmatch(txt,'%[%[:Category:(.-)%|') do
local s = string.gsub(v,'_',' ')
table.insert(tbl,s)
end
-- get a list of all mission categories
local mission = frame:preprocess([==[{{safesubst:#dpl:
|noresultsheader=
|namespace=Category
|category=Mission related entities
|count=
|format=,%TITLE%¦
}}]==])
-- trim whitespace
missions = mw.text.trim(missions)
-- delete unneeded delimiter
missions = string.gsub(missions,'|$','')
-- split into array
missions = mw.text.split(missions,'|')
local adddirect = {}
-- set each mission name as a valid category
for _, v in ipairs(missions) do
adddirect[v] = true
end
-- set specified direct to add categories as valid
for _, v in ipairs(directCategories) do
adddirect[v] = true
end
-- return list
local ret = {}
for _, v in ipairs(tbl) do
local cat_v = mw.text.trim(v)
local catmapped = category_map[cat_v]
-- map all categories to other categories
if catmapped then
table.insert(ret,string.format(frm,catmapped,catmapped))
-- for everything that's in a directadd category, add that exact category
elseif adddirect[cat_v] then
table.insert(ret,string.format(frm,cat_v,cat_v))
end
end
ret = table.concat(ret,'\n')
return ret
end
function p.list(frame)
local retList = {}
for w, v in pairs(category_map) do
table.insert(retList, { w, v })
end
for _, v in ipairs(directCategories) do
table.insert(retList, { v, v })
end
-- get a list of all mission categories
local missions = frame:preprocess([[{{safesubst:#dpl:
|noresultsheader=
|namespace=Category
|category=Mission related entities
|count=
|format=,%TITLE%¦
}}]])
-- trim whitespace
missions = mw.text.trim(missions)
-- delete unneeded delimiter
missions = string.gsub(missions,'|$','')
-- split into array
missions = mw.text.split(missions,'|')
-- set each mission name as a valid category
for _, v in ipairs(missions) do
table.insert(retList, { v, v })
end
-- sort alphabetically
table.sort(retList,
function(a,b)
local _a = string.lower(a[1])
local _b = string.lower(b[1])
-- remove articles at front
_a = string.gsub(_a,'^the ',''):gsub('^an? ','')
_b = string.gsub(_b,'^the ',''):gsub('^an? ','')
return _a < _b
end
)
local ret =
mw.html.create('table')
:css('border-collapse','collapse')
:tag('tr')
:css('border-bottom','3px solid #000')
:tag('th')
:css('border-bottom','inherit')
:wikitext('Subject category')
:done()
:tag('th')
:css('border-bottom','inherit')
:done()
:tag('th')
:css('border-bottom','inherit')
:wikitext('Image category')
:done()
:done()
for _, v in ipairs(retList) do
ret :tag('tr')
:css({ ['border-bottom'] = '1px dotted #999',
background = (v[1] == v[2]) and '#C9FFD2' or '' })
:tag('td')
:css('border-bottom','inherit')
:wikitext(string.format('[[:Category:%s|%s]]',v[1],v[1]))
:done()
:tag('td')
:css('border-bottom','inherit')
:tag('b')
:wikitext('→')
:done()
:done()
:tag('td')
:css({ ['text-align'] = 'right',
['border-bottom'] = 'inherit' })
:wikitext(string.format('[[:Category:%s|%s]]',v[2],v[2]))
:done()
:done()
end
return ret
end
return p