diff options
Diffstat (limited to 'xml.lua')
-rw-r--r-- | xml.lua | 34 |
1 files changed, 20 insertions, 14 deletions
@@ -2,6 +2,23 @@ -- modified by me a bit +entity_escapes = { + ["<"]="<", + [">"]=">", + ["&"]="&", + ['"']=""", + ["'"]="'" +} +entity_unescapes = {} +for k,v in pairs(entity_escapes) do entity_unescapes[v]=k end + +function escape(s) + return s:gsub("[<>&'\"]",entity_escapes) +end +function unescape(s) + return s:gsub("&[a-z]+;",entity_unescapes) +end + local function parseargs(s) local arg = {} string.gsub(s, "([%-%w]+)=([\"'])(.-)%2", function (w, _, a) @@ -46,14 +63,14 @@ psingle = function(s, i) elseif #rest == 0 then error('empty string') else - return i+#rest, rest + return i+#rest, unescape(rest) end end local nexti = j+1 local pretext = s:sub(i, ts-1) if not pretext:find("^%s*$") then -- not entirely whitespace - return ts, pretext + return ts, unescape(pretext) end if empty == "/" then @@ -80,24 +97,13 @@ local function stanzae(getmore) coroutine.yield(el) buf = buf:sub(ni) else - local more = getmore() + local more = assert(getmore()) buf = buf .. more end end end) end -local entity_escapes = { - ["<"]="<", - [">"]=">", - ["&"]="&", - ['"']=""", - ["'"]="'" -} - -local function escape(s) - return s:gsub("[<>&'\"]",entity_escapes) -end local safestr_mt = {name='SAFESTR', __tostring=function(x) return x.s end} local function safestr(s) return setmetatable({s=s}, safestr_mt) end |