diff --git a/documentation.py b/documentation.py index 46ccacb..cc7374d 100644 --- a/documentation.py +++ b/documentation.py @@ -6,7 +6,7 @@ import json import git import frontmatter from git_utils import get_first_remote -from markdown_utils import rewrite_image_paths +from markdown_utils import latex_centered_image, rewrite_image_paths class Docs(): @@ -85,11 +85,15 @@ class Docs(): # handle pictures yaml_data['picture_link'] = '' if yaml_data['picture']: - # skip SVG for now, not supported by pandoc - picture_name = yaml_data['picture'] - if 'svg' not in picture_name: - picture_filename = os.path.join(project.local_dir, picture_name) - yaml_data['picture_link'] = '![picture]({})'.format(picture_filename) + extension = os.path.splitext(yaml_data['picture'])[1] + picture_path = os.path.join(project.local_dir, f"picture{extension}") + if extension == '.svg': + # SVGs are not supported by pandoc + logging.warning(f"Skipping unsupported SVG picture {picture_path}") + elif os.path.exists(picture_path): + yaml_data['picture_link'] = latex_centered_image(picture_path) + else: + logging.warning(f"picture {picture_path} not found, skipping") # now build the doc & print it try: diff --git a/markdown_utils.py b/markdown_utils.py index ab812e7..7964538 100644 --- a/markdown_utils.py +++ b/markdown_utils.py @@ -33,3 +33,9 @@ class ImagePathRewriterRenderer(MarkdownRenderer): def rewrite_image_paths(source: str, prefix: str) -> str: markdown = mistune.create_markdown(renderer=ImagePathRewriterRenderer(prefix)) return markdown(source) + + +def latex_centered_image(src: str): + return "\n".join( + [r"\begin{center}", rf"\includegraphics[]{{{src}}}", r"\end{center}"] + ) diff --git a/project.py b/project.py index eefb777..5379d3e 100644 --- a/project.py +++ b/project.py @@ -373,6 +373,21 @@ class Project(): shutil.copyfile('golden_config.tcl', os.path.join(self.src_dir, 'config.tcl')) self.harden() + def copy_picture_for_docs(self): + picture = self.yaml['documentation']['picture'] + if not picture: + return + extension = os.path.splitext(picture)[1] + supported_extensions = ['.png', '.jpg', '.jpeg', '.svg'] + if not os.path.exists(picture): + logging.warning(f"Picture file '{picture}' not found in repo, skipping") + elif extension not in supported_extensions: + logging.warning(f"Picture file '{picture}' has unsupported extension '{extension}' (we support {', '.join(supported_extensions)}), skipping") + else: + picture_dir = os.path.join(self.local_dir, 'src/__tinytapeout') + os.makedirs(picture_dir, exist_ok=True) + shutil.copyfile(picture, os.path.join(picture_dir, f'picture{extension}')) + def harden(self): cwd = os.getcwd() os.chdir(self.local_dir) @@ -401,6 +416,9 @@ class Project(): }, f, indent=2) f.write("\n") + # Copy user provided picture (if exists) to project directory + self.copy_picture_for_docs() + os.chdir(cwd) # doc check