forked from oils-for-unix/oils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoil_doc.py
430 lines (323 loc) · 10.6 KB
/
oil_doc.py
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
#!/usr/bin/env python2
"""
oil_doc.py: HTML processing for Oil documentation.
Plugins:
ExpandLinks expands $xref, etc.
PygmentsPlugin -- for ```python, ```sh, ```c, etc.
HelpIndexPlugin -- for help-index.html
ShPromptPlugin -- understands $ echo hi, but doesn't run anything
ShSession -- runs shell snippets and caches the output
"""
from __future__ import print_function
import cgi
import cStringIO
import re
import sys
from lazylex import html
log = html.log
def RemoveComments(s):
""" Remove <!-- comments --> """
f = cStringIO.StringIO()
out = html.Output(s, f)
tag_lexer = html.TagLexer(s)
pos = 0
for tok_id, end_pos in html.ValidTokens(s):
if tok_id == html.Comment:
value = s[pos : end_pos]
# doc/release-index.md has <!-- REPLACE_WITH_DATE --> etc.
if 'REPLACE' not in value:
out.PrintUntil(pos)
out.SkipTo(end_pos)
pos = end_pos
out.PrintTheRest()
return f.getvalue()
class _Abbrev(object):
def __init__(self, fmt):
self.fmt = fmt
def __call__(self, value):
return self.fmt % {'value': value}
_ABBREVIATIONS = {
'xref':
_Abbrev('/cross-ref.html?tag=%(value)s#%(value)s'),
'help':
_Abbrev('help.html?topic=%(value)s#%(value)s'),
'blog-tag':
_Abbrev('/blog/tags.html?tag=%(value)s#%(value)s'),
'oil-commit':
_Abbrev('https://github.com/oilshell/oil/commit/%(value)s'),
'oil-src':
_Abbrev('https://github.com/oilshell/oil/blob/master/%(value)s'),
'blog-code-src':
_Abbrev('https://github.com/oilshell/blog-code/blob/master/%(value)s'),
'issue':
_Abbrev('https://github.com/oilshell/oil/issues/%(value)s'),
'wiki':
_Abbrev('https://github.com/oilshell/oil/wiki/%(value)s'),
}
# $xref:foo
_SHORTCUT_RE = re.compile(r'\$ ([a-z\-]+) (?: : (\S+))?', re.VERBOSE)
def ExpandLinks(s):
"""
Expand $xref:bash and so forth
"""
f = cStringIO.StringIO()
out = html.Output(s, f)
tag_lexer = html.TagLexer(s)
pos = 0
it = html.ValidTokens(s)
while True:
try:
tok_id, end_pos = next(it)
except StopIteration:
break
if tok_id == html.StartTag:
tag_lexer.Reset(pos, end_pos)
if tag_lexer.TagName() == 'a':
open_tag_right = end_pos
href_start, href_end = tag_lexer.GetSpanForAttrValue('href')
if href_start == -1:
continue
# TODO: Need to unescape like GetAttr()
href = s[href_start : href_end]
new = None
m = _SHORTCUT_RE.match(href)
if m:
abbrev_name, arg = m.groups()
if not arg:
close_tag_left, _ = html.ReadUntilEndTag(it, tag_lexer, 'a')
arg = s[open_tag_right : close_tag_left]
# Hack to so we can write [Wiki Page]($wiki) and have the link look
# like /Wiki-Page/
if abbrev_name == 'wiki':
arg = arg.replace(' ', '-')
func = _ABBREVIATIONS.get(abbrev_name)
if not func:
raise RuntimeError('Invalid abbreviation %r' % abbrev_name)
new = func(arg)
if new is not None:
out.PrintUntil(href_start)
f.write(cgi.escape(new))
out.SkipTo(href_end)
pos = end_pos
out.PrintTheRest()
return f.getvalue()
class _Plugin(object):
def __init__(self, s, start_pos, end_pos):
self.s = s
self.start_pos = start_pos
self.end_pos = end_pos
def PrintHighlighted(self, out):
raise NotImplementedError()
# Optional newline at end
_LINE_RE = re.compile(r'(.*) \n?', re.VERBOSE)
_PROMPT_LINE_RE = re.compile(r'''
(\S* \$)[ ] # flush-left non-whitespace, then dollar and space is a prompt
(.*?) # arbitrary text
(?: # don't highlight tab completion
(<TAB>) # it's HTML escaped!!!
.*?
)?
(?:
[ ][ ]([#] .*) # optionally: two spaces then a comment
)?
$
''', re.VERBOSE)
_EOL_COMMENT_RE = re.compile(r'''
.*? # arbitrary text
[ ][ ]([#] .*) # two spaces then a comment
$
''', re.VERBOSE)
_COMMENT_LINE_RE = re.compile(r'#.*')
def Lines(s, start_pos, end_pos):
pos = start_pos
while pos < end_pos:
m = _LINE_RE.match(s, pos, end_pos)
if not m:
raise RuntimeError("Should have matched a line")
line_end = m.end(0)
yield line_end
pos = line_end
class ShPromptPlugin(_Plugin):
"""
Highlight shell prompts.
"""
def PrintHighlighted(self, out):
pos = self.start_pos
for line_end in Lines(self.s, self.start_pos, self.end_pos):
m = _COMMENT_LINE_RE.match(self.s, pos, line_end)
if m:
out.PrintUntil(m.start(0))
out.Print('<span class="sh-comment">')
out.PrintUntil(m.end(0))
out.Print('</span>')
else:
m = _PROMPT_LINE_RE.match(self.s, pos, line_end)
if m:
#log('MATCH %r', m.groups())
out.PrintUntil(m.start(1))
out.Print('<span class="sh-prompt">')
out.PrintUntil(m.end(1))
out.Print('</span>')
out.PrintUntil(m.start(2))
out.Print('<span class="sh-command">')
out.PrintUntil(m.end(2))
out.Print('</span>')
if m.group(3):
out.PrintUntil(m.start(3))
out.Print('<span class="sh-tab-complete">')
out.PrintUntil(m.end(3))
out.Print('</span>')
if m.group(4):
out.PrintUntil(m.start(4))
out.Print('<span class="sh-comment">')
out.PrintUntil(m.end(4))
out.Print('</span>')
else:
m = _EOL_COMMENT_RE.match(self.s, pos, line_end)
if m:
out.PrintUntil(m.start(1))
out.Print('<span class="sh-comment">')
out.PrintUntil(m.end(1))
out.Print('</span>')
out.PrintUntil(line_end)
pos = line_end
class HelpIndexPlugin(_Plugin):
"""
Highlight blocks of help-index.md.
"""
def PrintHighlighted(self, out):
from doctools import make_help
pos = self.start_pos
for line_end in Lines(self.s, self.start_pos, self.end_pos):
# NOTE: HighlightLine accepts an HTML ESCAPED line. It's valid to just
# add tags and leave everything alone.
line = self.s[pos : line_end]
html_line = make_help.HighlightLine(line)
if html_line is not None:
out.PrintUntil(pos)
out.Print(html_line)
out.SkipTo(line_end)
pos = line_end
class PygmentsPlugin(_Plugin):
def __init__(self, s, start_pos, end_pos, lang):
_Plugin.__init__(self, s, start_pos, end_pos)
self.lang = lang
def PrintHighlighted(self, out):
from pygments import lexers
from pygments import formatters
from pygments import highlight
# unescape before passing to pygments, which will escape
code = html.ToText(self.s, self.start_pos, self.end_pos)
lexer = lexers.get_lexer_by_name(self.lang)
formatter = formatters.HtmlFormatter()
highlighted = highlight(code, lexer, formatter)
out.Print(highlighted)
def HighlightCode(s):
"""
Algorithm:
1. Collect what's inside <pre><code> ...
2. Then read lines with ShPromptPlugin.
3. If the line looks like a shell prompt and command, highlight them with
<span>
"""
f = cStringIO.StringIO()
out = html.Output(s, f)
tag_lexer = html.TagLexer(s)
pos = 0
it = html.ValidTokens(s)
while True:
try:
tok_id, end_pos = next(it)
except StopIteration:
break
if tok_id == html.StartTag:
tag_lexer.Reset(pos, end_pos)
if tag_lexer.TagName() == 'pre':
pre_start_pos = pos
pos = end_pos
try:
tok_id, end_pos = next(it)
except StopIteration:
break
tag_lexer.Reset(pos, end_pos)
if tok_id == html.StartTag and tag_lexer.TagName() == 'code':
css_class = tag_lexer.GetAttr('class')
code_start_pos = end_pos
if css_class is not None and css_class.startswith('language'):
slash_code_left, slash_code_right = \
html.ReadUntilEndTag(it, tag_lexer, 'code')
if css_class == 'language-sh-prompt':
# Here's we're KEEPING the original <pre><code>
# Print everything up to and including <pre><code language="...">
out.PrintUntil(code_start_pos)
plugin = ShPromptPlugin(s, code_start_pos, slash_code_left)
plugin.PrintHighlighted(out)
out.SkipTo(slash_code_left)
elif css_class == 'language-oil':
# TODO: Write an Oil syntax highlighter.
pass
elif css_class == 'language-oil-help-index':
out.PrintUntil(code_start_pos)
plugin = HelpIndexPlugin(s, code_start_pos, slash_code_left)
plugin.PrintHighlighted(out)
out.SkipTo(slash_code_left)
else:
# Here's we're REMOVING the original <pre><code>
# Pygments gives you a <pre> already
# We just read closing </code>, and the next one should be </pre>.
try:
tok_id, end_pos = next(it)
except StopIteration:
break
tag_lexer.Reset(slash_code_right, end_pos)
assert tok_id == html.EndTag, tok_id
assert tag_lexer.TagName() == 'pre', tag_lexer.TagName()
slash_pre_right = end_pos
out.PrintUntil(pre_start_pos)
lang = css_class[len('language-'):]
plugin = PygmentsPlugin(s, code_start_pos, slash_code_left, lang)
plugin.PrintHighlighted(out)
out.SkipTo(slash_pre_right)
f.write('<!-- done pygments -->\n')
pos = end_pos
out.PrintTheRest()
return f.getvalue()
class ShellSession(object):
"""
TODO: Pass this to HighlightCode as a plugin
$ x=one
$ echo $x
$ echo two
Becomes
$ x=one
$ echo $x
one
$ echo two
two
And then you will have
blog/2019/12/_shell_session/
$hash1-stdout.txt
$hash2-stdout.txt
It hashes the command with md5 and then brings it back.
If the file already exists then it doesn't run it again.
You can delete the file to redo it.
TODO: write a loop that reads one line at a time, writes, it, then reads
output from bash.
Use the Lines iterator to get lines.
For extra credit, you can solve the PS2 problem? That's easily done with
Oil's parser.
"""
def __init__(self, shell_exe, cache_dir):
"""
Args:
shell_exe: sh, bash, osh, or oil. Use the one in the $PATH by default.
cache_dir: ~/git/oilshell/oilshell.org/blog/2019/12/session/
"""
self.shell_exe = shell_exe
self.cache_dir = cache_dir
def PrintHighlighted(self, s, start_pos, end_pos, out):
"""
Args:
s: an HTML string.
"""
pass