]> Shamusworld >> Repos - ardour-manual-diverged/blobdiff - build.py
Initial stab at rewriting munge as a Python script.
[ardour-manual-diverged] / build.py
diff --git a/build.py b/build.py
new file mode 100755 (executable)
index 0000000..021f6a0
--- /dev/null
+++ b/build.py
@@ -0,0 +1,264 @@
+#!/usr/bin/python
+#
+# Script to take the master document and create something that build.rb wants.
+# Eventually, this will replace build.rb
+#
+# Ultimately, we will write directly to the finished web site structure instead
+# of creating this half-assed thing that we then rely on the other ruby script
+# to handle.
+#
+# by James Hammons
+#
+
+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
+
+
+# Preliminaries
+
+buildDir = './_manual.munge/'
+
+if os.access(buildDir, os.F_OK):
+       print('Removing stale manual data...')
+       shutil.rmtree(buildDir)
+
+os.mkdir(buildDir, 0o774)
+
+# Yeah, need to make a symlink in include/ too :-P
+# [this will go away when the rewrite happens]
+if (os.access('include/_manual', os.F_OK) == False):
+       os.symlink('../_manual/', 'include/_manual')
+
+
+# Here we go!
+
+master = open('master-doc.txt')
+
+toc = open(buildDir + '00_toc.html', 'w')
+toc.write('---\n' + 'title: Ardour Table of Contents\n' + '---\n\n')
+
+
+roman = [ '0', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X',
+       'XI', 'XII', 'XIII', 'XIV', 'XV', 'XVI', 'XVII', 'XVIII', 'XIX', 'XX',
+       'XXI', 'XXII', 'XXIII', 'XXIV', 'XXV', 'XXVI', 'XXVII', 'XXVIII', 'XXIX', 'XXX' ]
+level = 0
+lastLevel = 0
+lineCount = 0
+levelNames = [None]*6
+levelNums = [0]*6
+writingToFile = False
+# This is a shitty way of getting a filehandle...
+htmlFile = open('master-doc.txt')
+htmlFile.close()
+lastFile = ''
+
+firstLine = master.readline().rstrip('\r\n')
+
+if firstLine == '<!-- exploded -->':
+       print('Parsing exploded file...\n')
+elif firstLine == '<!-- imploded -->':
+       print('Parsing imploded file...\n')
+else:
+       print('Parsing unknown type...\n')
+
+for line in master:
+       lineCount = lineCount + 1
+
+       # Do any header parsing if needed...
+       if line.startswith('---'):
+
+               # Close any open files that may have been opened from the last header...
+               if writingToFile:
+                       htmlFile.close()
+                       writingToFile = False
+
+               header = {}
+
+               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 has been parsed, now do something about it...
+               lastLevel = level
+
+               # Handle Part/Chapter/subchapter/section/subsection numbering
+               if (header['part'] == 'part'):
+                       level = 0
+                       levelNums[2] = 0
+               elif (header['part'] == 'chapter'):
+                       level = 1
+                       levelNums[2] = 0
+               elif (header['part'] == 'subchapter'):
+                       level = 2
+                       levelNums[3] = 0
+               elif (header['part'] == 'section'):
+                       level = 3
+                       levelNums[4] = 0
+               elif (header['part'] == 'subsection'):
+                       level = 4
+
+               levelNums[level] = levelNums[level] + 1;
+
+               # This is totally unnecessary, but nice; besides which, you can capture
+               # the output to a file to look at later if you like :-)
+               for i in range(level):
+                       print('\t', end='')
+
+               if (level == 0):
+                       print('\nPart ' + roman[levelNums[0]] + ': ', end='')
+               elif (level == 1):
+                       print('\n\tChapter ' + str(levelNums[1]) + ': ', end='')
+
+               print(header['title'])
+
+               # Make a filename from the title...
+               levelNames[level] = MakeFilename(header['title'])
+
+               path = ''
+
+               for i in range(level):
+                       path = path + str(levelNums[i]).zfill(2) + '_' + levelNames[i] + '/'
+
+               path = path + str(levelNums[level]).zfill(2) + '_' + levelNames[level]
+
+               # Append the appropriate footer, if the current file is one level
+               # down from the previous...
+               if ((level > 0) and (level > lastLevel)):
+                       nfile = open(buildDir + lastFile + '.html', 'a')
+                       nfile.write('\n{% children %}\n\n')
+                       nfile.close()
+
+               # Parts DO NOT have any content, they are ONLY an organizing construct!
+               if (level == 0):
+                       toc.write('<h2>Part ' + roman[levelNums[level]] + ': ' + header['title'] + '</h2>\n');
+
+                       os.mkdir(buildDir + path, 0o774)
+                       nfile = open(buildDir + path + '.html', 'w')
+                       nfile.write('---\n' + 'title: ' + header['title'] + '\n')
+
+                       if ('menu_title' in header):
+                               nfile.write('menu_title: ' + header['menu_title'] + '\n')
+
+                       nfile.write('---\n' + '\n{% children %}\n\n')
+                       nfile.close()
+
+               # Chapters, subchapters, sections & subsections all can have content.
+               # But the basic fundamental organizing unit WRT content is still the
+               # chapter.
+               elif (level == 1):
+                       toc.write('  <p id=chapter>Ch. ' + str(levelNums[level]) + ':&nbsp;&nbsp;<a href="/' + levelNames[0] + '/' + levelNames[1] + '/">' + header['title'] + '</a></p>\n')
+
+                       os.mkdir(buildDir + path, 0o774)
+
+                       if ('include' in header):
+                               shutil.copy('include/' + header['include'], buildDir + path + '.html')
+                       else:
+                               htmlFile = open(buildDir + path + '.html', 'w')
+                               writingToFile = True
+                               htmlFile.write('---\n' + 'title: ' + header['title'] + '\n')
+
+                               if ('menu_title' in header):
+                                       htmlFile.write('menu_title: ' + header['menu_title'] + '\n')
+
+                               if ('style' in header):
+                                       htmlFile.write('style: ' + header['style'] + '\n')
+
+                               htmlFile.write('---\n\n')
+
+               elif (level == 2):
+                       toc.write('    <a id=subchapter href="/' + levelNames[0] + '/' + levelNames[1] + '/' + levelNames[2] + '/">' + header['title'] + '</a><br>\n')
+
+                       os.mkdir(buildDir + path, 0o774)
+
+                       if ('include' in header):
+                               shutil.copy('include/' + header['include'], buildDir + path + '.html')
+                       else:
+                               htmlFile = open(buildDir + path + '.html', 'w')
+                               writingToFile = True
+                               htmlFile.write('---\n' + 'title: ' + header['title'] + '\n')
+
+                               if ('menu_title' in header):
+                                       htmlFile.write('menu_title: ' + header['menu_title'] + '\n')
+
+                               if ('style' in header):
+                                       htmlFile.write('style: ' + header['style'] + '\n')
+
+                               htmlFile.write('---\n\n')
+
+               elif (level == 3):
+                       toc.write('      <a id=subchapter href="/' + levelNames[0] + '/' + levelNames[1] + '/' + levelNames[2] + '/' + levelNames[3] + '/">' + header['title'] + '</a><br>\n')
+
+                       os.mkdir(buildDir + path, 0o774)
+
+                       if ('include' in header):
+                               shutil.copy('include/' + header['include'], buildDir + path + '.html')
+                       else:
+                               htmlFile = open(buildDir + path + '.html', 'w')
+                               writingToFile = True
+                               htmlFile.write('---\n' + 'title: ' + header['title'] + '\n')
+
+                               if ('menu_title' in header):
+                                       htmlFile.write('menu_title: ' + header['menu_title'] + '\n')
+
+                               if ('style' in header):
+                                       htmlFile.write('style: ' + header['style'] + '\n')
+
+                               htmlFile.write('---\n\n')
+
+               elif (level == 4):
+                       toc.write('      <a id=subchapter href="/' + levelNames[0] + '/' + levelNames[1] + '/' + levelNames[2] + '/' + levelNames[3] + '/' + levelNames[4] + '/">' + header['title'] + '</a><br>\n')
+
+                       os.mkdir(buildDir + path, 0o774)
+
+                       if ('include' in header):
+                               shutil.copy('include/' + header['include'], buildDir + path + '.html')
+                       else:
+                               htmlFile = open(buildDir + path + '.html', 'w')
+                               writingToFile = True
+                               htmlFile.write('---\n' + 'title: ' + header['title'] + '\n')
+
+                               if ('menu_title' in header):
+                                       htmlFile.write('menu_title: ' + header['menu_title'] + '\n')
+
+                               if ('style' in header):
+                                       htmlFile.write('style: ' + header['style'] + '\n')
+
+                               htmlFile.write('---\n\n')
+
+               # Save filename for next header...
+               lastFile = path
+
+       # No header, in that case, just dump the lines into the currently open file
+       else:
+               if (writingToFile and (level > 0)):
+                       htmlFile.write(line)
+
+
+master.close()
+toc.close()
+
+if writingToFile:
+       htmlFile.close()
+
+print('\nProcessed ' + str(lineCount) + ' lines.')
+