Skip to content

Commit

Permalink
Fixed crashing tags plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
squidfunk committed Jan 31, 2025
1 parent 34dc4fe commit 2d147c7
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 122 deletions.
57 changes: 0 additions & 57 deletions material/plugins/tags/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
from .renderer import Renderer
from .structure.listing.manager import ListingManager
from .structure.mapping.manager import MappingManager
from .structure.mapping.storage import MappingStorage

# -----------------------------------------------------------------------------
# Classes
Expand All @@ -45,11 +44,6 @@
class TagsPlugin(BasePlugin[TagsConfig]):
"""
A tags plugin.
This plugin collects tags from the front matter of pages, and builds a tag
structure from them. The tag structure can be used to render listings on
pages, or to just create a site-wide tags index and export all tags and
mappings to a JSON file for consumption in another project.
"""

supports_multiple_instances = True
Expand Down Expand Up @@ -129,12 +123,6 @@ def on_config(self, config: MkDocsConfig) -> None:
else:
config.markdown_extensions.append("attr_list")

# If the author only wants to extract and export mappings, we allow to
# disable the rendering of all tags and listings with a single setting
if self.config.export_only:
self.config.tags = False
self.config.listings = False

@event_priority(-50)
def on_page_markdown(
self, markdown: str, *, page: Page, config: MkDocsConfig, **kwargs
Expand Down Expand Up @@ -163,10 +151,6 @@ def on_page_markdown(
if self.config.tags_file:
markdown = self._handle_deprecated_tags_file(page, markdown)

# Handle deprecation of `tags_extra_files` setting
if self.config.tags_extra_files:
markdown = self._handle_deprecated_tags_extra_files(page, markdown)

# Collect tags from page
try:
self.mappings.add(page, markdown)
Expand Down Expand Up @@ -202,15 +186,6 @@ def on_env(
# Populate and render all listings
self.listings.populate_all(self.mappings, Renderer(env, config))

# Export mappings to file, if enabled
if self.config.export:
path = os.path.join(config.site_dir, self.config.export_file)
path = os.path.normpath(path)

# Serialize mappings and save to file
storage = MappingStorage(self.config)
storage.save(path, self.mappings)

def on_page_context(
self, context: TemplateContext, *, page: Page, **kwargs
) -> None:
Expand Down Expand Up @@ -268,38 +243,6 @@ def _handle_deprecated_tags_file(
# Return markdown
return markdown

def _handle_deprecated_tags_extra_files(
self, page: Page, markdown: str
) -> str:
"""
Handle deprecation of `tags_extra_files` setting.
Arguments:
page: The page.
"""
directive = self.config.listings_directive
if page.file.src_uri not in self.config.tags_extra_files:
return markdown

# Compute tags to render on page
tags = self.config.tags_extra_files[page.file.src_uri]
if tags:
directive += f" {{ include: [{', '.join(tags)}] }}"

# Try to find the legacy tags marker and replace with directive
if "[TAGS]" in markdown:
markdown = markdown.replace(
"[TAGS]", f"<!-- {directive} -->"
)

# Try to find the directive and add it if not present
pattern = r"<!--\s+{directive}".format(directive = re.escape(directive))
if not re.search(pattern, markdown):
markdown += f"\n<!-- {directive} -->"

# Return markdown
return markdown

# -----------------------------------------------------------------------------
# Data
# -----------------------------------------------------------------------------
Expand Down
5 changes: 1 addition & 4 deletions material/plugins/tags/structure/listing/manager/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ def populate(listing: Listing, slugify: Slugify) -> dict[Tag, AnchorLink]:

# Filter top-level anchor links and insert them into the page
children = [anchors[tag] for tag in anchors if not tag.parent]
if listing.config.toc:
host.children[at:at + 1] = children
else:
host.children.pop(at)
host.children[at:at + 1] = children

# Return mapping of tags to anchor links
return anchors
Expand Down
57 changes: 0 additions & 57 deletions src/plugins/tags/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
from .renderer import Renderer
from .structure.listing.manager import ListingManager
from .structure.mapping.manager import MappingManager
from .structure.mapping.storage import MappingStorage

# -----------------------------------------------------------------------------
# Classes
Expand All @@ -45,11 +44,6 @@
class TagsPlugin(BasePlugin[TagsConfig]):
"""
A tags plugin.
This plugin collects tags from the front matter of pages, and builds a tag
structure from them. The tag structure can be used to render listings on
pages, or to just create a site-wide tags index and export all tags and
mappings to a JSON file for consumption in another project.
"""

supports_multiple_instances = True
Expand Down Expand Up @@ -129,12 +123,6 @@ def on_config(self, config: MkDocsConfig) -> None:
else:
config.markdown_extensions.append("attr_list")

# If the author only wants to extract and export mappings, we allow to
# disable the rendering of all tags and listings with a single setting
if self.config.export_only:
self.config.tags = False
self.config.listings = False

@event_priority(-50)
def on_page_markdown(
self, markdown: str, *, page: Page, config: MkDocsConfig, **kwargs
Expand Down Expand Up @@ -163,10 +151,6 @@ def on_page_markdown(
if self.config.tags_file:
markdown = self._handle_deprecated_tags_file(page, markdown)

# Handle deprecation of `tags_extra_files` setting
if self.config.tags_extra_files:
markdown = self._handle_deprecated_tags_extra_files(page, markdown)

# Collect tags from page
try:
self.mappings.add(page, markdown)
Expand Down Expand Up @@ -202,15 +186,6 @@ def on_env(
# Populate and render all listings
self.listings.populate_all(self.mappings, Renderer(env, config))

# Export mappings to file, if enabled
if self.config.export:
path = os.path.join(config.site_dir, self.config.export_file)
path = os.path.normpath(path)

# Serialize mappings and save to file
storage = MappingStorage(self.config)
storage.save(path, self.mappings)

def on_page_context(
self, context: TemplateContext, *, page: Page, **kwargs
) -> None:
Expand Down Expand Up @@ -268,38 +243,6 @@ def _handle_deprecated_tags_file(
# Return markdown
return markdown

def _handle_deprecated_tags_extra_files(
self, page: Page, markdown: str
) -> str:
"""
Handle deprecation of `tags_extra_files` setting.
Arguments:
page: The page.
"""
directive = self.config.listings_directive
if page.file.src_uri not in self.config.tags_extra_files:
return markdown

# Compute tags to render on page
tags = self.config.tags_extra_files[page.file.src_uri]
if tags:
directive += f" {{ include: [{', '.join(tags)}] }}"

# Try to find the legacy tags marker and replace with directive
if "[TAGS]" in markdown:
markdown = markdown.replace(
"[TAGS]", f"<!-- {directive} -->"
)

# Try to find the directive and add it if not present
pattern = r"<!--\s+{directive}".format(directive = re.escape(directive))
if not re.search(pattern, markdown):
markdown += f"\n<!-- {directive} -->"

# Return markdown
return markdown

# -----------------------------------------------------------------------------
# Data
# -----------------------------------------------------------------------------
Expand Down
5 changes: 1 addition & 4 deletions src/plugins/tags/structure/listing/manager/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ def populate(listing: Listing, slugify: Slugify) -> dict[Tag, AnchorLink]:

# Filter top-level anchor links and insert them into the page
children = [anchors[tag] for tag in anchors if not tag.parent]
if listing.config.toc:
host.children[at:at + 1] = children
else:
host.children.pop(at)
host.children[at:at + 1] = children

# Return mapping of tags to anchor links
return anchors
Expand Down

0 comments on commit 2d147c7

Please sign in to comment.