[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: htmlify a string?
- From: Gavin Wraith <gavin@...>
- Date: Tue, 18 Oct 2005 14:10:59 +0100
In message <ee01f29db00d01426bcf5e39ea65e7de@tombob.com> you wrote:
>
> Does anybody have a handy, little function to htmlify an 8 bit latin1
> string?
do
local entity = {
["<"] = "<",
[">"] = ">",
["&&"] = "&",
};
htmlify = function (s)
assert(type(s) == "string")
for i,v in pairs(entity) do
s = s:gsub(i,v)
end -- for
return s:gsub("(.)",function (c)
local n,fmts = c:byte(),"&#%s;"
return ((n > 127) and fmts:format(n)) or c
end)
end -- function
end --do
This is modified from code used in the TEXT function in Weave
(http://weave.riscos.org.uk/) which actually uses ropes rather
than strings [ rope ::= string | list of rope ]. It does not use fancy
names for all the entities.
--
Gavin Wraith (gavin@wra1th.plus.com)
Home page: http://www.wra1th.plus.com/