import re import json def syntax(): '''regex setup for the flat syntax''' # titles t = re.compile('\(.*\)') # sections s = re.compile('\[(.*?)\]') # notes n = re.compile('(-->)') return (t, s, n) def test_scan(file): with open(file, 'r') as f: t, s, n = syntax() # scan through the document line = f.readline() while line: if not line.rstrip('\n'): line=f.readline() continue if t.match(line): print(f"title: {line[1:-2]}") line = f.readline() continue if s.match(line): print(f'section: {line[1:-2]}') line = f.readline() continue if n.match(line): print(f'note: {line}') line = f.readline() continue print(f'text: {line}') line = f.readline() def parse(file): with open(file, 'r') as f: parsed = {} current_title = '' current_section = '' t, s, n = syntax() line = f.readline() while line: if not line.rstrip('\n'): line=f.readline() continue # titles if t.match(line): current_title = line[1:-2] parsed[current_title] = {} line = f.readline() continue # sections if s.match(line): current_section = line[1:-2] parsed[current_title][current_section]='' line = f.readline() continue # notes (TBD) if n.match(line): parsed[current_title][current_section]+=line.rstrip('\n') + ' \n' line = f.readline() continue # normal text parsed[current_title][current_section]+=line.rstrip('\n') + ' \n' line = f.readline() return json.dumps(parsed, indent=4) print(parse('list.1dl'))