יחידה:קידוד תווים מיוחדים

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:קידוד תווים מיוחדים/תיעוד

local p = {}

local html_entities = {
    ["""] = '"',
    ["'"] = "'",
    ["&"] = "&",
    ["&lt;"] = "<",
    ["&gt;"] = ">",
    ["&#39;"] = "'"
}

function p.decode_html_entities( frame )
    str = frame.args.string
    return p.decode_html_entities_internal(str)
end

function p.decode_html_entities_internal(str)
    -- First, decode any percent-encoded characters
    str = str:gsub("%%(%x%x)", function(hex)
        return string.char(tonumber(hex, 16))
    end)

    -- Then, decode HTML entities
    str = str:gsub("(&.-;)", function(entity)
        return html_entities[entity] or entity
    end)

    return str
end

return p