Jump to content
החלפת מצב תפריט
שינוי מצב תפריט ההעדפות
החלפת מצב תפריט אישי
לא בחשבון
כתובת ה־IP שלך תהיה גלויה לציבור אם תעשה עריכות כלשהן.

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

מתוך ויקיפדיה, האנציקלופדיה החופשית

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

local p = {}

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

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.
    -- The pattern, "%%(%x%x)", means: a single percent character (represented in the pattern by two percent characters), followed by two hex digits (the two %x). The parentheses mean that we are interested in these two hex digits.
    str = str:gsub("%%(%x%x)", function(hex)
        return string.char(tonumber(hex, 16))
    end)

    -- Then, decode HTML entities.
    -- The pattern, "(&.-;)", means: an ampersand character, followed by 0 or more characters of any kind (the period means a character of any kind, and the dash means 0 or more), followed by a semicolon. The parentheses mean that we are interested in all of this content (although in that case the parentheses can be omitted).
    str = str:gsub("(&.-;)", function(entity)
        return html_entities[entity] or entity
    end)

    return str
end

return p