1212from sphinx .errors import ExtensionError
1313from sphinx .util .docutils import SphinxDirective
1414from sphinx .util .logging import getLogger
15- from sphinx .util .rst import textwidth
1615from sphinx .util .matching import get_matching_files
16+ from sphinx .util .rst import textwidth
1717
1818__version__ = "0.4dev"
1919
@@ -60,13 +60,16 @@ def run(self):
6060 # normalize white space and remove "\n"
6161 if self .arguments :
6262 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 ("," )]
6464 )
6565 if self .content :
6666 # self.content: StringList(['different, tags,', 'separated'],
6767 # items=[(path, lineno), (path, lineno)])
6868 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+ ]
7073 )
7174 # Remove empty elements from page_tags
7275 # (can happen after _normalize_tag())
@@ -153,7 +156,7 @@ class Tag:
153156
154157 def __init__ (self , name ):
155158 self .items = []
156- self .name = name
159+ self .name = _normalize_display_tag ( name )
157160 self .file_basename = _normalize_tag (name , dashes = True )
158161
159162 def create_file (
@@ -260,12 +263,13 @@ def __init__(self, entrypath: Path):
260263 tagblock = []
261264 reading = False
262265 for line in self .lines :
266+ line = line .strip ()
263267 if tagstart in line :
264268 reading = True
265269 line = line .split (tagstart )[1 ]
266270 tagblock .extend (line .split ("," ))
267271 else :
268- if reading and line . strip () == tagend :
272+ if reading and line == tagend :
269273 # tagblock now contains at least one tag
270274 if tagblock != ["" ]:
271275 break
@@ -274,7 +278,7 @@ def __init__(self, entrypath: Path):
274278
275279 self .tags = []
276280 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 ]
278282
279283 def assign_to_tags (self , tag_dict ):
280284 """Append ourself to tags"""
@@ -300,6 +304,15 @@ def _normalize_tag(tag: str, dashes: bool = False) -> str:
300304 return re .sub (r"[\s\W]+" , char , tag ).lower ().strip (char )
301305
302306
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+
303316def tagpage (tags , outdir , title , extension , tags_index_head ):
304317 """Creates Tag overview page.
305318
0 commit comments