forked from lua/lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2html
executable file
·519 lines (420 loc) · 11.6 KB
/
2html
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
#!/usr/bin/env lua5.3
-- special marks:
-- \1 - paragraph (empty line)
-- \4 - remove spaces around it
-- \3 - ref (followed by label|)
---------------------------------------------------------------
header = [[
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<title>Lua 5.4 Reference Manual</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<link rel="stylesheet" href="lua.css">
<link rel="stylesheet" href="manual.css">
</head>
<body bgcolor="#FFFFFF">
<hr>
<h1>
<a href="http://www.lua.org/home.html"><img src="logo.gif" alt="[Lua logo]" border="0"></a>
Lua 5.4 Reference Manual
</h1>
by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes
<p>
<small>
<a href="http://www.lua.org/copyright.html">Copyright</a>
© 2023 Lua.org, PUC-Rio. All rights reserved.
</small>
<hr>
<!-- ====================================================================== -->
<p>
]]
footer = "\n\n</body></html>\n\n"
local seefmt = '(see %s)'
if arg[1] == 'port' then
seefmt = '(ver %s)'
header = string.gsub(header, "by (.-)\n",
"%1\n<p>Tradução: Sérgio Queiroz de Medeiros", 1)
header = string.gsub(header, "Lua (%d+.%d+) Reference Manual",
"Manual de Referência de Lua %1")
header = string.gsub(header, "All rights reserved",
"Todos os direitos reservados")
end
---------------------------------------------------------------
local function compose (f,g)
assert(f and g)
return function (s) return g(f(s)) end
end
local function concat (f, g)
assert(f and g)
return function (s) return f(s) .. g(s) end
end
local Tag = {}
setmetatable(Tag, {
__index = function (t, tag)
local v = function (n, att)
local e = ""
if type(att) == "table" then
for k,v in pairs(att) do e = string.format('%s %s="%s"', e, k, v) end
end
if n then
return string.format("<%s%s>%s</%s>", tag, e, n, tag)
else
return string.format("<%s%s>", tag, e)
end
end
t[tag] = v
return v
end
})
---------------------------------------------------------------
local labels = {}
local function anchor (text, label, link, textlink)
if labels[label] then
error("label " .. label .. " already defined")
end
labels[label] = {text = textlink, link = link}
return Tag.a(text, {name=link})
end
local function makeref (label)
assert(not string.find(label, "|"))
return string.format("\3%s\3", label)
end
local function ref (label)
local l = labels[label]
if not l then
io.stderr:write("label ", label, " undefined\n")
return "@@@@@@@"
else
return Tag.a(l.text, {href="#"..l.link})
end
end
---------------------------------------------------------------
local function nopara (t)
t = string.gsub(t, "\1", "\n\n")
t = string.gsub(t, "<p>%s*</p>", "")
return t
end
local function fixpara (t)
t = string.gsub(t, "\1", "\n</p>\n\n<p>\n")
t = string.gsub(t, "<p>%s*</p>", "")
return t
end
local function antipara (t)
return "</p>\n" .. t .. "<p>"
end
Tag.pre = compose(Tag.pre, antipara)
Tag.ul = compose(Tag.ul, antipara)
---------------------------------------------------------------
local Gfoots = 0
local footnotes = {}
local line = Tag.hr(nil)
local function dischargefoots ()
if #footnotes == 0 then return "" end
local fn = table.concat(footnotes)
footnotes = {}
return line .. Tag.h3"footnotes:" .. fn .. line
end
local Glists = 0
local listings = {}
local function dischargelist ()
if #listings == 0 then return "" end
local l = listings
listings = {}
return line .. table.concat(l, line..line) .. line
end
---------------------------------------------------------------
local counters = {
h1 = {val = 1},
h2 = {father = "h1", val = 1},
h3 = {father = "h2", val = 1},
listing = {father = "h1", val = 1},
}
local function inccounter (count)
counters[count].val = counters[count].val + 1
for c, v in pairs(counters) do
if v.father == count then v.val = 1 end
end
end
local function getcounter (count)
local c = counters[count]
if c.father then
return getcounter(c.father) .. "." .. c.val
else
return c.val .. ""
end
end
---------------------------------------------------------------
local function fixed (x)
return function () return x end
end
local function id (x) return x end
local function prepos (x, y)
assert(x and y)
return function (s) return string.format("%s%s%s", x, s, y) end
end
local rw = Tag.b
local function LuaName (name)
return Tag.code(name)
end
local function getparam (s)
local i, e = string.find(s, "^[^%s@|]+|")
if not i then return nil, s
else return string.sub(s, i, e - 1), string.sub(s, e + 1)
end
end
local function gettitle (h)
local title, p = assert(string.match(h, "<title>(.-)</title>()"))
return title, string.sub(h, p)
end
local function getparamtitle (what, h, nonum)
local label, title, c, count
label, h = getparam(h)
title, h = gettitle(h)
if not nonum then
count = getcounter(what)
inccounter(what)
c = string.format("%s – ", count)
else
c = ""
end
label = label or count
if label then
title = anchor(title, label, count, "§"..count)
end
title = string.format("%s%s", c, title)
return title, h
end
local function section (what, nonum)
return function (h)
local title
title, h = getparamtitle(what, h, nonum)
local fn = what == "h1" and dischargefoots() or ""
h = fixpara(Tag.p(h))
return "</p>\n" .. Tag[what](title) .. h .. fn ..
dischargelist() .. "<p>"
end
end
local function verbatim (s)
s = nopara(s)
s = string.gsub(s, "\n", "\n ")
s = string.gsub(s, "\n%s*$", "\n")
return Tag.pre(s)
end
local function verb (s)
return Tag.code(s)
end
local function lua2link (e)
return string.find(e, "luaL?_") and e or "pdf-"..e
end
local verbfixed = verb
local Tex = {
ANSI = function (func)
return "ISO C function " .. Tag.code(func)
end,
At = fixed"@",
B = Tag.b,
bigskip = fixed"",
bignum = id,
C = fixed"",
Ci = prepos("<!-- ", " -->"),
CId = function (func)
return "C function " .. Tag.code(func)
end,
chapter = section"h1",
Char = compose(verbfixed, prepos("'", "'")),
Cdots = fixed"···",
Close = fixed"}",
col = Tag.td,
defid = function (name)
local l = lua2link(name)
local c = Tag.code(name)
return anchor(c, l, l, c)
end,
def = Tag.em,
description = compose(nopara, Tag.ul),
Em = fixed("\4" .. "—" .. "\4"),
emph = Tag.em,
emphx = Tag.em, -- emphasis plus index (if there was an index)
En = fixed("–"),
format = fixed"",
["false"] = fixed(Tag.b"false"),
id = Tag.code,
idx = Tag.code,
index = fixed"",
Lidx = fixed"", -- Tag.code,
ldots = fixed"...",
x = id,
itemize = compose(nopara, Tag.ul),
leq = fixed"≤",
Lid = function (s)
return makeref(lua2link(s))
end,
M = Tag.em,
N = function (s) return (string.gsub(s, " ", " ")) end,
NE = id, -- tag"foreignphrase",
num = id,
["nil"] = fixed(Tag.b"nil"),
fail = fixed(Tag.b"fail"),
Open = fixed"{",
part = section("h1", true),
Pat = compose(verbfixed, prepos("'", "'")),
preface = section("h1", true),
psect = section("h2", true),
Q = prepos('"', '"'),
refchp = makeref,
refcode = makeref,
refsec = makeref,
pi = fixed"π",
rep = Tag.em, -- compose(prepos("<", ">"), Tag.em),
Rw = rw,
rw = rw,
sb = Tag.sub,
sp = Tag.sup,
St = compose(verbfixed, prepos('"', '"')),
sect1 = section"h1",
sect2 = section"h2",
sect3 = section"h3",
sect4 = section("h4", true),
simplesect = id,
Tab2 = function (s) return Tag.table(s, {border=1}) end,
row = Tag.tr,
title = Tag.title,
todo = Tag.todo,
["true"] = fixed(Tag.b"true"),
T = verb,
item = function (s)
local t, p = string.match(s, "^([^\n|]+)|()")
if t then
s = string.sub(s, p)
s = Tag.b(t..": ") .. s
end
return Tag.li(fixpara(s))
end,
verbatim = verbatim,
manual = id,
-- for the manual
link =function (s)
local l, t = getparam(s)
assert(l)
return string.format("%s (%s)", t, makeref(l))
end,
see = function (s) return string.format(seefmt, makeref(s)) end,
See = makeref,
seeC = function (s)
return string.format(seefmt, makeref(s))
end,
seeF = function (s)
return string.format(seefmt, makeref(lua2link(s)))
end,
APIEntry = function (e)
local h, name
h, e = string.match(e, "^%s*(.-)%s*|(.*)$")
name = string.match(h, "(luaL?_[%w_]+)%)? +%(") or
string.match(h, "luaL?_[%w_]+")
local a = anchor(Tag.code(name), name, name, Tag.code(name))
local apiicmd, ne = string.match(e, "^(.-</span>)(.*)")
--io.stderr:write(e)
if not apiicmd then
return antipara(Tag.hr() .. Tag.h3(a)) .. Tag.pre(h) .. e
else
return antipara(Tag.hr() .. Tag.h3(a)) .. apiicmd .. Tag.pre(h) .. ne
end
end,
LibEntry = function (e)
local h, name
h, e = string.match(e, "^(.-)|(.*)$")
name = string.gsub(h, " (.+", "")
local l = lua2link(name)
local a = anchor(Tag.code(h), l, l, Tag.code(name))
return Tag.hr() .. Tag.h3(a) .. e
end,
Produc = compose(nopara, Tag.pre),
producname = prepos("\t", " ::= "),
Or = fixed" | ",
VerBar = fixed"|", -- vertical bar
OrNL = fixed" | \4",
bnfNter = prepos("", ""),
bnfopt = prepos("[", "]"),
bnfrep = prepos("{", "}"),
bnfter = compose(Tag.b, prepos("‘", "’")),
producbody = function (s)
s = string.gsub(s, "%s+", " ")
s = string.gsub(s, "\4", "\n\t\t")
return s
end,
apii = function (s)
local pop,push,err = string.match(s, "^(.-),(.-),(.*)$")
if pop ~= "?" and string.find(pop, "%W") then
pop = "(" .. pop .. ")"
end
if push ~= "?" and string.find(push, "%W") then
push = "(" .. push .. ")"
end
err = (err == "-") and "–" or Tag.em(err)
return Tag.span(
string.format("[-%s, +%s, %s]", pop, push, err),
{class="apii"}
)
end,
}
local others = prepos("?? "," ??")
local function trata (t)
t = string.gsub(t, "@(%w+)(%b{})", function (w, f)
f = trata(string.sub(f, 2, -2))
if type(Tex[w]) ~= "function" then
io.stderr:write(w .. "\n")
return others(f)
else
return Tex[w](f, w)
end
end)
return t
end
---------------------------------------------------------------------
---------------------------------------------------------------------
-- read whole book
t = io.read"*a"
t = string.gsub(t, "[<>&\128-\255]",
{["<"] = "<",
[">"] = ">",
["&"] = "&",
["\170"] = "ª",
["\186"] = "º",
["\192"] = "À",
["\193"] = "Á",
["\194"] = "Â",
["\195"] = "Ã",
["\199"] = "Ç",
["\201"] = "É",
["\202"] = "Ê",
["\205"] = "Í",
["\211"] = "Ó",
["\212"] = "Ô",
["\218"] = "Ú",
["\224"] = "à",
["\225"] = "á",
["\226"] = "â",
["\227"] = "ã",
["\231"] = "ç",
["\233"] = "é",
["\234"] = "ê",
["\237"] = "í",
["\243"] = "ó",
["\244"] = "ô",
["\245"] = "õ",
["\250"] = "ú",
["\252"] = "ü"
})
t = string.gsub(t, "\n\n+", "\1")
-- complete macros with no arguments
t = string.gsub(t, "(@%w+)([^{%w])", "%1{}%2")
t = trata(t)
-- correct references
t = string.gsub(t, "\3(.-)\3", ref)
-- remove extra space (??)
t = string.gsub(t, "%s*\4%s*", "")
t = nopara(t)
-- HTML 3.2 does not need </p> (but complains when it is in wrong places :)
t = string.gsub(t, "</p>", "")
io.write(header, t, footer)