summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/lxml-tex.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/context/base/lxml-tex.lua')
-rw-r--r--Master/texmf-dist/tex/context/base/lxml-tex.lua331
1 files changed, 199 insertions, 132 deletions
diff --git a/Master/texmf-dist/tex/context/base/lxml-tex.lua b/Master/texmf-dist/tex/context/base/lxml-tex.lua
index aaa90217fec..26a8b29962e 100644
--- a/Master/texmf-dist/tex/context/base/lxml-tex.lua
+++ b/Master/texmf-dist/tex/context/base/lxml-tex.lua
@@ -9,33 +9,30 @@ if not modules then modules = { } end modules ['lxml-tst'] = {
local utf = unicode.utf8
local utfchar = utf.char
-local concat, insert, remove, gsub, find = table.concat, table.insert, table.remove
+local concat, insert, remove = table.concat, table.insert, table.remove
local format, sub, gsub, find, gmatch, match = string.format, string.sub, string.gsub, string.find, string.gmatch, string.match
local type, next, tonumber, tostring = type, next, tonumber, tostring
local lpegmatch = lpeg.match
local P, S, C, Cc = lpeg.P, lpeg.S, lpeg.C, lpeg.Cc
-if not tex and not tex.sprint then
- tex = {
- sprint = function(catcodes,...) texio.write(table.concat{...}) end,
- print = function(catcodes,...) texio.write(table.concat{...}) end,
- write = function( ...) texio.write(table.concat{...}) end,
- }
- commands = {
- writestatus = logs.report
- }
- resolvers.loadbinfile = function(filename) return true, io.loaddata(filename) end
-end
+local tex, xml = tex, xml
+local lowerchars, upperchars, lettered = characters.lower, characters.upper, characters.lettered
+
+lxml = lxml or { }
+local lxml = lxml
local texsprint, texprint, texwrite = tex.sprint, tex.print, tex.write
local texcatcodes, ctxcatcodes, vrbcatcodes, notcatcodes = tex.texcatcodes, tex.ctxcatcodes, tex.vrbcatcodes, tex.notcatcodes
local xmlelements, xmlcollected, xmlsetproperty = xml.elements, xml.collected, xml.setproperty
-local xmlparseapply, xmlwithelements = xml.parse_apply, xml.withelements
+local xmlwithelements = xml.withelements
local xmlserialize, xmlcollect, xmltext, xmltostring = xml.serialize, xml.collect, xml.text, xml.tostring
+local xmlapplylpath = xml.applylpath
local variables = (interfaces and interfaces.variables) or { }
+local insertbeforevalue, insertaftervalue = utilities.tables.insertbeforevalue, utilities.tables.insertaftervalue
+
local starttiming, stoptiming = statistics.starttiming, statistics.stoptiming
local trace_setups = false trackers.register("lxml.setups", function(v) trace_setups = v end)
@@ -43,9 +40,9 @@ local trace_loading = false trackers.register("lxml.loading", function(v) tra
local trace_access = false trackers.register("lxml.access", function(v) trace_access = v end)
local trace_comments = false trackers.register("lxml.comments", function(v) trace_comments = v end)
-lxml = lxml or { }
-lxml.loaded = lxml.loaded or { }
+local report_lxml = logs.reporter("xml","tex")
+lxml.loaded = lxml.loaded or { }
local loaded = lxml.loaded
-- print(contextdirective("context-mathml-directive function reduction yes "))
@@ -75,14 +72,14 @@ local xmltextcapture = (
space^0 * newline^2 * Cc("") / texprint + -- better ^-2 ?
space^0 * newline * space^0 * Cc(" ") / texsprint +
content / function(str) return texsprint(notcatcodes,str) end + -- was just texsprint, current catcodes regime is notcatcodes
- entity / xml.resolved_entity
+ entity / xml.resolvedentity
)^0
local ctxtextcapture = (
space^0 * newline^2 * Cc("") / texprint + -- better ^-2 ?
space^0 * newline * space^0 * Cc(" ") / texsprint +
content / function(str) return texsprint(ctxcatcodes,str) end + -- was just texsprint, current catcodes regime is notcatcodes
- entity / xml.resolved_entity
+ entity / xml.resolvedentity
)^0
local forceraw, rawroot = false, nil
@@ -137,23 +134,23 @@ local function toverbatim(str)
if aftercommand then texsprint(texcatcodes,aftercommand,"{}") end
end
-function lxml.set_verbatim(before,after,obeyedline,obeyedspace)
+function lxml.setverbatim(before,after,obeyedline,obeyedspace)
beforecommand, aftercommand, linecommand, spacecommand = before, after, obeyedline, obeyedspace
end
local obeycdata = true
-function lxml.set_cdata()
+function lxml.setcdata()
obeycdata = true
end
-function lxml.reset_cdata()
+function lxml.resetcdata()
obeycdata = false
end
-- cdata and verbatim
-lxml.set_verbatim("\\xmlcdatabefore", "\\xmlcdataafter", "\\xmlcdataobeyedline", "\\xmlcdataobeyedspace")
+lxml.setverbatim("\\xmlcdatabefore", "\\xmlcdataafter", "\\xmlcdataobeyedline", "\\xmlcdataobeyedspace")
-- local capture = (space^0*newline)^0 * capture * (space+newline)^0 * -1
@@ -189,7 +186,7 @@ function lxml.splitid(id)
end
end
-local function get_id(id, qualified)
+local function getid(id, qualified)
if id then
local lid = loaded[id]
if lid then
@@ -211,25 +208,25 @@ local function get_id(id, qualified)
return root
end
elseif trace_access then
- logs.report("lxml","'%s' has no index entry '%s'",d,i)
+ report_lxml("'%s' has no index entry '%s'",d,i)
end
elseif trace_access then
- logs.report("lxml","'%s' has no index",d)
+ report_lxml("'%s' has no index",d)
end
elseif trace_access then
- logs.report("lxml","'%s' is not loaded",d)
+ report_lxml("'%s' is not loaded",d)
end
elseif trace_access then
- logs.report("lxml","'%s' is not loaded",i)
+ report_lxml("'%s' is not loaded",i)
end
end
elseif trace_access then
- logs.report("lxml","invalid id (nil)")
+ report_lxml("invalid id (nil)")
end
end
-lxml.id = get_id
-lxml.get_id = get_id
+lxml.id = getid -- we provide two names as locals can already use such
+lxml.getid = getid -- names and we don't want clashes
function lxml.root(id)
return loaded[id]
@@ -240,7 +237,7 @@ end
local nofindices = 0
local function addindex(name,check_sum,force)
- local root = get_id(name)
+ local root = getid(name)
if root and (not root.index or force) then -- weird, only called once
local n, index, maxindex, check = 0, root.index or { }, root.maxindex or 0, root.check or { }
local function nest(root)
@@ -270,7 +267,7 @@ local function addindex(name,check_sum,force)
root.index = index
root.maxindex = maxindex
if trace_access then
- logs.report("lxml","%s indexed, %s nodes",tostring(name),maxindex)
+ report_lxml("%s indexed, %s nodes",tostring(name),maxindex)
end
end
end
@@ -279,27 +276,27 @@ lxml.addindex = addindex
-- another cache
-local function lxmlparseapply(id,pattern) -- better inline, saves call
- return xmlparseapply({ get_id(id) }, pattern)
+local function lxmlapplylpath(id,pattern) -- better inline, saves call
+ return xmlapplylpath(getid(id),pattern)
end
-lxml.filter = lxmlparseapply
+lxml.filter = lxmlapplylpath
function lxml.filterlist(list,pattern)
for s in gmatch(list,"[^, ]+") do -- we could cache a table
- lxmlparseapply(s,pattern)
+ xmlapplylpath(getid(s),pattern)
end
end
-lxml["function"] = function(id,name)
+function lxml.applyfunction(id,name)
local f = xml.functions[name]
- return f and f(get_id(id))
+ return f and f(getid(id))
end
-- rather new, indexed storage (backward refs), maybe i will merge this
function lxml.checkindex(name)
- local root = get_id(name)
+ local root = getid(name)
return (root and root.index) or 0
end
@@ -359,12 +356,14 @@ end
function lxml.load(id,filename,compress,entities)
filename = commands.preparedfile(filename)
if trace_loading then
- commands.writestatus("lxml","loading file '%s' as '%s'",filename,id)
+ report_lxml("loading file '%s' as '%s'",filename,id)
end
noffiles, nofconverted = noffiles + 1, nofconverted + 1
-- local xmltable = xml.load(filename)
+ starttiming(xml)
local ok, data = resolvers.loadbinfile(filename)
local xmltable = lxml.convert(id,(ok and data) or "",compress,entities)
+ stoptiming(xml)
lxml.store(id,xmltable,filename)
return xmltable, filename
end
@@ -376,7 +375,7 @@ end
function lxml.include(id,pattern,attribute,recurse)
starttiming(xml)
- local root = get_id(id)
+ local root = getid(id)
xml.include(root,pattern,attribute,recurse,function(filename)
if filename then
filename = commands.preparedfile(filename)
@@ -387,7 +386,7 @@ function lxml.include(id,pattern,attribute,recurse)
end
end
if trace_loading then
- commands.writestatus("lxml","including file: %s",filename)
+ report_lxml("including file: %s",filename)
end
noffiles, nofconverted = noffiles + 1, nofconverted + 1
return resolvers.loadtexfile(filename) or ""
@@ -403,7 +402,8 @@ function xml.getbuffer(name,compress,entities) -- we need to make sure that comm
name = tex.jobname
end
nofconverted = nofconverted + 1
- xmltostring(lxml.convert(name,concat(buffers.data[name] or {},""),compress,entities))
+ local data = buffers.getcontent(name)
+ xmltostring(lxml.convert(name,data,compress,entities)) -- one buffer
end
function lxml.loadbuffer(id,name,compress,entities)
@@ -412,7 +412,8 @@ function lxml.loadbuffer(id,name,compress,entities)
end
starttiming(xml)
nofconverted = nofconverted + 1
- local xmltable = lxml.convert(id,buffers.collect(name or id,"\n"),compress,entities)
+ local data = buffers.getcontent(name or id)
+ local xmltable = lxml.convert(id,data,compress,entities)
lxml.store(id,xmltable)
stoptiming(xml)
return xmltable, name or id
@@ -444,7 +445,7 @@ end
local function tex_comment(e,handlers)
if trace_comments then
- logs.report("lxml","comment: %s",e.dt[1])
+ report_lxml("comment: %s",e.dt[1])
end
end
@@ -470,7 +471,7 @@ local function tex_element(e,handlers)
end
texsprint(ctxcatcodes,"\\xmlw{",command,"}{",rootname,"::",ix,"}")
else
- logs.report("lxml", "fatal error: no index for '%s'",command)
+ report_lxml( "fatal error: no index for '%s'",command)
texsprint(ctxcatcodes,"\\xmlw{",command,"}{",ix or 0,"}")
end
elseif tc == "function" then
@@ -481,17 +482,16 @@ end
local pihandlers = { } xml.pihandlers = pihandlers
-local kind = P("context-") * C((1-P("-"))^1) * P("-directive")
-local space = S(" \n\r")
-local spaces = space^0
-local class = C((1-space)^0)
-local key = class
-local value = C(P(1-(space * -1))^0)
+local category = P("context-") * C((1-P("-"))^1) * P("-directive")
+local space = S(" \n\r")
+local spaces = space^0
+local class = C((1-space)^0)
+local key = class
+local value = C(P(1-(space * -1))^0)
-local parser = kind * spaces * class * spaces * key * spaces * value
+local parser = category * spaces * class * spaces * key * spaces * value
pihandlers[#pihandlers+1] = function(str)
--- local kind, class, key, value = lpegmatch(parser,str)
if str then
local a, b, c, d = lpegmatch(parser,str)
if d then
@@ -522,7 +522,7 @@ local function ctx_text(e)
end
local function tex_handle(...)
--- logs.report("lxml", "error while flushing: %s", concat { ... })
+-- report_lxml( "error while flushing: %s", concat { ... })
texsprint(...) -- notcatcodes is active anyway
end
@@ -548,7 +548,7 @@ function lxml.serialize(root)
end
function lxml.setaction(id,pattern,action)
- local collected = lxmlparseapply(id,pattern)
+ local collected = xmlapplylpath(getid(id),pattern)
if collected then
for c=1,#collected do
collected[c].command = action
@@ -614,7 +614,7 @@ xml.cprint = cprint local xmlcprint = cprint
-- now we can flush
function lxml.main(id)
- xmlserialize(get_id(id),xmltexhandler) -- the real root (@rt@)
+ xmlserialize(getid(id),xmltexhandler) -- the real root (@rt@)
end
--~ -- lines (untested)
@@ -670,12 +670,12 @@ end
local setups = { }
-function lxml.set_command_to_text(id)
- xmlwithelements(get_id(id),to_text)
+function lxml.setcommandtotext(id)
+ xmlwithelements(getid(id),to_text)
end
-function lxml.set_command_to_none(id)
- xmlwithelements(get_id(id),to_none)
+function lxml.setcommandtonone(id)
+ xmlwithelements(getid(id),to_none)
end
function lxml.installsetup(what,document,setup,where)
@@ -687,24 +687,24 @@ function lxml.installsetup(what,document,setup,where)
end
if what == 1 then
if trace_loading then
- commands.writestatus("lxml","prepending setup %s for %s",setup,document)
+ report_lxml("prepending setup %s for %s",setup,document)
end
insert(sd,1,setup)
elseif what == 2 then
if trace_loading then
- commands.writestatus("lxml","appending setup %s for %s",setup,document)
+ report_lxml("appending setup %s for %s",setup,document)
end
insert(sd,setup)
elseif what == 3 then
if trace_loading then
- commands.writestatus("lxml","inserting setup %s for %s before %s",setup,document,where)
+ report_lxml("inserting setup %s for %s before %s",setup,document,where)
end
- table.insert_before_value(sd,setup,where)
+ insertbeforevalue(sd,setup,where)
elseif what == 4 then
if trace_loading then
- commands.writestatus("lxml","inserting setup %s for %s after %s",setup,document,where)
+ report_lxml("inserting setup %s for %s after %s",setup,document,where)
end
- table.insert_after_value(sd,setup,where)
+ insertaftervalue(sd,setup,where)
end
end
@@ -718,21 +718,21 @@ function lxml.flushsetups(id,...)
local v= sd[k]
if not done[v] then
if trace_loading then
- commands.writestatus("lxml","applying setup %02i = %s to %s",k,v,document)
+ report_lxml("applying setup %02i = %s to %s",k,v,document)
end
texsprint(ctxcatcodes,"\\xmlsetup{",id,"}{",v,"}")
done[v] = true
end
end
elseif trace_loading then
- commands.writestatus("lxml","no setups for %s",document)
+ report_lxml("no setups for %s",document)
end
end
end
function lxml.resetsetups(document)
if trace_loading then
- commands.writestatus("lxml","resetting all setups for %s",document)
+ report_lxml("resetting all setups for %s",document)
end
setups[document] = { }
end
@@ -743,7 +743,7 @@ function lxml.removesetup(document,setup)
for i=1,#s do
if s[i] == setup then
if trace_loading then
- commands.writestatus("lxml","removing setup %s for %s",setup,document)
+ report_lxml("removing setup %s for %s",setup,document)
end
remove(t,i)
break
@@ -754,7 +754,7 @@ end
function lxml.setsetup(id,pattern,setup)
if not setup or setup == "" or setup == "*" or setup == "-" or setup == "+" then
- local collected = lxmlparseapply(id,pattern)
+ local collected = xmlapplylpath(getid(id),pattern)
if collected then
if trace_setups then
for c=1, #collected do
@@ -762,19 +762,19 @@ function lxml.setsetup(id,pattern,setup)
local ix = e.ix or 0
if setup == "-" then
e.command = false
- logs.report("lxml","lpath matched (a) %5i: %s = %s -> skipped",c,ix,setup)
+ report_lxml("lpath matched (a) %5i: %s = %s -> skipped",c,ix,setup)
elseif setup == "+" then
e.command = true
- logs.report("lxml","lpath matched (b) %5i: %s = %s -> text",c,ix,setup)
+ report_lxml("lpath matched (b) %5i: %s = %s -> text",c,ix,setup)
else
local tg = e.tg
if tg then -- to be sure
e.command = tg
local ns = e.rn or e.ns
if ns == "" then
- logs.report("lxml","lpath matched (c) %5i: %s = %s -> %s",c,ix,tg,tg)
+ report_lxml("lpath matched (c) %5i: %s = %s -> %s",c,ix,tg,tg)
else
- logs.report("lxml","lpath matched (d) %5i: %s = %s:%s -> %s",c,ix,ns,tg,tg)
+ report_lxml("lpath matched (d) %5i: %s = %s:%s -> %s",c,ix,ns,tg,tg)
end
end
end
@@ -792,12 +792,12 @@ function lxml.setsetup(id,pattern,setup)
end
end
elseif trace_setups then
- logs.report("lxml","no lpath matches for %s",pattern)
+ report_lxml("no lpath matches for %s",pattern)
end
else
local a, b = match(setup,"^(.+:)([%*%-])$")
if a and b then
- local collected = lxmlparseapply(id,pattern)
+ local collected = xmlapplylpath(getid(id),pattern)
if collected then
if trace_setups then
for c=1, #collected do
@@ -806,23 +806,23 @@ function lxml.setsetup(id,pattern,setup)
if b == "-" then
e.command = false
if ns == "" then
- logs.report("lxml","lpath matched (e) %5i: %s = %s -> skipped",c,ix,tg)
+ report_lxml("lpath matched (e) %5i: %s = %s -> skipped",c,ix,tg)
else
- logs.report("lxml","lpath matched (f) %5i: %s = %s:%s -> skipped",c,ix,ns,tg)
+ report_lxml("lpath matched (f) %5i: %s = %s:%s -> skipped",c,ix,ns,tg)
end
elseif b == "+" then
e.command = true
if ns == "" then
- logs.report("lxml","lpath matched (g) %5i: %s = %s -> text",c,ix,tg)
+ report_lxml("lpath matched (g) %5i: %s = %s -> text",c,ix,tg)
else
- logs.report("lxml","lpath matched (h) %5i: %s = %s:%s -> text",c,ix,ns,tg)
+ report_lxml("lpath matched (h) %5i: %s = %s:%s -> text",c,ix,ns,tg)
end
else
e.command = a .. tg
if ns == "" then
- logs.report("lxml","lpath matched (i) %5i: %s = %s -> %s",c,ix,tg,e.command)
+ report_lxml("lpath matched (i) %5i: %s = %s -> %s",c,ix,tg,e.command)
else
- logs.report("lxml","lpath matched (j) %5i: %s = %s:%s -> %s",c,ix,ns,tg,e.command)
+ report_lxml("lpath matched (j) %5i: %s = %s:%s -> %s",c,ix,ns,tg,e.command)
end
end
end
@@ -839,10 +839,10 @@ function lxml.setsetup(id,pattern,setup)
end
end
elseif trace_setups then
- logs.report("lxml","no lpath matches for %s",pattern)
+ report_lxml("no lpath matches for %s",pattern)
end
else
- local collected = lxmlparseapply(id,pattern)
+ local collected = xmlapplylpath(getid(id),pattern)
if collected then
if trace_setups then
for c=1, #collected do
@@ -850,9 +850,9 @@ function lxml.setsetup(id,pattern,setup)
e.command = setup
local ns, tg, ix = e.rn or e.ns, e.tg, e.ix or 0
if ns == "" then
- logs.report("lxml","lpath matched (k) %5i: %s = %s -> %s",c,ix,tg,setup)
+ report_lxml("lpath matched (k) %5i: %s = %s -> %s",c,ix,tg,setup)
else
- logs.report("lxml","lpath matched (l) %5i: %s = %s:%s -> %s",c,ix,ns,tg,setup)
+ report_lxml("lpath matched (l) %5i: %s = %s:%s -> %s",c,ix,ns,tg,setup)
end
end
else
@@ -861,7 +861,7 @@ function lxml.setsetup(id,pattern,setup)
end
end
elseif trace_setups then
- logs.report("lxml","no lpath matches for %s",pattern)
+ report_lxml("no lpath matches for %s",pattern)
end
end
end
@@ -917,7 +917,8 @@ local function position(collected,n)
end
local function match(collected) -- is match in preceding collected, never change, see bibxml
- texwrite((collected and collected[1].mi) or 0)
+ local m = collected and collected[1]
+ texwrite(m and m.mi or 0)
end
local function index(collected,n)
@@ -932,9 +933,10 @@ local function index(collected,n)
end
end
-local function command(collected,cmd)
- if collected then
- for c=1,#collected do
+local function command(collected,cmd,otherwise)
+ local n = collected and #collected
+ if n and n > 0 then
+ for c=1,n do
local e = collected[c]
local ix = e.ix
if not ix then
@@ -943,6 +945,8 @@ local function command(collected,cmd)
end
texsprint(ctxcatcodes,"\\xmlw{",cmd,"}{",e.name,"::",ix,"}")
end
+ elseif otherwise then
+ texsprint(ctxcatcodes,"\\xmlw{",otherwise,"}{#1}")
end
end
@@ -995,7 +999,19 @@ local function ctxtext(collected)
end
end
-local lowerchars, upperchars = characters.lower, characters.upper
+--~ local str = xmltext(getid(id),pattern) or ""
+--~ str = gsub(str,"^%s*(.-)%s*$","%1")
+--~ if nolines then
+--~ str = gsub(str,"%s+"," ")
+--~ end
+
+local function stripped(collected) -- tricky as we strip in place
+ if collected then
+ for c=1,#collected do
+ cprint(xml.stripelement(collected[c]))
+ end
+ end
+end
local function lower(collected)
if collected then
@@ -1059,6 +1075,7 @@ finalizers.count = count
finalizers.command = command
finalizers.attribute = attribute
finalizers.text = text
+finalizers.stripped = stripped
finalizers.lower = lower
finalizers.upper = upper
finalizers.ctxtext = ctxtext
@@ -1126,7 +1143,7 @@ end
--
local function verbatim(id,before,after)
- local root = get_id(id)
+ local root = getid(id)
if root then
if before then texsprint(ctxcatcodes,before,"[",root.tg or "?","]") end
lxml.toverbatim(xmltostring(root.dt))
@@ -1145,21 +1162,21 @@ lxml.verbatim = verbatim
-- helpers
function lxml.first(id,pattern)
- local collected = lxmlparseapply(id,pattern)
+ local collected = xmlapplylpath(getid(id),pattern)
if collected then
first(collected)
end
end
function lxml.last(id,pattern)
- local collected = lxmlparseapply(id,pattern)
+ local collected = xmlapplylpath(getid(id),pattern)
if collected then
last(collected)
end
end
function lxml.all(id,pattern)
- local collected = lxmlparseapply(id,pattern)
+ local collected = xmlapplylpath(getid(id),pattern)
if collected then
all(collected)
end
@@ -1167,18 +1184,18 @@ end
function lxml.count(id,pattern)
-- always needs to produce a result so no test here
- count(lxmlparseapply(id,pattern))
+ count(xmlapplylpath(getid(id),pattern))
end
function lxml.attribute(id,pattern,a,default)
- local collected = lxmlparseapply(id,pattern)
+ local collected = xmlapplylpath(getid(id),pattern)
if collected then
attribute(collected,a,default)
end
end
function lxml.raw(id,pattern) -- the content, untouched by commands
- local collected = (pattern and lxmlparseapply(id,pattern)) or get_id(id)
+ local collected = (pattern and xmlapplylpath(getid(id),pattern)) or getid(id)
if collected then
texsprint(xmltostring(collected[1].dt))
end
@@ -1186,11 +1203,11 @@ end
function lxml.context(id,pattern) -- the content, untouched by commands
if not pattern then
- local collected = get_id(id)
+ local collected = getid(id)
-- texsprint(ctxcatcodes,collected.dt[1])
ctx_text(collected.dt[1])
else
- local collected = lxmlparseapply(id,pattern) or get_id(id)
+ local collected = xmlapplylpath(getid(id),pattern) or getid(id)
if collected and #collected > 0 then
texsprint(ctxcatcodes,collected[1].dt)
end
@@ -1198,7 +1215,7 @@ function lxml.context(id,pattern) -- the content, untouched by commands
end
function lxml.text(id,pattern)
- local collected = (pattern and lxmlparseapply(id,pattern)) or get_id(id)
+ local collected = (pattern and xmlapplylpath(getid(id),pattern)) or getid(id)
if collected then
text(collected)
end
@@ -1207,40 +1224,40 @@ end
lxml.content = text
function lxml.position(id,pattern,n)
- local collected = lxmlparseapply(id,pattern)
+ local collected = xmlapplylpath(getid(id),pattern)
if collected then
position(collected,n)
end
end
function lxml.chainattribute(id,pattern,a,default)
- local collected = lxmlparseapply(id,pattern)
+ local collected = xmlapplylpath(getid(id),pattern)
if collected then
chainattribute(collected,a,default)
end
end
function lxml.concatrange(id,pattern,start,stop,separator,lastseparator,textonly) -- test this on mml
- concatrange(lxmlparseapply(id,pattern),start,stop,separator,lastseparator,textonly)
+ concatrange(xmlapplylpath(getid(id),pattern),start,stop,separator,lastseparator,textonly)
end
function lxml.concat(id,pattern,separator,lastseparator,textonly)
- concatrange(lxmlparseapply(id,pattern),false,false,separator,lastseparator,textonly)
+ concatrange(xmlapplylpath(getid(id),pattern),false,false,separator,lastseparator,textonly)
end
function lxml.element(id,n)
- position(lxmlparseapply(id,"/*"),n)
+ position(xmlapplylpath(getid(id),"/*"),n)
end
lxml.index = lxml.position
function lxml.pos(id)
- local root = get_id(id)
+ local root = getid(id)
texwrite((root and root.ni) or 0)
end
function lxml.att(id,a,default)
- local root = get_id(id)
+ local root = getid(id)
if root then
local at = root.at
local str = (at and at[a]) or default
@@ -1253,7 +1270,7 @@ function lxml.att(id,a,default)
end
function lxml.name(id) -- or remapped name? -> lxml.info, combine
- local r = get_id(id)
+ local r = getid(id)
local ns = r.rn or r.ns or ""
if ns ~= "" then
texsprint(ns,":",r.tg)
@@ -1263,20 +1280,20 @@ function lxml.name(id) -- or remapped name? -> lxml.info, combine
end
function lxml.match(id) -- or remapped name? -> lxml.info, combine
- texsprint(get_id(id).mi or 0)
+ texsprint(getid(id).mi or 0)
end
function lxml.tag(id) -- tag vs name -> also in l-xml tag->name
- texsprint(get_id(id).tg or "")
+ texsprint(getid(id).tg or "")
end
function lxml.namespace(id) -- or remapped name?
- local root = get_id(id)
+ local root = getid(id)
texsprint(root.rn or root.ns or "")
end
function lxml.flush(id)
- id = get_id(id)
+ id = getid(id)
local dt = id and id.dt
if dt then
xmlsprint(dt)
@@ -1284,7 +1301,7 @@ function lxml.flush(id)
end
function lxml.snippet(id,i)
- local e = get_id(id)
+ local e = getid(id)
if e then
local edt = e.dt
if edt then
@@ -1294,12 +1311,12 @@ function lxml.snippet(id,i)
end
function lxml.direct(id)
- xmlsprint(get_id(id))
+ xmlsprint(getid(id))
end
function lxml.command(id,pattern,cmd)
- local i, p = get_id(id,true)
- local collected = lxmlparseapply(i,pattern)
+ local i, p = getid(id,true)
+ local collected = xmlapplylpath(getid(i),pattern)
if collected then
local rootname = p or i.name
for c=1,#collected do
@@ -1317,11 +1334,11 @@ end
-- loops
function lxml.collected(id,pattern,reverse)
- return xmlcollected(get_id(id),pattern,reverse)
+ return xmlcollected(getid(id),pattern,reverse)
end
function lxml.elements(id,pattern,reverse)
- return xmlelements(get_id(id),pattern,reverse)
+ return xmlelements(getid(id),pattern,reverse)
end
-- obscure ones
@@ -1334,16 +1351,16 @@ local found, empty = xml.found, xml.empty
local doif, doifnot, doifelse = commands.doif, commands.doifnot, commands.doifelse
-function lxml.doif (id,pattern) doif (found(get_id(id),pattern)) end
-function lxml.doifnot (id,pattern) doifnot (found(get_id(id),pattern)) end
-function lxml.doifelse (id,pattern) doifelse(found(get_id(id),pattern)) end
-function lxml.doiftext (id,pattern) doif (not empty(get_id(id),pattern)) end
-function lxml.doifnottext (id,pattern) doifnot (not empty(get_id(id),pattern)) end
-function lxml.doifelsetext (id,pattern) doifelse(not empty(get_id(id),pattern)) end
+function lxml.doif (id,pattern) doif (found(getid(id),pattern)) end
+function lxml.doifnot (id,pattern) doifnot (found(getid(id),pattern)) end
+function lxml.doifelse (id,pattern) doifelse(found(getid(id),pattern)) end
+function lxml.doiftext (id,pattern) doif (not empty(getid(id),pattern)) end
+function lxml.doifnottext (id,pattern) doifnot (not empty(getid(id),pattern)) end
+function lxml.doifelsetext (id,pattern) doifelse(not empty(getid(id),pattern)) end
-- special case: "*" and "" -> self else lpath lookup
---~ function lxml.doifelseempty(id,pattern) doifelse(isempty(get_id(id),pattern ~= "" and pattern ~= nil)) end -- not yet done, pattern
+--~ function lxml.doifelseempty(id,pattern) doifelse(isempty(getid(id),pattern ~= "" and pattern ~= nil)) end -- not yet done, pattern
-- status info
@@ -1389,15 +1406,15 @@ end)
-- misc
function lxml.nonspace(id,pattern) -- slow, todo loop
- xmltprint(xmlcollect(get_id(id),pattern,true))
+ xmltprint(xmlcollect(getid(id),pattern,true))
end
function lxml.strip(id,pattern,nolines,anywhere)
- xml.strip(get_id(id),pattern,nolines,anywhere)
+ xml.strip(getid(id),pattern,nolines,anywhere)
end
function lxml.stripped(id,pattern,nolines)
- local str = xmltext(get_id(id),pattern) or ""
+ local str = xmltext(getid(id),pattern) or ""
str = gsub(str,"^%s*(.-)%s*$","%1")
if nolines then
str = gsub(str,"%s+"," ")
@@ -1406,5 +1423,55 @@ function lxml.stripped(id,pattern,nolines)
end
function lxml.delete(id,pattern)
- xml.delete(get_id(id),pattern)
+ xml.delete(getid(id),pattern)
+end
+
+lxml.obsolete = { }
+
+lxml.get_id = getid lxml.obsolete.get_id = getid
+
+-- goodies:
+
+function xml.finalizers.tex.lettered(collected)
+ if collected then
+ for c=1,#collected do
+ texsprint(ctxcatcodes,lettered(collected[1].dt[1]))
+ end
+ end
+end
+
+--~ function xml.finalizers.tex.apply(collected,what) -- to be tested
+--~ if collected then
+--~ for c=1,#collected do
+--~ texsprint(ctxcatcodes,what(collected[1].dt[1]))
+--~ end
+--~ end
+--~ end
+
+function lxml.toparameters(id)
+ local e = getid(id)
+ if e then
+ local a = e.at
+ if a and next(a) then
+ local setups, s = { }, 0
+ for k, v in next, a do
+ s = s + 1
+ setups[s] = k .. "=" .. v
+ end
+ setups = concat(setups,",")
+ -- tracing
+ context(setups)
+ end
+ end
+end
+
+local template = '<?xml version="1.0" ?>\n\n<!-- %s -->\n\n%s'
+
+function lxml.tofile(id,pattern,filename,comment)
+ local collected = xmlapplylpath(getid(id),pattern)
+ if collected then
+ io.savedata(filename,format(template,comment or "exported fragment",tostring(collected[1])))
+ else
+ os.remove(filename) -- get rid of old content
+ end
end