diff options
author | Marius <mariausol@gmail.com> | 2011-07-13 23:40:25 +0300 |
---|---|---|
committer | Marius <mariausol@gmail.com> | 2011-07-13 23:40:25 +0300 |
commit | 3dd416f677074c27a248e3433695a6fe8c13ef69 (patch) | |
tree | 20c4a573a64f2079e2e3d3fe93b004af3caf7b2f /tex/context/base/lxml-xml.lua | |
parent | 1ea50dab7f30289214b661f2cbcf53e97e6af0b6 (diff) | |
download | context-3dd416f677074c27a248e3433695a6fe8c13ef69.tar.gz |
beta 2011.07.13 20:14
Diffstat (limited to 'tex/context/base/lxml-xml.lua')
-rw-r--r-- | tex/context/base/lxml-xml.lua | 50 |
1 files changed, 41 insertions, 9 deletions
diff --git a/tex/context/base/lxml-xml.lua b/tex/context/base/lxml-xml.lua index 89fcba871..5012f69e5 100644 --- a/tex/context/base/lxml-xml.lua +++ b/tex/context/base/lxml-xml.lua @@ -6,15 +6,16 @@ if not modules then modules = { } end modules ['lxml-xml'] = { license = "see context related readme files" } -local concat = string.concat +local concat = table.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 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 xmlnewhandlers = xml.newhandlers local function first(collected) -- wrong ? return collected and collected[1] @@ -119,10 +120,39 @@ local function raw(collected) -- hybrid end end +-- + +local xmltexthandler = xmlnewhandlers { + name = "string", + initialize = function() + result = { } + return result + end, + finalize = function() + return concat(result) + end, + handle = function(...) + result[#result+1] = concat { ... } + end, + escape = false, +} + +local function xmltotext(root) + if not root then + return "" + elseif type(root) == 'string' then + return root + else + return xmlserialize(root,xmltexthandler) or "" + end +end + +-- + local function text(collected) -- hybrid if collected then local e = collected[1] or collected - return (e and xmltostring(e.dt)) or "" + return (e and xmltotext(e.dt)) or "" else return "" end @@ -270,10 +300,10 @@ function xml.text(id,pattern) if pattern then -- return text(xmlfilter(id,pattern)) local collected = xmlfilter(id,pattern) - return (collected and xmltostring(collected[1].dt)) or "" + return (collected and xmltotext(collected[1].dt)) or "" elseif id then -- return text(id) - return xmltostring(id.dt) or "" + return xmltotext(id.dt) or "" else return "" end @@ -281,6 +311,8 @@ end xml.content = text +-- + function xml.position(id,pattern,n) -- element return position(xmlfilter(id,pattern),n) end |