diff --git a/markdownextradata/plugin.py b/markdownextradata/plugin.py index 88077f3..f70cb65 100644 --- a/markdownextradata/plugin.py +++ b/markdownextradata/plugin.py @@ -3,12 +3,17 @@ import json import yaml import mkdocs +import logging from mkdocs.plugins import BasePlugin +from mkdocs.utils import warning_filter from jinja2 import Template from pathlib import Path from itertools import chain +log = logging.getLogger(__name__) +log.addFilter(warning_filter) + CONFIG_KEYS = ["site_name", "site_author", "site_url", "repo_url", "repo_name"] if sys.version_info[0] >= 3: @@ -83,8 +88,16 @@ def on_pre_build(self, config): ), ) - def on_page_markdown(self, markdown, config, **kwargs): + def on_page_read_source(self, page, config, **kwargs): context = {key: config.get(key) for key in CONFIG_KEYS if key in config} context.update(config.get("extra", {})) - md_template = Template(markdown) - return md_template.render(**config.get("extra")) + try: + with open(page.file.abs_src_path, 'r', encoding='utf-8-sig', errors='strict') as f: + md_template = Template(f.read()) + return md_template.render(**config.get("extra")) + except OSError: + log.error('File not found: {}'.format(self.file.src_path)) + raise + except ValueError: + log.error('Encoding error reading file: {}'.format(self.file.src_path)) + raise diff --git a/test/docs/index.md b/test/docs/index.md index 14546a2..10cb91a 100644 --- a/test/docs/index.md +++ b/test/docs/index.md @@ -1,3 +1,5 @@ -Hi there, {{ customer.name }} +# Hi there, {{ customer.name }} -Welcome to {{ customer.web_url }} \ No newline at end of file +Welcome to {{ customer.web_url }} + +Inside the included md file there 3 {{ star }} \ No newline at end of file diff --git a/test/mkdocs.yml b/test/mkdocs.yml index 6b0a6df..5a27c0d 100644 --- a/test/mkdocs.yml +++ b/test/mkdocs.yml @@ -8,6 +8,7 @@ plugins: - markdownextradata extra: + star: "" customer: name: Your name here web_url: www.example.com diff --git a/test/test_basic.py b/test/test_basic.py index 3d9454d..5af748d 100644 --- a/test/test_basic.py +++ b/test/test_basic.py @@ -22,7 +22,8 @@ def test_basic_working(): assert index_file.exists(), f"{index_file} does not exist, it should" contents = index_file.read_text() - assert f"Hi there, {customer.get('name')}" in contents, f"customer.name is not in index" + assert '
Inside the included md file there 3