lxml-ent.lua /size: 1869 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['lxml-ent'] = {
2    version   = 1.001,
3    comment   = "this module is the basis for the lxml-* ones",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local next = next
10local byte, format = string.byte, string.format
11local setmetatableindex = table.setmetatableindex
12
13--[[ldx--
14<p>We provide (at least here) two entity handlers. The more extensive
15resolver consults a hash first, tries to convert to <l n='utf'/> next,
16and finaly calls a handler when defines. When this all fails, the
17original entity is returned.</p>
18
19<p>We do things different now but it's still somewhat experimental</p>
20--ldx]]--
21
22local trace_entities = false  trackers.register("xml.entities", function(v) trace_entities = v end)
23
24local report_xml = logs.reporter("xml")
25
26local xml = xml
27
28xml.entities = xml.entities or { }
29
30storage.register("xml/entities", xml.entities, "xml.entities" )
31
32local entities = xml.entities  -- maybe some day properties
33
34function xml.registerentity(key,value)
35    entities[key] = value
36    if trace_entities then
37        report_xml("registering entity %a as %a",key,value)
38    end
39end
40
41if characters and characters.entities then
42
43    -- the big entity table also has amp, quot, apos, lt, gt in them
44
45    local loaded = false
46
47    function characters.registerentities(forcecopy)
48        if loaded then
49            return
50        end
51        if forcecopy then
52            setmetatableindex(entities,nil)
53            for name, value in next, characters.entities do
54                if not entities[name] then
55                    entities[name] = value
56                end
57            end
58        else
59            setmetatableindex(entities,characters.entities)
60        end
61        loaded = true
62    end
63
64end
65