1
0
mirror of https://github.com/squidfunk/mkdocs-material.git synced 2025-02-16 02:12:38 +01:00

Fixed crashing tags plugin

This commit is contained in:
squidfunk 2025-01-31 13:10:59 +07:00
parent 34dc4feba7
commit 2d147c72c0
No known key found for this signature in database
GPG Key ID: 5ED40BC4F9C436DF
4 changed files with 2 additions and 122 deletions

View File

@ -36,7 +36,6 @@ from .config import TagsConfig
from .renderer import Renderer from .renderer import Renderer
from .structure.listing.manager import ListingManager from .structure.listing.manager import ListingManager
from .structure.mapping.manager import MappingManager from .structure.mapping.manager import MappingManager
from .structure.mapping.storage import MappingStorage
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Classes # Classes
@ -45,11 +44,6 @@ from .structure.mapping.storage import MappingStorage
class TagsPlugin(BasePlugin[TagsConfig]): class TagsPlugin(BasePlugin[TagsConfig]):
""" """
A tags plugin. 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 supports_multiple_instances = True
@ -129,12 +123,6 @@ class TagsPlugin(BasePlugin[TagsConfig]):
else: else:
config.markdown_extensions.append("attr_list") 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) @event_priority(-50)
def on_page_markdown( def on_page_markdown(
self, markdown: str, *, page: Page, config: MkDocsConfig, **kwargs self, markdown: str, *, page: Page, config: MkDocsConfig, **kwargs
@ -163,10 +151,6 @@ class TagsPlugin(BasePlugin[TagsConfig]):
if self.config.tags_file: if self.config.tags_file:
markdown = self._handle_deprecated_tags_file(page, markdown) 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 # Collect tags from page
try: try:
self.mappings.add(page, markdown) self.mappings.add(page, markdown)
@ -202,15 +186,6 @@ class TagsPlugin(BasePlugin[TagsConfig]):
# Populate and render all listings # Populate and render all listings
self.listings.populate_all(self.mappings, Renderer(env, config)) 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( def on_page_context(
self, context: TemplateContext, *, page: Page, **kwargs self, context: TemplateContext, *, page: Page, **kwargs
) -> None: ) -> None:
@ -268,38 +243,6 @@ class TagsPlugin(BasePlugin[TagsConfig]):
# Return markdown # Return markdown
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 # Data
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------

View File

@ -81,10 +81,7 @@ def populate(listing: Listing, slugify: Slugify) -> dict[Tag, AnchorLink]:
# Filter top-level anchor links and insert them into the page # Filter top-level anchor links and insert them into the page
children = [anchors[tag] for tag in anchors if not tag.parent] children = [anchors[tag] for tag in anchors if not tag.parent]
if listing.config.toc:
host.children[at:at + 1] = children host.children[at:at + 1] = children
else:
host.children.pop(at)
# Return mapping of tags to anchor links # Return mapping of tags to anchor links
return anchors return anchors

View File

@ -36,7 +36,6 @@ from .config import TagsConfig
from .renderer import Renderer from .renderer import Renderer
from .structure.listing.manager import ListingManager from .structure.listing.manager import ListingManager
from .structure.mapping.manager import MappingManager from .structure.mapping.manager import MappingManager
from .structure.mapping.storage import MappingStorage
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Classes # Classes
@ -45,11 +44,6 @@ from .structure.mapping.storage import MappingStorage
class TagsPlugin(BasePlugin[TagsConfig]): class TagsPlugin(BasePlugin[TagsConfig]):
""" """
A tags plugin. 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 supports_multiple_instances = True
@ -129,12 +123,6 @@ class TagsPlugin(BasePlugin[TagsConfig]):
else: else:
config.markdown_extensions.append("attr_list") 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) @event_priority(-50)
def on_page_markdown( def on_page_markdown(
self, markdown: str, *, page: Page, config: MkDocsConfig, **kwargs self, markdown: str, *, page: Page, config: MkDocsConfig, **kwargs
@ -163,10 +151,6 @@ class TagsPlugin(BasePlugin[TagsConfig]):
if self.config.tags_file: if self.config.tags_file:
markdown = self._handle_deprecated_tags_file(page, markdown) 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 # Collect tags from page
try: try:
self.mappings.add(page, markdown) self.mappings.add(page, markdown)
@ -202,15 +186,6 @@ class TagsPlugin(BasePlugin[TagsConfig]):
# Populate and render all listings # Populate and render all listings
self.listings.populate_all(self.mappings, Renderer(env, config)) 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( def on_page_context(
self, context: TemplateContext, *, page: Page, **kwargs self, context: TemplateContext, *, page: Page, **kwargs
) -> None: ) -> None:
@ -268,38 +243,6 @@ class TagsPlugin(BasePlugin[TagsConfig]):
# Return markdown # Return markdown
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 # Data
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------

View File

@ -81,10 +81,7 @@ def populate(listing: Listing, slugify: Slugify) -> dict[Tag, AnchorLink]:
# Filter top-level anchor links and insert them into the page # Filter top-level anchor links and insert them into the page
children = [anchors[tag] for tag in anchors if not tag.parent] children = [anchors[tag] for tag in anchors if not tag.parent]
if listing.config.toc:
host.children[at:at + 1] = children host.children[at:at + 1] = children
else:
host.children.pop(at)
# Return mapping of tags to anchor links # Return mapping of tags to anchor links
return anchors return anchors