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 # Find all files in the master file and write them out to include/,
90 # while removing it from the master file.
91 explode.write('\n---\n' + 'title: ' + header['title'] + '\n')
93 if header['part'] != 'part':
94 if 'menu_title' in header:
95 explode.write('menu_title: ' + header['menu_title'] + '\n')
98 explode.write('link: ' + header['link'] + '\n')
100 if 'style' in header:
101 explode.write('style: ' + header['style'] + '\n')
103 if 'include' in header:
105 explode.write('include: ' + header['include'] + '\n')
106 explode.write('exclude: yes\n')
107 filenames.append(header['include'])
109 explode.write('include: ' + inclFile + '\n')
110 filenames.append(inclFile)
112 explode.write('part: ' + header['part'] + '\n' + '---\n')
114 # Only parts have no content...
115 if header['part'] != 'part':
119 fileCount = fileCount + 1
121 toFile = open('include/' + inclFile, 'w')
123 #no more headers in separate files ffs...
124 # toFile.write('---\n' + 'title: ' + header['title'] + '\n')
126 # if 'menu_title' in header:
127 # toFile.write('menu_title: ' + header['menu_title'] + '\n')
129 # if 'style' in header:
130 # toFile.write('style: ' + header['style'] + '\n')
132 # toFile.write('---\n')
140 print('Processed ' + str(lineCount) + ' lines.')
141 print('Exploded master document into ' + str(fileCount) + ' files.')
143 os.remove('master-doc.bak')