-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsplit_full_lesson.py
More file actions
80 lines (66 loc) · 2.61 KB
/
split_full_lesson.py
File metadata and controls
80 lines (66 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import re
import sys
import yaml
def main ():
# Get file path from first argument passed in command line
fl_path = sys.argv[1]
folder = os.path.dirname(fl_path)
s_file = None
s_list= []
l_structure = {'lesson': { 'en': {
'name': '',
'group': '',
'description': 'lesson.md',
'steps': [],
'nextLessons': [{'name': '', 'group': ''}]}}}
l_structure_en = l_structure['lesson']['en']
s_number = 0
with open(fl_path) as full_lesson:
for line in full_lesson:
# Patterns
l = re.match('#\s(.*)', line) # lesson title pattern
s = re.match('##\s(.*)', line) # step title pattern
b = re.match('\n', line) # step title pattern
if l:
l_title = (l.group(0))[2:]
l_structure_en['name'] = l_title
s_file = open(os.path.join(folder, 'lesson.md'), 'w+')
next(full_lesson) # skip empty line after heading
# Get step title from markdown heading 2
elif s:
s_number += 1
s_title = (s.group(0))[3:]
s_file_name = '{0:02d}_{1}.md'.format(s_number, s_title.replace(
' ', '_').lower())
l_structure_en['steps'].append({'name':s_title,
'description': s_file_name})
s_list.append((s_title, s_file_name))
# Close description file from previous step
if s_file != None:
clean_empty_lines(s_file)
s_file.close()
# Open step description file
s_file = open(os.path.join(folder, s_file_name), 'w+')
next(full_lesson) # skip empty line after heading
# Get step description
else:
s_file.write(line) #Write to opened file
s_file.close()
# Write Lesson's YAML file
with open(os.path.join(folder, 'lesson.yaml'), 'w') as yaml_file:
yaml.dump(l_structure, yaml_file, default_flow_style=False,
allow_unicode=True)
print "Splitting was completed."
def clean_empty_lines(open_file):
open_file.seek(0, os.SEEK_END)
pos = open_file.tell() -1
# Read the file backwords until it finds an non end of line character
while pos > 0 and open_file.read(1) != '\n':
pos -= 1
open_file.seek(pos, os.SEEK_SET)
if pos > 0:
open_file.seek(pos, os.SEEK_SET)
open_file.truncate()
if __name__ == '__main__':
main()