diff options
author | Hans Hagen <pragma@wxs.nl> | 2012-05-29 18:43:00 +0200 |
---|---|---|
committer | Hans Hagen <pragma@wxs.nl> | 2012-05-29 18:43:00 +0200 |
commit | 48e2f23903816dbe8843329bfcd3e6658f22b139 (patch) | |
tree | 922c6efc858e94853d103694603f9468c167168f /tex/context/base/lxml-aux.lua | |
parent | c99d2bf28d96648c7d1852c9f5705d58655baeab (diff) | |
download | context-48e2f23903816dbe8843329bfcd3e6658f22b139.tar.gz |
beta 2012.05.29 18:43
Diffstat (limited to 'tex/context/base/lxml-aux.lua')
-rw-r--r-- | tex/context/base/lxml-aux.lua | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/tex/context/base/lxml-aux.lua b/tex/context/base/lxml-aux.lua index 09411bfc0..be12659ba 100644 --- a/tex/context/base/lxml-aux.lua +++ b/tex/context/base/lxml-aux.lua @@ -22,7 +22,7 @@ local xmlfilter = xml.filter local type, setmetatable, getmetatable = type, setmetatable, getmetatable local insert, remove, fastcopy, concat = table.insert, table.remove, table.fastcopy, table.concat -local gmatch, gsub, format = string.gmatch, string.gsub, string.format +local gmatch, gsub, format, find, strip = string.gmatch, string.gsub, string.format, string.find, string.strip local utfbyte = utf.byte local function report(what,pattern,c,e) @@ -721,3 +721,48 @@ function xml.setcdata(e,str) -- also setcomment dt = { str }, } } end + +-- maybe helpers like this will move to an autoloader + +function xml.separate(x,pattern) + local collected = xmlapplylpath(x,pattern) + if collected then + for c=1,#collected do + local e = collected[c] + local d = e.dt + if d == x then + report_xml("warning: xml.separate changes root") + x = d + end + local t, n = { "\n" }, 1 + local i, nd = 1, #d + while i <= nd do + while i <= nd do + local di = d[i] + if type(di) == "string" then + if di == "\n" or find(di,"^%s+$") then -- first test is speedup + i = i + 1 + else + d[i] = strip(di) + break + end + else + break + end + end + if i > nd then + break + end + t[n+1] = "\n" + t[n+2] = d[i] + t[n+3] = "\n" + n = n + 3 + i = i + 1 + end + t[n+1] = "\n" + setmetatable(t,getmetatable(d)) + e.dt = t + end + end + return x +end |