fix(datasheet): broken user module pictures / diagrams

close #22
This commit is contained in:
Uri Shaked
2023-07-31 09:02:16 +03:00
parent 1aa125fdd5
commit 24c15519e8
3 changed files with 34 additions and 6 deletions
+10 -6
View File
@@ -6,7 +6,7 @@ import json
import git import git
import frontmatter import frontmatter
from git_utils import get_first_remote 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(): class Docs():
@@ -85,11 +85,15 @@ class Docs():
# handle pictures # handle pictures
yaml_data['picture_link'] = '' yaml_data['picture_link'] = ''
if yaml_data['picture']: if yaml_data['picture']:
# skip SVG for now, not supported by pandoc extension = os.path.splitext(yaml_data['picture'])[1]
picture_name = yaml_data['picture'] picture_path = os.path.join(project.local_dir, f"picture{extension}")
if 'svg' not in picture_name: if extension == '.svg':
picture_filename = os.path.join(project.local_dir, picture_name) # SVGs are not supported by pandoc
yaml_data['picture_link'] = '![picture]({})'.format(picture_filename) 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 # now build the doc & print it
try: try:
+6
View File
@@ -33,3 +33,9 @@ class ImagePathRewriterRenderer(MarkdownRenderer):
def rewrite_image_paths(source: str, prefix: str) -> str: def rewrite_image_paths(source: str, prefix: str) -> str:
markdown = mistune.create_markdown(renderer=ImagePathRewriterRenderer(prefix)) markdown = mistune.create_markdown(renderer=ImagePathRewriterRenderer(prefix))
return markdown(source) return markdown(source)
def latex_centered_image(src: str):
return "\n".join(
[r"\begin{center}", rf"\includegraphics[]{{{src}}}", r"\end{center}"]
)
+18
View File
@@ -373,6 +373,21 @@ class Project():
shutil.copyfile('golden_config.tcl', os.path.join(self.src_dir, 'config.tcl')) shutil.copyfile('golden_config.tcl', os.path.join(self.src_dir, 'config.tcl'))
self.harden() 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): def harden(self):
cwd = os.getcwd() cwd = os.getcwd()
os.chdir(self.local_dir) os.chdir(self.local_dir)
@@ -401,6 +416,9 @@ class Project():
}, f, indent=2) }, f, indent=2)
f.write("\n") f.write("\n")
# Copy user provided picture (if exists) to project directory
self.copy_picture_for_docs()
os.chdir(cwd) os.chdir(cwd)
# doc check # doc check