Skip to content

Commit f616f9b

Browse files
committed
Fix tags with extra leading/trailing/consecutive whitespace
1 parent c8d0119 commit f616f9b

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/sphinx_tags/__init__.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
from sphinx.errors import ExtensionError
1313
from sphinx.util.docutils import SphinxDirective
1414
from sphinx.util.logging import getLogger
15-
from sphinx.util.rst import textwidth
1615
from 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(
@@ -274,7 +277,7 @@ def __init__(self, entrypath: Path):
274277

275278
self.tags = []
276279
if tagblock:
277-
self.tags = [tag.strip().rstrip('"') for tag in tagblock if tag != ""]
280+
self.tags = [_normalize_display_tag(tag).rstrip('"') for tag in tagblock if tag != ""]
278281

279282
def assign_to_tags(self, tag_dict):
280283
"""Append ourself to tags"""
@@ -300,6 +303,14 @@ def _normalize_tag(tag: str, dashes: bool = False) -> str:
300303
return re.sub(r"[\s\W]+", char, tag).lower().strip(char)
301304

302305

306+
def _normalize_display_tag(tag: str) -> str:
307+
"""Strip extra whitespace from a tag name for display purposes.
308+
309+
Example: ' Tag:with (extra whitespace) ' -> 'Tag:with (extra whitespace)'
310+
"""
311+
return re.sub(r"\s+", " ", tag.strip('"').strip())
312+
313+
303314
def tagpage(tags, outdir, title, extension, tags_index_head):
304315
"""Creates Tag overview page.
305316

0 commit comments

Comments
 (0)