Skip to content

Commit

Permalink
Preprocess during on_page_read_source (#20)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
miffels and Ross Crawford-d'Heureuse authored Jun 16, 2020
1 parent 2cb6e78 commit 1701284
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
19 changes: 16 additions & 3 deletions markdownextradata/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
6 changes: 4 additions & 2 deletions test/docs/index.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Hi there, {{ customer.name }}
# Hi there, {{ customer.name }}

Welcome to {{ customer.web_url }}
Welcome to {{ customer.web_url }}

Inside the included md file there 3 {{ star }}
1 change: 1 addition & 0 deletions test/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ plugins:
- markdownextradata

extra:
star: "![star](ressources/star.png)"
customer:
name: Your name here
web_url: www.example.com
Expand Down
3 changes: 2 additions & 1 deletion test/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<h1 id="hi-there-your-name-here">Hi there, Your name here</h1>' in contents, f"customer.name is not in index"
assert '<p>Inside the included md file there 3 <img alt="star" src="ressources/star.png" /></p></div>' 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"

0 comments on commit 1701284

Please sign in to comment.