Aller au contenu

Module:Tags

De Wiki Undertale FR

Shared module that converts <tag>s in certain areas into colored text or text with a specified font.

Documentation

Package items

tags.replace(text) (function)
Module's main function. Given a string of text, replaces all tags with text with appropriate fonts and colors.
Parameter: text Text to replace (string)
Returns: Replacement text (string)
f.main(frame) (function)
Module's entry point for invocation.
Parameter: frame Scribunto frame object (table)
Returns: Text with the tags replaced (string)

Private items

replacer_open(tag) (function • local)
Replacer function for opening tags.
Parameter: tag Tag whose opening tag to replace (string)
Returns: Replacement text for the tag (string)
replacer_close(tag) (function • local)
Replacer function for closing tags.
Parameter: tag Tag whose closing tag to replace (string)
Returns: Replacement text for the tag (string)
--- Shared module that converts <code>&lt;tag&gt;</code>s in certain areas into
--  colored text or text with a specified font.
--  @module             tags
--  @alias              p
--  @author             [[User:KockaAdmiralac|KockaAdmiralac]]
--  <nowiki>
local p = {}

--  Module dependencies.
local data = mw.loadData('Module:Tags/data')

--  Private logic.

--- Replacer function for opening tags.
--  @function           replacer_open
--  @param              {string} tag Tag whose opening tag to replace
--  @returns            {string} Replacement text for the tag
--  @local
local function replacer_open(tag)
    if data.fonts[tag] then
        return table.concat({'<span class="', tag, '">'})
    elseif data.colors[tag] then
        return table.concat({'<span class="', tag, '">'})
    else
        return table.concat({'<', tag, '>'})
    end
end

--- Replacer function for closing tags.
--  @function           replacer_close
--  @param              {string} tag Tag whose closing tag to replace
--  @returns            {string} Replacement text for the tag
--  @local
local function replacer_close(tag, text)
    if data.fonts[tag] or data.colors[tag] then
        return '</span>'
    else
        return table.concat({'</', tag, '>'})
    end
end

-- Package items.

--- Module's main function.
--  Given a string of text, replaces all tags with text with appropriate fonts
--  and colors.
--  @function           p.replace
--  @param              {string} text Text to replace
--  @returns            {string} Replacement text
function p.replace(text)
    text = mw.text.trim(text)
    text = mw.ustring.gsub(text, '\n', '<br />')
    text = mw.ustring.gsub(text, '<(%w+)>', replacer_open)
    text = mw.ustring.gsub(text, '</(%w+)>', replacer_close)
    return text
end

--- Module's entry point for invocation.
--  @function           f.main
--  @param              {table} frame Scribunto frame object
--  @return             {string} Text with the tags replaced
function p.main(frame)
    return p.replace(frame.args[1])
end

return p
--  </nowiki>