#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""content.json（worklog_rtf.py と同じスキーマ）→ 印刷用HTML。
体裁は worklog_rtf.py の3階層アウトラインに合わせる（番号は青・自動採番）。"""
import json, argparse, html, re

_LEAD = re.compile(r'^(?:[0-9]+(?:\.[0-9]+)*\.\s+|[●○◆■・★☆]\s*)+')
def strip_lead(s): return _LEAD.sub('', s).strip()
def e(s): return html.escape(s)

CSS = """
@page { size: A4; margin: 18mm 16mm 16mm 16mm; }
* { box-sizing: border-box; }
body { font-family: "Hiragino Sans", "Hiragino Kaku Gothic ProN", sans-serif;
       font-size: 10.5pt; line-height: 1.65; color: #111; margin: 0;
       -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.doctitle { font-size: 20pt; font-weight: 600; letter-spacing: .02em; margin: 0 0 2mm; }
.period { font-size: 10.5pt; color: #555; margin: 0 0 5mm; }
.rule { border: 0; border-top: 2px solid #2a5db0; margin: 0 0 6mm; }
.intro { font-size: 10.5pt; margin: 0 0 9mm; text-align: justify; }
.toc { margin: 0 0 9mm; padding: 4mm 5mm; background: #f4f6fa; border-left: 3px solid #2a5db0; }
.toc h3 { font-size: 11pt; margin: 0 0 2mm; color: #2a5db0; }
.toc ol { margin: 0; padding-left: 6mm; font-size: 10pt; }
.toc li { margin: 0 0 1mm; }
body { counter-reset: sec; }
section { counter-reset: item; margin: 0 0 7mm; break-inside: auto; }
section:first-of-type { break-before: page; }
h2 { font-size: 16pt; font-weight: 600; margin: 0 0 3mm; padding-bottom: 1.5mm;
     border-bottom: 1px solid #c9d4e6; break-after: avoid; }
h2::before { counter-increment: sec; content: counter(sec) ". "; color: #2a5db0; }
.entry { margin: 0 0 4mm; break-inside: avoid; }
section h3 { font-size: 12pt; font-weight: 700; margin: 0 0 1.5mm; break-after: avoid; }
section h3::before { counter-increment: item; content: counter(sec) "." counter(item) ". "; color: #2a5db0; }
ol.b { counter-reset: bul; list-style: none; margin: 0; padding: 0 0 0 7mm; }
ol.b > li { position: relative; font-size: 10.5pt; margin: 0 0 1.2mm; text-align: justify; }
ol.b > li::before { counter-increment: bul; content: counter(bul) ") "; color: #2a5db0;
     position: absolute; left: -7mm; width: 7mm; }
ul.s { list-style: none; margin: 1.5mm 0 0; padding: 0 0 0 7mm; }
ul.s > li { font-size: 9.5pt; color: #444; margin: 0 0 1mm; text-align: justify; }
.concerns { margin: 0 0 7mm; padding: 4mm 5mm; border: 1px solid #c00000;
        background: #fdf4f4; color: #c00000; }
.concerns h3 { font-size: 11pt; margin: 0 0 2mm; color: #c00000; }
.concerns ul { margin: 0; padding-left: 6mm; font-size: 10pt; }
.concerns li { margin: 0 0 1mm; text-align: justify; }
.meetlist { padding-left: 7mm; }
.foot { margin-top: 8mm; padding-top: 3mm; border-top: 1px solid #ddd;
        font-size: 9pt; color: #666; }
"""

def main():
    ap = argparse.ArgumentParser()
    ap.add_argument('--in', dest='src', required=True)
    ap.add_argument('--out', dest='dst', required=True)
    ap.add_argument('--period', default='')
    ap.add_argument('--foot', default='')
    a = ap.parse_args()
    d = json.load(open(a.src, encoding='utf-8'))

    o = ['<html lang="ja"><head><meta charset="utf-8">',
         '<title>%s</title>' % e(d['title']), '<style>%s</style></head><body>' % CSS]
    o.append('<div class="doctitle">%s</div>' % e(d['title']))
    if a.period:
        o.append('<div class="period">%s</div>' % e(a.period))
    o.append('<hr class="rule">')
    _cs = [c for c in (d.get('concerns') or []) if c and c.strip()]
    if _cs:
        o.append('<div class="concerns"><h3>■ ご確認をお願いしたい事項</h3><ul>')
        for c in _cs:
            o.append('<li>%s</li>' % e(c))
        o.append('</ul></div>')
    if d.get('intro'):
        o.append('<div class="intro">%s</div>' % e(d['intro']))
    o.append('<div class="toc"><h3>目次</h3><ol>')
    for s in d['sections']:
        o.append('<li>%s</li>' % e(strip_lead(s['h2'])))
    o.append('</ol></div>')

    for s in d['sections']:
        o.append('<section><h2>%s</h2>' % e(strip_lead(s['h2'])))
        for en in s['entries']:
            o.append('<div class="entry">')
            o.append('<h3>%s</h3>' % e(strip_lead(en['head'])))
            if en.get('bullets'):
                o.append('<ol class="b">')
                for b in en['bullets']:
                    o.append('<li>%s</li>' % e(b))
                o.append('</ol>')
            if en.get('subs'):
                o.append('<ul class="s">')
                for t in en['subs']:
                    o.append('<li>%s</li>' % e(t))
                o.append('</ul>')
            o.append('</div>')
        o.append('</section>')
    if a.foot:
        o.append('<div class="foot">%s</div>' % e(a.foot))
    o.append('</body></html>')
    open(a.dst, 'w', encoding='utf-8').write('\n'.join(o))
    print('wrote', a.dst)

main()
