import re
import shutil
+lineCount = 0
+
#
# Create an all lowercase filename without special characters and with spaces
# replaced with dashes.
#
def MakeFilename(s):
- # This RE is shitty, but I can't think of a better one right now
- fn = re.sub("[?!'&#:;_*()/\\,.]+", "", s)
+ # Cleans up the file name, removing all non ASCII or .-_ chars
+ fn = re.sub(r'[^.\-_a-zA-Z0-9 ]', '', s)
fn = fn.lower()
fn = fn.replace(' ', '-')
return fn
-lineCount = 0
+#
+# 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()
writingFile = False
- header = {}
noMove = False
-
- while (True):
- hdrLine = master.readline().rstrip('\r\n')
- lineCount = lineCount + 1
-
- # Break out of the loop if we hit the end of header marker
- if hdrLine.startswith('---'):
- break
-
- # Parse out foo: bar pairs & put into header dictionary
- a = re.split(': ', hdrLine, 1)
- header[a[0]] = a[1]
+ header = ParseHeader(master)
# Make sure the filename we're making is unique...
- inclFile = MakeFilename(header['title']) + '.html'
+ basename = MakeFilename(header['title'])
+ inclFile = basename + '.html'
- if inclFile in filenames:
- suffix = 2
- inclFile = MakeFilename(header['title']) + str(suffix) + '.html'
+ if 'file' in header:
+ inclFile = header['file']
+ else:
+ suffix = 1
while inclFile in filenames:
suffix = suffix + 1
- inclFile = MakeFilename(header['title']) + str(suffix) + '.html'
+ inclFile = basename + '_' + str(suffix) + '.html'
- # Pull in files and write the result to the master file
+ # 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':
toFile = open('include/' + inclFile, 'w')
writingFile = True
- toFile.write('---\n' + 'title: ' + header['title'] + '\n')
-
- if 'menu_title' in header:
- toFile.write('menu_title: ' + header['menu_title'] + '\n')
-
- if 'style' in header:
- toFile.write('style: ' + header['style'] + '\n')
- toFile.write('---\n')
else:
if writingFile:
toFile.write(line)