import re, json, sys, html
src = open(sys.argv[1]).read().splitlines()
def inline(t):
t = html.escape(t, quote=False)
t = re.sub(r'\[([^\]]+)\]\(([^)]+)\)', r'\1', t)
t = re.sub(r'\*\*(.+?)\*\*', r'\1', t)
t = re.sub(r'`([^`]+)`', r'\1', t)
t = re.sub(r'(?\1', t)
return t
chunks, captions, cur = [], [], []
lst = None
def close():
global lst
if lst: cur.append('%s>' % lst); lst = None
i = 0
for line in src:
s = line.strip()
if s.startswith('# ') or s.startswith('[COVER'):
continue
m = re.match(r'\[ANIMATION (\d+)', s)
if m:
close(); chunks.append(''.join(cur)); cur = []; continue
if s.startswith('Caption:'):
captions.append(s[len('Caption:'):].strip()); continue
if not s:
close(); continue
if s.startswith('### '):
close(); cur.append('
%s
' % inline(s)) close(); chunks.append(''.join(cur)) json.dump({'chunks': chunks, 'captions': captions}, open(sys.argv[2], 'w')) print(len(chunks), len(captions), [len(c) for c in chunks])