#!/usr/bin/env python3
"""Ekstrak judul + paragraf utama artikel studiosoft.id dari file HTML."""
import re, sys, html

for path in sys.argv[1:]:
    raw = open(path, encoding="utf-8", errors="ignore").read()
    m = re.search(r"<title>(.*?)</title>", raw, re.S)
    print("=" * 100)
    print("FILE:", path)
    print("TITLE:", html.unescape(m.group(1)).strip() if m else "-")
    body = raw
    for pat in (r"<script.*?</script>", r"<style.*?</style>", r"<nav.*?</nav>", r"<header.*?</header>", r"<footer.*?</footer>"):
        body = re.sub(pat, " ", body, flags=re.S | re.I)
    # ambil area entry-content
    m2 = re.search(r'(class="[^"]*entry-content[^"]*".*)', body, re.S)
    if m2:
        body = m2.group(1)
    body = re.sub(r"<h([1-6])[^>]*>", r"\n\n## ", body)
    body = re.sub(r"</h[1-6]>", "\n", body)
    body = re.sub(r"<li[^>]*>", "\n- ", body)
    body = re.sub(r"<p[^>]*>", "\n", body)
    body = re.sub(r"<[^>]+>", " ", body)
    body = html.unescape(body)
    body = re.sub(r"[ \t]+", " ", body)
    body = re.sub(r"\n\s*\n+", "\n", body)
    print(body.strip()[:4500])
