summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/lxml-ent.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/context/base/lxml-ent.lua')
-rw-r--r--Master/texmf-dist/tex/context/base/lxml-ent.lua124
1 files changed, 39 insertions, 85 deletions
diff --git a/Master/texmf-dist/tex/context/base/lxml-ent.lua b/Master/texmf-dist/tex/context/base/lxml-ent.lua
index c91d12706bb..19361193745 100644
--- a/Master/texmf-dist/tex/context/base/lxml-ent.lua
+++ b/Master/texmf-dist/tex/context/base/lxml-ent.lua
@@ -6,110 +6,64 @@ if not modules then modules = { } end modules ['lxml-ent'] = {
license = "see context related readme files"
}
-local type, next, tonumber, tostring, setmetatable, loadstring = type, next, tonumber, tostring, setmetatable, loadstring
-local format, gsub, find = string.format, string.gsub, string.find
-local utfchar = unicode.utf8.char
+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
-function xml.entity_handler(e)
- return format("[%s]",e)
-end
+storage.register("xml/entities",xml.entities,"xml.entities") -- this will move to lxml
-local function toutf(s)
- return utfchar(tonumber(s,16))
-end
+local entities = xml.entities -- this is a shared hash
-local function utfize(root)
- local d = root.dt
- for k=1,#d do
- local dk = d[k]
- if type(dk) == "string" then
- -- test prevents copying if no match
- if find(dk,"&#x.-;") then
- d[k] = gsub(dk,"&#x(.-);",toutf)
- end
- else
- utfize(dk)
- end
- end
-end
+xml.unknown_any_entity_format = nil -- has to be per xml
-xml.utfize = utfize
+local parsedentity = xml.parsedentitylpeg
-local function resolve(e) -- hex encoded always first, just to avoid mkii fallbacks
- if find(e,"^#x") then
- return utfchar(tonumber(e:sub(3),16))
- elseif find(e,"^#") then
- return utfchar(tonumber(e:sub(2)))
- else
- local ee = xml.entities[e] -- we cannot shortcut this one (is reloaded)
- if ee then
- return ee
- else
- local h = xml.entity_handler
- return (h and h(e)) or "&" .. e .. ";"
- end
+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
-local function resolve_entities(root)
- if not root.special or root.tg == "@rt@" then
- local d = root.dt
- for k=1,#d do
- local dk = d[k]
- if type(dk) == "string" then
- if find(dk,"&.-;") then
- d[k] = gsub(dk,"&(.-);",resolve)
- end
- else
- resolve_entities(dk)
- 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
- end
-end
-
-xml.resolve_entities = resolve_entities
-
-function xml.utfize_text(str)
- if find(str,"&#") then
- return (gsub(str,"&#x(.-);",toutf))
else
- return str
- end
-end
-
-function xml.resolve_text_entities(str) -- maybe an lpeg. maybe resolve inline
- if find(str,"&") then
- return (gsub(str,"&(.-);",resolve))
- else
- return str
- end
-end
-
-function xml.show_text_entities(str)
- if find(str,"&") then
- return (gsub(str,"&(.-);","[%1]"))
- else
- return str
- end
-end
-
--- experimental, this will be done differently
-
-function xml.merge_entities(root)
- local documententities = root.entities
- local allentities = xml.entities
- if documententities then
- for k, v in next, documententities do
- allentities[k] = v
+ -- 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