3 # Small program to 'explode' the master document automagically into separate
4 # files in the include/ directory.
7 # (C) 2017 Underground Software
15 cleanString = re.compile(r'[^a-zA-Z0-9 \._-]+')
19 # Create an all lowercase filename without special characters and with spaces
20 # replaced with dashes.
24 # Clean up the file name, removing all non letter/number or " .-_" chars.
25 # Also, convert to lower case and replace all spaces with dashes.
26 fn = cleanString.sub('', s).lower().replace(' ', '-')
27 # Double dashes can creep in from the above replacement, so we check for
29 fn = fn.replace('--', '-')
35 # Parse headers into a dictionary
37 def ParseHeader(fileObj):
42 hdrLine = fileObj.readline().rstrip('\r\n')
43 lineCount = lineCount + 1
45 # Break out of the loop if we hit the end of header marker
46 if hdrLine.startswith('---'):
49 # Check to see that we have a well-formed header construct
50 match = re.findall(': ', hdrLine)
53 # Parse out foo: bar pairs & put into header dictionary
54 a = re.split(': ', hdrLine, 1)
62 toFile = open('master-doc.txt')
67 master = open('master-doc.txt')
68 firstLine = master.readline().rstrip('\r\n')
71 if firstLine == '<!-- exploded -->':
72 print('Master file has already been exploded.')
75 if os.rename('master-doc.txt', 'master-doc.bak') == False:
76 print('Could not rename master-doc.txt!')
79 master = open('master-doc.bak', 'r')
80 explode = open('master-doc.txt', 'w')
82 explode.write('<!-- exploded -->\n')
85 lineCount = lineCount + 1
87 # Do any header parsing if needed...
88 if line.startswith('---'):
90 # Close any open file from the previous header
96 header = ParseHeader(master)
98 # Make sure the filename we're making is unique...
99 basename = MakeFilename(header['title'])
100 inclFile = basename + '.html'
103 inclFile = header['file']
107 while inclFile in filenames:
109 inclFile = basename + '_' + str(suffix) + '.html'
111 # Find all files in the master file and write them out to include/,
112 # while removing it from the master file.
113 explode.write('\n---\n' + 'title: ' + header['title'] + '\n')
115 if header['part'] != 'part':
116 if 'menu_title' in header:
117 explode.write('menu_title: ' + header['menu_title'] + '\n')
119 if 'style' in header:
120 explode.write('style: ' + header['style'] + '\n')
122 if 'include' in header:
124 explode.write('include: ' + header['include'] + '\n')
125 explode.write('exclude: yes\n')
126 filenames.append(header['include'])
128 explode.write('include: ' + inclFile + '\n')
129 filenames.append(inclFile)
132 explode.write('link: ' + header['link'] + '\n')
135 explode.write('uri: ' + header['uri'] + '\n')
137 explode.write('part: ' + header['part'] + '\n' + '---\n')
139 # Only parts have no content...
140 if header['part'] != 'part':
144 fileCount = fileCount + 1
146 toFile = open('include/' + inclFile, 'w')
156 print('Processed ' + str(lineCount) + ' lines.')
157 print('Exploded master document into ' + str(fileCount) + ' files.')
159 os.remove('master-doc.bak')