Module:StripTags

From JoJo's Bizarre Encyclopedia - JoJo Wiki
Revision as of 03:41, 24 July 2024 by Vish (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:StripTags/doc

local p = {}

local function decodeHtmlEntities(text)
    local entities = {
        ["""] = '"',
        ["&"] = "&",
        ["&lt;"] = "<",
        ["&gt;"] = ">",
        ["&#39;"] = "'",
        ["&apos;"] = "'",
        ["&ldquo;"] = '“',
        ["&rdquo;"] = '”',
        ["&lsquo;"] = '‘',
        ["&rsquo;"] = '’',
    }
    return text:gsub("(&#?[%w]+;)", entities)
end

local function stripWikiMarkup(text)
    -- Remove [[ ]] wiki links, keep the display text
    text = mw.ustring.gsub(text, '%[%[([^|%]]+)|([^%]]+)%]%]', '%2')
    text = mw.ustring.gsub(text, '%[%[([^|%]]+)%]%]', '%1')
    -- Remove '' for italics and ''' for bold
    text = mw.ustring.gsub(text, "'''", "")
    text = mw.ustring.gsub(text, "''", "")
    return text
end

function p.stripTags(frame)
    local text = frame.args[1]
    -- Remove HTML tags
    text = mw.ustring.gsub(text, '<[^>]->', '')
    -- Decode HTML entities
    text = decodeHtmlEntities(text)
    -- Strip wiki markup
    text = stripWikiMarkup(text)
    return text
end

return p