[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: htmlify a string?
- From: Mike Pall <mikelu-0510@...>
- Date: Wed, 19 Oct 2005 14:29:03 +0200
Hi,
Rici Lake wrote:
> If the string to be escaped is ISO-8859-1, and you really want to
> escape high-ascii numerically, just extend the escapes table:
>
> do
> local escapes = {["&"] = "&", ["<"] = "<", [">"] = ">"}
> for i = 128, 255 do escapes[string.char(i)] = "&#"..i..";" end
> local function escape(c) return escapes[c] end
> function html_escape(str) return (str:gsub("[&<>\128-\255]", escape)) end
> end
I suggest lazy creation of the non-ascii substitutions since
only a handful of them is ever used in most texts:
do
local byte = string.byte
local escapes = setmetatable({["&"]="&", ["<"]="<", [">"]=">"},
{ __index = function(t, c)
local s = "&#"..byte(c)..";"; t[c] = s; return s; end})
local function escape(c) return escapes[c] end
function html_escape(str) return (str:gsub("[&<>\128-\255]", escape)) end
end
[Yes, this works just fine with my patch, too.]
Bye,
Mike