Jump to content

Module:IPA symbol/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Lfdder (talk | contribs) at 17:30, 22 July 2013. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p, __output, args, data = {}, {}, {}, mw.loadData("Module:IPA symbol/sandbox/data")
local id, output

__output.input = function() return args[1] end
__output.name = function() return data.correspondences[id]["name"] end
__output.page = function() return data.correspondences[id]["page"] end
__output.soundfile = function() return data.correspondences[id]["soundfile"] end
__output.type = function() return data.correspondences[id]["type"] end

local function __link()
  return mw.ustring.format('[[%s|%s]]', (args.link_option or data.correspondences[id]["page"]), output)
end

local function __ipa()
  local ipa_begin, ipa_end = '<span title="Representation in the International Phonetic Alphabet (IPA)" class="IPA">', '</span>'

  if args.ipa_option then
    if     args.ipa_option == "[" or args.ipa_option == "]" then return mw.ustring.format("%s&#x005b;%s&#x005d;%s", ipa_begin, output, ipa_end)
    elseif args.ipa_option == "<" or args.ipa_option == ">" then return mw.ustring.format("&#x27e8;%s%s%s&#x27e9;", ipa_begin, output, ipa_end)
    elseif args.ipa_option == "/"                           then return mw.ustring.format("%s/%s/%s"              , ipa_begin, output, ipa_end)
    else return mw.ustring.format("%s%s%s" , ipa_begin, output, ipa_end)
    end
  else   return mw.ustring.format("%s%s%s" , ipa_begin, output, ipa_end)
  end
end

local function __soundbox()
  return mw.ustring.format('%s[[File:%s|%s]]', output, data.correspondences[id]["soundfile"], args.soundbox_option)
end

local function html_error(message)
  if args.custom_error then
    return args.custom_error
  end

  local title = mw.title.getCurrentTitle()
  if title.isContentPage then
    return mw.ustring.format(
            '<strong class="error">Error using {{tl|IPA symbol}}: %s</strong>[[Category:International Phonetic Alphabet pages needing attention|IPAsymbol, %s]]]'
            ,message
            ,title.text
           )
  else
    return mw.ustring.format(
            '<strong class="error">Error using {{tl|IPA symbol}}: %s</strong>'
            ,message
           )
  end
end

function p.main(frame)
  -- discard empty strings to allow for comparison with neg operators; trim everything else
  -- accepted parameters are:
  --   name             description
  --   ====             ===========
  --   (1)              the input
  --   output           the output, one of: input; name; page; soundfile; type
  --   custom_output    overrides `output`
  --   link             `true` or any non-empty value except false to link
  --   link_option      link to something other than the corresponding page
  --   ipa              `true` or any non-empty value except false to format in {{IPA}}
  --   ipa_option       one of: [ or ]; < or >; / to enclose in brackets, angles or slashes, respectively
  --   soundbox         `true` or any non-empty value except false for the customary soundbox
  --   soundbox_option  anything that could go in [[File:]] after the filename
  --   custom_error     overwrite the error message
  -- everything other than the input is optional (but you'll probably nearly always want one of output or custom_output)
  for k, v in pairs(frame.args) do
    if v ~= "" then
      args[k] = mw.text.trim(v)
    end
  end

  if not args[1] then return args.custom_output or "" end

  id = data.symbols[args[1]]
  if not id then return html_error('"' .. args[1] .. '" not found in list')end

  output = args.custom_output or __output[args.output] and __output[args.output]()
  if not output then return html_error("No such output option") end

  if args.link then output = __link() end
  if args.ipa then output = __ipa() end
  if args.soundbox then output = __soundbox() end

  return output
end

return p