Open main menu

Changes

Module:Listen

1,403 bytes added, 17:12, 7 July 2022
we cookin with templatestyles now
-- This module implements {{listen}}.
 
local mFileLink = require('Module:File link')
local mTableTools = require('Module:TableTools')
local mSideBox = require('Module:Side box')
local lang = mw.language.new('en')
local p = {}
local hasMissing -- For the tracking category
local function p.mainformatLength(framelength) local origArgs = frame-- Formats a duration in seconds in "(h:)mm:getParentss" (minutes are zero-padded -- only if there are hours).args if not length or length == 0 then return nil end  -- Add 0.5 to offset the rounding down local args t = lang:getDurationIntervals(length + 0.5, {'hours', 'minutes', 'seconds' }) for klocal s = t.seconds and string.format('%02d', v in pairst.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(origArgs':') do v m = vstring.format('%02d', m) end span :tag('span') :matchaddClass('^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 v ~currentTitle.namespace == 0 then -- We are in mainspace. if not isEmpty then cats[#cats + 1] = 'Articles with hAudio microformats' end if hasMissing then argscats[k#cats + 1] = v'Articles with empty listen template'
end
end
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 ptable._mainconcat(argscats)
end
function p._main(args)
-- Find whether we are outputting a plain or an embedded boxOrganise the arguments by number.
local isPlain = args.plain == 'yes'
local isEmbedded = args.embed and true
local hasImage = not isPlain and not isEmbedded and args.image ~= 'none'
-- Organise the arguments by number. local numArgs , missingFiles = {}, {}
do
local origNumArgs = mTableTools.numData(args)
for i, t in ipairs(origNumArgs) do
-- Check if the files exist.
local obj = t.filename and mw.title.newmakeTitle('Media:' .. -2, t.filename)
if obj and obj.exists then
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
else
hasMissing missingFiles[#missingFiles + 1] = truet.filename or i
end
end
end  -- Exit early if none exist.Render warning if local hasMissing = #numArgs missingFiles ~=0 local previewWarning = 0 '' if hasMissing then for i, v in ipairs(missingFiles) do return pmissingFiles[i] = type(v) == 'string' and string.format('missing file "%s"', v) or string.renderTrackingCategoriesformat(isPlain'empty filename #%s', truev)
end
previewWarning = string.format(
'Page using [[Template:Listen]] with %s',
mw.text.listToText(missingFiles)
)
previewWarning = require('Module:If preview')._warning({previewWarning})
end
 
-- Exit early if none exist.
if #numArgs == 0 then
return previewWarning .. renderTrackingCategories(isPlain, hasMissing, true)
end
-- Build the arguments for {{side box}}
local sbargs sbArgs = {} sbargs.class metadata = 'noprintno', sbargs.metadata position = (isPlain or isEmbedded) and 'noleft'or args.pos, sbargs style = args.position style, templatestyles = args'Module:Listen/styles.poscss' }
-- Style Class arguments
do
local style class = { 'listen', 'noprint' }
if isPlain then
style[#style + 1] = table.insert(class, 'border:none' style[#style + 1] = 'background:transparent' style[#style + 1] = 'float:nonelisten-plain')
end
if isEmbedded then
style[#style + 1] = table.insert(class, 'borderlisten-collapse:collapse' style[#style + 1] = 'border-width:1px 0 0 0' style[#style + 1] = 'background:transparent' style[#style + 1] = 'float:none' style[#style + 1] = 'margin:0 -5pxembedded')
end
if not hasImage then table.insert(class, 'listen-noimage') end if args.pos == 'left' and not isPlain and not isEmbedded then style[#style + 1] = table.insert(class, 'float:listen-left')
elseif args.pos == 'center' then
style[#style + 1] = table.insert(class, 'float:none' style[#style + 1] = 'margin-left:auto' style[#style + 1] = 'marginlisten-right:autocenter')
end
style[#style + 1] = args.style sbargssbArgs.style class = table.concat(styleclass, '; ')
end
sbargs.textstyle = 'line-height:1.1em'
-- Image
if not isPlain and not isEmbedded then
if args.image then
sbargssbArgs.image = args.image
else
local images = {
speech = 'Audio-input-microphone.svg',
music = 'Gnome-mime-audio-openclipart.svg', default = 'Gnome-mime-sound-openclipart.svg'
}
local sbArgs.image = mFileLink._main{ file = args.type and images[args.type] or 'Gnome-mime-sound-openclipartimages.svg' sbargs.image = mFileLink._main{ file = imagedefault,
size = '65x50px',
location = 'center',
if args.header then
header = mw.html.create('div')
header:css{ background = addClass('transparent', ['textlisten-align'] = 'left', padding = args.embed and '2px 0' or '2pxheader' })
:wikitext(args.header)
header = tostring(header) header = header .. '\n'
else
header = ''
local text = {}
for i, t in ipairs(numArgs) do
text[#text + 1] = p.renderRow( t.filename, t.title, t.play, t.alt, t.description, t.start, t.length, hasImage
)
if numArgs[i + 1] then
end
end
sbargssbArgs.text = header .. table.concat(text)
end
-- Below
if not isPlain and not isEmbedded and args.help ~= 'no' then
sbargssbArgs.below = string.format(
'<hr/><i class="selfreference">Problems playing %s? See [[Help:Media|media help]].</i>',
#numArgs == 1 and 'this file' or 'these files'
-- Render the side box.
local sideBox = mSideBox._main(sbargssbArgs)
-- Render the tracking categories.
local trackingCategories = p.renderTrackingCategories(isPlain, hasMissing)
return previewWarning .. sideBox .. trackingCategories
end
function p.renderRowmain(filename, title, play, alt, description, startframe) -- Renders the HTML for one file description row. if not filename then return nil end local root = mw.html.create('') root:tag('div') :addClass('haudio') :newline() :tag('div') :css('padding', '4px 0') :wikitext(string.format('[[:File:%s|%s]]', filename, title or '')) :done() :newline() :tag('div') :wikitext( play ~= 'no' and mFileLink._main{ file = filename, size = '220px', alt = alt, start origArgs = start } or nil ) :done() :newline() frame:taggetParent('div') :css('padding', '2px 0 0 0') :addClass('description') :wikitext(description) :done() :done() return tostring(root)end function p.renderTrackingCategories(isPlain, isEmpty, titleObj) -- Renders all tracking categories produced by the template. -- isPlain and isEmpty are passed through from p._main, -- and the titleObj is only used for testing purposes.args local cats args = {} local currentTitle = titleObj or mw.title.getCurrentTitlefor k, v in pairs(origArgs) if currentTitle.namespace == 0 then -- We are in mainspace.do if not isEmpty then cats[#cats + 1] v ~= 'Articles with hAudio microformats' end if hasMissing then catsargs[#cats + 1k] = 'Articles with empty listen template'v
end
end
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 tablep.concat_main(catsargs)
end
return p
Anonymous user