summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/mkiv/lxml-dir.lua
blob: 48c0ac41e733df9849ff567e45aac357b53ac50d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
if not modules then modules = { } end modules ['lxml-dir'] = {
    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 gsub = string.gsub
local formatters = string.formatters

-- <?xml version="1.0" standalone="yes"?>
-- <!-- demo.cdx -->
-- <directives>
-- <!--
--     <directive attribute='id' value="100" setup="cdx:100"/>
--     <directive attribute='id' value="101" setup="cdx:101"/>
-- -->
-- <!--
--     <directive attribute='cdx' value="colors"   element="cals:table" setup="cdx:cals:table:colors"/>
--     <directive attribute='cdx' value="vertical" element="cals:table" setup="cdx:cals:table:vertical"/>
--     <directive attribute='cdx' value="noframe"  element="cals:table" setup="cdx:cals:table:noframe"/>
-- -->
-- <directive attribute='cdx' value="*" element="cals:table" setup="cdx:cals:table:*"/>
-- </directives>

local lxml        = lxml
local context     = context

local getid       = lxml.getid

local directives  = lxml.directives or { }
lxml.directives   = directives

local report_lxml = logs.reporter("xml","tex")

local data = {
    setup  = { },
    before = { },
    after  = { }
}

local function load_setup(filename)
    local fullname = resolvers.findtexfile(filename) or ""
    if fullname ~= "" then
        filename = fullname
    end
    local collection = xml.applylpath({ getid(xml.load(filename)) },"directive") -- is { } needed ?
    if collection then
        local valid = 0
        for i=1,#collection do
            local at = collection[i].at
            local attribute, value, element = at.attribute or "", at.value or "", at.element or '*'
            local setup, before, after = at.setup or "", at.before or "", at.after or ""
            if attribute ~= "" and value ~= "" then
                local key = formatters["%s::%s::%s"](element,attribute,value)
                local t = data[key] or { }
                if setup  ~= "" then t.setup  = setup  end
                if before ~= "" then t.before = before end
                if after  ~= "" then t.after  = after  end
                data[key] = t
                valid = valid + 1
            end
        end
        report_lxml("%s directives found in %a, valid %s",#collection,filename,valid)
    else
        report_lxml("no directives found in %a",filename)
    end
end

local function handle_setup(category,root,attribute,element)
    root = getid(root)
    if attribute then
        local value = root.at[attribute]
        if value then
            if not element then
                local ns, tg = root.rn or root.ns, root.tg
                if ns == "" then
                    element = tg
                else
                    element = ns .. ':' .. tg
                end
            end
            local setup = data[formatters["%s::%s::%s"](element,attribute,value)]
            if setup then
                setup = setup[category]
            end
            if setup then
                context.directsetup(setup)
            else
                setup = data[formatters["%s::%s::*"](element,attribute)]
                if setup then
                    setup = setup[category]
                end
                if setup then
                    setup = gsub(setup,'%*',value)
                    context.directsetup(setup)
                end
            end
        end
    end
end

directives.load   = load_setup
directives.handle = handle_setup

function directives.setup(root,attribute,element)
    handle_setup('setup',root,attribute,element)
end

function directives.before(root,attribute,element)
    handle_setup('before',root,attribute,element)
end

function directives.after(root,attribute,element)
    handle_setup('after',root,attribute,element)
end