1
1
# Translates Vim documentation to HTML
2
2
3
3
from datetime import datetime
4
- import flask
5
4
import functools
6
5
import html
7
6
import json
8
7
import re
9
8
import urllib .parse
10
9
10
+ import flask
11
+ import markupsafe
12
+
11
13
12
14
class MacVimProject :
13
15
name = "MacVim"
@@ -98,7 +100,10 @@ class NeovimProject:
98
100
)
99
101
RE_EG_START = re .compile (r"(.* )?>(?:vim|lua)?$" )
100
102
RE_EG_END = re .compile (r"[^ \t]" )
101
- RE_SECTION = re .compile (r"(?!NOTE$|CTRL-|\.\.\.$)([A-Z.][-A-Z0-9 .,()_?]*)(?:\s+\*|$)" )
103
+ RE_SECTION = re .compile (
104
+ r"(?!NOTE$|UTF-8.$|VALID.$|OLE.$|CTRL-|\.\.\.$)"
105
+ r"([A-Z.][-A-Z0-9 .,()_?]*?)\s*(?:\s\*|$)"
106
+ )
102
107
RE_STARTAG = re .compile (r'\*([^ \t"*]+)\*(?:\s|$)' )
103
108
RE_LOCAL_ADD = re .compile (r"LOCAL ADDITIONS:\s+\*local-additions\*$" )
104
109
@@ -275,11 +280,13 @@ def to_html(self, filename, contents):
275
280
276
281
out = []
277
282
sidebar_headings = []
283
+ sidebar_lvl = 2
278
284
in_example = False
279
285
for idx , line in enumerate (lines ):
280
286
prev_line = "" if idx == 0 else lines [idx - 1 ]
281
287
if prev_line == "" and idx > 1 :
282
288
prev_line = lines [idx - 2 ]
289
+
283
290
if in_example :
284
291
if RE_EG_END .match (line ):
285
292
in_example = False
@@ -288,18 +295,31 @@ def to_html(self, filename, contents):
288
295
else :
289
296
out .extend (('<span class="e">' , html_escape (line ), "</span>\n " ))
290
297
continue
298
+
291
299
if RE_HRULE .match (line ):
292
300
out .extend (('<span class="h">' , html_escape (line ), "</span>\n " ))
293
301
continue
302
+
294
303
if m := RE_EG_START .match (line ):
295
304
in_example = True
296
305
line = m .group (1 ) or ""
297
- span_opened = False
306
+
307
+ heading = None
308
+ skip_to_col = None
298
309
if m := RE_SECTION .match (line ):
299
- out .extend (('<span class="c">' , m .group (1 ), "</span>" ))
300
- line = line [m .end (1 ) :]
310
+ heading = m .group (1 )
311
+ heading_lvl = 2
312
+ out .extend (('<span class="c">' , heading , "</span>" ))
313
+ skip_to_col = m .end (1 )
301
314
elif RE_HRULE1 .match (prev_line ) and (m := RE_HEADING .match (line )):
302
315
heading = m .group (1 )
316
+ heading_lvl = 1
317
+
318
+ span_opened = False
319
+ if heading is not None and sidebar_lvl >= heading_lvl :
320
+ if sidebar_lvl > heading_lvl :
321
+ sidebar_lvl = heading_lvl
322
+ sidebar_headings = []
303
323
if m := RE_STARTAG .search (line ):
304
324
tag = m .group (1 )
305
325
else :
@@ -308,8 +328,14 @@ def to_html(self, filename, contents):
308
328
span_opened = True
309
329
tag_escaped = urllib .parse .quote_plus (tag )
310
330
sidebar_headings .append (
311
- flask .Markup (f'<a href="#{ tag_escaped } ">{ html_escape (heading )} </a>' )
331
+ markupsafe .Markup (
332
+ f'<a href="#{ tag_escaped } ">{ html_escape (heading )} </a>'
333
+ )
312
334
)
335
+
336
+ if skip_to_col is not None :
337
+ line = line [skip_to_col :]
338
+
313
339
is_faq_line = (
314
340
self ._project is VimProject and is_help_txt and RE_LOCAL_ADD .match (line )
315
341
)
@@ -392,7 +418,7 @@ def to_html(self, filename, contents):
392
418
filename = filename ,
393
419
static_dir = static_dir ,
394
420
helptxt = helptxt ,
395
- content = flask .Markup ("" .join (out )),
421
+ content = markupsafe .Markup ("" .join (out )),
396
422
sidebar_headings = sidebar_headings ,
397
423
current_time = current_time ,
398
424
commit = self ._commit ,
0 commit comments