Skip to content

Commit 8d03d56

Browse files
authored
Merge pull request #103 from JWCook/multiline-fix
Fixes for multiline tags
2 parents c8d0119 + 7f82671 commit 8d03d56

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

docs/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#
1313
import os
1414
import sys
15+
1516
from sphinx_tags import __version__
1617

1718
sys.path.insert(0, os.path.abspath("../src"))
@@ -35,7 +36,7 @@
3536
extensions = ["sphinx_design", "sphinx_tags", "nbsphinx", "myst_parser"]
3637

3738
tags_create_tags = True
38-
tags_create_badges = False
39+
tags_create_badges = True
3940
# tags_output_dir = "_tags" # default
4041
tags_overview_title = "All tags" # default: "Tags overview"
4142
tags_extension = ["rst", "md", "ipynb"] # default: ["rst"]

src/sphinx_tags/__init__.py

Lines changed: 19 additions & 6 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(
@@ -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+
303316
def tagpage(tags, outdir, title, extension, tags_index_head):
304317
"""Creates Tag overview page.
305318

0 commit comments

Comments
 (0)