lxml-dir.lua /size: 3900 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['lxml-dir'] = {
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 gsub = string.gsub
10local formatters = string.formatters
11
12-- <?xml version="1.0" standalone="yes"?>
13-- <!-- demo.cdx -->
14-- <directives>
15-- <!--
16--     <directive attribute='id' value="100" setup="cdx:100"/>
17--     <directive attribute='id' value="101" setup="cdx:101"/>
18-- -->
19-- <!--
20--     <directive attribute='cdx' value="colors"   element="cals:table" setup="cdx:cals:table:colors"/>
21--     <directive attribute='cdx' value="vertical" element="cals:table" setup="cdx:cals:table:vertical"/>
22--     <directive attribute='cdx' value="noframe"  element="cals:table" setup="cdx:cals:table:noframe"/>
23-- -->
24-- <directive attribute='cdx' value="*" element="cals:table" setup="cdx:cals:table:*"/>
25-- </directives>
26
27local lxml        = lxml
28local context     = context
29
30local getid       = lxml.getid
31
32local directives  = lxml.directives or { }
33lxml.directives   = directives
34
35local report_lxml = logs.reporter("xml","tex")
36
37local data = {
38    setup  = { },
39    before = { },
40    after  = { }
41}
42
43local function load_setup(filename)
44    local fullname = resolvers.findtexfile(filename) or ""
45    if fullname ~= "" then
46        filename = fullname
47    end
48    local collection = xml.applylpath({ getid(xml.load(filename)) },"directive") -- is { } needed ?
49    if collection then
50        local valid = 0
51        for i=1,#collection do
52            local at = collection[i].at
53            local attribute, value, element = at.attribute or "", at.value or "", at.element or '*'
54            local setup, before, after = at.setup or "", at.before or "", at.after or ""
55            if attribute ~= "" and value ~= "" then
56                local key = formatters["%s::%s::%s"](element,attribute,value)
57                local t = data[key] or { }
58                if setup  ~= "" then t.setup  = setup  end
59                if before ~= "" then t.before = before end
60                if after  ~= "" then t.after  = after  end
61                data[key] = t
62                valid = valid + 1
63            end
64        end
65        report_lxml("%s directives found in %a, valid %s",#collection,filename,valid)
66    else
67        report_lxml("no directives found in %a",filename)
68    end
69end
70
71local function handle_setup(category,root,attribute,element)
72    root = getid(root)
73    if attribute then
74        local value = root.at[attribute]
75        if value then
76            if not element then
77                local ns, tg = root.rn or root.ns, root.tg
78                if ns == "" then
79                    element = tg
80                else
81                    element = ns .. ':' .. tg
82                end
83            end
84            local setup = data[formatters["%s::%s::%s"](element,attribute,value)]
85            if setup then
86                setup = setup[category]
87            end
88            if setup then
89                context.directsetup(setup)
90            else
91                setup = data[formatters["%s::%s::*"](element,attribute)]
92                if setup then
93                    setup = setup[category]
94                end
95                if setup then
96                    setup = gsub(setup,'%*',value)
97                    context.directsetup(setup)
98                end
99            end
100        end
101    end
102end
103
104directives.load   = load_setup
105directives.handle = handle_setup
106
107function directives.setup(root,attribute,element)
108    handle_setup('setup',root,attribute,element)
109end
110
111function directives.before(root,attribute,element)
112    handle_setup('before',root,attribute,element)
113end
114
115function directives.after(root,attribute,element)
116    handle_setup('after',root,attribute,element)
117end
118