-
Notifications
You must be signed in to change notification settings - Fork 104
/
explode.py
executable file
·160 lines (119 loc) · 3.61 KB
/
explode.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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/python
#
# Small program to 'explode' the master document automagically into separate
# files in the include/ directory.
#
# by James Hammons
# (C) 2017 Underground Software
#
import os
import re
import shutil
lineCount = 0
cleanString = re.compile(r'[^a-zA-Z0-9 \._-]+')
#
# Create an all lowercase filename without special characters and with spaces
# replaced with dashes.
#
def MakeFilename(s):
global cleanString
# Clean up the file name, removing all non letter/number or " .-_" chars.
# Also, convert to lower case and replace all spaces with dashes.
fn = cleanString.sub('', s).lower().replace(' ', '-')
# Double dashes can creep in from the above replacement, so we check for
# that here.
fn = fn.replace('--', '-')
return fn
#
# Parse headers into a dictionary
#
def ParseHeader(fileObj):
global lineCount
header = {}
while (True):
hdrLine = fileObj.readline().rstrip('\r\n')
lineCount = lineCount + 1
# Break out of the loop if we hit the end of header marker
if hdrLine.startswith('---'):
break
# Check to see that we have a well-formed header construct
match = re.findall(': ', hdrLine)
if match:
# Parse out foo: bar pairs & put into header dictionary
a = re.split(': ', hdrLine, 1)
header[a[0]] = a[1]
return header
fileCount = 0
writingFile = False
toFile = open('master-doc.txt')
toFile.close()
filenames = []
master = open('master-doc.txt')
firstLine = master.readline().rstrip('\r\n')
master.close()
if firstLine == '<!-- exploded -->':
print('Master file has already been exploded.')
exit(0)
if os.rename('master-doc.txt', 'master-doc.bak') == False:
print('Could not rename master-doc.txt!')
exit(-1)
master = open('master-doc.bak', 'r')
explode = open('master-doc.txt', 'w')
explode.write('<!-- exploded -->\n')
for line in master:
lineCount = lineCount + 1
# Do any header parsing if needed...
if line.startswith('---'):
# Close any open file from the previous header
if (writingFile):
toFile.close()
writingFile = False
noMove = False
header = ParseHeader(master)
# Make sure the filename we're making is unique...
basename = MakeFilename(header['title'])
inclFile = basename + '.html'
if 'file' in header:
inclFile = header['file']
else:
suffix = 1
while inclFile in filenames:
suffix = suffix + 1
inclFile = basename + '_' + str(suffix) + '.html'
# Find all files in the master file and write them out to include/,
# while removing it from the master file.
explode.write('\n---\n' + 'title: ' + header['title'] + '\n')
if header['part'] != 'part':
if 'menu_title' in header:
explode.write('menu_title: ' + header['menu_title'] + '\n')
if 'style' in header:
explode.write('style: ' + header['style'] + '\n')
if 'include' in header:
noMove = True
explode.write('include: ' + header['include'] + '\n')
explode.write('exclude: yes\n')
filenames.append(header['include'])
else:
explode.write('include: ' + inclFile + '\n')
filenames.append(inclFile)
if 'link' in header:
explode.write('link: ' + header['link'] + '\n')
if 'uri' in header:
explode.write('uri: ' + header['uri'] + '\n')
explode.write('part: ' + header['part'] + '\n' + '---\n')
# Only parts have no content...
if header['part'] != 'part':
if noMove:
explode.write('\n')
else:
fileCount = fileCount + 1
toFile = open('include/' + inclFile, 'w')
writingFile = True
else:
if writingFile:
toFile.write(line)
master.close()
explode.close()
print('Processed ' + str(lineCount) + ' lines.')
print('Exploded master document into ' + str(fileCount) + ' files.')
os.remove('master-doc.bak')