summaryrefslogtreecommitdiff
path: root/tex/context/base/trac-exp.lua
blob: b9b4143d3ea8f418e33649d6d08ce464404668bd (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
if not modules then modules = { } end modules ['trac-exp'] = {
    version   = 1.001,
    comment   = "companion to trac-log.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local formatters   = string.formatters
local reporters    = logs.reporters
local xmlserialize = xml.serialize
local xmlcollected = xml.collected
local xmltext      = xml.text
local xmlfirst     = xml.first
local xmlfilter    = xml.filter

-- there is no need for a newhandlers { name = "help", parent = "string" }

local function flagdata(flag)
    local name  = flag.at.name or ""
    local value = flag.at.value or ""
 -- local short = xmlfirst(s,"/short")
 -- local short = xmlserialize(short,xs)
    local short = xmltext(xmlfirst(flag,"/short")) or ""
    return name, value, short
end

local function exampledata(example)
    local command = xmltext(xmlfirst(example,"/command")) or ""
    local comment = xmltext(xmlfirst(example,"/comment")) or ""
    return command, comment
end

local function categorytitle(category)
    return xmltext(xmlfirst(category,"/title")) or ""
end

local exporters = logs.exporters

function exporters.man(specification,...)
    local root = xml.convert(specification.helpinfo or "")
    if not root then
        return
    end
    local xs = xml.gethandlers("string")
    xml.sethandlersfunction(xs,"short",function(e,handler) xmlserialize(e.dt,handler) end)
    xml.sethandlersfunction(xs,"ref",  function(e,handler) handler.handle("--"..e.at.name) end)
    local wantedcategories = select("#",...) == 0 and true or table.tohash { ... }
    local nofcategories = xml.count(root,"/application/flags/category")
    local name    = xmlfilter(root,"/application/metadata/entry[@name='name']/text()")
    local detail  = xmlfilter(root,"/application/metadata/entry[@name='detail']/text()") or name
    local version = xmlfilter(root,"/application/metadata/entry[@name='version']/text()") or "0.00"
    local banner  = specification.banner or detail or name
    --
    local result = { }
    --
    -- .TH "context" "1" "some date" "version" "ConTeXt" -- we use a fake date as I don't want to polute the git repos
    --
    local runner = string.match(name,"^mtx%-(.*)")
    if runner then
        runner = formatters["mtxrun --script %s"](runner)
    else
        runner = name
    end
    --
    result[#result+1] = formatters[ [[.TH "%s" "1" "%s" "version %s" "%s" ]] ](name,os.date("01-01-%Y"),version,detail)
    result[#result+1] = ".PP"
    result[#result+1] = formatters[ [[.SH "NAME" %s]] ] ()
    result[#result+1] = ".PP"
    result[#result+1] = formatters[ [[.SH "SYNOPSIS" ]] ](name)
    result[#result+1] = ".PP"
    result[#result+1] = formatters[ [[.SH \fB%s\fP [ \fIOPTIONS\fP ... ] [ \fIFILENAMES\fP ] ]] ](runner)
    result[#result+1] = ".PP"
    result[#result+1] = formatters[ [[.SH "DESCRIPTION"\n%s\n ]] ](detail)
    --
    for category in xmlcollected(root,"/application/flags/category") do
        if nofcategories > 1 then
            result[#result+1] = formatters['.SH "OPTIONS: %s"'](string.upper(category.at.name or "all"))
        else
            result[#result+1] = '.SH "OPTIONS"'
        end
        for subcategory in xmlcollected(category,"/subcategory") do
            for flag in xmlcollected(subcategory,"/flag") do
                local name, value, short = flagdata(flag)
                if value == "" then
                    result[#result+1] = formatters[".TP\n.B --%s\n%s"](name,short)
                else
                    result[#result+1] = formatters[".TP\n.B --%s=%s\n%s"](name,value,short)
                end
            end
        end
    end
    result[#result+1] = formatters['.SH "AUTHOR"\n%s'](specification.moreinfo)
    return table.concat(result,"\n")
end

local craptemplate = [[
<?xml version="1.0"?>
<application>
<metadata>
<entry name="banner">%s</entry>
</metadata>
<verbose>
%s
</verbose>
]]

function exporters.xml(specification,...)
    local helpinfo = specification.helpinfo
    if type(helpinfo) == "string" then
        if string.find(helpinfo,"^<%?xml") then
            return helpinfo
        end
    elseif type(helpinfo) == "table" then
        helpinfo = table.concat(helpinfo,"\n\n")
    else
        helpinfo = "no help"
    end
    return formatters[craptemplate](specification.banner or "?",helpinfo)
end

-- the following template is optimized a bit for space

-- local bodytemplate = [[
-- <h1>Command line options</h1>
-- <table>
--     <tr>
--         <th style="width: 10em">flag</th>
--         <th style="width: 8em">value</th>
--         <th>description</th>
--     </tr>
--     <?lua
--         for category in xml.collected(variables.root,"/application/flags/category") do
--             if variables.nofcategories > 1 then
--                 ?><tr>
--                     <th colspan="3"><?lua inject(category.at.name) ?></th>
--                 </tr><?lua
--             end
--             for subcategory in xml.collected(category,"/subcategory") do
--                 ?><tr><th/><td/><td/></tr><?lua
--                 for flag in xml.collected(subcategory,"/flag") do
--                     local name, value, short = variables.flagdata(flag)
--                     ?><tr>
--                         <th>--<?lua inject(name) ?></th>
--                         <td><?lua inject(value) ?></td>
--                         <td><?lua inject(short) ?></td>
--                     </tr><?lua
--                 end
--             end
--         end
--     ?>
-- </table>
-- <br/>
-- <?lua
--     for category in xml.collected(variables.root,"/application/examples/category") do
--         local title = variables.categorytitle(category)
--         if title ~= "" then
--             ?><h1><?lua inject(title) ?></h1><?lua
--         end
--         for subcategory in xml.collected(category,"/subcategory") do
--             for example in xml.collected(subcategory,"/example") do
--                 local command, comment = variables.exampledata(example)
--                 ?><tt><?lua inject(command) ?></tt><br/><?lua
--             end
--             ?><br/><?lua
--         end
--     end
--     for comment in xml.collected(root,"/application/comments/comment") do
--         ?><br/><?lua inject(xml.text(comment)) ?><br/><?lua
--     end
-- ?>
-- ]]

local bodytemplate = [[
<h1>Command line options</h1>
<table>
    <tr><th style="width: 10em">flag</th><th style="width: 8em">value</th><th>description</th></tr>
    <?lua for category in xml.collected(variables.root,"/application/flags/category") do if variables.nofcategories > 1 then ?>
    <tr><th colspan="3"><?lua inject(category.at.name) ?></th></tr>
    <?lua end for subcategory in xml.collected(category,"/subcategory") do ?>
    <tr><th/><td/><td/></tr>
    <?lua for flag in xml.collected(subcategory,"/flag") do local name, value, short = variables.flagdata(flag) ?>
    <tr><th>--<?lua inject(name) ?></th><td><?lua inject(value) ?></td><td><?lua inject(short) ?></td></tr>
    <?lua end end end ?>
</table>
<br/>
<?lua for category in xml.collected(variables.root,"/application/examples/category") do local title = variables.categorytitle(category) if title ~= "" then ?>
<h1><?lua inject(title) ?></h1>
<?lua end for subcategory in xml.collected(category,"/subcategory") do for example in xml.collected(subcategory,"/example") do local command, comment = variables.exampledata(example) ?>
<tt><?lua inject(command) ?></tt>
<br/><?lua end ?><br/><?lua end end for comment in xml.collected(root,"/application/comments/comment") do ?>
<br/><?lua inject(xml.text(comment)) ?><br/><?lua end ?>
]]

function exporters.html(specification,...)
    local root = xml.convert(specification.helpinfo or "")
    if not root then
        return
    end
    local xs = xml.gethandlers("string")
    xml.sethandlersfunction(xs,"short",function(e,handler) xmlserialize(e.dt,handler) end)
    xml.sethandlersfunction(xs,"ref",  function(e,handler) handler.handle("--"..e.at.name) end)
    local wantedcategories = select("#",...) == 0 and true or table.tohash { ... }
    local nofcategories = xml.count(root,"/application/flags/category")
    local name    = xmlfilter(root,"/application/metadata/entry[@name='name']/text()")
    local detail  = xmlfilter(root,"/application/metadata/entry[@name='detail']/text()") or name
    local version = xmlfilter(root,"/application/metadata/entry[@name='version']/text()") or "0.00"
    local banner  = specification.banner or detail or name
    --
    dofile(resolvers.findfile("trac-lmx.lua","tex"))
    --
    local htmltemplate = io.loaddata(resolvers.findfile("context-base.lmx","tex")) or "no template"
    --
    local body = lmx.convertstring(bodytemplate, {
        nofcategories    = nofcategories,
        wantedcategories = wantedcategories,
        root             = root,
    --  moreinfo         = specification.moreinfo,
        flagdata         = flagdata,
        exampledata      = exampledata,
        categorytitle    = categorytitle,
    })
    local html = lmx.convertstring(htmltemplate, {
        maintext   = body,
        title      = banner,
        bottomtext = "wiki: http://contextgarden.net | mail: ntg-context@ntg.nl | website: http://www.pragma-ade.nl",
    })
    --
    return html
end