3 # Small program to 'explode' the master document automagically into separate
4 # files in the include/ directory.
7 # (C) 2017 Underground Software
18 # Create an all lowercase filename without special characters and with spaces
19 # replaced with dashes.
22 # Cleans up the file name, removing all non ASCII or .-_ chars
23 fn = re.sub(r'[^.\-_a-zA-Z0-9 ]', '', s)
25 fn = fn.replace(' ', '-')
30 # Parse headers into a dictionary
32 def ParseHeader(fileObj):
37 hdrLine = fileObj.readline().rstrip('\r\n')
38 lineCount = lineCount + 1
40 # Break out of the loop if we hit the end of header marker
41 if hdrLine.startswith('---'):
44 # Check to see that we have a well-formed header construct
45 match = re.findall(': ', hdrLine)
48 # Parse out foo: bar pairs & put into header dictionary
49 a = re.split(': ', hdrLine, 1)
57 toFile = open('master-doc.txt')
62 master = open('master-doc.txt')
63 firstLine = master.readline().rstrip('\r\n')
66 if firstLine == '<!-- exploded -->':
67 print('Master file has already been exploded.')
70 if os.rename('master-doc.txt', 'master-doc.bak') == False:
71 print('Could not rename master-doc.txt!')
74 master = open('master-doc.bak', 'r')
75 explode = open('master-doc.txt', 'w')
77 explode.write('<!-- exploded -->\n')
80 lineCount = lineCount + 1
82 # Do any header parsing if needed...
83 if line.startswith('---'):
85 # Close any open file from the previous header
91 header = ParseHeader(master)
93 # Make sure the filename we're making is unique...
94 basename = MakeFilename(header['title'])
95 inclFile = basename + '.html'
98 inclFile = header['file']
102 while inclFile in filenames:
104 inclFile = basename + '_' + str(suffix) + '.html'
106 # Find all files in the master file and write them out to include/,
107 # while removing it from the master file.
108 explode.write('\n---\n' + 'title: ' + header['title'] + '\n')
110 if header['part'] != 'part':
111 if 'menu_title' in header:
112 explode.write('menu_title: ' + header['menu_title'] + '\n')
114 if 'style' in header:
115 explode.write('style: ' + header['style'] + '\n')
117 if 'include' in header:
119 explode.write('include: ' + header['include'] + '\n')
120 explode.write('exclude: yes\n')
121 filenames.append(header['include'])
123 explode.write('include: ' + inclFile + '\n')
124 filenames.append(inclFile)
127 explode.write('link: ' + header['link'] + '\n')
130 explode.write('uri: ' + header['uri'] + '\n')
132 explode.write('part: ' + header['part'] + '\n' + '---\n')
134 # Only parts have no content...
135 if header['part'] != 'part':
139 fileCount = fileCount + 1
141 toFile = open('include/' + inclFile, 'w')
151 print('Processed ' + str(lineCount) + ' lines.')
152 print('Exploded master document into ' + str(fileCount) + ' files.')
154 os.remove('master-doc.bak')