Skip to content

Commit

Permalink
Added customized intro_text.
Browse files Browse the repository at this point in the history
Also removes files from tag folder before (re)creating them.
  • Loading branch information
nebelgrau77 authored and melissawm committed Oct 9, 2022
1 parent 9a55042 commit c468d50
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ Version 0.1 (August 23, 2022)
Version 0.1.6 (October 7, 2022)

- Added page title and header as a parameter
- Alphabetic sorting of tags
- Alphabetic sorting of tags

Version 0.1.7 (October 8, 2022)

- Added removal of all .md and .rst files in the tag folder before generating them again (avoids having duplicates after removing/changing some tag)
- Tag intro text as a parameter
3 changes: 2 additions & 1 deletion docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ help:

clean:
rm -rf _build/
rm -rf _tags

# rm -rf _tags

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
Expand Down
3 changes: 3 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ A few custom configuration keys can be used in your ``conf.py`` file.
- ``tags_index_head``
- The string used as caption in the tagsindex file.
Default: ``Tags``
- ``tags_intro_text``
- The string used on pages that have tags.
Default: ``Tags``

Tags overview page
------------------
Expand Down
29 changes: 21 additions & 8 deletions src/sphinx_tags/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from docutils import nodes
from pathlib import Path

__version__ = "0.1.6"
__version__ = "0.1.7"

logger = getLogger("sphinx-tags")

Expand All @@ -28,14 +28,14 @@ class TagLinks(SphinxDirective):

# Custom attributes
separator = ","
intro_text = "In categories: "


def run(self):
tags = [arg.replace(self.separator, "") for arg in self.arguments]
result = nodes.paragraph()
result["classes"] = ["tags"]
result += nodes.inline(text=self.intro_text)
result["classes"] = ["tags"]
result += nodes.inline(text=self.env.app.config.tags_intro_text)
count = 0

for tag in tags:
count += 1
# We want the link to be the path to the _tags folder, relative to this document's path
Expand All @@ -47,10 +47,12 @@ def run(self):
# |
# - current_doc_path
docpath = Path(self.env.doc2path(self.env.docname)).parent

rootdir = os.path.relpath(
os.path.join(self.env.app.srcdir, self.env.app.config.tags_output_dir),
docpath,
)

link = os.path.join(rootdir, f"{tag}.html")
tag_node = nodes.reference(refuri=link, text=tag)
result += tag_node
Expand Down Expand Up @@ -87,9 +89,11 @@ def create_file(self, items, extension, tags_output_dir, srcdir, tags_page_title
srcdir : str
root folder for the documentation (usually, project/docs)
tags_page_title: str
the title of the tag page, after which the tag is listed (e.g. "Tag: ...")
the title of the tag page, after which the tag is listed (e.g. "Tag: programming")
tags_page_header: str
the words after which the pages with the tag are listed, e.g. "With this tag")
the words after which the pages with the tag are listed, e.g. "With this tag: Hello World")
tag_intro_text: str
the words after which the tags of a given page are listed, e.g. "Tags: programming, python")
"""
Expand All @@ -99,7 +103,7 @@ def create_file(self, items, extension, tags_output_dir, srcdir, tags_page_title
content.append(f"# {tags_page_title}: {self.name}")
content.append("")
content.append("```{toctree}")
content.append("--------")
content.append("---")
content.append("maxdepth: 1")
content.append(f"caption: {tags_page_header}")
content.append("---")
Expand Down Expand Up @@ -225,10 +229,16 @@ def assign_entries(app):
def update_tags(app):
"""Update tags according to pages found"""
if app.config.tags_create_tags:

tags_output_dir = Path(app.config.tags_output_dir)

if not os.path.exists(os.path.join(app.srcdir, tags_output_dir)):
os.makedirs(os.path.join(app.srcdir, tags_output_dir))

for file in os.listdir(os.path.join(app.srcdir, tags_output_dir)):
if file.endswith('md') or file.endswith('rst'):
os.remove(os.path.join(app.srcdir, tags_output_dir, file))

# Create pages for each tag
tags, pages = assign_entries(app)
for tag in tags.values():
Expand Down Expand Up @@ -265,6 +275,7 @@ def setup(app):
app.add_config_value("tags_output_dir", "_tags", "html")
app.add_config_value("tags_overview_title", "Tags overview", "html")
app.add_config_value("tags_extension", ["rst"], "html")
app.add_config_value("tags_intro_text", "Tags: ", "html")
app.add_config_value("tags_page_title", "My tags", "html")
app.add_config_value("tags_page_header", "With this tag", "html")
app.add_config_value("tags_index_head", "Tags", "html")
Expand All @@ -284,6 +295,8 @@ def setup(app):
app.connect("builder-inited", update_tags)
app.add_directive("tags", TagLinks)



return {
"version": __version__,
"parallel_read_safe": True,
Expand Down

0 comments on commit c468d50

Please sign in to comment.