summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/lxml-xml.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/context/base/lxml-xml.lua')
-rw-r--r--Master/texmf-dist/tex/context/base/lxml-xml.lua74
1 files changed, 59 insertions, 15 deletions
diff --git a/Master/texmf-dist/tex/context/base/lxml-xml.lua b/Master/texmf-dist/tex/context/base/lxml-xml.lua
index f791ec0f83f..89fcba871bd 100644
--- a/Master/texmf-dist/tex/context/base/lxml-xml.lua
+++ b/Master/texmf-dist/tex/context/base/lxml-xml.lua
@@ -6,10 +6,15 @@ if not modules then modules = { } end modules ['lxml-xml'] = {
license = "see context related readme files"
}
+local concat = string.concat
+
+local xml = xml
+
local finalizers = xml.finalizers.xml
local xmlfilter = xml.filter -- we could inline this one for speed
local xmltostring = xml.tostring
local xmlserialize = xml.serialize
+local xmlcollected = xml.collected
local function first(collected) -- wrong ?
return collected and collected[1]
@@ -23,15 +28,18 @@ local function all(collected)
return collected
end
-local function reverse(collected)
- if collected then
- local reversed = { }
- for c=#collected,1,-1 do
- reversed[#reversed+1] = collected[c]
- end
- return reversed
- end
-end
+--~ local function reverse(collected)
+--~ if collected then
+--~ local reversed, r = { }, 0
+--~ for c=#collected,1,-1 do
+--~ r = r + 1
+--~ reversed[r] = collected[c]
+--~ end
+--~ return reversed
+--~ end
+--~ end
+
+local reverse = table.reversed
local function attribute(collected,name)
if collected and #collected > 0 then
@@ -122,11 +130,12 @@ end
local function texts(collected)
if collected then
- local t = { }
+ local t, n = { }, 0
for c=1,#collected do
- local e = collection[c]
+ local e = collected[c]
if e and e.dt then
- t[#t+1] = e.dt
+ n = n + 1
+ t[n] = e.dt
end
end
return t
@@ -169,14 +178,15 @@ end
local function tags(collected,nonamespace)
if collected then
- local t = { }
+ local t, n = { }, 0
for c=1,#collected do
local e = collected[c]
local ns, tg = e.ns, e.tg
+ n = n + 1
if nonamespace or ns == "" then
- t[#t+1] = tg
+ t[n] = tg
else
- t[#t+1] = ns .. ":" .. tg
+ t[n] = ns .. ":" .. tg
end
end
return t
@@ -286,3 +296,37 @@ end
xml.all = xml.filter
xml.index = xml.position
xml.found = xml.filter
+
+-- a nice one:
+
+local function totable(x)
+ local t = { }
+ for e in xmlcollected(x[1] or x,"/*") do
+ t[e.tg] = xmltostring(e.dt) or ""
+ end
+ return next(t) and t or nil
+end
+
+xml.table = totable
+finalizers.table = totable
+
+local function textonly(e,t)
+ if e then
+ local edt = e.dt
+ if edt then
+ for i=1,#edt do
+ local e = edt[i]
+ if type(e) == "table" then
+ textonly(e,t)
+ else
+ t[#t+1] = e
+ end
+ end
+ end
+ end
+ return t
+end
+
+function xml.textonly(e) -- no pattern
+ return concat(textonly(e,{}))
+end