1
Fork 0
mkdocs-site/hooks/include-file.py

20 lines
545 B
Python
Raw Normal View History

2023-09-15 18:05:28 +01:00
from jinja2_simple_tags import StandaloneTag
import os
def on_env(env, config, files):
env.add_extension(IncludeFileTag)
return env
class IncludeFileTag(StandaloneTag):
tags = {"include_file"}
def render(self, path):
if path.startswith("/"):
file_path = os.path.join(self.context["config"]["site_dir"], path.removeprefix("/"))
else:
file_path = os.path.join(os.path.dirname(self.context["page"].file.abs_src_path), path)
with open(file_path) as f:
return f.read()