Module:Listen
Appearance
![]() | This module depends on the following other modules: |
![]() | This module uses TemplateStyles: |
This module implements the {{listen}} template.
Usage from wikitext
This module cannot be used directly from wikitext. It can only be used through the {{listen}} template. Please see the template page for documentation.
Usage from Lua modules
To use this module from other Lua modules, first load the module.
localmListen=require('Module:Listen')
You can then generate the listen box by using the _main function.
mListen._main(args)
The args variable should be a table containing the arguments to pass to the module. To see the different arguments that can be specified and how they affect the module output, please refer to the {{listen}} template documentation.
Tracking/maintenance categories
localmFileLink=require('Module:File link')localmTableTools=require('Module:TableTools')localmSideBox=require('Module:Side box')locallang=mw.language.new('en')localp={}localfunctionformatLength(length)-- Formats a duration in seconds in "(h:)mm:ss" (minutes are zero-padded-- only if there are hours).ifnotlengthorlength==0thenreturnnilend-- Add 0.5 to offset the rounding downlocalt=lang:getDurationIntervals(length+0.5,{'hours','minutes','seconds'})locals=t.secondsandstring.format('%02d',t.seconds)or'00'localm=t.minutesor0localspan=mw.html.create('span'):addClass('duration')ift.hoursthenspan:tag('span'):addClass('h'):wikitext(t.hours):done():wikitext(':')m=string.format('%02d',m)endspan:tag('span'):addClass('min'):wikitext(m):done():wikitext(':'):tag('span'):addClass('s'):wikitext(s):done()returntostring(span)endlocalfunctionrenderRow(filename,title,play,alt,description,start,length,hasImage)-- Renders the HTML for one file description row.ifnotfilenamethenreturnnilendlength=formatLength(length)length=lengthandstring.format(' (%s)',length)or''localroot=mw.html.create('')root:tag('div'):addClass('haudio'):newline():tag('div'):addClass('listen-file-header'):wikitext(string.format('[[:File:%s|%s]]%s',filename,titleor'',length)):done():newline():tag('div'):wikitext(play~='no'andmFileLink._main{file=filename,size=hasImageand'232px'or'215px',alt=alt,start=start}ornil):done():newline():tag('div'):addClass('description'):wikitext(description):done():done()returntostring(root)endlocalfunctionrenderTrackingCategories(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.localcats={}localcurrentTitle=titleObjormw.title.getCurrentTitle()ifcurrentTitle.namespace==0then-- We are in mainspace.ifnotisEmptythencats[#cats+1]='Articles with hAudio microformats'endifhasMissingthencats[#cats+1]='Articles with empty listen template'endendifisPlainthencats[#cats+1]='Listen template using plain parameter'endfori,catinipairs(cats)docats[i]=string.format('[[Category:%s]]',cat)endreturntable.concat(cats)endfunctionp._main(args)-- Organise the arguments by number.localisPlain=args.plain=='yes'localisEmbedded=args.embedandtruelocalhasImage=notisPlainandnotisEmbeddedandargs.image~='none'localnumArgs,missingFiles={},{}dolocalorigNumArgs=mTableTools.numData(args)origNumArgs[1]=origNumArgs.other-- Overwrite args.filename1 etc. with args.filename etc.origNumArgs=mTableTools.compressSparseArray(origNumArgs)fori,tinipairs(origNumArgs)do-- Check if the files exist.localobj=t.filenameandmw.title.makeTitle(-2,t.filename)ifobjandobj.existsthenift.length=='yes'or-- Show length if the video height would be less than 150pxobj.file.width/obj.file.height>(hasImageand1.547or1.434)thent.length=obj.file.lengthelset.length=nilendnumArgs[#numArgs+1]=telsemissingFiles[#missingFiles+1]=t.filenameoriendendend-- Render warninglocalhasMissing=#missingFiles~=0localpreviewWarning=''ifhasMissingthenfori,vinipairs(missingFiles)domissingFiles[i]=type(v)=='string'andstring.format('missing file "%s"',v)orstring.format('empty filename #%s',v)endpreviewWarning=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==0thenreturnpreviewWarning..renderTrackingCategories(isPlain,hasMissing,true)end-- Build the arguments for {{side box}}localsbArgs={metadata='no',position=(isPlainorisEmbedded)and'left'orargs.pos,style=args.style,templatestyles='Module:Listen/styles.css'}-- Class argumentsdolocalclass={'listen','noprint'}ifisPlainthentable.insert(class,'listen-plain')endifisEmbeddedthentable.insert(class,'listen-embedded')endifnothasImagethentable.insert(class,'listen-noimage')endifargs.pos=='left'andnotisPlainandnotisEmbeddedthentable.insert(class,'listen-left')elseifargs.pos=='center'thentable.insert(class,'listen-center')endsbArgs.class=table.concat(class,' ')end-- ImageifnotisPlainandnotisEmbeddedthenifargs.imagethensbArgs.image=args.imageelselocalimages={speech='Audio-input-microphone.svg',music='Gnome-mime-audio-openclipart.svg',default='Gnome-mime-sound-openclipart.svg'}sbArgs.image=mFileLink._main{file=args.typeandimages[args.type]orimages.default,size='65x50px',location='center',link='',alt=''}endend-- Textdolocalheaderifargs.headerthenheader=mw.html.create('div')header:addClass('listen-header'):wikitext(args.header)header=tostring(header)..'\n'elseheader=''endlocaltext={}fori,tinipairs(numArgs)dotext[#text+1]=renderRow(t.filename,t.title,t.play,t.alt,t.description,t.start,t.length,hasImage)ifnumArgs[i+1]thentext[#text+1]='<hr/>'endendsbArgs.text=header..table.concat(text)end-- BelowifnotisPlainandnotisEmbeddedandargs.help~='no'thensbArgs.below=string.format('<hr/><i class="selfreference">Problems playing %s? See [[Help:Media|media help]].</i>',#numArgs==1and'this file'or'these files')end-- Render the side box.localsideBox=mSideBox._main(sbArgs)-- Render the tracking categories.localtrackingCategories=renderTrackingCategories(isPlain,hasMissing)returnpreviewWarning..sideBox..trackingCategoriesendfunctionp.main(frame)localorigArgs=frame:getParent().argslocalargs={}fork,vinpairs(origArgs)doifv~=''thenargs[k]=vendendreturnp._main(args)endreturnp