summaryrefslogtreecommitdiff
path: root/xml.lua
diff options
context:
space:
mode:
Diffstat (limited to 'xml.lua')
-rw-r--r--xml.lua34
1 files changed, 20 insertions, 14 deletions
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 = {
+ ["<"]="&lt;",
+ [">"]="&gt;",
+ ["&"]="&amp;",
+ ['"']="&quot;",
+ ["'"]="&apos;"
+}
+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 = {
- ["<"]="&lt;",
- [">"]="&gt;",
- ["&"]="&amp;",
- ['"']="&quot;",
- ["'"]="&apos;"
-}
-
-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