Jump to content
šŸ—³ļøšŸ–„ļø The Wikimedia Foundation December 2024 Developer Satisfaction Survey closes on the 2025-01-03. This survey helps us measure developer satisfaction, and prioritize work on tools and platforms for our technical contributors.

Module:N34

From wikiNonStop
Revision as of 07:01, 27 December 2024 by Majestix (talk | contribs) (1 revision imported: Initial )
(diff) ā† Older revision | Latest revision (diff) | Newer revision ā†’ (diff)
Module documentation
local n34 = {}
local gsub, char, byte, format = string.gsub, string.char, string.byte, string.format
local entityMap  = {["lt"]="<",["gt"]=">",["amp"]="&",["quot"]='"',["apos"]="'"}
local entitySwap = function(orig,n,s)
  return (n=='' and entityMap[s])
         or (n=="#" and tonumber(s)) and char(s)
         or (n=="#x" and tonumber(s,16)) and char(tonumber(s,16))
         or orig
end

function n34.encode ( frame )
    local args = frame.args;
    local str = args[1];

    str = gsub (str, "\n", "\r\n")
    str = gsub (str, "([^%w ])",
        function (c) return format ("%%%02X", byte(c)) end)
    str = gsub (str, " ", "+")
    return str
end

function n34.decode( frame )
    local args = frame.args;
    local str = args[1];

    str = gsub (str, "+", " ")
    str = gsub (str, "%%(%x%x)",
        function(h) return char(tonumber(h,16)) end)
    str = gsub (str, "\r\n", "\n")

    return str
end

function n34.hextoden( frame )
    local args = frame.args;
    local str = args[1];

    str = tostring(tonumber(str, 16))

    return str
end

function n34.unescapehtml( frame )
    local args = frame.args;
    local str = args[1];

    return (gsub( str, '(&(#?x?)([%d%a]+);)', entitySwap ))
end

function n34.escapehtml( frame )
    local args = frame.args;
    local str = args[1];

    str = gsub (str, "\n", "\r\n")
    str = gsub (str, "([^%w ])",
        function (c) return format ("&#%u;", byte(c)) end)
    str = gsub (str, " ", "+")

    return str
end

return n34