Skip to content

Commit 2d147c7

Browse files
committed
Fixed crashing tags plugin
1 parent 34dc4fe commit 2d147c7

File tree

4 files changed

+2
-122
lines changed

4 files changed

+2
-122
lines changed

material/plugins/tags/plugin.py

-57
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
from .renderer import Renderer
3737
from .structure.listing.manager import ListingManager
3838
from .structure.mapping.manager import MappingManager
39-
from .structure.mapping.storage import MappingStorage
4039

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

5549
supports_multiple_instances = True
@@ -129,12 +123,6 @@ def on_config(self, config: MkDocsConfig) -> None:
129123
else:
130124
config.markdown_extensions.append("attr_list")
131125

132-
# If the author only wants to extract and export mappings, we allow to
133-
# disable the rendering of all tags and listings with a single setting
134-
if self.config.export_only:
135-
self.config.tags = False
136-
self.config.listings = False
137-
138126
@event_priority(-50)
139127
def on_page_markdown(
140128
self, markdown: str, *, page: Page, config: MkDocsConfig, **kwargs
@@ -163,10 +151,6 @@ def on_page_markdown(
163151
if self.config.tags_file:
164152
markdown = self._handle_deprecated_tags_file(page, markdown)
165153

166-
# Handle deprecation of `tags_extra_files` setting
167-
if self.config.tags_extra_files:
168-
markdown = self._handle_deprecated_tags_extra_files(page, markdown)
169-
170154
# Collect tags from page
171155
try:
172156
self.mappings.add(page, markdown)
@@ -202,15 +186,6 @@ def on_env(
202186
# Populate and render all listings
203187
self.listings.populate_all(self.mappings, Renderer(env, config))
204188

205-
# Export mappings to file, if enabled
206-
if self.config.export:
207-
path = os.path.join(config.site_dir, self.config.export_file)
208-
path = os.path.normpath(path)
209-
210-
# Serialize mappings and save to file
211-
storage = MappingStorage(self.config)
212-
storage.save(path, self.mappings)
213-
214189
def on_page_context(
215190
self, context: TemplateContext, *, page: Page, **kwargs
216191
) -> None:
@@ -268,38 +243,6 @@ def _handle_deprecated_tags_file(
268243
# Return markdown
269244
return markdown
270245

271-
def _handle_deprecated_tags_extra_files(
272-
self, page: Page, markdown: str
273-
) -> str:
274-
"""
275-
Handle deprecation of `tags_extra_files` setting.
276-
277-
Arguments:
278-
page: The page.
279-
"""
280-
directive = self.config.listings_directive
281-
if page.file.src_uri not in self.config.tags_extra_files:
282-
return markdown
283-
284-
# Compute tags to render on page
285-
tags = self.config.tags_extra_files[page.file.src_uri]
286-
if tags:
287-
directive += f" {{ include: [{', '.join(tags)}] }}"
288-
289-
# Try to find the legacy tags marker and replace with directive
290-
if "[TAGS]" in markdown:
291-
markdown = markdown.replace(
292-
"[TAGS]", f"<!-- {directive} -->"
293-
)
294-
295-
# Try to find the directive and add it if not present
296-
pattern = r"<!--\s+{directive}".format(directive = re.escape(directive))
297-
if not re.search(pattern, markdown):
298-
markdown += f"\n<!-- {directive} -->"
299-
300-
# Return markdown
301-
return markdown
302-
303246
# -----------------------------------------------------------------------------
304247
# Data
305248
# -----------------------------------------------------------------------------

material/plugins/tags/structure/listing/manager/toc.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ def populate(listing: Listing, slugify: Slugify) -> dict[Tag, AnchorLink]:
8181

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

8986
# Return mapping of tags to anchor links
9087
return anchors

src/plugins/tags/plugin.py

-57
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
from .renderer import Renderer
3737
from .structure.listing.manager import ListingManager
3838
from .structure.mapping.manager import MappingManager
39-
from .structure.mapping.storage import MappingStorage
4039

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

5549
supports_multiple_instances = True
@@ -129,12 +123,6 @@ def on_config(self, config: MkDocsConfig) -> None:
129123
else:
130124
config.markdown_extensions.append("attr_list")
131125

132-
# If the author only wants to extract and export mappings, we allow to
133-
# disable the rendering of all tags and listings with a single setting
134-
if self.config.export_only:
135-
self.config.tags = False
136-
self.config.listings = False
137-
138126
@event_priority(-50)
139127
def on_page_markdown(
140128
self, markdown: str, *, page: Page, config: MkDocsConfig, **kwargs
@@ -163,10 +151,6 @@ def on_page_markdown(
163151
if self.config.tags_file:
164152
markdown = self._handle_deprecated_tags_file(page, markdown)
165153

166-
# Handle deprecation of `tags_extra_files` setting
167-
if self.config.tags_extra_files:
168-
markdown = self._handle_deprecated_tags_extra_files(page, markdown)
169-
170154
# Collect tags from page
171155
try:
172156
self.mappings.add(page, markdown)
@@ -202,15 +186,6 @@ def on_env(
202186
# Populate and render all listings
203187
self.listings.populate_all(self.mappings, Renderer(env, config))
204188

205-
# Export mappings to file, if enabled
206-
if self.config.export:
207-
path = os.path.join(config.site_dir, self.config.export_file)
208-
path = os.path.normpath(path)
209-
210-
# Serialize mappings and save to file
211-
storage = MappingStorage(self.config)
212-
storage.save(path, self.mappings)
213-
214189
def on_page_context(
215190
self, context: TemplateContext, *, page: Page, **kwargs
216191
) -> None:
@@ -268,38 +243,6 @@ def _handle_deprecated_tags_file(
268243
# Return markdown
269244
return markdown
270245

271-
def _handle_deprecated_tags_extra_files(
272-
self, page: Page, markdown: str
273-
) -> str:
274-
"""
275-
Handle deprecation of `tags_extra_files` setting.
276-
277-
Arguments:
278-
page: The page.
279-
"""
280-
directive = self.config.listings_directive
281-
if page.file.src_uri not in self.config.tags_extra_files:
282-
return markdown
283-
284-
# Compute tags to render on page
285-
tags = self.config.tags_extra_files[page.file.src_uri]
286-
if tags:
287-
directive += f" {{ include: [{', '.join(tags)}] }}"
288-
289-
# Try to find the legacy tags marker and replace with directive
290-
if "[TAGS]" in markdown:
291-
markdown = markdown.replace(
292-
"[TAGS]", f"<!-- {directive} -->"
293-
)
294-
295-
# Try to find the directive and add it if not present
296-
pattern = r"<!--\s+{directive}".format(directive = re.escape(directive))
297-
if not re.search(pattern, markdown):
298-
markdown += f"\n<!-- {directive} -->"
299-
300-
# Return markdown
301-
return markdown
302-
303246
# -----------------------------------------------------------------------------
304247
# Data
305248
# -----------------------------------------------------------------------------

src/plugins/tags/structure/listing/manager/toc.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ def populate(listing: Listing, slugify: Slugify) -> dict[Tag, AnchorLink]:
8181

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

8986
# Return mapping of tags to anchor links
9087
return anchors

0 commit comments

Comments
 (0)