diff --git a/docs/conf.py b/docs/conf.py index e04d61f..79a769c 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -12,6 +12,7 @@ # import os import sys + from sphinx_tags import __version__ sys.path.insert(0, os.path.abspath("../src")) @@ -35,7 +36,7 @@ extensions = ["sphinx_design", "sphinx_tags", "nbsphinx", "myst_parser"] tags_create_tags = True -tags_create_badges = False +tags_create_badges = True # tags_output_dir = "_tags" # default tags_overview_title = "All tags" # default: "Tags overview" tags_extension = ["rst", "md", "ipynb"] # default: ["rst"] diff --git a/src/sphinx_tags/__init__.py b/src/sphinx_tags/__init__.py index 4993653..7bdff08 100644 --- a/src/sphinx_tags/__init__.py +++ b/src/sphinx_tags/__init__.py @@ -12,8 +12,8 @@ from sphinx.errors import ExtensionError from sphinx.util.docutils import SphinxDirective from sphinx.util.logging import getLogger -from sphinx.util.rst import textwidth from sphinx.util.matching import get_matching_files +from sphinx.util.rst import textwidth __version__ = "0.4dev" @@ -60,13 +60,16 @@ def run(self): # normalize white space and remove "\n" if self.arguments: page_tags.extend( - [_normalize_tag(tag) for tag in self.arguments[0].split(",")] + [_normalize_display_tag(tag) for tag in self.arguments[0].split(",")] ) if self.content: # self.content: StringList(['different, tags,', 'separated'], # items=[(path, lineno), (path, lineno)]) page_tags.extend( - [_normalize_tag(tag) for tag in ",".join(self.content).split(",")] + [ + _normalize_display_tag(tag) + for tag in ",".join(self.content).split(",") + ] ) # Remove empty elements from page_tags # (can happen after _normalize_tag()) @@ -153,7 +156,7 @@ class Tag: def __init__(self, name): self.items = [] - self.name = name + self.name = _normalize_display_tag(name) self.file_basename = _normalize_tag(name, dashes=True) def create_file( @@ -260,12 +263,13 @@ def __init__(self, entrypath: Path): tagblock = [] reading = False for line in self.lines: + line = line.strip() if tagstart in line: reading = True line = line.split(tagstart)[1] tagblock.extend(line.split(",")) else: - if reading and line.strip() == tagend: + if reading and line == tagend: # tagblock now contains at least one tag if tagblock != [""]: break @@ -274,7 +278,7 @@ def __init__(self, entrypath: Path): self.tags = [] if tagblock: - self.tags = [tag.strip().rstrip('"') for tag in tagblock if tag != ""] + self.tags = [_normalize_display_tag(tag) for tag in tagblock if tag] def assign_to_tags(self, tag_dict): """Append ourself to tags""" @@ -300,6 +304,15 @@ def _normalize_tag(tag: str, dashes: bool = False) -> str: return re.sub(r"[\s\W]+", char, tag).lower().strip(char) +def _normalize_display_tag(tag: str) -> str: + """Strip extra whitespace from a tag name for display purposes. + + Example: ' Tag:with (extra whitespace) ' -> 'Tag:with (extra whitespace)' + """ + tag = tag.replace("\\n", "\n").strip('"').strip() + return re.sub(r"\s+", " ", tag) + + def tagpage(tags, outdir, title, extension, tags_index_head): """Creates Tag overview page.