Skip to content

Commit

Permalink
rebased on top of #89
Browse files Browse the repository at this point in the history
  • Loading branch information
story645 committed Dec 22, 2023
1 parent 2e7ec6e commit 2c2b829
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 52 deletions.
46 changes: 19 additions & 27 deletions src/sphinx_tags/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
import os
import re
from collections import defaultdict
from fnmatch import fnmatch
from pathlib import Path
from typing import List
Expand Down Expand Up @@ -32,29 +31,26 @@ class TagLinks(SphinxDirective):
# Sphinx directive class attributes
required_arguments = 0
optional_arguments = 200 # Arbitrary.
final_argument_whitespace = True
has_content = True

final_argument_whitespace = True
# Custom attributes
separator = ","

def run(self):

# Undo splitting args by whitespace, and use our own separator (to support tags with spaces)
if not (self.arguments or self.content):
raise ExtensionError(
"Tags must be passed in either as an argument or in the body of the tag."
)
tagline = []

tagline = []
if self.arguments:
tagline.extend((" ".join(self.arguments)).split(self.separator))

tagline.extend(self.arguments)
if self.content:
tagline.extend((" ".join(self.content)).split(self.separator))
# normalize white space and remove "\n"
tagline.extend((" ".join(self.content)).strip().split())

tags = [tag.strip() for tag in (" ".join(tagline)).split(self.separator)]

tags = [tag.strip() for tag in tagline]

tag_dir = Path(self.env.app.srcdir) / self.env.app.config.tags_output_dir
result = nodes.paragraph()
result["classes"] = ["tags"]
Expand All @@ -63,7 +59,6 @@ def run(self):

current_doc_dir = Path(self.env.doc2path(self.env.docname)).parent
relative_tag_dir = Path(os.path.relpath(tag_dir, current_doc_dir))

for tag in tags:
count += 1
# We want the link to be the path to the _tags folder, relative to
Expand All @@ -75,8 +70,6 @@ def run(self):
# |
# - current_doc_path

file_basename = _normalize_tag(tag)

file_basename = _normalize_tag(tag)
print(f"file_base: {file_basename} tag: {tag}")
if self.env.app.config.tags_create_badges:
Expand Down Expand Up @@ -179,7 +172,10 @@ def create_file(
"""
# Get sorted file paths for tag pages, relative to /docs/_tags
tag_page_paths = sorted(i.relpath(srcdir) for i in items)
tag_page_paths = sorted(
[i.relpath(srcdir) for i in items], key=lambda p: Path(p).stem
)
print(f"{tag_page_paths}")

content = []
if "md" in extension:
Expand Down Expand Up @@ -299,17 +295,16 @@ def assign_entries(app):
pages = []
tags = {}

# Get document paths in the project that match specified file extensions
doc_paths = get_matching_files(
app.srcdir,
include_patterns=[f"**.{extension}" for extension in app.config.tags_extension],
exclude_patterns=app.config.exclude_patterns,
)

for path in doc_paths:
entry = Entry(Path(app.srcdir) / path)
for docname in app.env.found_docs:
doctags = app.env.metadata[docname].get("tags", None)
if doctags is None:
continue # skip if no tags
print(f"{docname}: {doctags}")
entry = Entry(app.env.doc2path(docname), doctags)
entry.assign_to_tags(tags)
pages.append(entry)

print(f"entry tags: {tags}, {pages}")
return tags, pages


Expand Down Expand Up @@ -378,9 +373,6 @@ def setup(app):
"html",
)

# reset Entry list cache:
Entry.page_tags = defaultdict(list)

# Update tags
# TODO: tags should be updated after sphinx-gallery is generated, and the
# gallery is also connected to builder-inited. Are there situations when
Expand Down
4 changes: 2 additions & 2 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def create_symlinks():
symlink_dest_dir = SOURCE_ROOT_DIR / "test-symlink"

for file in SOURCE_ROOT_DIR.glob("test-rst/*.rst"):
(symlink_dest_dir / file.name).symlink_to(file)
symlink(file, symlink_dest_dir / file.name)
for dir in SOURCE_ROOT_DIR.glob("test-rst/subdir*"):
(symlink_dest_dir / dir.name).symlink_to(dir, target_is_directory=True)
symlink(dir, symlink_dest_dir / dir.name, target_is_directory=True)

yield

Expand Down
6 changes: 1 addition & 5 deletions test/outputs/general/_tags/tagsindex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ Tags overview
Tags
^^^^

<<<<<<< HEAD
* [{(tag 4)}] (1)
=======
* [{(tag 4)}] (2)
>>>>>>> 0afce42 (doc: allowing tags in body)
* [{(tag 4)}] (2)

* tag 3 (3)

Expand Down
2 changes: 1 addition & 1 deletion test/outputs/general/page_5.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Page 5
******

Tags: tag_1, tag_5, tag2, tag 3, [{(tag 4)}]
Tags: tag_1, tag_5, tag2, tag 3, [{(tag 4)}]
37 changes: 20 additions & 17 deletions test/test_general_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def run_all_formats():

@run_all_formats()
def test_build(app: SphinxTestApp, status: StringIO, warning: StringIO):
app.build()
app.build(force_all=True)
assert "build succeeded" in status.getvalue()

# Build with notebooks and text output results in spurious errors "File Not Found: _tags/*.html"
Expand All @@ -43,37 +43,39 @@ def test_build(app: SphinxTestApp, status: StringIO, warning: StringIO):
@pytest.mark.sphinx(confoverrides={"tags_create_tags": True})
@run_all_formats()
def test_index(app: SphinxTestApp):
app.build()
app.build(force_all=True)
build_dir = Path(app.srcdir) / "_build" / "text"
print(app.srcdir)
# Check tags index page
contents = (build_dir / "_tags" / "tagsindex.txt").read_text()
expected_contents = (OUTPUT_DIR / "_tags" / "tagsindex.txt").read_text()

assert contents == expected_contents
contents = build_dir / "_tags" / "tagsindex.txt"
expected_contents = OUTPUT_DIR / "_tags" / "tagsindex.txt"
with open(contents, "r") as actual, open(expected_contents, "r") as expected:
assert list(actual) == list(expected)

# Check full toctree created by index
contents = (build_dir / "index.txt").read_text()
expected_contents = (OUTPUT_DIR / "index.txt").read_text()
assert contents == expected_contents
contents = build_dir / "index.txt"
expected_contents = OUTPUT_DIR / "index.txt"
with open(contents, "r") as actual, open(expected_contents, "r") as expected:
assert list(actual) == list(expected)


@pytest.mark.sphinx(confoverrides={"tags_create_tags": True})
@run_all_formats()
def test_tag_pages(app: SphinxTestApp):
app.build()
app.build(force_all=True)
build_dir = Path(app.srcdir) / "_build" / "text"

# Check all expected tag pages
for tag in ["tag_1", "tag2", "tag-3", "tag-4", "tag_5", "test-tag-please-ignore"]:
contents = (build_dir / "_tags" / f"{tag}.txt").read_text()
expected_contents = (OUTPUT_DIR / "_tags" / f"{tag}.txt").read_text()
assert contents == expected_contents
contents = build_dir / "_tags" / f"{tag}.txt"
expected_contents = OUTPUT_DIR / "_tags" / f"{tag}.txt"
with open(contents, "r") as actual, open(expected_contents, "r") as expected:
assert list(actual) == list(expected)


@run_all_formats()
def test_tagged_pages(app: SphinxTestApp):
app.build()
app.build(force_all=True)
build_dir = Path(app.srcdir) / "_build" / "text"

# Check all expected tag pages
Expand All @@ -83,9 +85,10 @@ def test_tagged_pages(app: SphinxTestApp):
Path("page_5.txt"),
Path("subdir") / "page_3.txt",
]:
contents = (build_dir / page).read_text()
expected_contents = (OUTPUT_DIR / page).read_text()
assert contents == expected_contents
contents = build_dir / page
expected_contents = OUTPUT_DIR / page
with open(contents, "r") as actual, open(expected_contents, "r") as expected:
assert list(actual) == list(expected)


def test_empty_taglinks():
Expand Down

0 comments on commit 2c2b829

Please sign in to comment.