Module:Spoken Wikipedia
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Spoken Wikipedia/doc
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 = '<span class="fn">[[' .. page .. ']]</span>'
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 .. "<br/>(" .. 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('[[File:Sound-icon.svg|45px|alt=Spoken Wikipedia icon|link=|Spoken Wikipedia]]')
return tostring(res)
end
local function formatFiles(filenames, nocat)
if #filenames == 0
then
return wikiError('Error: no filename provided') .. category('[[Category:Spoken Wikipedia without file]]', nocat)
end
local res = {}
table.insert(res, '<div style="text-align:center; margin-top:10px; font-size:90%; margin-bottom:.4em;>')
if #filenames == 1
then
table.insert(res, '[[File:' .. filenames[1] .. '|noicon|200px|center]]')
else
for i, filename in ipairs(filenames)
do
table.insert(res, string.format("# [[File:%s|Part %d]]", filename, i))
end
end
table.insert(res, '</div>')
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('[[Category:Spoken Wikipedia without date]]', 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 = "[[:File:" .. filenames[1] .. "|This audio file]] was"
verbText = "does"
else
thisFileText = "These audio files were"
verbText = "do"
end
return '<div style="margin-left:60px; margin-top:10px; font-size: 95%; line-height: 1.6em;">' .. string.format(formatStr,
thisFileText,
formatPageText(page),
formatDateText(frame, dateArg, nocat),
verbText
) .. '</div>'
end
local function formatFooter()
return '<div style="margin-top:10px; text-align:center;">' ..
"([[Project:Media help|Audio help]] · [[Project:Spoken articles|More spoken articles]])" ..
'</div>'
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('[[Category:Articles with hAudio microformats]][[Category:Spoken articles]]', nocat))
end
return tostring(root)
end
p.main = main
return p