subsea-compute-open-artifacts / scripts /build_photoroom_technical_package.py
monish133's picture
Upload open subsea compute engineering artifacts
aa6138d verified
Raw
History Blame Contribute Delete
10.4 kB
from pathlib import Path
import re
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.section import WD_SECTION_START
from docx.shared import Inches, Pt, RGBColor
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from reportlab.lib import colors
from reportlab.lib.enums import TA_LEFT, TA_CENTER
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus import (
SimpleDocTemplate,
Paragraph,
Spacer,
Table,
TableStyle,
Image,
PageBreak,
Preformatted,
)
ROOT = Path(r"C:\Users\brind\Documents\New project\subsea-compute-prototype")
MD = ROOT / "docs" / "photoroom_technical_routing_package_2026-05-10.md"
DOCX = ROOT / "docs" / "photoroom_technical_routing_package_2026-05-10.docx"
PDF = ROOT / "docs" / "photoroom_technical_routing_package_2026-05-10.pdf"
CONCEPT = ROOT / "assets" / "containerized_multi_pod_concept_sheet_2026-05-09.png"
def set_cell_shading(cell, fill):
tc_pr = cell._tc.get_or_add_tcPr()
shd = OxmlElement("w:shd")
shd.set(qn("w:fill"), fill)
tc_pr.append(shd)
def set_cell_text(cell, text, bold=False):
cell.text = ""
p = cell.paragraphs[0]
r = p.add_run(text)
r.bold = bold
r.font.size = Pt(8.5)
def clean_inline(text):
text = text.replace("**", "")
return text
def parse_markdown(md_text):
lines = md_text.splitlines()
blocks = []
i = 0
while i < len(lines):
line = lines[i]
if not line.strip():
i += 1
continue
if line.startswith("```"):
code = []
i += 1
while i < len(lines) and not lines[i].startswith("```"):
code.append(lines[i])
i += 1
i += 1
blocks.append(("code", "\n".join(code)))
continue
if line.startswith("#"):
level = len(line) - len(line.lstrip("#"))
blocks.append((f"h{level}", line[level:].strip()))
i += 1
continue
if line.startswith("|"):
table_lines = []
while i < len(lines) and lines[i].startswith("|"):
table_lines.append(lines[i])
i += 1
rows = []
for t in table_lines:
cells = [c.strip() for c in t.strip("|").split("|")]
if all(re.fullmatch(r":?-{3,}:?", c or "") for c in cells):
continue
rows.append(cells)
if rows:
blocks.append(("table", rows))
continue
if line.lstrip().startswith("- "):
items = []
while i < len(lines) and lines[i].lstrip().startswith("- "):
items.append(lines[i].lstrip()[2:].strip())
i += 1
blocks.append(("bullets", items))
continue
para = [line.strip()]
i += 1
while (
i < len(lines)
and lines[i].strip()
and not lines[i].startswith("#")
and not lines[i].startswith("|")
and not lines[i].startswith("```")
and not lines[i].lstrip().startswith("- ")
):
para.append(lines[i].strip())
i += 1
blocks.append(("p", " ".join(para)))
return blocks
def build_docx(blocks):
doc = Document()
section = doc.sections[0]
section.top_margin = Inches(0.65)
section.bottom_margin = Inches(0.65)
section.left_margin = Inches(0.7)
section.right_margin = Inches(0.7)
styles = doc.styles
styles["Normal"].font.name = "Aptos"
styles["Normal"].font.size = Pt(9.3)
styles["Normal"].paragraph_format.space_after = Pt(5)
styles["Normal"].paragraph_format.line_spacing = 1.08
for name, size, color in [
("Heading 1", 19, RGBColor(21, 55, 85)),
("Heading 2", 12.5, RGBColor(21, 55, 85)),
("Heading 3", 10.5, RGBColor(40, 83, 120)),
]:
style = styles[name]
style.font.name = "Aptos Display"
style.font.size = Pt(size)
style.font.color.rgb = color
style.font.bold = True
style.paragraph_format.space_before = Pt(8)
style.paragraph_format.space_after = Pt(4)
for kind, content in blocks:
if kind == "h1":
p = doc.add_paragraph()
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
r = p.add_run(content)
r.bold = True
r.font.name = "Aptos Display"
r.font.size = Pt(21)
r.font.color.rgb = RGBColor(21, 55, 85)
elif kind == "h2":
doc.add_paragraph(content, style="Heading 2")
elif kind == "h3":
doc.add_paragraph(content, style="Heading 3")
elif kind == "p":
p = doc.add_paragraph(clean_inline(content))
if content.startswith(">"):
p.paragraph_format.left_indent = Inches(0.22)
elif kind == "bullets":
for item in content:
doc.add_paragraph("• " + clean_inline(item))
elif kind == "code":
p = doc.add_paragraph()
p.paragraph_format.left_indent = Inches(0.22)
r = p.add_run(content)
r.font.name = "Consolas"
r.font.size = Pt(8)
elif kind == "table":
rows = content
cols = max(len(r) for r in rows)
table = doc.add_table(rows=len(rows), cols=cols)
table.style = "Table Grid"
for ri, row in enumerate(rows):
for ci in range(cols):
text = clean_inline(row[ci]) if ci < len(row) else ""
set_cell_text(table.cell(ri, ci), text, bold=(ri == 0))
if ri == 0:
set_cell_shading(table.cell(ri, ci), "D9EAF7")
doc.add_paragraph()
for section in doc.sections:
footer = section.footer.paragraphs[0]
footer.text = "Non-confidential concept routing package | Monish Khatri | 2026-05-10"
footer.alignment = WD_ALIGN_PARAGRAPH.CENTER
footer.runs[0].font.size = Pt(8)
footer.runs[0].font.color.rgb = RGBColor(100, 100, 100)
doc.save(DOCX)
def html_escape(text):
return (
text.replace("&", "&amp;")
.replace("<", "&lt;")
.replace(">", "&gt;")
.replace("**", "")
)
def build_pdf(blocks):
styles = getSampleStyleSheet()
styles.add(
ParagraphStyle(
name="TitleBlue",
parent=styles["Title"],
fontName="Helvetica-Bold",
fontSize=20,
leading=24,
textColor=colors.HexColor("#153755"),
alignment=TA_CENTER,
spaceAfter=12,
)
)
styles.add(
ParagraphStyle(
name="Section",
parent=styles["Heading2"],
fontName="Helvetica-Bold",
fontSize=12.5,
leading=15,
textColor=colors.HexColor("#153755"),
spaceBefore=9,
spaceAfter=5,
)
)
styles.add(
ParagraphStyle(
name="BodySmall",
parent=styles["BodyText"],
fontName="Helvetica",
fontSize=8.9,
leading=11.2,
spaceAfter=5,
)
)
styles.add(
ParagraphStyle(
name="BulletSmall",
parent=styles["BodyText"],
fontName="Helvetica",
fontSize=8.8,
leading=11,
leftIndent=14,
bulletIndent=4,
spaceAfter=3,
)
)
styles.add(
ParagraphStyle(
name="Quote",
parent=styles["BodyText"],
fontName="Helvetica-Oblique",
fontSize=9,
leading=12,
leftIndent=14,
textColor=colors.HexColor("#2f4f67"),
spaceAfter=6,
)
)
story = []
for kind, content in blocks:
if kind == "h1":
story.append(Paragraph(html_escape(content), styles["TitleBlue"]))
elif kind == "h2":
story.append(Paragraph(html_escape(content), styles["Section"]))
elif kind == "h3":
story.append(Paragraph(f"<b>{html_escape(content)}</b>", styles["BodySmall"]))
elif kind == "p":
style = styles["Quote"] if content.startswith(">") else styles["BodySmall"]
story.append(Paragraph(html_escape(content), style))
elif kind == "bullets":
for item in content:
story.append(Paragraph(html_escape(item), styles["BulletSmall"], bulletText="•"))
elif kind == "code":
story.append(Preformatted(content, styles["Code"]))
story.append(Spacer(1, 4))
elif kind == "table":
data = [[Paragraph(html_escape(cell), styles["BodySmall"]) for cell in row] for row in content]
tbl = Table(data, repeatRows=1, hAlign="LEFT")
tbl.setStyle(
TableStyle(
[
("BACKGROUND", (0, 0), (-1, 0), colors.HexColor("#D9EAF7")),
("TEXTCOLOR", (0, 0), (-1, 0), colors.HexColor("#153755")),
("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
("GRID", (0, 0), (-1, -1), 0.35, colors.HexColor("#B8C5CC")),
("VALIGN", (0, 0), (-1, -1), "TOP"),
("LEFTPADDING", (0, 0), (-1, -1), 5),
("RIGHTPADDING", (0, 0), (-1, -1), 5),
("TOPPADDING", (0, 0), (-1, -1), 4),
("BOTTOMPADDING", (0, 0), (-1, -1), 4),
]
)
)
story.append(tbl)
story.append(Spacer(1, 7))
doc = SimpleDocTemplate(
str(PDF),
pagesize=A4,
rightMargin=0.55 * inch,
leftMargin=0.55 * inch,
topMargin=0.48 * inch,
bottomMargin=0.48 * inch,
title="Retrievable Underwater GPU Pod Technical Routing Package",
)
doc.build(story)
if __name__ == "__main__":
blocks = parse_markdown(MD.read_text(encoding="utf-8"))
build_docx(blocks)
build_pdf(blocks)
print(DOCX)
print(PDF)