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, design = function(subject) return { description = { 'Render of ',               subject,                ' for a design gallery.' },           license     = 'fairuse' }   end, headshot = function(subject) return { description = { 'Headshot of ',               subject,                '.' },           license     = 'fairuse' }   end, location = function(subject) return { description = { 'Image depicting ',               subject,                '.' },           license     = 'fairuse' }   end, map = function(subject, info) return { description = { 'Map of ',               subject,                '', (info ==  and  or '\'s '), info, '.'           },            license     = 'fairuse' }   end, music = function(subject, info, _, args) return { description = { 'The', (info ==  and  or ' '), info, ' audio file of ',               subject,                '.' },           filename    = args[1], license    = 'fairuse' }   end, object = function(subject) return { description = { 'Render of ',               subject,                '.' },           license     = 'fairuse' }   end, palette = function(subject) return { description = { 'Render of ',               subject,                ' for a palette gallery.' },           license     = 'fairuse' }   end, render = function(subject) return { description = { 'Infobox render of ',               subject,                ' for their main article.' },           license     = 'fairuse' }   end, screenshot = function(subject, _, __, args) return { description = args[1] or {'Screenshot of ', subject, '.'}, license    = 'fairuse' }   end, visual = function(subject) return { description = { 'Visual of ',               subject,                '.' },           license     = 'fairuse' }   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, typedata.name) if isfile and typedata.category then table.insert(res, '[[Category:')               table.insert(res, typedata.category)                table.insert(res, ']]') end table.insert(res, ' ') 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({ 'Unknown or unspecified source.' }))   if args.filename then makeRow(html, 'Title', args.filename) end makeRow(html, 'Author', type(args.author) == 'string' and args.author ~= '' and       data.authors[args.author] and            data.authors[args.author] or            args.author or        data.authors.default    ) 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