lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


In message <ee01f29db00d01426bcf5e39ea65e7de@tombob.com> you wrote:


> 
> Does anybody have a handy, little function to htmlify an 8 bit latin1
> string?

do
local entity = {
  ["<"] = "&lt;",
  [">"] = "&gt;",
  ["&&"] = "&amp;",
                };

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/