Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mkdocstrings_handlers/crystal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections.abc import Mapping, Sequence
from typing import Any

from mkdocstrings.handlers.base import BaseHandler
from mkdocstrings import BaseHandler

from . import inventory
from .collector import CrystalCollector
Expand Down
2 changes: 1 addition & 1 deletion mkdocstrings_handlers/crystal/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from functools import cached_property
from typing import TYPE_CHECKING, Any, Callable, TypeVar, cast

from mkdocstrings.handlers.base import BaseHandler, CollectionError
from mkdocstrings import BaseHandler, CollectionError

from . import inventory
from .items import DocConstant, DocItem, DocLocation, DocMapping, DocMethod, DocModule, DocType
Expand Down
2 changes: 1 addition & 1 deletion mkdocstrings_handlers/crystal/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from functools import cached_property
from typing import TYPE_CHECKING, Any, ClassVar, Generic, TypeVar, overload

from mkdocstrings.handlers.base import CollectionError
from mkdocstrings import CollectionError

from . import crystal_html

Expand Down
36 changes: 16 additions & 20 deletions mkdocstrings_handlers/crystal/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,32 @@

import contextlib
import xml.etree.ElementTree as etree
from collections.abc import Mapping
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING

import jinja2
import markdown_callouts
from markdown.treeprocessors import Treeprocessor
from markupsafe import Markup
from mkdocstrings.handlers import base
from mkdocstrings import BaseHandler, CollectionError, HandlerOptions

from . import crystal_html

if TYPE_CHECKING:
from markdown import Markdown

from .items import DocItem, DocPath


class CrystalRenderer(base.BaseHandler):
class CrystalRenderer(BaseHandler):
fallback_theme = "material"

@property
def collector(self):
return self

def render(self, data: DocItem, config: Mapping[str, Any]) -> str:
def render(self, data: DocItem, options: HandlerOptions, *, locale: str | None = None) -> str:
subconfig = {
"show_source_links": True,
"heading_level": 2,
**config,
**options,
}
template = self.env.get_template(data._TEMPLATE)

Expand All @@ -46,21 +43,20 @@ def render(self, data: DocItem, config: Mapping[str, Any]) -> str:
def get_anchors(cls, data: DocItem) -> tuple[str, ...]:
return (data.abs_id,)

def update_env(self, md: Markdown, config: dict) -> None:
super().update_env(md, config)
self._md = md

def update_env(self, config: dict) -> None:
self._pymdownx_hl = None
for ext in md.registeredExtensions:
for ext in self.md.registeredExtensions:
with contextlib.suppress(AttributeError):
self._pymdownx_hl = ext.get_pymdownx_highlighter() # type: ignore[attr-defined]

# Disallow raw HTML.
md.preprocessors.deregister("html_block")
md.inlinePatterns.deregister("html")
self.md.preprocessors.deregister("html_block")
self.md.inlinePatterns.deregister("html")

md.treeprocessors.register(_RefInsertingTreeprocessor(md), "mkdocstrings_crystal_xref", 12)
markdown_callouts.CalloutsExtension().extendMarkdown(md)
self.md.treeprocessors.register(
_RefInsertingTreeprocessor(self.md), "mkdocstrings_crystal_xref", 12
)
markdown_callouts.CalloutsExtension().extendMarkdown(self.md)

self.env.trim_blocks = True
self.env.lstrip_blocks = True
Expand Down Expand Up @@ -94,7 +90,7 @@ def do_reference(self, path: str | DocPath, text: str | None = None) -> str:
return text
try:
ref_obj = self.collector.root.lookup(path)
except base.CollectionError:
except CollectionError:
return text
else:
return Markup('<span data-autorefs-optional="{}">{}</span>').format(
Expand All @@ -104,7 +100,7 @@ def do_reference(self, path: str | DocPath, text: str | None = None) -> str:
def do_convert_markdown_ctx(
self, text: str, context: DocItem, heading_level: int, html_id: str
):
p: _RefInsertingTreeprocessor = self._md.treeprocessors["mkdocstrings_crystal_xref"] # type: ignore[assignment]
p: _RefInsertingTreeprocessor = self.md.treeprocessors["mkdocstrings_crystal_xref"] # type: ignore[assignment]
p.context = context
return super().do_convert_markdown(text, heading_level=heading_level, html_id=html_id)

Expand Down Expand Up @@ -147,7 +143,7 @@ def run(self, root: etree.Element):
assert self.context, "Bug: `CrystalRenderer` should have set the `context` member"
try:
ref_obj = self.context.lookup("".join(el.itertext()))
except base.CollectionError:
except CollectionError:
continue

# Replace the `code` with a new `span` (need to propagate the tail too).
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ classifiers = [
dynamic = ["version"]
requires-python = ">=3.9"
dependencies = [
"mkdocstrings >=0.19.0",
"mkdocstrings >=0.30.0",
"markdown-callouts >=0.1.0",
"mkdocs-autorefs >=0.3.1",
"markupsafe >=1.1.1",
Expand Down