Skip to content

Commit

Permalink
Exclude files that match config.exclude_patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
JWCook committed Aug 11, 2023
1 parent 0613f55 commit 7e32d2f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
- Fixed tag labels with spaces
- Fixed tag labels with emoji
- Added normalization for tag URLs to remove/replace urlencoded characters
- Exclude files that match `exclude_patterns` from being tagged
33 changes: 18 additions & 15 deletions src/sphinx_tags/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import os
import re
from fnmatch import fnmatch
from glob import glob
from pathlib import Path
from typing import List

from docutils import nodes
from sphinx.util.docutils import SphinxDirective
from sphinx.util.logging import getLogger
from sphinx.util.matching import get_matching_files
from sphinx.util.rst import textwidth

__version__ = "0.2.2"
Expand Down Expand Up @@ -195,17 +195,16 @@ def create_file(
class Entry:
"""Extracted info from source file (*.rst/*.md/*.ipynb)"""

def __init__(self, entrypath):
self.filepath = Path(entrypath)
with open(self.filepath, "r", encoding="utf8") as f:
self.lines = f.read().split("\n")
if self.filepath.name.endswith(".rst"):
def __init__(self, entrypath: Path):
self.filepath = entrypath
self.lines = self.filepath.read_text(encoding="utf8").split("\n")
if self.filepath.suffix == ".rst":
tagstart = ".. tags::"
tagend = ""
elif self.filepath.name.endswith(".md"):
elif self.filepath.suffix == ".md":
tagstart = "```{tags}"
tagend = "```"
elif self.filepath.name.endswith(".ipynb"):
elif self.filepath.suffix == ".ipynb":
tagstart = '".. tags::'
tagend = '"'
else:
Expand Down Expand Up @@ -294,15 +293,19 @@ def assign_entries(app):
"""Assign all found entries to their tag."""
pages = []
tags = {}
result = []
# glob(pattern, recursive=True) includes the detected srcdir with any
# additional symlinked files/dirs (see issue gh-28)
for extension in app.config.tags_extension:
result.extend(glob(f"{app.srcdir}/**/*.{extension}", recursive=True))
for entrypath in result:
entry = Entry(entrypath)

# 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)
entry.assign_to_tags(tags)
pages.append(entry)

return tags, pages


Expand Down
1 change: 1 addition & 0 deletions test/sources/test-rst/conf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
extensions = ["sphinx_tags"]
tags_create_tags = True
tags_extension = ["rst"]
exclude_patterns = ["excluded"]
3 changes: 3 additions & 0 deletions test/sources/test-rst/excluded/page_4.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Page 4
======
.. tags:: tag_1, tag_5,
2 changes: 0 additions & 2 deletions test/test_general_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
OUTPUT_DIR = OUTPUT_ROOT_DIR / "general"


# TODO: ipynb tests are currently failing because _build/doctrees/nbsphinx/*.ipynb files are
# getting included
def run_all_formats():
"""Return a decorator that runs a test in all supported markup formats"""
return pytest.mark.parametrize(
Expand Down

0 comments on commit 7e32d2f

Please sign in to comment.