Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for multiline tags #103

Merged
merged 4 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#
import os
import sys

from sphinx_tags import __version__

sys.path.insert(0, os.path.abspath("../src"))
Expand All @@ -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"]
Expand Down
25 changes: 19 additions & 6 deletions src/sphinx_tags/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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"""
Expand All @@ -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.

Expand Down
Loading