]> Shamusworld >> Repos - ardour-manual-diverged/blobdiff - explode.py
Sync to master.
[ardour-manual-diverged] / explode.py
diff --git a/explode.py b/explode.py
new file mode 100755 (executable)
index 0000000..85eb250
--- /dev/null
@@ -0,0 +1,142 @@
+#!/usr/bin/python
+#
+# Small program to 'explode' the master document automagically into separate
+# files in the include/ directory.
+#
+# by James Hammons
+# (C) 2017 Underground Software
+#
+
+import os
+import re
+import shutil
+
+
+#
+# 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)
+       fn = fn.lower()
+       fn = fn.replace(' ', '-')
+       return fn
+
+
+lineCount = 0
+fileCount = 0
+writingFile = False
+toFile = open('master-doc.txt')
+toFile.close()
+filenames = []
+
+
+master = open('master-doc.txt')
+firstLine = master.readline().rstrip('\r\n')
+master.close()
+
+if firstLine == '<!-- exploded -->':
+       print('Master file has already been exploded.')
+       exit(0)
+
+if os.rename('master-doc.txt', 'master-doc.bak') == False:
+       print('Could not rename master-doc.txt!')
+       exit(-1)
+
+master = open('master-doc.bak', 'r')
+explode = open('master-doc.txt', 'w')
+
+explode.write('<!-- exploded -->\n')
+
+for line in master:
+       lineCount = lineCount + 1
+
+       # Do any header parsing if needed...
+       if line.startswith('---'):
+
+               # Close any open file from the previous header
+               if (writingFile):
+                       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]
+
+               # Make sure the filename we're making is unique...
+               inclFile = MakeFilename(header['title']) + '.html'
+
+               if inclFile in filenames:
+                       suffix = 2
+                       inclFile = MakeFilename(header['title']) + str(suffix) + '.html'
+
+                       while inclFile in filenames:
+                               suffix = suffix + 1
+                               inclFile = MakeFilename(header['title']) + str(suffix) + '.html'
+
+               # Pull in files and write the result to the master file
+               explode.write('\n---\n' + 'title: ' + header['title'] + '\n')
+
+               if header['part'] != 'part':
+                       if 'menu_title' in header:
+                               explode.write('menu_title: ' + header['menu_title'] + '\n')
+
+                       if 'link' in header:
+                               explode.write('link: ' + header['link'] + '\n')
+
+                       if 'style' in header:
+                               explode.write('style: ' + header['style'] + '\n')
+
+                       if 'include' in header:
+                               noMove = True
+                               explode.write('include: ' + header['include'] + '\n')
+                               explode.write('exclude: yes\n')
+                               filenames.append(header['include'])
+                       else:
+                               explode.write('include: ' + inclFile + '\n')
+                               filenames.append(inclFile)
+
+               explode.write('part: ' + header['part'] + '\n' + '---\n')
+
+               # Only parts have no content...
+               if header['part'] != 'part':
+                       if noMove:
+                               explode.write('\n')
+                       else:
+                               fileCount = fileCount + 1
+
+                               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)
+
+master.close()
+explode.close()
+
+print('Processed ' + str(lineCount) + ' lines.')
+print('Exploded master document into ' + str(fileCount) + ' files.')
+
+os.remove('master-doc.bak')
+