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('' % 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[4:])); continue if s.startswith('## '): close(); cur.append('

%s

' % inline(s[3:])); continue mb = re.match(r'- (.*)', s); mn = re.match(r'\d+\. (.*)', s) if mb or mn: tag = 'ul' if mb else 'ol' if lst != tag: close(); cur.append('<%s>' % tag); lst = tag cur.append('
  • %s
  • ' % inline((mb or mn).group(1))); continue 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])