mNo edit summary
mNo edit summary
Line 17: Line 17:
         :attr('data-sound-file', fileUrl)
         :attr('data-sound-file', fileUrl)


     container:tag('span')
     container:wikitext(string.format('[[File:Icon-Volume.png|%s|link=]]', size))
        :addClass('sound-icon-static')
        :wikitext(string.format('[[File:Icon-Volume.png|%s|link=]]', size))
 
    container:tag('span')
        :addClass('sound-icon-playing')
        :wikitext(string.format('[[File:Icon-Volume.gif|%s|link=]]', size))


     return tostring(container)
     return tostring(container)

Revision as of 12:49, 31 December 2025

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 = args[1]
    local size = args[2] or '16px'
    
    if not fileName or fileName == '' then
        return '<span style="color:red; font-weight:bold;">Error: No file specified</span>'
    end

    local fileUrl = 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|link=]]', size))

    return tostring(container)
end

return p