mNo edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 3: Line 3:
function p.main(frame)
function p.main(frame)
     local args = frame:getParent().args
     local args = frame:getParent().args
     local fileName = args[1]
     local fileName = mw.text.trim(args[1] or "")
     local size = args[2] or '20px'
     local size = args[2] or '20px'
      
      
     if not fileName or fileName == '' then
     if fileName == '' then
         return '<span style="color:red; font-weight:bold;">Error: No file specified</span>'
         return '<span style="color:red; font-weight:bold;">Error: No file specified</span>'
     end
     end


     local fileUrl = frame:callParserFunction('filepath', fileName)
     local fileUrl = mw.text.trim(frame:callParserFunction('filepath', fileName))
     if fileUrl:sub(1,2) == "//" then fileUrl = "https:" .. fileUrl end
     if fileUrl:sub(1,2) == "//" then fileUrl = "https:" .. fileUrl end
      
      
Line 17: Line 17:
         :attr('data-sound-file', fileUrl)
         :attr('data-sound-file', fileUrl)


     container:wikitext(string.format('[[File:Icon-Volume.png|%s|link=]]', size))
     container:wikitext(string.format('[[File:Icon-Volume.png|%s|middle|link=]]', size))


     return tostring(container)
     return (tostring(container):gsub('[\n\r]', ''))
end
end


return p
return p

Latest revision as of 14:42, 1 January 2026

Module:Sound creates a small clickable volume icon that plays an uploaded audio file when clicked.

Overview

This module powers {{Sound}}. Given the name of an audio file uploaded to the wiki, it outputs a volume-icon button that the wiki's JavaScript turns into a play control. It does this by resolving the file's URL and storing it on the button as a data-sound-file attribute; a site gadget/script then plays that URL when the icon is clicked. It does not play audio by itself — it only builds the button markup and supplies the file URL.

Editors use the {{Sound}} template rather than calling the module directly.

Functions / entry points

Function (#invoke) What it does Called by
p.main Reads the file name (arg 1) and optional icon size (arg 2, default 20px) from the parent template, resolves the file's URL via the filepath parser function, and returns a containing a File:Icon-Volume.png image. Returns a red error message if no file name is given. {{Sound}}

How it's used

{{Sound}} is simply:

<includeonly>{{#invoke:Sound|main}}</includeonly>

So on a page an editor writes, for example:

{{Sound|MyBossRoar.ogg}}
{{Sound|MyBossRoar.ogg|32px}}
  • Argument 1 — the audio file name as uploaded (e.g. MyBossRoar.ogg). Read from the parent template's parameters.
  • Argument 2 (optional) — the size of the volume icon, default 20px.

Editors should use {{Sound}}; the raw Error: No file specified only appears inside that template.

Notes

  • The play behaviour depends on an external site script/gadget that reads data-sound-file and the sound-button class. The module only emits the markup and the resolved URL; if that script isn't loaded, clicking does nothing.
  • The file URL comes from . Protocol-relative URLs (starting //) are prefixed with https:.
  • The clickable icon is File:Icon-Volume.png, rendered with link= (no link target) so the click is handled by the script, not by following a link.
  • Newlines/carriage returns are stripped from the output so it can be used inline.
  • Related: {{Sound}}.

local p = {}

function p.main(frame)
    local args = frame:getParent().args
    local fileName = mw.text.trim(args[1] or "")
    local size = args[2] or '20px'
    
    if fileName == '' then
        return '<span style="color:red; font-weight:bold;">Error: No file specified</span>'
    end

    local fileUrl = mw.text.trim(frame:callParserFunction('filepath', fileName))
    if fileUrl:sub(1,2) == "//" then fileUrl = "https:" .. fileUrl end
    
    local container = mw.html.create('span')
        :addClass('sound-button')
        :attr('data-sound-file', fileUrl)

    container:wikitext(string.format('[[File:Icon-Volume.png|%s|middle|link=]]', size))

    return (tostring(container):gsub('[\n\r]', ''))
end

return p