from pathlib import Path
import re, html, sys
import markdown

src = Path(sys.argv[1]) if len(sys.argv) > 1 else Path('/home/adolforeyna/brain/projects/livestream_media_system_report_for_ari_sardius.md')
out_dir = Path('/home/adolforeyna/brain/exports')
out_dir.mkdir(parents=True, exist_ok=True)
slug = src.stem
text = src.read_text(encoding='utf-8')
text = re.sub(r'^---\n.*?\n---\n\s*', '', text, flags=re.S)
text = re.sub(r'\[\[([^\]|]+)\|([^\]]+)\]\]', r'\2', text)
text = re.sub(r'\[\[([^\]]+)\]\]', lambda m: m.group(1).replace('_',' '), text)
text = re.sub(r'(?m)(^[^\n#][^\n]*:\s*)\n(-\s+)', r'\1\n\n\2', text)
body = markdown.markdown(text, extensions=['extra','toc','sane_lists','smarty'])
css = r'''
@page { size: Letter; margin: 0.62in 0.58in; }
* { box-sizing: border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; color: #1f2933; line-height: 1.48; font-size: 10.4pt; }
h1 { font-size: 28pt; color: #0f2f4f; margin: 0 0 10px; letter-spacing: -0.03em; padding-bottom: 14px; border-bottom: 5px solid #2f80ed; }
h2 { font-size: 17pt; color: #0f2f4f; margin-top: 27px; padding: 8px 12px; background: linear-gradient(90deg,#eaf3ff,#ffffff); border-left: 5px solid #2f80ed; break-after: avoid; }
h3 { font-size: 13pt; color: #164b7a; margin-top: 18px; border-bottom: 1px solid #d6e4f0; padding-bottom: 4px; break-after: avoid; }
h4 { font-size: 11.5pt; color: #2f5f8f; margin-top: 14px; break-after: avoid; }
p { margin: 7px 0; }
ul, ol { margin-top: 5px; padding-left: 22px; }
li { margin: 3px 0; }
blockquote { margin: 14px 0; padding: 12px 16px; border-left: 5px solid #f2c94c; background: #fff9e6; color: #3d3200; font-size: 11pt; }
code { background: #eef4fa; padding: 1px 4px; border-radius: 4px; color: #0b456f; font-family: "SFMono-Regular", Consolas, monospace; font-size: 8.7pt; }
table { width: 100%; border-collapse: collapse; margin: 12px 0 18px; font-size: 8.2pt; page-break-inside: auto; }
th { background: #0f2f4f; color: white; text-align: left; padding: 7px 6px; }
td { border: 1px solid #d9e2ec; padding: 6px; vertical-align: top; }
tr:nth-child(even) td { background: #f7fbff; }
a { color: #1a73e8; text-decoration: none; }
strong { color: #102a43; }
.cover { border: 1px solid #d6e4f0; border-radius: 16px; padding: 20px; background: linear-gradient(135deg,#f7fbff 0%,#ffffff 55%,#f8fbff 100%); }
.footer-note { margin-top: 24px; font-size: 8.3pt; color: #64748b; border-top: 1px solid #d9e2ec; padding-top: 8px; }
@media print { h2, h3, h4 { break-after: avoid; } table, blockquote { break-inside: avoid; } }
'''
full = f'''<!doctype html><html><head><meta charset="utf-8"><title>{html.escape(src.stem)}</title><style>{css}</style></head><body><div class="cover">{body}</div><div class="footer-note">Generated from: {html.escape(str(src))}</div></body></html>'''
html_path = out_dir / f'{slug}.html'
html_path.write_text(full, encoding='utf-8')
print(html_path)
