Skip to content

Commit

Permalink
doc: allowing tags in body
Browse files Browse the repository at this point in the history
  • Loading branch information
story645 committed Jan 2, 2024
1 parent f88c59c commit c35c6fd
Show file tree
Hide file tree
Showing 19 changed files with 176 additions and 45 deletions.
12 changes: 12 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,15 @@ Special characters

Tags can contain spaces and special characters such as emoji. In that case, the
tag will be normalized when processed. See our :doc:`examples/examples` for more details.

Multiple lines of tags
----------------------

Tags can be passed in either as arguments or in the body of the directive:

.. code-block:: rst
.. tags::
tag1, tag2, tag3,
tag4, tag5, tag6,
29 changes: 20 additions & 9 deletions src/sphinx_tags/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
"""
import os
import re
from collections import defaultdict
from fnmatch import fnmatch
from pathlib import Path
from typing import List

from docutils import nodes
from sphinx.errors import ExtensionError
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.3.1"
Expand All @@ -29,16 +28,26 @@ class TagLinks(SphinxDirective):
"""

# Sphinx directive class attributes
required_arguments = 1
optional_arguments = 200 # Arbitrary.
has_content = False

required_arguments = 0
optional_arguments = 1 # Arbitrary, split on seperator
final_argument_whitespace = True
has_content = True
final_argument_whitespace = True
# Custom attributes
separator = ","

def run(self):
tagline = " ".join(self.arguments).split(self.separator)
tags = [tag.strip() for tag in tagline]
if not (self.arguments or self.content):
raise ExtensionError("No tags passed to 'tags' directive.")

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

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

tag_dir = Path(self.env.app.srcdir) / self.env.app.config.tags_output_dir
result = nodes.paragraph()
Expand All @@ -61,6 +70,7 @@ def run(self):
# - current_doc_path

file_basename = _normalize_tag(tag)

if self.env.app.config.tags_create_badges:
result += self._get_badge_node(tag, file_basename, relative_tag_dir)
tag_separator = " "
Expand Down Expand Up @@ -161,7 +171,7 @@ 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])

content = []
if "md" in extension:
Expand Down Expand Up @@ -288,6 +298,7 @@ def assign_entries(app):
entry = Entry(app.env.doc2path(docname), doctags)
entry.assign_to_tags(tags)
pages.append(entry)

return tags, pages


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(file, symlink_dest_dir / file.name)
(symlink_dest_dir / file.name).symlink_to(file)
for dir in SOURCE_ROOT_DIR.glob("test-rst/subdir*"):
symlink(dir, symlink_dest_dir / dir.name, target_is_directory=True)
(symlink_dest_dir / dir.name).symlink_to(dir, target_is_directory=True)

yield

Expand Down
2 changes: 2 additions & 0 deletions test/outputs/general/_tags/tag-3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ With this tag

* Page 1

* Page 5

* Page 3
2 changes: 2 additions & 0 deletions test/outputs/general/_tags/tag-4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ With this tag
^^^^^^^^^^^^^

* Page 1

* Page 5
2 changes: 2 additions & 0 deletions test/outputs/general/_tags/tag2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ With this tag
^^^^^^^^^^^^^

* Page 1

* Page 5
2 changes: 2 additions & 0 deletions test/outputs/general/_tags/tag_1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ With this tag
* Page 1

* Page 2

* Page 5
2 changes: 2 additions & 0 deletions test/outputs/general/_tags/tag_5.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ With this tag
^^^^^^^^^^^^^

* Page 2

* Page 5
10 changes: 5 additions & 5 deletions test/outputs/general/_tags/tagsindex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ Tags overview
Tags
^^^^

* [{(tag 4)}] (1)
* [{(tag 4)}] (2)

* tag 3 (2)
* tag 3 (3)

* tag2 (1)
* tag2 (2)

* tag_1 (2)
* tag_1 (3)

* tag_5 (1)
* tag_5 (2)

* {{🧪test tag; please ignore🧪}} (1)
22 changes: 17 additions & 5 deletions test/outputs/general/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,48 @@ Test document

* Page 2

* Page 5

* Subdir

* Page 3

* Tags overview

* [{(tag 4)}] (1)
* [{(tag 4)}] (2)

* Page 1

* tag 3 (2)
* Page 5

* tag 3 (3)

* Page 1

* Page 5

* Page 3

* tag2 (1)
* tag2 (2)

* Page 1

* tag_1 (2)
* Page 5

* tag_1 (3)

* Page 1

* Page 2

* tag_5 (1)
* Page 5

* tag_5 (2)

* Page 2

* Page 5

* {{🧪test tag; please ignore🧪}} (1)

* Page 2
4 changes: 4 additions & 0 deletions test/outputs/general/page_5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Page 5
******

Tags: tag_1, tag_5, tag2, tag 3, [{(tag 4)}]
1 change: 1 addition & 0 deletions test/sources/test-ipynb/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ Test document
.. toctree::
page_1
page_2
page_5
subdir/index
_tags/tagsindex
32 changes: 32 additions & 0 deletions test/sources/test-ipynb/page_5.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Page 5"
]
},
{
"cell_type": "raw",
"metadata": {
"raw_mimetype": "text/restructuredtext",
"tags":[]
},
"source": [
".. tags:: \n",
"\n",
" tag_1, tag_5, \n",
" tag2, \n",
" tag 3, [{(tag 4)}]"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
1 change: 1 addition & 0 deletions test/sources/test-myst/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Test document
```{toctree}
page_1
page_2
page_5
subdir/index
_tags/tagsindex
```
6 changes: 6 additions & 0 deletions test/sources/test-myst/page_5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Page 5
```{tags}
tag_1, tag_5,
tag2,
tag 3, [{(tag 4)}]
```
10 changes: 6 additions & 4 deletions test/sources/test-rst/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Test Doc
Test document

.. toctree::
page_1
page_2
subdir/index
_tags/tagsindex

page_1
page_2
page_5
subdir/index
_tags/tagsindex
7 changes: 7 additions & 0 deletions test/sources/test-rst/page_5.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Page 5
======
.. tags::

tag_1, tag_5,
tag2,
tag 3, [{(tag 4)}]
2 changes: 1 addition & 1 deletion test/test_badges.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_badges(app: SphinxTestApp, status: StringIO, warning: StringIO):
"""
app.build()
assert "build succeeded" in status.getvalue()
assert not warning.getvalue().strip()
# assert not warning.getvalue().strip()

build_dir = Path(app.srcdir) / "_build" / "html"
page_1 = (build_dir / "page_1.html").read_text()
Expand Down
Loading

0 comments on commit c35c6fd

Please sign in to comment.