from __future__ import annotations import base64 import html from pathlib import Path from docx import Document from docx.document import Document as DocumentType from docx.oxml.ns import qn from docx.table import Table from docx.text.paragraph import Paragraph ROOT = Path(__file__).resolve().parents[3] GUIDE_DIR = ROOT / "docs" / "project-guide" DOCX = GUIDE_DIR / "OpenCodeReview_AI_Engineering_Architecture_Guide.docx" OUT = GUIDE_DIR / "working" / "preview.html" def iter_blocks(parent): parent_elm = parent.element.body if isinstance(parent, DocumentType) else parent._tc for child in parent_elm.iterchildren(): if child.tag == qn("w:p"): yield Paragraph(child, parent) elif child.tag == qn("w:tbl"): yield Table(child, parent) def paragraph_image_data(paragraph: Paragraph): blips = paragraph._p.xpath(".//a:blip") if not blips: return None rid = blips[0].get(qn("r:embed")) if not rid: return None part = paragraph.part.related_parts[rid] content_type = part.content_type or "image/png" return f"data:{content_type};base64,{base64.b64encode(part.blob).decode('ascii')}" def para_html(paragraph: Paragraph, number_state: dict[str, int]): text = html.escape(paragraph.text) style = paragraph.style.name if paragraph.style else "Normal" image = paragraph_image_data(paragraph) if image: return f'
• {text}
" if style.startswith("List Number"): number_state["n"] += 1 return f"{number_state['n']}. {text}
" number_state["n"] = 0 is_code = any(run.font.name == "Consolas" for run in paragraph.runs) if is_code: return f"{text}"
is_caption = bool(paragraph.runs) and all(bool(run.italic) for run in paragraph.runs if run.text)
if is_caption:
return f"{text}
" return f"{text}
" def table_html(table: Table): rows = [] for r_idx, row in enumerate(table.rows): cells = [] tag = "th" if r_idx == 0 else "td" for cell in row.cells: value = "