Module:File

-- local p = {}

local yesno = require('Dev:Yesno') local data = mw.loadData('Module:File/data') local cData = mw.loadData('Module:Cite/data')

local title = mw.title.getCurrentTitle

local function makeRow(html, title, content) html:tag('tr') :tag('th') :wikitext(title) :done :tag('td') :wikitext(content) :done :done end

local template_map = { artwork = function(subject, _, __, args) local author local source if args[1] == 'twitter' then local handle = args[2] if cData.twitter[handle] then author = cData.twitter[handle] else author = handle end source = table.concat({               'handle,                '/status/',                args[3,                ' Twitter]'            }) end return { author = author, description = { 'Artwork of ',               subject,                '.' },           source = source }   end, attack = function(subject, info, ext) return { description = { '',               subject,                '\'s ', info, ' attack ', (ext == 'gif' and 'animation' or 'screenshot'), '.'           },            license     = 'fairuse' }   end, battle = function(subject, info, ext, args) return { description = { '',               subject,                '\'s ', info, (info ==  and  or ' '), 'battle ', (ext == 'gif' and 'animation' or 'sprite'), '.'           },            filename    = args[1], license    = 'fairuse' }   end, face = function(subject, info, _, args) local desc = { '',           subject,            '\'s ' }       if info == 'shop' then table.insert(desc, 'shop') elseif info == 'partymenu' then table.insert(desc, 'party menu') elseif info == 'battlemenu' then table.insert(desc, 'battle menu') else if info ~= '' then table.insert(desc, info) table.insert(desc, ' ') end table.insert(desc, 'face') end table.insert(desc, ' graphic.') return { description = desc, filename   = args[1], license    = 'fairuse' }   end, icon = function(subject, info) return { description = { 'An emoticon of ',               subject,                '', (info ==  and  or ', '), info, '.'           }        }    end, item = function(subject, info, _, args) return { description = { '',               subject,                '\'s ', info, (info ==  and  or ' '), 'item sprite/location.' },           filename    = args[1], license    = 'fairuse' }   end, leitmotif = function(subject, info) return { description = { 'Sheet music for ',               subject,                '\'s', (info ==  and  or ' '), info, ' leitmotif.' }       }    end, location = function(subject) return { description = { 'Screenshot depicting ',               subject,                '.' },           license     = 'fairuse' }   end, map = function(subject, info) return { description = { 'Map of ',               subject,                '', (info ==  and  or '\'s '), info, '.'           }        }    end, overworld = function(subject, info, ext, args) local desc = { '',           subject,            '\'s ' }       if info == 'darkworld' then table.insert(desc, 'Dark World') else if info ~= '' then table.insert(desc, info) table.insert(desc, ' ') end table.insert(desc, 'overworld') end table.insert(desc, ' ') table.insert(desc, ext == 'gif' and 'animation' or 'sprite') table.insert(desc, '.') return { description = desc, filename   = args[1], license    = 'fairuse' }   end, screenshot = function(subject, _, __, args) return { description = args[1] or {'Screenshot of ', subject, '.'}, license    = 'fairuse' }   end, sprite = function(subject, info, ext, args) return { description = { '',               subject,                '\'s ', info, (info ==  and  or ' '), (ext == 'gif' and 'animation' or 'sprite'), '.'           },            filename    = args[1], license    = 'fairuse' }   end, tarot = function(subject) return { description = { '',               subject,                '\'s tarot card.' }       }    end }

template_map.soundtrack = template_map.screenshot

local function mapper(t, spl, ext, args) if template_map[t] then if not args.type then args.type = t       end local mapped = template_map[t](mw.text.trim(spl[1]), mw.text.trim(spl[2]), ext, args) for k, v in pairs(mapped) do           if not args[k] then args[k] = v           end end if type(args.description) == 'table' then args.description = table.concat(args.description) end end return args end

local function template(args) local ext local noext = {} local mapped for _, v in ipairs(mw.text.split(title.text, '.', true)) do       if ext then if noext[1] then table.insert(noext, '.') end table.insert(noext, ext) end ext = v   end noext = table.concat(noext) for t, _ in pairs(template_map) do       local spl = mw.text.split(noext, table.concat({'%s', t})) if spl[2] then -- Found file type mapped = mapper(t, spl, ext, args) break end end if not mapped then local t = args.type or 'misc' t = mw.text.split(t, ',', true)[1] t = mw.text.trim(t) mapped = mapper(t, {,}, ext, args) end return mapped end

function p.main(frame) local args = template(frame:getParent.args) local t = args.type or 'misc' local isfile = title.namespace == 6 local html = mw.html.create('table') :attr('class', 'wikitable') :attr('data-template', 'file') if args.description then makeRow(html, 'Description', args.description) end if args.notes then makeRow(html, 'Notes', args.notes) end local res = {} for _, value in ipairs(mw.text.split(t, ',', true)) do       local typedata = data.types[mw.text.trim(value)] if typedata then table.insert(res, '\n* ') table.insert(res, typedata.name) if isfile and typedata.category then table.insert(res, '[[Category:')               table.insert(res, typedata.category)                table.insert(res, ']]') end end end if isfile and args.interface then table.insert(res, '[[Media:')       table.insert(res, title.text)        table.insert(res, '| ]]') end makeRow(html, 'Type', table.concat(res)) makeRow(html, 'Source', args.source or table.concat({ '\'\,        data.game,         '\'\ }))   if args.filename then makeRow(html, 'In-game name', args.filename) end makeRow(html, 'Author', args.author or table.concat({ ,       data.author,         }))   makeRow(html, 'Licensing', table.concat({ 'This file ', data.license[args.license or args.licensing or 'default'] or data.license.default, '.'   }))    if args.issues then makeRow(html, 'Quality issues', table.concat({ 'This file ', yesno(args.issues, false) and 'has bad quality' or args.issues, '. You can help the ', mw.site.siteName, ' by uploading a new version.', isfile and  or  }))   end return tostring(html) end

return p