12
12
from sphinx .errors import ExtensionError
13
13
from sphinx .util .docutils import SphinxDirective
14
14
from sphinx .util .logging import getLogger
15
- from sphinx .util .rst import textwidth
16
15
from sphinx .util .matching import get_matching_files
16
+ from sphinx .util .rst import textwidth
17
17
18
18
__version__ = "0.4dev"
19
19
@@ -60,13 +60,16 @@ def run(self):
60
60
# normalize white space and remove "\n"
61
61
if self .arguments :
62
62
page_tags .extend (
63
- [_normalize_tag (tag ) for tag in self .arguments [0 ].split ("," )]
63
+ [_normalize_display_tag (tag ) for tag in self .arguments [0 ].split ("," )]
64
64
)
65
65
if self .content :
66
66
# self.content: StringList(['different, tags,', 'separated'],
67
67
# items=[(path, lineno), (path, lineno)])
68
68
page_tags .extend (
69
- [_normalize_tag (tag ) for tag in "," .join (self .content ).split ("," )]
69
+ [
70
+ _normalize_display_tag (tag )
71
+ for tag in "," .join (self .content ).split ("," )
72
+ ]
70
73
)
71
74
# Remove empty elements from page_tags
72
75
# (can happen after _normalize_tag())
@@ -153,7 +156,7 @@ class Tag:
153
156
154
157
def __init__ (self , name ):
155
158
self .items = []
156
- self .name = name
159
+ self .name = _normalize_display_tag ( name )
157
160
self .file_basename = _normalize_tag (name , dashes = True )
158
161
159
162
def create_file (
@@ -260,12 +263,13 @@ def __init__(self, entrypath: Path):
260
263
tagblock = []
261
264
reading = False
262
265
for line in self .lines :
266
+ line = line .strip ()
263
267
if tagstart in line :
264
268
reading = True
265
269
line = line .split (tagstart )[1 ]
266
270
tagblock .extend (line .split ("," ))
267
271
else :
268
- if reading and line . strip () == tagend :
272
+ if reading and line == tagend :
269
273
# tagblock now contains at least one tag
270
274
if tagblock != ["" ]:
271
275
break
@@ -274,7 +278,7 @@ def __init__(self, entrypath: Path):
274
278
275
279
self .tags = []
276
280
if tagblock :
277
- self .tags = [tag . strip (). rstrip ( '"' ) for tag in tagblock if tag != "" ]
281
+ self .tags = [_normalize_display_tag ( tag ) for tag in tagblock if tag ]
278
282
279
283
def assign_to_tags (self , tag_dict ):
280
284
"""Append ourself to tags"""
@@ -300,6 +304,15 @@ def _normalize_tag(tag: str, dashes: bool = False) -> str:
300
304
return re .sub (r"[\s\W]+" , char , tag ).lower ().strip (char )
301
305
302
306
307
+ def _normalize_display_tag (tag : str ) -> str :
308
+ """Strip extra whitespace from a tag name for display purposes.
309
+
310
+ Example: ' Tag:with (extra whitespace) ' -> 'Tag:with (extra whitespace)'
311
+ """
312
+ tag = tag .replace ("\\ n" , "\n " ).strip ('"' ).strip ()
313
+ return re .sub (r"\s+" , " " , tag )
314
+
315
+
303
316
def tagpage (tags , outdir , title , extension , tags_index_head ):
304
317
"""Creates Tag overview page.
305
318
0 commit comments