from html import escape
from pathlib import Path

source = Path("dokumen/Laporan_FS_CMU_GoogleDocs_Rapih.md")
out_html = Path("final/Laporan_FS_CMU_Revisi_BAB1_s_d_BAB8_DaftarIsi.html")
text = source.read_text()
lines = text.splitlines()
out = []
i = 0
while i < len(lines):
    stripped = lines[i].strip()
    if not stripped:
        i += 1
        continue
    if stripped.startswith("|") and "|" in stripped[1:]:
        table = []
        while i < len(lines) and lines[i].strip().startswith("|"):
            row = [c.strip() for c in lines[i].strip().strip("|").split("|")]
            if not all(set(c.replace(":", "").replace("-", "")) == set() for c in row):
                table.append(row)
            i += 1
        if table:
            out.append("<table>")
            for r, row in enumerate(table):
                tag = "th" if r == 0 else "td"
                out.append(
                    "<tr>"
                    + "".join(f"<{tag}>{escape(c)}</{tag}>" for c in row)
                    + "</tr>"
                )
            out.append("</table>")
        continue
    if stripped in [
        "LAPORAN STUDI KELAYAKAN",
        "PENGEMBANGAN CENTRAL MEDICAL UNIT (CMU)",
        "GEDUNG LAYANAN TERINTEGRASI",
        "RSUD KH AHMAD HANAFIAH - SUKADANA",
        "KABUPATEN LAMPUNG TIMUR",
    ]:
        out.append(f'<h1 class="cover">{escape(stripped)}</h1>')
    elif stripped in [
        "KATA PENGANTAR",
        "DAFTAR ISI",
        "DAFTAR TABEL",
        "DAFTAR GAMBAR",
        "DAFTAR GRAFIK",
        "DAFTAR SINGKATAN",
        "RINGKASAN EKSEKUTIF",
    ]:
        out.append(f"<h1>{escape(stripped)}</h1>")
    elif stripped.startswith("BAB "):
        out.append(f'<h1 class="bab">{escape(stripped)}</h1>')
    elif (
        stripped.startswith("Tabel ")
        or stripped.startswith("Gambar ")
        or stripped.startswith("Grafik ")
    ):
        out.append(f'<p class="caption">{escape(stripped)}</p>')
    elif stripped.split()[0].replace(".", "").isdigit() and "." in stripped.split()[0]:
        first = stripped.split()[0]
        out.append(
            f"<h3>{escape(stripped)}</h3>"
            if first.count(".") >= 2
            else f"<h2>{escape(stripped)}</h2>"
        )
    elif stripped[0].isdigit() and ". " in stripped[:5]:
        out.append(f'<p class="num">{escape(stripped)}</p>')
    else:
        out.append(f"<p>{escape(stripped)}</p>")
    i += 1

html = (
    """<!doctype html><html><head><meta charset="utf-8"><style>
@page { size: A4; margin: 2cm 1.8cm 2cm 1.8cm; }
body { font-family: Arial, sans-serif; font-size: 10.5pt; line-height: 1.45; color:#111; }
h1 { font-size: 16pt; text-align: center; margin: 18pt 0 12pt; font-weight: bold; }
h1.cover { margin: 5pt 0; }
h1.bab { page-break-before: always; margin-top: 0; }
h2 { font-size: 12pt; margin: 14pt 0 8pt; font-weight: bold; }
h3 { font-size: 11pt; margin: 12pt 0 6pt; font-weight: bold; }
p { text-align: justify; margin: 0 0 8pt; }
p.num { margin-left: 14pt; text-indent: -14pt; }
p.caption { text-align:center; font-weight:bold; margin: 10pt 0 6pt; }
table { border-collapse: collapse; width: 100%; margin: 8pt 0 12pt; font-size: 8.3pt; }
th,td { border: 1px solid #333; padding: 4pt; vertical-align: top; }
th { background: #eaeaea; text-align: center; font-weight: bold; }
</style></head><body>"""
    + "\n".join(out)
    + "</body></html>"
)
out_html.write_text(html)
print(out_html)
