The official discord link if you wish to join the discord: https://discord.gg/j5RKwCvAFu

Support the wiki on our official Ko-Fi page or Patreon page!

Module:Tags

From The Codex
Revision as of 16:20, 9 March 2023 by GiverOfThePeace (talk | contribs) (Created page with "--- Shared module that converts <code><tag></code>s in certain areas into -- colored text or text with a specified font. -- @module tags -- @alias p -- @author 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}...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Lua error in package.lua at line 80: module 'Dev:Docbunto' not found.


--- 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 data-font="', tag, '">'})
    elseif data.colors[tag] then
        return table.concat({'<span data-color="', 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

--- 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.ustring.gsub(text, '<(%w+)>', replacer_open)
    text = mw.ustring.gsub(text, '</(%w+)>', replacer_close)
    return text
end

return p
--  </nowiki>