#!/usr/bin/env python3
"""hc60: cetak teks bersih isi artikel dari /tmp/hc60_articles.json."""
import html
import json
import re
import sys

d = json.load(open("/tmp/hc60_articles.json"))
slug = sys.argv[1] if len(sys.argv) > 1 else "9-contoh-konten-digital-marketing-rumah-sakit-yang-edukatif-dan-tetap-etis"
p = d[slug]
raw = p["content"]["rendered"]


def clean(h):
    h = re.sub(r"<(script|style)[^>]*>.*?</\1>", " ", h, flags=re.S | re.I)
    h = re.sub(r"<h2[^>]*>", "\n## ", h, flags=re.I)
    h = re.sub(r"<h3[^>]*>", "\n### ", h, flags=re.I)
    h = re.sub(r"<li[^>]*>", "\n- ", h, flags=re.I)
    h = re.sub(r"<p[^>]*>", "\n", h, flags=re.I)
    h = re.sub(r"<br\s*/?>", "\n", h, flags=re.I)
    h = re.sub(r"<[^>]+>", "", h)
    h = html.unescape(h)
    h = re.sub(r"\n{3,}", "\n\n", h)
    return h.strip()


txt = clean(raw)
print("TITLE:", p["title"]["rendered"])
print("WORDS:", len(txt.split()))
print("=" * 70)
print(txt[:9000])
