summaryrefslogtreecommitdiff
path: root/tex/context/base/lxml-ent.lua
diff options
context:
space:
mode:
authorMarius <mariausol@gmail.com>2010-07-04 15:32:09 +0300
committerMarius <mariausol@gmail.com>2010-07-04 15:32:09 +0300
commit85b7bc695629926641c7cb752fd478adfdf374f3 (patch)
tree80293f5aaa7b95a500a78392c39688d8ee7a32fc /tex/context/base/lxml-ent.lua
downloadcontext-85b7bc695629926641c7cb752fd478adfdf374f3.tar.gz
stable 2010-05-24 13:10
Diffstat (limited to 'tex/context/base/lxml-ent.lua')
-rw-r--r--tex/context/base/lxml-ent.lua69
1 files changed, 69 insertions, 0 deletions
diff --git a/tex/context/base/lxml-ent.lua b/tex/context/base/lxml-ent.lua
new file mode 100644
index 000000000..193611937
--- /dev/null
+++ b/tex/context/base/lxml-ent.lua
@@ -0,0 +1,69 @@
+if not modules then modules = { } end modules ['lxml-ent'] = {
+ version = 1.001,
+ comment = "this module is the basis for the lxml-* ones",
+ author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
+ copyright = "PRAGMA ADE / ConTeXt Development Team",
+ license = "see context related readme files"
+}
+
+local type, next, tonumber = type, next, tonumber
+local texsprint, ctxcatcodes = tex.sprint, tex.ctxcatcodes
+local utf = unicode.utf8
+local byte, format = string.byte, string.format
+local utfupper, utfchar = utf.upper, utf.char
+local lpegmatch = lpeg.match
+
+--[[ldx--
+<p>We provide (at least here) two entity handlers. The more extensive
+resolver consults a hash first, tries to convert to <l n='utf'/> next,
+and finaly calls a handler when defines. When this all fails, the
+original entity is returned.</p>
+
+<p>We do things different now but it's still somewhat experimental</p>
+--ldx]]--
+
+local trace_entities = false trackers.register("xml.entities", function(v) trace_entities = v end)
+
+xml.entities = xml.entities or { } -- xml.entity_handler == function
+
+storage.register("xml/entities",xml.entities,"xml.entities") -- this will move to lxml
+
+local entities = xml.entities -- this is a shared hash
+
+xml.unknown_any_entity_format = nil -- has to be per xml
+
+local parsedentity = xml.parsedentitylpeg
+
+function xml.register_entity(key,value)
+ entities[key] = value
+ if trace_entities then
+ logs.report("xml","registering entity '%s' as: %s",key,value)
+ end
+end
+
+function xml.resolved_entity(str)
+ local e = entities[str]
+ if e then
+ local te = type(e)
+ if te == "function" then
+ e(str)
+ elseif e then
+ texsprint(ctxcatcodes,e)
+ end
+ else
+ -- resolve hex and dec, todo: escape # & etc for ctxcatcodes
+ -- normally this is already solved while loading the file
+ local chr, err = lpegmatch(parsedentity,str)
+ if chr then
+ texsprint(ctxcatcodes,chr)
+ elseif err then
+ texsprint(ctxcatcodes,err)
+ else
+ texsprint(ctxcatcodes,"\\xmle{",str,"}{",utfupper(str),"}") -- we need to use our own upper
+ end
+ end
+end
+
+entities.amp = function() tex.write("&") end
+entities.lt = function() tex.write("<") end
+entities.gt = function() tex.write(">") end