summaryrefslogtreecommitdiff
path: root/tex/context/base/lxml-aux.lua
diff options
context:
space:
mode:
authorMarius <mariausol@gmail.com>2011-11-22 18:00:14 +0200
committerMarius <mariausol@gmail.com>2011-11-22 18:00:14 +0200
commit53d861c241aac04f8afacbecc493b18ac04661cc (patch)
treeccdd266d73cb7a220b7975fd0f04522c3ce1e5a3 /tex/context/base/lxml-aux.lua
parent411958915524daa27f2ef8cd6299b31191d88572 (diff)
downloadcontext-53d861c241aac04f8afacbecc493b18ac04661cc.tar.gz
beta 2011.11.22 16:49
Diffstat (limited to 'tex/context/base/lxml-aux.lua')
-rw-r--r--tex/context/base/lxml-aux.lua75
1 files changed, 73 insertions, 2 deletions
diff --git a/tex/context/base/lxml-aux.lua b/tex/context/base/lxml-aux.lua
index 4798fe06a..560ef557d 100644
--- a/tex/context/base/lxml-aux.lua
+++ b/tex/context/base/lxml-aux.lua
@@ -20,8 +20,9 @@ local xmlinheritedconvert = xml.inheritedconvert
local xmlapplylpath = xml.applylpath
local type, setmetatable, getmetatable = type, setmetatable, getmetatable
-local insert, remove, fastcopy = table.insert, table.remove, table.fastcopy
-local gmatch, gsub = string.gmatch, string.gsub
+local insert, remove, fastcopy, concat = table.insert, table.remove, table.fastcopy, table.concat
+local gmatch, gsub, format = string.gmatch, string.gsub, string.format
+local utfbyte = utf.byte
local function report(what,pattern,c,e)
report_xml("%s element '%s' (root: '%s', position: %s, index: %s, pattern: %s)",what,xmlname(e),xmlname(e.__p__),c,e.ni,pattern)
@@ -516,6 +517,75 @@ function xml.remapname(root, pattern, newtg, newns, newrn)
end
--[[ldx--
+<p>Helper (for q2p).</p>
+--ldx]]--
+
+function xml.cdatatotext(e)
+ local dt = e.dt
+ if #dt == 1 then
+ local first = dt[1]
+ if first.tg == "@cd@" then
+ e.dt = first.dt
+ end
+ else
+ -- maybe option
+ end
+end
+
+xml.builtinentities = table.tohash { "amp", "quot", "apos", "lt", "gt" } -- used often so share
+
+local entities = characters and characters.entities or nil
+local builtinentities = xml.builtinentities
+
+function xml.addentitiesdoctype(root,option) -- we could also have a 'resolve' i.e. inline hex
+ if not entities then
+ require("char-ent")
+ entities = characters.entities
+ end
+ if entities and root and root.tg == "@rt@" and root.statistics then
+ local list = { }
+ local hexify = option == "hexadecimal"
+ for k, v in table.sortedhash(root.statistics.entities.names) do
+ if not builtinentities[k] then
+ local e = entities[k]
+ if not e then
+ e = format("[%s]",k)
+ elseif hexify then
+ e = format("&#%05X;",utfbyte(k))
+ end
+ list[#list+1] = format(" <!ENTITY %s %q >",k,e)
+ end
+ end
+ local dt = root.dt
+ local n = dt[1].tg == "@pi@" and 2 or 1
+ if #list > 0 then
+ insert(dt, n, { "\n" })
+ insert(dt, n, {
+ tg = "@dt@", -- beware, doctype is unparsed
+ dt = { format("Something [\n%s\n] ",concat(list)) },
+ ns = "",
+ special = true,
+ })
+ insert(dt, n, { "\n\n" })
+ else
+ -- insert(dt, n, { table.serialize(root.statistics) })
+ end
+ end
+end
+
+-- local str = [==[
+-- <?xml version='1.0' standalone='yes' ?>
+-- <root>
+-- <a>test &nbsp; test &#123; test</a>
+-- <b><![CDATA[oeps]]></b>
+-- </root>
+-- ]==]
+--
+-- local x = xml.convert(str)
+-- xml.addentitiesdoctype(x,"hexadecimal")
+-- print(x)
+
+--[[ldx--
<p>Here are a few synonyms.</p>
--ldx]]--
@@ -547,3 +617,4 @@ xml.inject_element = xml.inject obsolete.inject_eleme
xml.remap_tag = xml.remaptag obsolete.remap_tag = xml.remaptag
xml.remap_name = xml.remapname obsolete.remap_name = xml.remapname
xml.remap_namespace = xml.remapnamespace obsolete.remap_namespace = xml.remapnamespace
+