diff options
Diffstat (limited to 'Master/texmf-dist/tex/context/base/lxml-tex.lua')
-rw-r--r-- | Master/texmf-dist/tex/context/base/lxml-tex.lua | 190 |
1 files changed, 94 insertions, 96 deletions
diff --git a/Master/texmf-dist/tex/context/base/lxml-tex.lua b/Master/texmf-dist/tex/context/base/lxml-tex.lua index 0b21e0a9c86..112f627511f 100644 --- a/Master/texmf-dist/tex/context/base/lxml-tex.lua +++ b/Master/texmf-dist/tex/context/base/lxml-tex.lua @@ -1,4 +1,4 @@ -if not modules then modules = { } end modules ['lxml-tst'] = { +if not modules then modules = { } end modules ['lxml-tex'] = { version = 1.001, comment = "companion to lxml-ini.mkiv", author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", @@ -10,12 +10,10 @@ if not modules then modules = { } end modules ['lxml-tst'] = { -- interface and not the context one. If we ever do that there will -- be an cldf-xml helper library. -local utf = unicode.utf8 - -local utfchar, utfupper = utf.char, utf.upper +local utfchar = utf.char 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 type, next, tonumber, tostring, select = type, next, tonumber, tostring, select local lpegmatch = lpeg.match local P, S, C, Cc = lpeg.P, lpeg.S, lpeg.C, lpeg.Cc @@ -25,9 +23,12 @@ local lowerchars, upperchars, lettered = characters.lower, characters.upper, cha lxml = lxml or { } local lxml = lxml -local ctxcatcodes, notcatcodes = tex.ctxcatcodes, tex.notcatcodes +local catcodenumbers = catcodes.numbers +local ctxcatcodes = catcodenumbers.ctxcatcodes -- todo: use different method +local notcatcodes = catcodenumbers.notcatcodes -- todo: use different method -local contextsprint = context.sprint -- with catcodes (here we use fast variants, but with option for tracing) +local context = context +local contextsprint = context.sprint -- with catcodes (here we use fast variants, but with option for tracing) local xmlelements, xmlcollected, xmlsetproperty = xml.elements, xml.collected, xml.setproperty local xmlwithelements = xml.withelements @@ -69,14 +70,14 @@ local parsedentity = xml.parsedentitylpeg function lxml.registerentity(key,value) texentities[key] = value if trace_entities then - report_xml("registering tex entity '%s' as: %s",key,value) + report_xml("registering tex entity %a as %a",key,value) end end function lxml.resolvedentity(str) if forceraw then if trace_entities then - report_xml("passing entity '%s' as &%s;",str,str) + report_xml("passing entity %a as &%s;",str,str) end context("&%s;",str) else @@ -85,12 +86,12 @@ function lxml.resolvedentity(str) local te = type(e) if te == "function" then if trace_entities then - report_xml("passing entity '%s' using function",str) + report_xml("passing entity %a using function",str) end e(str) elseif e then if trace_entities then - report_xml("passing entity '%s' as '%s'using ctxcatcodes",str,e) + report_xml("passing entity %a as %a using %a",str,e,"ctxcatcodes") end context(e) end @@ -104,7 +105,7 @@ function lxml.resolvedentity(str) end if e then if trace_entities then - report_xml("passing entity '%s' as '%s' using notcatcodes",str,e) + report_xml("passing entity %a as %a using %a",str,e,"notcatcodes") end contextsprint(notcatcodes,e) return @@ -115,18 +116,18 @@ function lxml.resolvedentity(str) local chr, err = lpegmatch(parsedentity,str) if chr then if trace_entities then - report_xml("passing entity '%s' as '%s' using ctxcatcodes",str,chr) + report_xml("passing entity %a as %a using %a",str,chr,"ctxcatcodes") end context(chr) elseif err then if trace_entities then - report_xml("passing faulty entity '%s' as '%s'",str,err) + report_xml("passing faulty entity %a as %a",str,err) end context(err) else - local tag = utfupper(str) + local tag = upperchars(str) if trace_entities then - report_xml("passing entity '%s' to \\xmle using tag '%s'",str,tag) + report_xml("passing entity %a to \\xmle using tag %a",str,tag) end context.xmle(str,tag) -- we need to use our own upper end @@ -260,16 +261,16 @@ local function getid(id, qualified) return root end elseif trace_access then - report_lxml("'%s' has no index entry '%s'",d,i) + report_lxml("%a has no index entry %a",d,i) end elseif trace_access then - report_lxml("'%s' has no index",d) + report_lxml("%a has no index",d) end elseif trace_access then - report_lxml("'%s' is not loaded",d) + report_lxml("%a is not loaded",d) end elseif trace_access then - report_lxml("'%s' is not loaded",i) + report_lxml("%a is not loaded",i) end end elseif trace_access then @@ -319,7 +320,7 @@ local function addindex(name,check_sum,force) root.index = index root.maxindex = maxindex if trace_access then - report_lxml("%s indexed, %s nodes",tostring(name),maxindex) + report_lxml("indexed entries %a, found nodes %a",tostring(name),maxindex) end end end @@ -381,7 +382,10 @@ function xml.load(filename,settings) noffiles, nofconverted = noffiles + 1, nofconverted + 1 starttiming(xml) local ok, data = resolvers.loadbinfile(filename) + settings = settings or { } + settings.currentresource = filename local xmltable = xml.convert((ok and data) or "",settings) + settings.currentresource = nil stoptiming(xml) return xmltable end @@ -390,12 +394,13 @@ local function entityconverter(id,str) return xmlentities[str] or xmlprivatetoken(str) or "" -- roundtrip handler end -function lxml.convert(id,data,entities,compress) +function lxml.convert(id,data,entities,compress,currentresource) local settings = { -- we're now roundtrip anyway unify_predefined_entities = true, utfize_entities = true, resolve_predefined_entities = true, resolve_entities = function(str) return entityconverter(id,str) end, -- needed for mathml + currentresource = tostring(currentresource or id), } if compress and compress == variables.yes then settings.strip_cm_and_dt = true @@ -410,13 +415,13 @@ end function lxml.load(id,filename,compress,entities) filename = commands.preparedfile(filename) -- not commands! if trace_loading then - report_lxml("loading file '%s' as '%s'",filename,id) + report_lxml("loading file %a as %a",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) + local xmltable = lxml.convert(id,(ok and data) or "",compress,entities,format("id: %s, file: %s",id,filename)) stoptiming(xml) lxml.store(id,xmltable,filename) return xmltable, filename @@ -440,7 +445,7 @@ function lxml.include(id,pattern,attribute,recurse) end end if trace_loading then - report_lxml("including file: %s",filename) + report_lxml("including file %a",filename) end noffiles, nofconverted = noffiles + 1, nofconverted + 1 return resolvers.loadtexfile(filename) or "" @@ -457,14 +462,14 @@ function xml.getbuffer(name,compress,entities) -- we need to make sure that comm end nofconverted = nofconverted + 1 local data = buffers.getcontent(name) - xmltostring(lxml.convert(name,data,compress,entities)) -- one buffer + xmltostring(lxml.convert(name,data,compress,entities,format("buffer: %s",tostring(name or "?")))) -- one buffer end function lxml.loadbuffer(id,name,compress,entities) starttiming(xml) nofconverted = nofconverted + 1 local data = buffers.collectcontent(name or id) -- name can be list - local xmltable = lxml.convert(id,data,compress,entities) + local xmltable = lxml.convert(id,data,compress,entities,format("buffer: %s",tostring(name or id or "?"))) lxml.store(id,xmltable) stoptiming(xml) return xmltable, name or id @@ -473,7 +478,7 @@ end function lxml.loaddata(id,str,compress,entities) starttiming(xml) nofconverted = nofconverted + 1 - local xmltable = lxml.convert(id,str or "",compress,entities) + local xmltable = lxml.convert(id,str or "",compress,entities,format("id: %s",id)) lxml.store(id,xmltable) stoptiming(xml) return xmltable, id @@ -496,7 +501,7 @@ end local function tex_comment(e,handlers) if trace_comments then - report_lxml("comment: %s",e.dt[1]) + report_lxml("comment %a",e.dt[1]) end end @@ -523,7 +528,7 @@ local function tex_element(e,handlers) -- faster than context.xmlw contextsprint(ctxcatcodes,"\\xmlw{",command,"}{",rootname,"::",ix,"}") else - report_lxml( "fatal error: no index for '%s'",command) + report_lxml("fatal error: no index for %a",command) contextsprint(ctxcatcodes,"\\xmlw{",command,"}{",ix or 0,"}") end elseif tc == "function" then @@ -745,37 +750,37 @@ function lxml.main(id) xmlserialize(getid(id),xmltexhandler) -- the real root (@rt@) end ---~ -- lines (untested) ---~ ---~ local buffer = { } ---~ ---~ local xmllinescapture = ( ---~ newline^2 / function() buffer[#buffer+1] = "" end + ---~ newline / function() buffer[#buffer] = buffer[#buffer] .. " " end + ---~ content / function(s) buffer[#buffer] = buffer[#buffer] .. s end ---~ )^0 ---~ ---~ local xmllineshandler = table.copy(xmltexhandler) ---~ ---~ xmllineshandler.handle = function(...) lpegmatch(xmllinescapture,concat{ ... }) end ---~ ---~ function lines(root) ---~ if not root then ---~ -- rawroot = false ---~ -- quit ---~ elseif type(root) == 'string' then ---~ -- rawroot = false ---~ lpegmatch(xmllinescapture,root) ---~ elseif next(root) then -- tr == 'table' ---~ xmlserialize(root,xmllineshandler) ---~ end ---~ end ---~ ---~ function xml.lines(root) -- used at all? ---~ buffer = { "" } ---~ lines(root) ---~ return result ---~ end +-- -- lines (untested) +-- +-- local buffer = { } +-- +-- local xmllinescapture = ( +-- newline^2 / function() buffer[#buffer+1] = "" end + +-- newline / function() buffer[#buffer] = buffer[#buffer] .. " " end + +-- content / function(s) buffer[#buffer] = buffer[#buffer] .. s end +-- )^0 +-- +-- local xmllineshandler = table.copy(xmltexhandler) +-- +-- xmllineshandler.handle = function(...) lpegmatch(xmllinescapture,concat{ ... }) end +-- +-- function lines(root) +-- if not root then +-- -- rawroot = false +-- -- quit +-- elseif type(root) == 'string' then +-- -- rawroot = false +-- lpegmatch(xmllinescapture,root) +-- elseif next(root) then -- tr == 'table' +-- xmlserialize(root,xmllineshandler) +-- end +-- end +-- +-- function xml.lines(root) -- used at all? +-- buffer = { "" } +-- lines(root) +-- return result +-- end local function to_text(e) if e.command == nil then @@ -815,52 +820,52 @@ function lxml.installsetup(what,document,setup,where) end if what == 1 then if trace_loading then - report_lxml("prepending setup %s for %s",setup,document) + report_lxml("prepending setup %a for %a",setup,document) end insert(sd,1,setup) elseif what == 2 then if trace_loading then - report_lxml("appending setup %s for %s",setup,document) + report_lxml("appending setup %a for %a",setup,document) end insert(sd,setup) elseif what == 3 then if trace_loading then - report_lxml("inserting setup %s for %s before %s",setup,document,where) + report_lxml("inserting setup %a for %a before %a",setup,document,where) end insertbeforevalue(sd,setup,where) elseif what == 4 then if trace_loading then - report_lxml("inserting setup %s for %s after %s",setup,document,where) + report_lxml("inserting setup %a for %a after %a",setup,document,where) end insertaftervalue(sd,setup,where) end end function lxml.flushsetups(id,...) - local done, list = { }, { ... } - for i=1,#list do - local document = list[i] + local done = { } + for i=1,select("#",...) do + local document = select(i,...) local sd = setups[document] if sd then for k=1,#sd do local v= sd[k] if not done[v] then if trace_loading then - report_lxml("applying setup %02i = %s to %s",k,v,document) + report_lxml("applying setup %02i : %a to %a",k,v,document) end contextsprint(ctxcatcodes,"\\xmlsetup{",id,"}{",v,"}") done[v] = true end end elseif trace_loading then - report_lxml("no setups for %s",document) + report_lxml("no setups for %a",document) end end end function lxml.resetsetups(document) if trace_loading then - report_lxml("resetting all setups for %s",document) + report_lxml("resetting all setups for %a",document) end setups[document] = { } end @@ -871,7 +876,7 @@ function lxml.removesetup(document,setup) for i=1,#s do if s[i] == setup then if trace_loading then - report_lxml("removing setup %s for %s",setup,document) + report_lxml("removing setup %a for %a",setup,document) end remove(t,i) break @@ -922,10 +927,10 @@ function lxml.setsetup(id,pattern,setup) end end elseif trace_setups then - report_lxml("zero lpath matches for %s",pattern) + report_lxml("%s lpath matches for pattern: %s","zero",pattern) end elseif trace_setups then - report_lxml("no lpath matches for %s",pattern) + report_lxml("%s lpath matches for pattern: %s","no",pattern) end else local a, b = match(setup,"^(.+:)([%*%-])$") @@ -974,10 +979,10 @@ function lxml.setsetup(id,pattern,setup) end end elseif trace_setups then - report_lxml("zero lpath matches for %s",pattern) + report_lxml("%s lpath matches for pattern: %s","zero",pattern) end elseif trace_setups then - report_lxml("no lpath matches for %s",pattern) + report_lxml("%s lpath matches for pattern: %s","no",pattern) end else local collected = xmlapplylpath(getid(id),pattern) @@ -1001,10 +1006,10 @@ function lxml.setsetup(id,pattern,setup) end end elseif trace_setups then - report_lxml("zero lpath matches for %s",pattern) + report_lxml("%s lpath matches for pattern: %s","zero",pattern) end elseif trace_setups then - report_lxml("no lpath matches for %s",pattern) + report_lxml("%s lpath matches for pattern: %s","no",pattern) end end end @@ -1397,18 +1402,6 @@ function lxml.raw(id,pattern) -- the content, untouched by commands end function lxml.context(id,pattern) -- the content, untouched by commands - if not pattern then - local collected = getid(id) - ctx_text(collected.dt[1]) - else - local collected = xmlapplylpath(getid(id),pattern) or getid(id) - if collected and #collected > 0 then - contextsprint(ctxcatcodes,collected[1].dt) - end - end -end - -function lxml.context(id,pattern) -- the content, untouched by commands if pattern then local collected = xmlapplylpath(getid(id),pattern) or getid(id) if collected and #collected > 0 then @@ -1416,8 +1409,11 @@ function lxml.context(id,pattern) -- the content, untouched by commands end else local collected = getid(id) - if collected and #collected > 0 then - ctx_text(collected.dt[1]) + if collected then + local dt = collected.dt + if #dt > 0 then + ctx_text(dt[1]) + end end end end @@ -1592,16 +1588,18 @@ statistics.register("lxml lpath profile", function() if p and next(p) then local s = table.sortedkeys(p) local tested, matched, finalized = 0, 0, 0 - texio.write_nl("log","\nbegin of lxml profile\n") - texio.write_nl("log","\n tested matched finalized pattern\n\n") + logs.pushtarget("logfile") + logs.writer("\nbegin of lxml profile\n") + logs.writer("\n tested matched finalized pattern\n\n") for i=1,#s do local pattern = s[i] local pp = p[pattern] local t, m, f = pp.tested, pp.matched, pp.finalized tested, matched, finalized = tested + t, matched + m, finalized + f - texio.write_nl("log",format("%9i %9i %9i %s",t,m,f,pattern)) + logs.writer(format("%9i %9i %9i %s",t,m,f,pattern)) end - texio.write_nl("log","\nend of lxml profile\n") + logs.writer("\nend of lxml profile\n") + logs.poptarget() return format("%s patterns, %s tested, %s matched, %s finalized (see log for details)",#s,tested,matched,finalized) else return nil |