Module:audio

local export = {}

local headword_data_module = "Module:headword/data" local IPA_module = "Module:IPA" local labels_module = "Module:labels" local links_module = "Module:links" local parameters_module = "Module:parameters" local qualifier_module = "Module:qualifier" local references_module = "Module:references" local string_utilities_module = "Module:string utilities" local table_module = "Module:table" local template_styles_module = "Module:TemplateStyles" local utilities_module = "Module:utilities" local audio_styles_css = "audio/styles.css"

local function track(page) require("Module:debug/track")("audio/" .. page) return true end

local function wrap_qualifier_css(text, suffix) return require(qualifier_module).wrap_qualifier_css(text, suffix) end

--[==[ Display a box that can be used to play an audio file. `data` is a table containing the following fields: and displayed before the audio box and after the caption (and any accent qualifiers); accent qualifiers); Module:accent qualifier and displayed before the audio box and after the caption;  is either a string containing the reference text (typically a call to a citation template such as cite-book, or  a template wrapping such a call), or an object with fields `text` (the reference text), `name` (the name of the reference, as in or )  and/or `group` (the group of the reference, as in  or  ); this uses a parser function to format the reference  appropriately and insert a footnote number that hyperlinks to the actual reference, located in the  {{cd| "
 * `lang` (required): language object for the audio files;
 * `file` (required): file containing the audio;
 * `caption`: Caption to display before the audio box; normally {"Audio"}, and does not usually need to be changed;
 * `nocaption`: If specified, don't display the caption;
 * `q`: {nil} or a list of left regular qualifier strings, formatted using {format_qualifier} in Module:qualifier
 * `qq`: {nil} or a list of right regular qualifier strings, displayed directly after the audio box (and after any
 * `a`: {nil} or a list of left accent qualifier strings, formatted using {format_qualifiers} in
 * `aa`: {nil} or a list of right accent qualifier strings, displayed directly after the homophone in question;
 * `refs`: {nil} or a list of references or reference specs to add directly after the audio box; the value of a list item

local stylesheet = require(template_styles_module)(audio_styles_css) local categories = data.nocat and "" or		cats[1] and require(utilities_module).format_categories(cats, data.lang, data.sort) or "" return stylesheet .. text .. categories end

--[==[ FIXME: Old entry point for formatting multiple audios in a single table. Not used anywhere and needs rewriting to the standard of format_audio.

Meant to be called from a module. `data` is a table containing the following fields:

{ lang = LANGUAGE_OBJECT, audios = {{file = "FILENAME", qualifiers = nil or {"QUALIFIER", "QUALIFIER", ...}}, ...}, caption = nil or "CAPTION" }

Here:

QUALIFIER is a qualifier string to display after the specific audio file in question, formatted using {format_qualifier} in Module:qualifier. ]==] function export.format_multiple_audios(data) local audiocats = { data.lang:getFullName .. " terms with audio links" } local rows = { } local caption = data.caption
 * `lang` is a language object.
 * `audios` is the list of audio files to display. FILENAME is the name of the audio file without a namespace.
 * `caption`, if specified, adds a caption before the audio file.

for _, audio in ipairs(data.audios) do		local qualifiers = audio.qualifiers local function repl(key) if key == "file" then return audio.file elseif key == "caption" then if not caption then return "" end return "" .. caption .. ": "			elseif key == "qualifiers" then if not qualifiers or not qualifiers[1] then return "" end return " " .. require(qualifier_module).format_qualifier(qualifiers) .. " "			end end local template = local text = (mw.ustring.gsub(template, "{{{([a-z0-9_:]+)}}}", repl)) table.insert(rows, text) caption = nil end

local function repl(key) if key == "rows" then return table.concat(rows, "\n") end end

local template = [=[ ]=]

local stylesheet = require(template_styles_module)(audio_styles_css) local text = mw.ustring.gsub(template, "{{{([a-z0-9_:]+)}}}", repl) local categories = data.nocat and "" or		#audiocats > 0 and require(utilities_module).format_categories(audiocats, data.lang, data.sort) or "" -- remove newlines due to HTML generator bug in MediaWiki(?) - newlines in tables cause list items to not end correctly text = mw.ustring.gsub(text, "\n", "") return stylesheet .. text .. categories end

--[==[ Construct the `text` object passed into {format_audio}, from raw-ish arguments (essentially, the output of {process} in Module:parameters). On entry, `args` contains the following fields: function returns {nil}. If not specified, taken from the actual pagename. ]==] function export.construct_audio_textobj(args) local textobj if args.text or args.gloss or args.tr or args.ts or args.pos or args.lit or args.genders and args.genders[1] then local text = args.text or args.pagename or mw.loadData("Module:headword/data").pagename textobj = { lang = args.lang, alt = wrap_qualifier_css("“", "quote") .. text .. wrap_qualifier_css("”", "quote"), gloss = args.gloss, tr = args.tr, ts = args.ts, pos = args.pos, lit = args.lit, genders = args.genders, sc = args.sc, }	end return textobj end
 * `lang` (required): Language object.
 * `text`: Text. If this isn't defined and neither are any of `gloss`, `tr`, `ts`, `pos`, `lit` or `genders`, the
 * `gloss`: Gloss of text.
 * `tr`: Manual transliteration of text.
 * `ts`: Transcription of text.
 * `pos`: Part of speech of text.
 * `lit`: Literal meaning of text.
 * `genders`: List of gender/number spec(s) of text.
 * `sc`: Optional script object of text (rarely needs to be set).
 * `pagename`: Pagename; used in place of `text` when `text` is unset but other text-related parameters are set.

--[==[ Entry point for {{tl|audio}} template. ]==] function export.show(frame) local parent_args = frame:getParent.args local compat = parent_args.lang local offset = compat and 0 or 1

local params = { [compat and "lang" or 1] = {required = true, type = "language", default = "en"}, [1 + offset] = {required = true, default = "Example.ogg"}, [2 + offset] = {}, ["q"] = {type = "qualifier"}, ["qq"] = {type = "qualifier"}, ["a"] = {type = "labels"}, ["aa"] = {type = "labels"}, ["ref"] = {type = "references"}, ["IPA"] = {sublist = true}, ["text"] = {}, ["t"] = {}, ["gloss"] = {alias_of = "t"}, ["tr"] = {}, ["ts"] = {}, ["pos"] = {}, ["lit"] = {}, ["g"] = {sublist = true}, ["sc"] = {type = "script"}, ["bad"] = {}, ["nocat"] = {type = "boolean"}, ["sort"] = {}, ["pagename"] = {}, }

local args = require(parameters_module).process(parent_args, params)

local lang = args[compat and "lang" or 1]

-- Needed in construct_audio_textobj. args.lang = lang local textobj = export.construct_audio_textobj(args)

local caption = args[2 + offset] local nocaption if caption == "-" then caption = nil nocaption = true end if caption then -- Remove final colon if given, to avoid two colons. caption = caption:gsub(":$", "") end local data = { lang = lang, file = args[1 + offset], caption = caption, nocaption = nocaption, q = args.q,		qq = args.qq, a = args.a,		aa = args.aa, refs = args.ref, text = textobj, IPA = args.IPA, bad = args.bad, nocat = args.nocat, sort = args.sort, }

return export.format_audio(data) end

return export