1
1
# Translates Vim documentation to HTML
2
2
3
- import flask
4
3
import functools
5
4
import html
6
5
import re
7
6
import urllib .parse
8
7
8
+ import flask
9
+ import markupsafe
10
+
9
11
10
12
class VimProject :
11
13
name = "Vim"
@@ -86,7 +88,10 @@ class NeovimProject:
86
88
)
87
89
RE_EG_START = re .compile (r"(.* )?>(?:vim|lua)?$" )
88
90
RE_EG_END = re .compile (r"[^ \t]" )
89
- RE_SECTION = re .compile (r"(?!NOTE$|CTRL-|\.\.\.$)([A-Z.][-A-Z0-9 .,()_?]*)(?:\s+\*|$)" )
91
+ RE_SECTION = re .compile (
92
+ r"(?!NOTE$|UTF-8.$|VALID.$|OLE.$|CTRL-|\.\.\.$)"
93
+ r"([A-Z.][-A-Z0-9 .,()_?]*?)\s*(?:\s\*|$)"
94
+ )
90
95
RE_STARTAG = re .compile (r'\*([^ \t"*]+)\*(?:\s|$)' )
91
96
RE_LOCAL_ADD = re .compile (r"LOCAL ADDITIONS:\s+\*local-additions\*$" )
92
97
@@ -226,11 +231,13 @@ def to_html(self, filename, contents):
226
231
227
232
out = []
228
233
sidebar_headings = []
234
+ sidebar_lvl = 2
229
235
in_example = False
230
236
for idx , line in enumerate (lines ):
231
237
prev_line = "" if idx == 0 else lines [idx - 1 ]
232
238
if prev_line == "" and idx > 1 :
233
239
prev_line = lines [idx - 2 ]
240
+
234
241
if in_example :
235
242
if RE_EG_END .match (line ):
236
243
in_example = False
@@ -239,18 +246,31 @@ def to_html(self, filename, contents):
239
246
else :
240
247
out .extend (('<span class="e">' , html_escape (line ), "</span>\n " ))
241
248
continue
249
+
242
250
if RE_HRULE .match (line ):
243
251
out .extend (('<span class="h">' , html_escape (line ), "</span>\n " ))
244
252
continue
253
+
245
254
if m := RE_EG_START .match (line ):
246
255
in_example = True
247
256
line = m .group (1 ) or ""
248
- span_opened = False
257
+
258
+ heading = None
259
+ skip_to_col = None
249
260
if m := RE_SECTION .match (line ):
250
- out .extend (('<span class="c">' , m .group (1 ), "</span>" ))
251
- line = line [m .end (1 ) :]
261
+ heading = m .group (1 )
262
+ heading_lvl = 2
263
+ out .extend (('<span class="c">' , heading , "</span>" ))
264
+ skip_to_col = m .end (1 )
252
265
elif RE_HRULE1 .match (prev_line ) and (m := RE_HEADING .match (line )):
253
266
heading = m .group (1 )
267
+ heading_lvl = 1
268
+
269
+ span_opened = False
270
+ if heading is not None and sidebar_lvl >= heading_lvl :
271
+ if sidebar_lvl > heading_lvl :
272
+ sidebar_lvl = heading_lvl
273
+ sidebar_headings = []
254
274
if m := RE_STARTAG .search (line ):
255
275
tag = m .group (1 )
256
276
else :
@@ -259,8 +279,14 @@ def to_html(self, filename, contents):
259
279
span_opened = True
260
280
tag_escaped = urllib .parse .quote_plus (tag )
261
281
sidebar_headings .append (
262
- flask .Markup (f'<a href="#{ tag_escaped } ">{ html_escape (heading )} </a>' )
282
+ markupsafe .Markup (
283
+ f'<a href="#{ tag_escaped } ">{ html_escape (heading )} </a>'
284
+ )
263
285
)
286
+
287
+ if skip_to_col is not None :
288
+ line = line [skip_to_col :]
289
+
264
290
is_faq_line = (
265
291
self ._project is VimProject and is_help_txt and RE_LOCAL_ADD .match (line )
266
292
)
@@ -335,7 +361,7 @@ def to_html(self, filename, contents):
335
361
filename = filename ,
336
362
static_dir = static_dir ,
337
363
helptxt = helptxt ,
338
- content = flask .Markup ("" .join (out )),
364
+ content = markupsafe .Markup ("" .join (out )),
339
365
sidebar_headings = sidebar_headings ,
340
366
)
341
367
0 commit comments