fix(docs): update code to support new info.yaml format

This commit is contained in:
Uri Shaked
2023-12-23 22:37:55 +02:00
parent c2c0d8ffbd
commit 45633298e8
9 changed files with 79 additions and 180 deletions
+20 -80
View File
@@ -1,28 +1,22 @@
import json
import logging
import os
import shutil
import subprocess
from typing import List
from typing import List, Optional
import frontmatter # type: ignore
import git # type: ignore
from config import Config
from git_utils import get_first_remote
from markdown_utils import latex_centered_image, rewrite_image_paths
from markdown_utils import rewrite_image_paths
from project import Project
class DocsArgs:
dump_json: str
class Docs:
def __init__(self, config: Config, projects: List[Project], args: DocsArgs):
def __init__(self, config: Config, projects: List[Project]):
self.config = config
self.projects = projects
self.args = args
self.script_dir = os.path.dirname(os.path.realpath(__file__))
# stuff related to docs
@@ -52,17 +46,6 @@ class Docs:
logging.info(cmd)
os.system(cmd)
# create a json file of all the project info, this is then used by tinytapeout.com to show projects
def dump_json(self):
designs = []
for project in self.projects:
design = project.get_yaml()
designs.append(design)
with open(self.args.dump_json, "w") as fh:
fh.write(json.dumps(designs, indent=4))
logging.info(f"wrote json to {self.args.dump_json}")
def load_doc_template(self, name: str) -> str:
root = os.path.join(self.script_dir, "docs")
doc_path = os.path.join(root, name)
@@ -76,14 +59,14 @@ class Docs:
else:
return doc.content + "\n"
def dump_markdown(self):
def write_datasheet(self, markdown_file: str, pdf_file: Optional[str] = None):
doc_header = self.load_doc_template("doc_header.md")
doc_template = self.load_doc_template("doc_template.md")
doc_pinout = self.load_doc_template("PINOUT.md")
doc_info = self.load_doc_template("../../tt-multiplexer/docs/INFO.md")
doc_credits = self.load_doc_template("CREDITS.md")
with open(self.args.dump_markdown, "w") as fh:
with open(markdown_file, "w") as fh:
repo = git.Repo(".")
fh.write(
doc_header.format(name=self.config["name"], repo=get_first_remote(repo))
@@ -92,30 +75,16 @@ class Docs:
self.projects.sort(key=lambda x: x.mux_address)
for project in self.projects:
yaml_data = project.get_project_doc_yaml()
yaml_data = project.get_project_docs_dict()
yaml_data["mux_address"] = project.mux_address
logging.info(f"building datasheet for {project}")
# handle pictures
yaml_data["picture_link"] = ""
if yaml_data["picture"]:
extension = os.path.splitext(yaml_data["picture"])[1]
picture_path = os.path.join(
project.local_dir, f"picture{extension}"
)
if os.path.exists(picture_path):
yaml_data["picture_link"] = latex_centered_image(picture_path)
else:
logging.warning(f"picture {picture_path} not found, skipping")
# ensure there are no LaTeX escape sequences in various fields, and that optional fields are set
for key in [
"author",
"description",
"how_it_works",
"how_to_test",
"external_hw",
"user_docs",
"clock_hz",
"git_url",
"doc_link",
@@ -127,12 +96,6 @@ class Docs:
else:
yaml_data[key] = ""
# many people remove unused pins in input / output / bidirectional
for key in ["inputs", "outputs", "bidirectional"]:
if not yaml_data[key]:
yaml_data[key] = []
yaml_data[key].extend((8 - len(yaml_data[key])) * ["n/a"])
# now build the doc & print it
try:
doc = (
@@ -152,17 +115,16 @@ class Docs:
fh.write("\n\\clearpage\n")
fh.write(doc_credits)
logging.info(f"wrote markdown to {self.args.dump_markdown}")
logging.info(f"wrote markdown to {markdown_file}")
if self.args.dump_pdf:
pdf_cmd = f"pandoc --toc --toc-depth 2 --pdf-engine=xelatex -i {self.args.dump_markdown} -o {self.args.dump_pdf}"
if pdf_file is not None:
pdf_cmd = f"pandoc --toc --toc-depth 2 --pdf-engine=xelatex -i {markdown_file} -o {pdf_file}"
logging.info(pdf_cmd)
p = subprocess.run(pdf_cmd, shell=True)
if p.returncode != 0:
logging.error("pdf command failed")
def build_hugo_content(self) -> None:
hugo_root = self.args.build_hugo_content
def build_hugo_content(self, hugo_root: str) -> None:
hugo_images = os.path.join(hugo_root, "images")
shutil.rmtree(hugo_root)
os.makedirs(hugo_root)
@@ -185,8 +147,14 @@ class Docs:
# index page
logging.info("building pages")
shuttle_info = {
"shuttle_name": self.config["name"],
"shuttle_id": self.config["id"],
"project_count": len(self.projects),
"end_date": self.config["end_date"],
}
with open(os.path.join(hugo_root, "_index.md"), "w") as fh:
fh.write(index_template)
fh.write(index_template.format(**shuttle_info))
fh.write("# All projects\n")
fh.write("| Index | Title | Author |\n")
fh.write("| ----- | ----- | -------|\n")
@@ -199,41 +167,13 @@ class Docs:
project_image_dir = os.path.join(project_dir, "images")
os.makedirs(project_dir)
os.makedirs(project_image_dir)
yaml_data = project.get_project_doc_yaml()
yaml_data = project.get_project_docs_dict()
yaml_data["mux_address"] = project.mux_address
if '""' in yaml_data["title"]:
yaml_data["title"] = yaml_data["title"].replace('""', "")
yaml_data["index"] = project.index
yaml_data["weight"] = project.index + 1
yaml_data["git_action"] = project.get_workflow_url_when_submitted()
for key in "external_hw", "clock_hz":
if key not in yaml_data:
yaml_data[key] = ""
yaml_data["shuttle_id"] = self.config["id"]
# many people remove unused pins in input / output / bidirectional
for key in ["inputs", "outputs", "bidirectional"]:
yaml_data[key].extend((8 - len(yaml_data[key])) * ["n/a"])
yaml_data["picture_link"] = ""
if yaml_data["picture"]:
extension = os.path.splitext(yaml_data["picture"])[1]
picture_path = os.path.join(
project.local_dir, f"picture{extension}"
)
picture_basename = os.path.basename(picture_path)
try:
shutil.copyfile(
picture_path,
os.path.join(project_image_dir, picture_basename),
)
yaml_data[
"picture_link"
] = f"![picture](images/{picture_basename})"
logging.warning(f"picture found {picture_path}")
except FileNotFoundError:
logging.warning(f"picture not found {picture_path}")
yaml_data["picture_link"] = "Image path is broken"
doc = doc_template.format(**yaml_data)
with open(os.path.join(project_dir, "_index.md"), "w") as pfh:
pfh.write(doc)