forked from lua/manual
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathof_to_html.lua
More file actions
executable file
·445 lines (388 loc) · 11.7 KB
/
Copy pathof_to_html.lua
File metadata and controls
executable file
·445 lines (388 loc) · 11.7 KB
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
#!/bin/lua
--[[
Run `./of_to_html.lua input.of output.html` or `./of_to_html.lua < input.of > output.html`
Depends on lpeg
--]]
local lpeg = require "lpeg"
lpeg.locale(lpeg)
local commands = { "ANSI", "APIEntry", "C", "CId", "Char", "Ci", "En", "LibEntry", "Lid", "M", "N", "Produc", "Q", "Rw", "See", "St", "T", "apii", "bnfNter", "bnfopt", "bnfrep", "bnfter", "def", "defid", "description", "emph", "emphx", "id", "idx", "index", "item", "itemize", "link", "manual", "num", "producbody", "producname", "refsec", "rep", "rw", "sect1", "sect2", "sect3", "sect4", "see", "seeC", "seeF", "sp", "title", "verbatim", "x", "At", "Cdots", "Close", "Open", "Or", "OrNL", "VerBar", "false", "ldots", "leq", "nil", "pi", "true", }
local function escape_html(s)
return s:gsub('.', {['<'] = '<', ['>'] = '>'})
end
local function see_fun_tag(content)
local format = '(see <a href="#%s"><code>%s</code></a>)'
return string.format(format, content, content)
end
local function Lid_tag(content)
local format = '<a href="#pdf-%s"><code>%s</code></a>'
return string.format(format, content, content)
end
local function defid_tag(content)
local format = '<a name="pdf-%s"><code>%s</code></a>'
return string.format(format, content, content)
end
local function apii_tag(content)
local o,p,x = content:match('([^,]+),([^,]+),(.+)')
return string.format('<span class="apii">[-%s, +%s, <em>%s</em>]</span>', o, p, x)
end
local function item_tag(content)
local s,e,title = content:find('([^\n]-)|')
if s then
return string.format('<li><b>%s: </b>%s</li>', title, content:sub(e+1))
end
return string.format('<li>%s</li>', content)
end
local function LibEntry_tag(content)
local s,e,title = content:find('([^\n]+)|')
if s then
local short = title:match('%S+')
return string.format('<hr/><h3><a name="pdf-%s"><code>%s</code></a></h3>%s', short, title, content:sub(e+1))
end
end
local function simple_tag(tag)
return string.format('<%s>%%s</%s>', tag, tag)
end
local function producbody_tag(content)
local s,e = content:find('^%s+')
if s then content = content:sub(e+1) end
return content:gsub('\n',' '):gsub('[\t ]+',' '):gsub('@OrNL','|\n ')
end
local function code_tag(content)
return string.format('<code>%s</code>', escape_html(content))
end
local function pre_tag(content)
return string.format('<pre>%s</pre>', escape_html(content))
end
local sub_tag = {
['C'] = '', ['Ci'] = '',
['At'] = '@',
['Cdots'] = '···',
['En'] = '–',
['true'] = '<b>true</b>',
['false'] = '<b>false</b>',
['nil'] = '<b>nil</b>',
['pi'] = 'π',
['VerBar'] = '|',
['ldots'] = '...',
['leq'] = '≤',
['index'] = '', -- je sais pas non plus
['ANSI'] = 'fonction ISO C <code>%s</code>',
['CId'] = 'fonction C <code>%s</code>',
['Char'] = "'<code>%s</code>'",
['M'] = simple_tag 'em',
['N'] = function (content) return content:gsub(' ', ' ') end,
['Q'] = '"%s"', -- quote ?
['Rw'] = simple_tag 'b',
['St'] = function (content) return string.format('"%s"', code_tag(content)) end,
['T'] = code_tag,
-- Tag de définition de grammaire
['Produc'] = pre_tag,
['bnfNter'] = '%s', -- non-terminal
['bnfopt'] = '[%s]', -- optionnel
['bnfrep'] = '{%s}', -- répétition
['bnfter'] = '‘<b>%s</b>’', -- symbole
['producbody'] = producbody_tag,
['producname'] = ' %s ::= ',
['Close'] = '‘<b>}</b>’',
['Open'] = '‘<b>{</b>’',
['Or'] = '|',
['OrNL'] = '@OrNL',
-- fin grammaire
['def'] = simple_tag 'em',
['description'] = simple_tag 'ul',
['emph'] = simple_tag 'em',
['emphx'] = simple_tag 'em',
['id'] = code_tag,
['idx'] = code_tag,
['itemize'] = simple_tag 'ul',
['manual'] = '%s', -- ok...
['num'] = '%s',
['em'] = simple_tag 'em',
['rw'] = simple_tag 'b',
['sp'] = simple_tag 'sup',
['verbatim'] = pre_tag,
['x'] = '%s',
-- ref simple
['Lid'] = Lid_tag,
['defid'] = defid_tag,
['seeC'] = see_fun_tag,
['seeF'] = see_fun_tag,
['apii'] = apii_tag,
['item'] = item_tag,
['LibEntry'] = LibEntry_tag,
-- special
['#block'] = '{%s}',
}
local function K (k) -- keyword
return lpeg.P(k) * -(lpeg.alnum + lpeg.P "_");
end
local command_patt
for _,v in ipairs(commands) do
if not command_patt then
command_patt = K(v)
else
command_patt = command_patt + K(v)
end
end
local function text_capture(text)
return { tag = 'text', content = text }
end
local function my_fun(tag, content)
assert(type(tag) == 'string')
return { tag = tag, content = content or {} }
end
local function trim_nl(str)
local res = str:gsub("^%s*\n(.-)%s*$", "%1")
if not res then res = str:gsub("^(.-)%s*$", "%1") end
return res
end
local function block_capture(t)
return { tag = '#block', content = t }
end
local ligne = 1
local function compteur(s,i,c)
ligne = ligne + 1
return i
end
local function pmt(p)
return lpeg.Cmt(p,compteur)
end
local lang = lpeg.P{
"S",
NL = pmt(lpeg.P"\r"^-1 * lpeg.P"\n"),
normal_word = ("{}" + (lpeg.P(1) - lpeg.S"{}@\n")) ^ 1,
normal_words = lpeg.V"normal_word" ,--* (lpeg.V"NL" * lpeg.V"normal_word") ^ 0 ,
block = "{" * lpeg.Ct((lpeg.V"text"))^-1 * "}",
tag = ("@" * (lpeg.C(command_patt) * lpeg.V"block" ^ -1)) / my_fun,
line = lpeg.Ct((lpeg.V"tag" + lpeg.V"block" / block_capture + lpeg.C(lpeg.V"normal_words")) ^ 1),
lines = lpeg.Ct(lpeg.V"line" * (lpeg.V"NL" * lpeg.V"line") ^ 0),
paras = lpeg.V"lines" * (lpeg.V"NL" ^ 2 * lpeg.V"lines") ^ 0,
text = lpeg.C(lpeg.V"NL" ^ 1) ^ -1 * lpeg.V"paras" * lpeg.C(lpeg.V"NL" ^ 1) ^ -1,
S = lpeg.Ct(lpeg.V"text")
}
function find_tag(elem, tag)
if type(elem) == 'table' then
if not elem.tag then
for _,k in ipairs(elem) do
ret = find_tag(k, tag)
if ret then return ret end
end
else
if elem.tag == tag then return elem end
if elem.content then
return find_tag(elem.content, tag)
end
end
end
return
end
local function APIEntry_pre(elem)
elem.header = elem.content[1]
elem.apii = find_tag(elem.header, 'apii')
table.remove(elem.content, 1)
end
local section = {}
local function section_pre(elem)
local cp = #section
local cs = tonumber(elem.tag:sub(5))
for i=cp,cs+1,-1 do section[i] = nil end
section[cs] = (section[cs] or 0) + 1
elem.id = table.concat(section,'.')
local para1 = elem.content[1][1]
if type(para1[1]) == 'string' then
local s = para1[1]:find('|')
if s then
local shortname = para1[1]:sub(1,s-1)
section[shortname] = elem.id
end
table.remove(para1,1)
end
local title = para1[1].content[1][1][1]
if cs < 4 then
local format = '<h%d>%s – <a name="%s">%s</a></h%d>'
para1[1] = string.format(format, cs, elem.id, elem.id, title, cs)
else
local format = '<h%d>%s</h%d>'
para1[1] = string.format(format, cs, title, cs)
end
end
function link_resolv(content)
local format = '%s (<a href="#%s">§%s</a>)'
local s = content:find('|')
local id = section[content:sub(1,s-1)]
local content = content:sub(s+1)
return string.format(format, content, id, id)
end
function refsec_resolv(content)
local format = '<a href="#%s">§%s</a>'
local id = section[content]
return string.format(format, id, id)
end
function see_resolv(content)
local format = '(voir <a href="#%s">§%s</a>)'
local id = section[content]
return string.format(format, id, id)
end
local function APIEntry_tag(elem, content)
local para1 = para_to_html(elem.tag, elem.header)
local s,e,prototype = para1:find('([^|]+)|')
local apii = ''
if elem.apii then
apii = sub_tag['apii'](paras_to_html{ tag = '#default', content = elem.apii.content }) .. '\n'
end
if s then
local format = '<hr/><h3><a name="%s"><code>%s</code></a></h3>\n' .. apii .. '<pre>%s</pre>%s'
local title = prototype:match('%s.-(lua[%w_]+)') -- FIXME yolo
return string.format(format, title, title, prototype, content)
end
end
local id_ref = {
['link'] = link_resolv,
['refsec'] = refsec_resolv,
['See'] = refsec_resolv,
['see'] = see_resolv,
}
local spec_tag = {
['APIEntry'] = APIEntry_tag,
}
local paraph_comm = { "sect1", "sect2", "sect3", "sect4", }
local paraph_in_paraph = { APIEntry = true, LibEntry = true, }
local pre_rules = {
--["producbody"] = remove_nl,
["sect1"] = section_pre, ["sect2"] = section_pre, ["sect3"] = section_pre, ["sect4"] = section_pre,
["APIEntry"] = APIEntry_pre,
}
local keep_nl_set = { verbatim = true, Produc = true}
local ignore_tag = { apii = true }
local paraph_set = {}
for _,k in ipairs(paraph_comm) do paraph_set[k] = true end
local path = {}
function path.contains(tag)
for _,v in ipairs(path) do
if v == tag then return true end
end
return false
end
function ref_find(elem)
if type(elem) == 'table' then
if not elem.tag then
for _,k in ipairs(elem) do
ref_find(k)
end
else
if pre_rules[elem.tag] then
pre_rules[elem.tag](elem)
end
ref_find(elem.content)
end
end
end
function line_to_html(tag, line)
local ret = {}
for _,k in ipairs(line) do
if type(k) == 'string' then
table.insert(ret, k)
elseif not k.tag then
table.insert(ret, paras_to_html{ tag = "#default", content = k })
else
table.insert(ret, paras_to_html(k))
end
end
local str = table.concat(ret, '')
return table.concat(ret, '')
end
function para_to_html(tag, para)
local ret = {}
for _,k in ipairs(para) do
table.insert(ret, line_to_html(tag, k))
end
local str = table.concat(ret, '\n')
if tag == 'verbatim' then str = ' ' .. str:gsub('\n', '\n ') end
return str
end
function paras_to_html(elem)
local ret = {}
local tag = elem.tag
local paras = elem.content
if ignore_tag[tag] then return '' end
table.insert(path, tag)
for _,k in ipairs(paras) do
local content = para_to_html(tag, k)
if #content > 0 then
if paraph_set[tag] then table.insert(ret, '<p>' .. content .. '</p>\n')
else table.insert(ret, content) end
end
end
local sep = '\n'
if keep_nl_set[tag] then sep = '\n\n' end
if paraph_in_paraph[tag] then sep = '</p>\n<p>' end
local str = table.concat(ret,sep)
if keep_nl_set[tag] then str = trim_nl(str) end
if type(sub_tag[tag]) == 'string' then
str = string.format(sub_tag[tag], str)
elseif type(sub_tag[tag]) == 'function' then
str = sub_tag[tag](str)
elseif type(id_ref[tag]) == 'function' then
str = id_ref[tag](str)
elseif type(spec_tag[tag]) == 'function' then
str = spec_tag[tag](elem, str)
end
table.remove(path)
return str
end
local header = [[
<html>
<head>
<title>Lua 5.3 Manuel de référence</title>
<link rel="stylesheet" type="text/css" href="https://www.lua.org/lua.css"/>
<link rel="stylesheet" type="text/css" href="https://www.lua.org/manual/manual.css"/>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<body>
<h1>
<a href="https://www.lua.org/home.html"><img src="https://www.lua.org/images/logo.gif" alt="Lua"/></a>
Lua 5.3 Manuel de référence
</h1>
<p>by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes</p>
<p>traduction par la communauté <a href="https://lua-fr.github.io/">LuaFr</a></p>
<p>
<small>
Copyright © 2015–2017 Lua.org, PUC-Rio.
Freely available under the terms of the
<a href="../../license.html">Lua license</a>.
</small>
</p>
<div class="menubar">
<a href="contents.html#contents">contents</a>
·
<a href="contents.html#index">index</a>
·
<a href="../">other versions</a>
</div>
]]
local footer = [[
<p class="footer">
Last update:
Fri Feb 3 07:26:45 BRST 2017
</p>
<!--
Last change: revised for Lua 5.3.4
-->
</body></html>
]]
-- main part
local finname, foutname = table.unpack(arg)
if finname then io.input(finname) end
if foutname then io.output(foutname) end
local input_string = io.read('a')
local ast = lang:match(input_string)
ref_find(ast)
local html = paras_to_html{ tag = '#default', content = ast}
html = html:match('^%s*(.-)%s*$')
if #html > 0 then
io.write(header)
io.write(paras_to_html{ tag = '#default', content = ast})
io.write(footer)
else
error(string.format('il semble y avoir une erreur en rapport à avec la ligne %d', ligne))
end