You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.3 KiB
Python

import re
def syntax():
'''regex setup for the flat syntax'''
# titles
t = re.compile('\(.*\)')
# sections
s = re.compile('\[(.*?)\]')
# notes
n = re.compile('(-->)')
# comments
c = re.compile('(>>)')
return (t, s, n, c)
def parse(file):
with open(file, 'r') as f:
parsed = {}
current_title = ''
current_section = ''
t, s, n, c = 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 parsed