]> Shamusworld >> Repos - ardour-manual-diverged/blobdiff - implode.py
Sync to master.
[ardour-manual-diverged] / implode.py
diff --git a/implode.py b/implode.py
new file mode 100755 (executable)
index 0000000..46c26e5
--- /dev/null
@@ -0,0 +1,130 @@
+#!/usr/bin/python
+#
+# Small program to 'implode' the master document automagically from separate
+# files in the include/ directory.
+#
+# by James Hammons
+# (C) 2017 Underground Software
+#
+
+import os
+import re
+import shutil
+
+
+def CheckForHeader(fn):
+       check = open(fn)
+
+       for line in check:
+               if line.startswith('---'):
+                       check.close()
+                       return True
+
+       check.close()
+       return False
+
+
+lineCount = 0
+fileCount = 0
+delList = []
+
+master = open('master-doc.txt')
+firstLine = master.readline().rstrip('\r\n')
+master.close()
+
+if firstLine == '<!-- imploded -->':
+       print('Master file has already been imploded.')
+       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')
+implode = open('master-doc.txt', 'w')
+
+implode.write('<!-- imploded -->\n')
+
+for line in master:
+       lineCount = lineCount + 1
+
+       # Do any header parsing if needed...
+       if line.startswith('---'):
+
+               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]
+
+               # Pull in files and write the result to the master file
+               implode.write('\n---\n' + 'title: ' + header['title'] + '\n')
+
+               if header['part'] != 'part':
+                       if 'menu_title' in header:
+                               implode.write('menu_title: ' + header['menu_title'] + '\n')
+
+                       if 'link' in header:
+                               implode.write('link: ' + header['link'] + '\n')
+
+                       if 'style' in header:
+                               implode.write('style: ' + header['style'] + '\n')
+
+                       if ('exclude' in header) and ('include' in header):
+                               noMove = True
+                               implode.write('include: ' + header['include'] + '\n')
+                               implode.write('exclude: yes\n')
+
+               implode.write('part: ' + header['part'] + '\n' + '---\n')
+
+               # Only parts have no content...
+               if header['part'] != 'part':
+                       if noMove:
+                               implode.write('\n')
+                       else:
+                               fileCount = fileCount + 1
+                               inclFile = 'include/' + header['include']
+
+                               try:
+                                       fromFile = open(inclFile)
+                               except (FileNotFoundError):
+                                       print('Could not find include file "include/' + header['include'] + '"; aborting!')
+                                       os.remove('master-doc.txt')
+                                       os.rename('master-doc.bak', 'master-doc.txt')
+                                       exit(-1)
+
+                               if CheckForHeader(inclFile) == True:
+
+                                       # Skip the header
+                                       while fromFile.readline().startswith('---') == False:
+                                               pass
+
+                                       ln = fromFile.readline()
+
+                                       while fromFile.readline().startswith('---') == False:
+                                               pass
+
+                               shutil.copyfileobj(fromFile, implode)
+                               fromFile.close()
+                               delList.append(inclFile)
+
+master.close()
+implode.close()
+
+print('Processed ' + str(lineCount) + ' lines.')
+print('Imploded master document from ' + str(fileCount) + ' files.')
+
+os.remove('master-doc.bak')
+
+for name in delList:
+       os.remove(name)
+