From 17012842e8283b4191318cfcbc8079d97ba3298d Mon Sep 17 00:00:00 2001 From: Michael Jess Date: Tue, 16 Jun 2020 23:48:52 +0200 Subject: [PATCH] Preprocess during on_page_read_source (#20) * Preprocess during on_page_read_source * added test for h1 * added test for comlaint in #18 * added test for comlaint in #18 Co-authored-by: Ross Crawford-d'Heureuse --- markdownextradata/plugin.py | 19 ++++++++++++++++--- test/docs/index.md | 6 ++++-- test/mkdocs.yml | 1 + test/test_basic.py | 3 ++- 4 files changed, 23 insertions(+), 6 deletions(-) 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: "![star](ressources/star.png)" 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 '

Hi there, Your name here

' in contents, f"customer.name is not in index" + assert '

Inside the included md file there 3 star

' in contents, f"customer.star is not in index or not rendering as expected" assert f"Welcome to {customer.get('web_url')}" in contents, f"customer.web_url is not in index" assert isinstance(test_json_string, str), "test_json_string is not a str it should be" assert '{"name": "Bob"}' == test_json_string, f"Json string is not correct"