-
Notifications
You must be signed in to change notification settings - Fork 11
/
splitter.py
48 lines (37 loc) · 1.19 KB
/
splitter.py
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
# Use this to split a file based on headers
# Looks for reStructuredText headers, which looks like:
# ==============
# Example header
# ==============
# Creates files with the header contents as file name or "missing" if not found
# New files is placed in a folder called "split"
import re
import os
script_dir = os.path.dirname(__file__)
# Select file
theFile = script_dir+'/1 - Introduction/index.rst' # Replace with your source file
# open file
originalFile = open(theFile)
originalFileContent = originalFile.read()
originalFile.close()
# Split file
headerRegex = re.compile(r"(\n=+\n([^\n]+)\n=+)")
inserSplit = headerRegex.sub( r"=====split\1", originalFileContent)
splitFile = re.split("=====split", inserSplit)
# Create files
index = 1
if not os.path.exists(script_dir + '/split/'):
os.makedirs(script_dir + '/split/')
for split in splitFile:
# Write to new file
if split is "":
continue
match = headerRegex.search(split)
if match:
fileName = str(index) + " - " + match.group(2)
else:
fileName = str(index) + " - missing"
index += 1
newFile = open(script_dir + '/split/' + fileName + '.rst', 'w+')
newFile.write(split)
newFile.close()