Module:Spoken Ingenpedia

local getArgs = require('Module:Arguments').getArgs local yesno = require('Module:Yesno') local Date = require('Module:Date')._Date

-- TODO use TemplateStyles

p = {}

local function wikiError(message) local ret = mw.html.create('div') :addClass('error') :wikitext(message) return tostring(ret) end

local function category(c, nocat) if nocat then return '' else return c	end end

local function formatPageText(page) local currentTitle = mw.title.getCurrentTitle local pageText if page then pageText = ' ' .. page .. ' ' else if currentTitle.namespace == 0 then pageText = "this article" else pageText = "this page" end end return pageText end

local function formatFileSize(filenames) local size = 0 for _, filename in ipairs(filenames) do local fileTitle = mw.title.new(filename, 'File') if fileTitle and fileTitle.file and fileTitle.file.exists then size = size + fileTitle.file.size end end if size > 1024*1024 then return string.format('%.1f megabytes', size / (1024*1024)) else return string.format('%.1f kilobytes', size / 1024) end end

local function formatHeader(filenames, page) headerText = "Listen to " headerText = "'''" .. headerText .. formatPageText(page) .. "'''"	if #filenames > 1 then headerText = headerText .. " (" .. tostring(#filenames) .. " parts, " .. formatFileSize(filenames) .. ")" else headerText = headerText .. " (" .. formatFileSize(filenames) .. ")" end local ret = mw.html.create('div') :addClass('center') :wikitext(headerText) return ret end

local function formatIcon local res = mw.html.create('div') :cssText('float: left; margin-left: 5px; margin-top: 10px;') :wikitext('') return tostring(res) end

local function formatFiles(filenames, nocat) if #filenames == 0 then return wikiError('Error: no filename provided') .. category('', nocat) end local res = {} table.insert(res, ' ') if #filenames == 1 then table.insert(res, '') else for i, filename in ipairs(filenames) do table.insert(res, string.format("# ", filename, i)) end end table.insert(res, ' ') return table.concat(res, '\n') end

local function formatDateText(frame, dateArg, nocat) local d = dateArg and Date(dateArg) or nil return d and frame:expandTemplate{ title = 'Start date', args = { d.year, d.month, d.day, df='y'			} } or (wikiError("Error: no date provided") ..		category('', nocat)) end

local function formatDisclaimer(frame, filenames, page, dateArg, nocat) local formatStr = "%s created from a revision of %s dated %s, and %s not reflect subsequent edits." local thisFileText local verbText if #filenames == 1 then thisFileText = "This audio file was" verbText = "does" else thisFileText = "These audio files were" verbText = "do" end return ' ' .. string.format(formatStr,		thisFileText,		formatPageText(page),		formatDateText(frame, dateArg, nocat),		verbText	) .. ' ' end

local function formatFooter return ' ' .. "(Audio help · More spoken articles)" .. ' ' end

local function formatTopicon(frame, filenames) local wikilink if #filenames > 0 then wikilink = 'File:' .. filenames[1] else wikilink = 'Wikipedia:WikiProject Spoken Wikipedia' end return frame:expandTemplate{ title = "Top icon", args = { imagename='Sound-icon.svg', wikilink=wikilink, text = 'Listen to this article', id = 'spoken-icon' }	} end

local function extractFilenames(args) local filenames = {} for key, rawValue in ipairs(args) do local value = mw.text.trim(rawValue) if type(key) == "number" and value ~= '' then table.insert(filenames, value) end end return filenames end

local function sidebox(nodes) root = mw.html.create('div') :attr('id', 'section_SpokenWikipedia') :addClass('infobox sisterproject plainlinks noprint haudio') for _, node in ipairs(nodes) do root:node(node) end return root end

function main(frame) local args = getArgs(frame)

-- Mandatory parameters local filenames = extractFilenames(args) local dateArg = args['date'] -- Optional parameters local page = args['page'] local nocat = yesno(args['nocat'], false) or false

local root = sidebox({		formatHeader(filenames, page),		formatFiles(filenames, nocat),		formatIcon,		formatDisclaimer(frame, filenames, page, dateArg, nocat),		formatFooter	})

local currentTitle = mw.title.getCurrentTitle if currentTitle.namespace == 0 then root:wikitext(formatTopicon(frame, filenames)) root:wikitext(category('', nocat)) end

return tostring(root) end

p.main = main

return p