From bf776624fb59d147b82d2a6a13c36292844a47b7 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Fri, 6 Sep 2024 16:52:27 +0100 Subject: mysterious uncommited changes (actually committing 2025-02-20) --- xml.lua | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) (limited to 'xml.lua') diff --git a/xml.lua b/xml.lua index 35d2804..68fa741 100644 --- a/xml.lua +++ b/xml.lua @@ -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 -- cgit v1.2.3