Module:Listen: Difference between revisions

we cookin with templatestyles now
m (1 revision imported)
imported>Izno
(we cookin with templatestyles now)
 
Line 1: Line 1:
-- This module implements {{listen}}.
local mFileLink = require('Module:File link')
local mFileLink = require('Module:File link')
local mTableTools = require('Module:TableTools')
local mTableTools = require('Module:TableTools')
Line 8: Line 6:
local p = {}
local p = {}


function p.main(frame)
local function formatLength(length)
local origArgs = frame:getParent().args
-- Formats a duration in seconds in "(h:)mm:ss" (minutes are zero-padded
local args = {}
-- only if there are hours).
for k, v in pairs(origArgs) do
if not length or length == 0 then
if v ~= '' then
return nil
args[k] = v
end
 
-- Add 0.5 to offset the rounding down
local t = lang:getDurationIntervals(length + 0.5, { 'hours', 'minutes', 'seconds' })
local s = t.seconds and string.format('%02d', t.seconds) or '00'
local m = t.minutes or 0
 
local span = mw.html.create('span'):addClass('duration')
if t.hours then
span
:tag('span')
:addClass('h')
:wikitext(t.hours)
:done()
:wikitext(':')
m = string.format('%02d', m)
end
span
:tag('span')
:addClass('min')
:wikitext(m)
:done()
:wikitext(':')
:tag('span')
:addClass('s')
:wikitext(s)
:done()
return tostring(span)
end
 
local function renderRow(filename, title, play, alt, description, start, length, hasImage)
-- Renders the HTML for one file description row.
if not filename then
return nil
end
 
length = formatLength(length)
length = length and string.format(' (%s)', length) or ''
 
local root = mw.html.create('')
root:tag('div')
:addClass('haudio')
:newline()
:tag('div')
:addClass('listen-file-header')
:wikitext(string.format(
'[[:File:%s|%s]]%s',
filename,
title or '',
length
))
:done()
:newline()
:tag('div')
:wikitext(play ~= 'no' and mFileLink._main{
file = filename,
size = hasImage and '232px' or '215px',
alt = alt,
start = start
}
or nil
)
:done()
:newline()
:tag('div')
:addClass('description')
:wikitext(description)
:done()
:done()
return tostring(root)
end
 
local function renderTrackingCategories(isPlain, hasMissing, isEmpty, titleObj)
-- Renders all tracking categories produced by the template.
-- isPlain, hasMissing and isEmpty are passed through from p._main,
-- and the titleObj is only used for testing purposes.
local cats = {}
local currentTitle = titleObj or mw.title.getCurrentTitle()
if currentTitle.namespace == 0 then
-- We are in mainspace.
if not isEmpty then
cats[#cats + 1] = 'Articles with hAudio microformats'
end
if hasMissing then
cats[#cats + 1] = 'Articles with empty listen template'
end
end
end
end
return p._main(args)
if isPlain then
cats[#cats + 1] = 'Listen template using plain parameter'
end
for i, cat in ipairs(cats) do
cats[i] = string.format('[[Category:%s]]', cat)
end
return table.concat(cats)
end
end


function p._main(args)
function p._main(args)
-- Find whether we are outputting a plain or an embedded box.
-- Organise the arguments by number.
local isPlain = args.plain == 'yes'
local isPlain = args.plain == 'yes'
local isEmbedded = args.embed and true
local isEmbedded = args.embed and true
local hasMissing, previewWarning
local hasImage = not isPlain and not isEmbedded and args.image ~= 'none'


-- Organise the arguments by number.
local numArgs, missingFiles = {}, {}
local numArgs, missingFiles = {}, {}
do
do
Line 33: Line 120:
for i, t in ipairs(origNumArgs) do
for i, t in ipairs(origNumArgs) do
-- Check if the files exist.
-- Check if the files exist.
local obj = t.filename and mw.title.new('Media:' .. t.filename)
local obj = t.filename and mw.title.makeTitle(-2, t.filename)
if obj and obj.exists then
if obj and obj.exists then
t.length = t.length ~= 'hide' and obj.file.length
if t.length == 'yes' or
-- Show length if the video height would be less than 150px
obj.file.width / obj.file.height > (hasImage and 1.547 or 1.434)
then
t.length = obj.file.length
else
t.length = nil
end
numArgs[#numArgs + 1] = t
numArgs[#numArgs + 1] = t
else
else
Line 44: Line 138:


-- Render warning
-- Render warning
hasMissing = #missingFiles ~= 0
local hasMissing = #missingFiles ~= 0
local previewWarning = ''
if hasMissing then
if hasMissing then
for i, v in ipairs(missingFiles) do
for i, v in ipairs(missingFiles) do
Line 56: Line 151:
)
)
previewWarning = require('Module:If preview')._warning({previewWarning})
previewWarning = require('Module:If preview')._warning({previewWarning})
else
previewWarning = ''
end
end


-- Exit early if none exist.
-- Exit early if none exist.
if #numArgs == 0 then
if #numArgs == 0 then
return previewWarning .. p.renderTrackingCategories(isPlain, hasMissing, true)
return previewWarning .. renderTrackingCategories(isPlain, hasMissing, true)
end
end


-- Build the arguments for {{side box}}
-- Build the arguments for {{side box}}
local sbArgs = {}
local sbArgs = {
sbArgs.class = 'noprint'
metadata = 'no',
sbArgs.metadata = 'no'
position = (isPlain or isEmbedded) and 'left' or args.pos,
sbArgs.position = args.pos
style = args.style,
templatestyles = 'Module:Listen/styles.css'
}


-- Style arguments
-- Class arguments
do
do
local style = {}
local class = {
'listen',
'noprint'
}
if isPlain then
if isPlain then
style[#style + 1] = 'border:none'
table.insert(class, 'listen-plain')
style[#style + 1] = 'background:transparent'
style[#style + 1] = 'float:none'
end
end
if isEmbedded then
if isEmbedded then
style[#style + 1] = 'border-collapse:collapse'
table.insert(class, 'listen-embedded')
style[#style + 1] = 'border-width:1px 0 0 0'
end
style[#style + 1] = 'background:transparent'
if not hasImage then
style[#style + 1] = 'float:none'
table.insert(class, 'listen-noimage')
style[#style + 1] = 'margin:0 -5px'
end
end
if args.pos == 'left' then
if args.pos == 'left' and not isPlain and not isEmbedded then
style[#style + 1] = 'float:left'
table.insert(class, 'listen-left')
style[#style + 1] = 'overflow:visible'
elseif args.pos == 'center' then
elseif args.pos == 'center' then
style[#style + 1] = 'float:none'
table.insert(class, 'listen-center')
style[#style + 1] = 'margin-left:auto'
style[#style + 1] = 'margin-right:auto'
end
end
 
style[#style + 1] = args.style
sbArgs.class = table.concat(class, ' ')
sbArgs.style = table.concat(style, '; ')
end
end
sbArgs.textstyle = 'line-height:1.1em'


-- Image
-- Image
Line 107: Line 197:
local images = {
local images = {
speech = 'Audio-input-microphone.svg',
speech = 'Audio-input-microphone.svg',
music = 'Gnome-mime-audio-openclipart.svg'
music = 'Gnome-mime-audio-openclipart.svg',
default = 'Gnome-mime-sound-openclipart.svg'
}
}
local image = args.type
and images[args.type]
or 'Gnome-mime-sound-openclipart.svg'
sbArgs.image = mFileLink._main{
sbArgs.image = mFileLink._main{
file = image,
file = args.type and images[args.type] or images.default,
size = '65x50px',
size = '65x50px',
location = 'center',
location = 'center',
Line 127: Line 215:
if args.header then
if args.header then
header = mw.html.create('div')
header = mw.html.create('div')
header:css{
header:addClass('listen-header')
background = 'transparent',
['text-align'] = 'left',
padding = args.embed and '2px 0' or '2px'
}
:wikitext(args.header)
:wikitext(args.header)
header = tostring(header)
header = tostring(header) .. '\n'
header = header .. '\n'
else
else
header = ''
header = ''
Line 140: Line 223:
local text = {}
local text = {}
for i, t in ipairs(numArgs) do
for i, t in ipairs(numArgs) do
text[#text + 1] = p.renderRow(
text[#text + 1] = renderRow(
t.filename, t.title, t.play, t.alt, t.description, t.start, t.length
t.filename, t.title, t.play, t.alt, t.description, t.start,
t.length, hasImage
)
)
if numArgs[i + 1] then
if numArgs[i + 1] then
Line 162: Line 246:


-- Render the tracking categories.
-- Render the tracking categories.
local trackingCategories = p.renderTrackingCategories(isPlain, hasMissing)
local trackingCategories = renderTrackingCategories(isPlain, hasMissing)


return previewWarning .. sideBox .. trackingCategories
return previewWarning .. sideBox .. trackingCategories
end
end


function p.renderRow(filename, title, play, alt, description, start, length)
function p.main(frame)
-- Renders the HTML for one file description row.
local origArgs = frame:getParent().args
if not filename then
local args = {}
return nil
for k, v in pairs(origArgs) do
end
if v ~= '' then
 
args[k] = v
length = p.formatLength(length)
length = length and string.format(' (%s)', length) or ''
 
local root = mw.html.create('')
root:tag('div')
:addClass('haudio')
:newline()
:tag('div')
:css('padding', '4px 0')
:wikitext(string.format('[[:File:%s|%s]]%s', filename, title or '', length))
:done()
:newline()
:tag('div')
:wikitext(
play ~= 'no'
and mFileLink._main{
file = filename,
size = '233px',
alt = alt,
start = start
}
or nil
)
:done()
:newline()
:tag('div')
:css('padding', '2px 0 0 0')
:addClass('description')
:wikitext(description)
:done()
:done()
return tostring(root)
end
 
function p.formatLength(length)
-- Formats a duration in seconds in "(h:)mm:ss" (minutes are zero-padded
-- only if there are hours).
if not length or length == 0 then
return nil
end
 
-- Add 0.5 to offset the rounding down
local t = lang:getDurationIntervals(length + 0.5, { 'hours', 'minutes', 'seconds' })
local s = t.seconds and string.format('%02d', t.seconds) or '00'
local m = t.minutes or 0
 
local span = mw.html.create('span'):addClass('duration')
if t.hours then
span
:tag('span')
:addClass('h')
:wikitext(t.hours)
:done()
:wikitext(':')
m = string.format('%02d', m)
end
span
:tag('span')
:addClass('min')
:wikitext(m)
:done()
:wikitext(':')
:tag('span')
:addClass('s')
:wikitext(s)
:done()
return tostring(span)
end
 
function p.renderTrackingCategories(isPlain, hasMissing, isEmpty, titleObj)
-- Renders all tracking categories produced by the template.
-- isPlain, hasMissing and isEmpty are passed through from p._main,
-- and the titleObj is only used for testing purposes.
local cats = {}
local currentTitle = titleObj or mw.title.getCurrentTitle()
if currentTitle.namespace == 0 then
-- We are in mainspace.
if not isEmpty then
cats[#cats + 1] = 'Articles with hAudio microformats'
end
if hasMissing then
cats[#cats + 1] = 'Articles with empty listen template'
end
end
end
end
if isPlain then
return p._main(args)
cats[#cats + 1] = 'Listen template using plain parameter'
end
for i, cat in ipairs(cats) do
cats[i] = string.format('[[Category:%s]]', cat)
end
return table.concat(cats)
end
end


return p
return p
Anonymous user