|
| 1 | +import logging |
| 2 | + |
| 3 | +from mkdocs.plugins import BasePlugin |
| 4 | +from mkdocs.exceptions import BuildError |
| 5 | +from mkdocs.utils.meta import YAML_RE |
| 6 | +import yaml |
| 7 | +try: |
| 8 | + from yaml import CSafeLoader as SafeLoader |
| 9 | +except ImportError: # pragma: no cover |
| 10 | + from yaml import SafeLoader |
| 11 | + |
| 12 | +from eva_contrib_builder.schemas import EasyPageMeta |
| 13 | + |
| 14 | + |
| 15 | +log = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +# TODO: Decide if it's even helpful |
| 20 | +# import click |
| 21 | +# from mkdocs.__main__ import ColorFormatter |
| 22 | +# mkdocs_logger = logging.getLogger("mkdocs") |
| 23 | +# class GHActionsColorFormatter(ColorFormatter): |
| 24 | + |
| 25 | +# PREFIX_LEVELS = { |
| 26 | +# "WARNING": "::error::", |
| 27 | +# "ERROR": "::error::", |
| 28 | +# } |
| 29 | + |
| 30 | +# def format(self, record): |
| 31 | +# message = super().format(record) |
| 32 | +# prefix = "" |
| 33 | +# if record.levelname in self.PREFIX_LEVELS: |
| 34 | +# prefix = self.PREFIX_LEVELS[record.levelname] |
| 35 | +# return prefix + message |
| 36 | +# mkdocs_logger.handlers[0].setFormatter(GHActionsColorFormatter()) |
| 37 | + |
| 38 | +class EVAContribPlugin(BasePlugin): |
| 39 | + |
| 40 | + def on_config(self, config): |
| 41 | + self.env = config['theme'].get_env() |
| 42 | + self.env.filters["yes_no"] = self.yes_no |
| 43 | + |
| 44 | + @staticmethod |
| 45 | + def yes_no(value: bool): |
| 46 | + return "yes" if value else "no" |
| 47 | + |
| 48 | + def _get_markdown_context(self, page, config): |
| 49 | + page.eva = None |
| 50 | + if "eva" in page.meta: |
| 51 | + page.eva = EasyPageMeta(**page.meta["eva"]) |
| 52 | + |
| 53 | + return { |
| 54 | + "eva": page.eva, |
| 55 | + } |
| 56 | + |
| 57 | + def on_page_context(self, context, page, config, nav): |
| 58 | + context = {**self._get_markdown_context(page, config), **context} |
| 59 | + return context |
| 60 | + |
| 61 | + def on_page_read_source(self, page, config): |
| 62 | + # Mkdocs hides YAML parsing errors, to access those I need to load the |
| 63 | + # source and do an initial YAML load on my own, it get properly loded |
| 64 | + # again by mkdocs |
| 65 | + try: |
| 66 | + with open(page.file.abs_src_path, 'r', encoding='utf-8-sig', errors='strict') as f: |
| 67 | + source = f.read() |
| 68 | + except OSError: |
| 69 | + log.error(f'File not found: {page.file.src_path}') |
| 70 | + raise |
| 71 | + except ValueError: |
| 72 | + log.error(f'Encoding error reading file: {page.file.src_path}') |
| 73 | + raise |
| 74 | + |
| 75 | + m = YAML_RE.match(source) |
| 76 | + if m: |
| 77 | + try: |
| 78 | + data = yaml.load(m.group(1), SafeLoader) |
| 79 | + if not isinstance(data, dict): |
| 80 | + data = {} |
| 81 | + except Exception as exc: |
| 82 | + raise BuildError(f"Page's YAML metadata is malformed: {exc}") |
| 83 | + page.meta = data |
| 84 | + return source |
| 85 | + |
| 86 | + def on_page_markdown(self, markdown, page, config, files): |
| 87 | + md_template = self.env.from_string(markdown) |
| 88 | + return md_template.render(**self._get_markdown_context(page, config)) |
0 commit comments