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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
|
-- Copyright 2013 by Till Tantau
--
-- This file may be distributed an/or modified
--
-- 1. under the LaTeX Project Public License and/or
-- 2. under the GNU Public License
--
-- See the file doc/generic/pgf/licenses/LICENSE for more information
-- @release $Header$
---
-- This class offers functions for converting the documentation of Lua
-- and other code into \TeX.
--
-- @field renderers This array must consist of tables having two
-- fields: |test| and |renderer|. The first must be set to a function
-- that is called with an information table as parameter and must
-- return |true| if the funciton stored in the |renderer| should be
-- called. (Typically, the |test| will test whether the ``head line''
-- following a documentation block has a special form.)
local DocumentParser = {
renderers = {}
}
-- Namespace:
require 'pgf.manual'.DocumentParser = DocumentParser
-- Imports
local keys = require 'pgf.gd.interface.InterfaceCore'.keys
-- Forwards:
local collect_infos, render_infos
---
-- Includes the documentation stored in some file in the manual.
--
-- @param filename The file from which the documentation is to be read
-- @param type The type of the file.
function DocumentParser.include(filename, typ)
local fullname = assert(kpse.find_file(filename:gsub("%.", "/"), typ or "lua")
or kpse.find_file(filename:gsub("%.", "\\"), typ or "lua"),
"file " .. filename .. " not found")
local file, error = io.open(fullname)
-- First, let us read the file into a table to make handling easier:
local lines = {}
for line in file:lines() do
lines [#lines + 1] = line
end
-- A table storing the current output. The array part contains TeX
-- lines, the table part contains information about special table.s
local output = {}
-- Now, start the main parser loop.
local i = 1
while i <= #lines do
if lines[i]:match("^%-%-%-") then
local infos = collect_infos (lines, i)
infos.filename = filename
render_infos(infos, output)
i = infos.last_line
end
i = i + 1
end
-- Render output:
for _,l in ipairs(output) do
if type(l) == "string" then
tex.print(l)
else
l()
end
end
end
---
-- Add a test and a renderer to the array of renderers.
--
-- @param test A test function (see |DocumentParser.renderers|).
-- @param renderer A rendering function.
function DocumentParser.addRenderer (test, renderer)
DocumentParser.renderers [#DocumentParser.renderers + 1] =
{ test = test, renderer = renderer }
end
-- Forwards:
local print_on_output, print_on_output_escape, print_docline_on_output, open_mode, close_mode, print_lines_on_output
local function strip_quotes(s)
if s then return string.gsub(s, '^"(.*)"$', "%1") end
end
local function split(s)
local t = {}
for line in string.gmatch(s, ".-\n") do
t[#t+1] = line
end
for i=#t,1,-1 do
if t[i]:match("^%s*$") then
t[i] = nil
else
return t
end
end
return t
end
local function process_string(s)
if s then
local t = split(s.."\n")
-- Compute min spaces
local min = math.huge
for _,l in ipairs(t) do
min = math.min(min, string.find(l, "%S") or math.huge)
end
if min < math.huge then
-- Now, trim 'em all!
for i=1,#t do
t[i] = string.sub(t[i],min,-2)
end
end
return t
end
end
local function process_examples(t)
if not t then
return nil
end
if type(t) == "string" then
t = {t}
end
local n = {}
for i=1,#t do
n[i] = process_string(strip_quotes(t[i]))
end
return n
end
-- The standard renderers:
-- The function renderer
DocumentParser.addRenderer (
function (infos)
-- The test
return
infos.keywords["function"] or
infos.head_line:match("^%s*function%s+") or
infos.head_line:match("^%s*local%s+function%s+")
end,
function (infos, output)
-- The renderer
if infos.keywords["function"] then
local k = infos.keywords["function"][1]
infos.head_line = k:match("^%s*@(.*)")
end
local rest = infos.head_line:match("function%s+(.*)")
local tab = rest:match("([^(]*[%.%:]).*")
local fun = rest:match("[^(]*[%.%:](.-)%s*%(") or rest:match("(.-)%s*%(")
local pars = rest:match(".-%((.*)%)")
-- Render the head
print_on_output_escape(output, [[\begin{luacommand}]])
output[#output+1] = "{" .. (tab or "") .. fun .. "}"
print_on_output_escape(output,
"{", tab or "", "}",
"{", fun, "}",
"{", pars, "}")
if tab then
local table_name = tab:sub(1,-2)
local t = output[table_name] or {}
t[#t+1] = {
link = "pgf/lua/" .. tab .. fun,
text = "function " .. tab .. "\\declare{" .. fun .. "} (" .. pars .. ")"
}
output[table_name] = t
end
local mode = "text"
for _,l in ipairs(infos.doc_lines) do
if mode ~= "done" then
mode = print_docline_on_output(output, l, mode)
end
end
close_mode(output, mode)
print_on_output(output, [[\end{luacommand}]])
end
)
-- The table renderer
DocumentParser.addRenderer (
function (infos)
-- The test
return
infos.keywords["table"] or
infos.head_line:match("=%s*{")
end,
function (infos, output)
-- The renderer
if infos.keywords["table"] then
local k = infos.keywords["table"][1]
infos.head_line = k:match("^%s*@table(.*)") .. "="
end
local name =
infos.head_line:match("^%s*local%s+(.-)%s*=") or
infos.head_line:match("^%s*(.*)%s*=")
-- Render the head
print_on_output_escape(output,
[[\begin{luatable}]],
"{", name:match("(.*[%.%:]).*") or "", "}",
"{", name:match(".*[%.%:](*-)") or name,"}",
"{", infos.filename, "}")
local mode = "text"
for _,l in ipairs(infos.doc_lines) do
mode = print_docline_on_output(output, l, mode)
end
close_mode(output, mode)
output[#output+1] =
function ()
if output[name] then
tex.print("\\par\\emph{Alphabetical method summary:}\\par{\\small")
table.sort(output[name], function (a,b) return a.text < b.text end)
for _,l in ipairs(output[name]) do
tex.print("\\texttt{\\hyperlink{" .. l.link .. "}{" .. l.text:gsub("_", "\\_") .. "}}\\par")
end
tex.print("}")
end
end
print_on_output(output, [[\end{luatable}]])
end
)
-- The library renderer
DocumentParser.addRenderer (
function (infos)
-- The test
return infos.keywords["library"]
end,
function (infos, output)
-- The renderer
local name = infos.filename:gsub("%.library$",""):gsub("^pgf%.gd%.","")
-- Render the head
print_on_output_escape(output, "\\begin{lualibrary}{", name, "}")
local mode = "text"
for _,l in ipairs(infos.doc_lines) do
mode = print_docline_on_output(output, l, mode)
end
close_mode(output, mode)
print_on_output(output, "\\end{lualibrary}")
end
)
-- The section renderer
DocumentParser.addRenderer (
function (infos)
-- The test
return infos.keywords["section"]
end,
function (infos, output)
-- The renderer
local mode = "text"
for _,l in ipairs(infos.doc_lines) do
mode = print_docline_on_output(output, l, mode)
end
close_mode(output, mode)
end
)
-- The documentation (plain text) renderer
DocumentParser.addRenderer (
function (infos)
-- The test
return infos.keywords["documentation"]
end,
function (infos, output)
-- The renderer
local mode = "text"
for _,l in ipairs(infos.doc_lines) do
mode = print_docline_on_output(output, l, mode)
end
close_mode(output, mode)
end
)
-- The declare renderer
DocumentParser.addRenderer (
function (infos)
-- The test
return
infos.keywords["declare"] or
infos.head_line:match("declare%s*{") or
infos.head_line:match("^%s*key%s*")
end,
function (infos, output)
-- The renderer
local key_name
if infos.keywords["declare"] then
local k = infos.keywords["declare"][1]
key_name = k:match("^%s*@declare%s*(.*)")
elseif infos.head_line:match("^%s*key%s*") then
key_name = infos.head_line:match('^%s*key%s*"(.*)"') or
infos.head_line:match("^%s*key%s*'(.*)'")
else
local l = infos.lines [infos.last_line + 1]
key_name = l:match('key%s*=%s*"(.*)"') or l:match("key%s*=%s*'(.*)'")
end
assert (key_name, "could not determine key")
local key = assert (keys[key_name], "unknown key '" .. key_name .. "'")
-- Render the head
if key.type then
print_on_output_escape(output,
"\\begin{luadeclare}",
"{", key.key, "}",
"{\\meta{", key.type, "}}",
"{", key.default or "", "}",
"{", key.initial or "", "}")
else
print_on_output_escape(output,
"\\begin{luadeclarestyle}",
"{", key.key, "}",
"{}",
"{", key.default or "", "}",
"{", key.initial or "", "}")
end
local mode = "text"
print_lines_on_output(output, process_string(strip_quotes(key.summary)))
print_lines_on_output(output, process_string(strip_quotes(key.documentation)))
if key.examples then
local e = process_examples(key.examples)
print_on_output(output,
"\\par\\smallskip\\emph{Example" .. (((#e>1) and "s") or "") .. "}\\par")
for _,example in ipairs(e) do
print_on_output(output, "\\begin{codeexample}[]")
print_lines_on_output(output, example)
print_on_output(output, "\\end{codeexample}")
end
end
print_on_output(output, key.type and "\\end{luadeclare}" or "\\end{luadeclarestyle}")
end
)
-- The empty line
DocumentParser.addRenderer (
function (infos)
-- The test
return
#infos.doc_lines == 1 and
infos.doc_lines[1]:match("^%-*%s*$")
end,
function (infos, output)
end
)
function print_lines_on_output(output, lines)
for _,l in ipairs(lines or {}) do
output[#output+1] = l
end
end
function print_on_output(output, ...)
local args = {...}
if #args > 0 then
for i = 1, #args do
args[i] = tostring(args[i])
end
output[#output+1] = table.concat(args)
end
end
function print_on_output_escape(output, ...)
local args = {...}
if #args > 0 then
for i = 1, #args do
args[i] = tostring(args[i]):gsub("_", "\\_")
end
output[#output+1] = table.concat(args)
end
end
function print_docline_on_output(output, l, mode)
if l:match("^%s*@section") then
print_on_output(output, "\\", l:match("%s*@section%s*(.*)"))
elseif l:match("^%s*@param%s+") then
if mode ~= "param" then
close_mode (output, mode)
mode = open_mode (output, "param")
end
print_on_output(output, "\\item[\\texttt{",
l:match("%s@param%s+(.-)%s"):gsub("_", "\\_"),
"}] ",
l:match("%s@param%s+.-%s+(.*)"))
elseif l:match("^%s*@return%s+") then
if mode ~= "return" then
close_mode (output, mode)
mode = open_mode (output, "returns")
end
print_on_output(output, "\\item[]", l:match("%s@return%s+(.*)"))
elseif l:match("^%s*@see%s+") then
if mode ~= "text" then
close_mode (output, mode)
mode = open_mode (output, "text")
end
print_on_output(output, "\\par\\emph{See also:} \\texttt{",
l:match("%s@see%s+(.*)"):gsub("_", "\\_"),
"}")
elseif l:match("^%s*@usage%s+") then
if mode ~= "text" then
close_mode (output, mode)
mode = open_mode (output, "text")
end
print_on_output(output, "\\par\\emph{Usage:} ",
l:match("%s@usage%s+(.*)"))
elseif l:match("^%s*@field+") then
close_mode (output, mode)
mode = open_mode (output, "field")
print_on_output(output, "{",
(l:match("%s@field%s+(.-)%s") or l:match("%s@field%s+(.*)")):gsub("_", "\\_"),
"}",
l:match("%s@field%s+.-%s+(.*)"))
elseif l:match("^%s*@done") or l:match("^%s*@text") then
close_mode(output, mode)
print_on_output(output, l)
mode = "text"
elseif l:match("^%s*@library") then
-- do nothing
elseif l:match("^%s*@function") then
-- do nothing
elseif l:match("^%s*@end") then
close_mode(output, mode)
mode = "done"
elseif l:match("^%s*@") then
error("Unknown mark " .. l)
else
print_on_output(output, l)
end
return mode
end
function open_mode (output, mode)
if mode == "param" then
print_on_output(output, "\\begin{luaparameters}")
elseif mode == "field" then
print_on_output(output, "\\begin{luafield}")
elseif mode == "returns" then
print_on_output(output, "\\begin{luareturns}")
end
return mode
end
function close_mode (output, mode)
if mode == "param" then
print_on_output(output, "\\end{luaparameters}")
elseif mode == "field" then
print_on_output(output, "\\end{luafield}")
elseif mode == "returns" then
print_on_output(output, "\\end{luareturns}")
end
return mode
end
function collect_infos (lines, i, state)
local doc_lines = {}
local keywords = {}
local function find_keywords(line)
local keyword = line:match("^%s*@([^%s]*)")
if keyword then
local t = keywords[keyword] or {}
t[#t+1] = line
keywords[keyword] = t
end
return line
end
-- Copy triple matches:
while lines[i] and lines[i]:match("^%-%-%-") do
doc_lines [#doc_lines + 1] = find_keywords(lines[i]:sub(4))
i = i + 1
end
-- Continue with double matches:
while lines[i] and lines[i]:match("^%-%-") do
doc_lines [#doc_lines + 1] = find_keywords(lines[i]:sub(3))
i = i + 1
end
local head_line = ""
if not keywords["end"] then
-- Skip empty lines
while lines[i] and lines[i]:match("^%s*$") do
i = i + 1
end
head_line = lines[i] or ""
if lines[i] and lines[i]:match("^%-%-%-") then
i = i - 1
end
end
return {
lines = lines,
last_line = i,
doc_lines = doc_lines,
keywords = keywords,
head_line = head_line
}
end
function render_infos(infos, state)
for _,renderer in ipairs(DocumentParser.renderers) do
if renderer.test (infos, state) then
renderer.renderer (infos, state)
return
end
end
pgf.debug(infos)
error("Unkown documentation type")
end
return DocumentParser
|