Module:Spoken Wikipedia
Appearance
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This module depends on the following other modules: |
![]() | This module uses TemplateStyles: |
Implements {{Spoken Wikipedia}}. See tests at Template:Spoken Wikipedia/testcases.
Usage
{{#invoke:Spoken Wikipedia|main}}
Parameters
Mandatory parameters
|1=
,|2=
,|3=
...- Filenames of the audio files.
|date=
- Date of the version of the page, which was recorded.
Optional parameters
|page=
- Overrides the "this article" text with a wikilink—for placement of the template for an article on another page.
|nocat=true
- Suppress categorization.
See also
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.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(dateArg, nocat)
return dateArg and Date(dateArg):text() or (wikiError("Error: no date provided") ..
category('[[Category:Spoken Wikipedia without date]]', nocat))
end
local function formatDisclaimer(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(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(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