3 # Small program to 'explode' the master document automagically into separate
4 # files in the include/ directory.
7 # (C) 2017 Underground Software
16 # Create an all lowercase filename without special characters and with spaces
17 # replaced with dashes.
20 # This RE is shitty, but I can't think of a better one right now
21 fn = re.sub("[?!'&#:;_*()/\\,.]+", "", s)
23 fn = fn.replace(' ', '-')
30 toFile = open('master-doc.txt')
35 master = open('master-doc.txt')
36 firstLine = master.readline().rstrip('\r\n')
39 if firstLine == '<!-- exploded -->':
40 print('Master file has already been exploded.')
43 if os.rename('master-doc.txt', 'master-doc.bak') == False:
44 print('Could not rename master-doc.txt!')
47 master = open('master-doc.bak', 'r')
48 explode = open('master-doc.txt', 'w')
50 explode.write('<!-- exploded -->\n')
53 lineCount = lineCount + 1
55 # Do any header parsing if needed...
56 if line.startswith('---'):
58 # Close any open file from the previous header
67 hdrLine = master.readline().rstrip('\r\n')
68 lineCount = lineCount + 1
70 # Break out of the loop if we hit the end of header marker
71 if hdrLine.startswith('---'):
74 # Parse out foo: bar pairs & put into header dictionary
75 a = re.split(': ', hdrLine, 1)
78 # Make sure the filename we're making is unique...
79 inclFile = MakeFilename(header['title']) + '.html'
81 if inclFile in filenames:
83 inclFile = MakeFilename(header['title']) + str(suffix) + '.html'
85 while inclFile in filenames:
87 inclFile = MakeFilename(header['title']) + str(suffix) + '.html'
89 # Pull in files and write the result to the master file
90 explode.write('\n---\n' + 'title: ' + header['title'] + '\n')
92 if header['part'] != 'part':
93 if 'menu_title' in header:
94 explode.write('menu_title: ' + header['menu_title'] + '\n')
97 explode.write('link: ' + header['link'] + '\n')
100 explode.write('style: ' + header['style'] + '\n')
102 if 'include' in header:
104 explode.write('include: ' + header['include'] + '\n')
105 explode.write('exclude: yes\n')
106 filenames.append(header['include'])
108 explode.write('include: ' + inclFile + '\n')
109 filenames.append(inclFile)
111 explode.write('part: ' + header['part'] + '\n' + '---\n')
113 # Only parts have no content...
114 if header['part'] != 'part':
118 fileCount = fileCount + 1
120 toFile = open('include/' + inclFile, 'w')
122 toFile.write('---\n' + 'title: ' + header['title'] + '\n')
124 if 'menu_title' in header:
125 toFile.write('menu_title: ' + header['menu_title'] + '\n')
127 if 'style' in header:
128 toFile.write('style: ' + header['style'] + '\n')
130 toFile.write('---\n')
138 print('Processed ' + str(lineCount) + ' lines.')
139 print('Exploded master document into ' + str(fileCount) + ' files.')
141 os.remove('master-doc.bak')