lxml-ctx.lua /size: 5007 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['lxml-ctx'] = {
2    version   = 1.001,
3    comment   = "companion to lxml-ini.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9-- will be cleaned up
10
11local format, find, gsub = string.format, string.find, string.gsub
12
13local xml         = xml
14xml.ctx           = { }
15xml.ctx.enhancers = { }
16
17local context     = context
18local commands    = commands
19
20-- hashen
21
22function xml.ctx.enhancers.compound(root,lpath,before,tokens,after) -- todo lpeg
23    local before  = before or "[%a%d][%a%d][%a%d]"
24    local tokens  = tokens or "[%/%-]"
25    local after   = after  or "[%a%d][%a%d][%a%d]"
26    local pattern = "(" .. before .. ")(" .. tokens .. ")(" .. after .. ")"
27    local action  = function(a,b,c)
28        return a .. "<compound token=" .. format("%q",b) .. "/>" .. c -- formatters["%s<compound token=%q/>%s"](a,b,c)
29    end
30    xml.enhance(root,lpath,pattern,action) -- still present?
31end
32
33local loaded = { }
34
35local nodesettostring = xml.nodesettostring
36
37-- maybe use detokenize instead of \type
38
39local function cleaned(str)
40    str = gsub(str,"|","\\textbar ")
41    return str
42end
43
44function xml.ctx.tshow(specification)
45    local pattern = specification.pattern
46    local xmlroot = specification.xmlroot
47    local attribute = specification.attribute
48    if context then
49        local xmlpattern = pattern
50        if not find(xmlpattern,"^[%a]+://") then
51            xmlpattern = "xml://" .. pattern
52        end
53        local parsed = xml.lpath(xmlpattern)
54        local titlecommand = specification.title or "type"
55        if parsed.state then
56            context[titlecommand]("pattern: " .. cleaned(pattern) .. " (".. parsed.state .. ")")
57        else
58            context[titlecommand]("pattern: " .. cleaned(pattern))
59        end
60        context.starttabulate({ "|Tr|Tl|Tp|" } )
61        if specification.warning then
62            local comment = parsed.comment
63            if comment then
64                for k=1,#comment do
65                    context.NC()
66                    context("!")
67                    context.NC()
68                    context.rlap(comment[k])
69                    context.NR()
70                end
71                context.TB()
72            end
73        end
74        for p=1,#parsed do
75            local pp = parsed[p]
76            local kind = pp.kind
77            context.NC()
78            context(p)
79            context.NC()
80            context(kind)
81            context.NC()
82            if kind == "axis" then
83                context(cleaned(pp.axis))
84            elseif kind == "nodes" then
85                context(cleaned(nodesettostring(pp.nodes,pp.nodetest)))
86            elseif kind == "expression" then
87--              -- context("%s => %s",pp.expression,pp.converted)
88                context(cleaned(pp.expression))
89            elseif kind == "finalizer" then
90                context("%s(%s)",pp.name,pp.arguments)
91            elseif kind == "error" and pp.eqrror then
92                context(pp.error)
93            end
94            context.NC()
95            context.NR()
96        end
97        context.stoptabulate()
98        if xmlroot and xmlroot ~= "" then
99            if not loaded[xmlroot] then
100                loaded[xmlroot] = xml.convert(buffers.getcontent(xmlroot))
101            end
102            local collected = xml.filter(loaded[xmlroot],xmlpattern)
103            if collected then
104                local tc = type(collected)
105                if not tc then
106                    -- skip
107                else
108                    context.blank()
109                    context.type("result : ")
110                    if tc == "string" then
111                        context.type(collected)
112                    elseif tc == "table" then
113                        if collected.tg then
114                            collected  = { collected }
115                        end
116                        for c=1,#collected do
117                            local cc = collected[c]
118                            if attribute and attribute ~= "" then
119                                local ccat = cc.at
120                                local a = ccat and ccat[attribute]
121                                if a and a ~= "" then
122                                    context.type(a)
123                                    context.type(">")
124                                end
125                            end
126                            local ccns = cc.ns
127                            if ccns == "" then
128                                context.type(cc.tg)
129                            else
130                                context.type(ccns .. ":" .. cc.tg)
131                            end
132                            context.space()
133                        end
134                    else
135                        context.type(tostring(tc))
136                    end
137                    context.blank()
138                end
139            end
140        end
141    end
142end
143