| |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| from __future__ import annotations |
|
|
| import logging |
|
|
| from material.utilities.filter import FileFilter, FilterConfig |
| from mkdocs.structure.pages import _RelativePathTreeprocessor |
| from markdown import Extension, Markdown |
| from markdown.treeprocessors import Treeprocessor |
| from mkdocs.exceptions import ConfigurationError |
| from urllib.parse import urlparse |
| from xml.etree.ElementTree import Element |
|
|
| |
| |
| |
|
|
| class PreviewProcessor(Treeprocessor): |
| """ |
| A Markdown treeprocessor to enable instant previews on links. |
| |
| Note that this treeprocessor is dependent on the `relpath` treeprocessor |
| registered programmatically by MkDocs before rendering a page. |
| """ |
|
|
| def __init__(self, md: Markdown, config: dict): |
| """ |
| Initialize the treeprocessor. |
| |
| Arguments: |
| md: The Markdown instance. |
| config: The configuration. |
| """ |
| super().__init__(md) |
| self.config = config |
|
|
| def run(self, root: Element): |
| """ |
| Run the treeprocessor. |
| |
| Arguments: |
| root: The root element of the parsed Markdown document. |
| """ |
| at = self.md.treeprocessors.get_index_for_name("relpath") |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| processor = self.md.treeprocessors[at] |
| if not isinstance(processor, _RelativePathTreeprocessor): |
| raise TypeError("Relative path processor not registered") |
|
|
| |
| configurations = self.config["configurations"] |
| configurations.append({ |
| "sources": self.config.get("sources"), |
| "targets": self.config.get("targets") |
| }) |
|
|
| |
| |
| for configuration in configurations: |
|
|
| |
| |
| |
| |
| |
| if ( |
| not configuration.get("sources") and |
| not configuration.get("targets") |
| ): |
| continue |
|
|
| |
| filter = get_filter(configuration, "sources") |
| if not filter(processor.file): |
| continue |
|
|
| |
| filter = get_filter(configuration, "targets") |
| for el in root.iter("a"): |
| href = el.get("href") |
| if not href: |
| continue |
|
|
| |
| if "footnote-ref" in el.get("class", ""): |
| continue |
|
|
| |
| url = urlparse(href) |
| if url.scheme or url.netloc: |
| continue |
|
|
| |
| for path in processor._possible_target_uris( |
| processor.file, url.path, |
| processor.config.use_directory_urls |
| ): |
| target = processor.files.get_file_from_path(path) |
| if not target: |
| continue |
|
|
| |
| if filter(target): |
| el.set("data-preview", "") |
|
|
| |
|
|
| class PreviewExtension(Extension): |
| """ |
| A Markdown extension to enable instant previews on links. |
| |
| This extensions allows to automatically add the `data-preview` attribute to |
| internal links matching specific criteria, so Material for MkDocs renders a |
| nice preview on hover as part of a tooltip. It is the recommended way to |
| add previews to links in a programmatic way. |
| """ |
|
|
| def __init__(self, *args, **kwargs): |
| """ |
| """ |
| self.config = { |
| "configurations": [[], "Filter configurations"], |
| "sources": [{}, "Link sources"], |
| "targets": [{}, "Link targets"] |
| } |
| super().__init__(*args, **kwargs) |
|
|
| def extendMarkdown(self, md: Markdown): |
| """ |
| Register Markdown extension. |
| |
| Arguments: |
| md: The Markdown instance. |
| """ |
| md.registerExtension(self) |
|
|
| |
| |
| |
| |
| processor = PreviewProcessor(md, self.getConfigs()) |
| md.treeprocessors.register(processor, "preview", 0) |
|
|
| |
| |
| |
|
|
| def get_filter(settings: dict, key: str): |
| """ |
| Get file filter from settings. |
| |
| Arguments: |
| settings: The settings. |
| key: The key in the settings. |
| |
| Returns: |
| The file filter. |
| """ |
| config = FilterConfig() |
| config.load_dict(settings.get(key) or {}) |
|
|
| |
| errors, warnings = config.validate() |
| for _, w in warnings: |
| log.warning( |
| f"Error reading filter configuration in '{key}':\n" |
| f"{w}" |
| ) |
| for _, e in errors: |
| raise ConfigurationError( |
| f"Error reading filter configuration in '{key}':\n" |
| f"{e}" |
| ) |
|
|
| |
| return FileFilter(config = config) |
|
|
| def makeExtension(**kwargs): |
| """ |
| Register Markdown extension. |
| |
| Arguments: |
| **kwargs: Configuration options. |
| |
| Returns: |
| The Markdown extension. |
| """ |
| return PreviewExtension(**kwargs) |
|
|
| |
| |
| |
|
|
| |
| log = logging.getLogger("mkdocs.material.extensions.preview") |
|
|