]> Shamusworld >> Repos - ardour-manual-diverged/blobdiff - build.py
Should be final version of Python build script.
[ardour-manual-diverged] / build.py
index e3eeef3ca8135376ada362f4b0d8262bd4b5c7f4..ec09ad657393f4e2ac4bd77146132355cdc029db 100755 (executable)
--- a/build.py
+++ b/build.py
@@ -15,6 +15,7 @@
 import os
 import re
 import shutil
+import argparse
 
 
 #
@@ -183,13 +184,32 @@ def FindTopLevelNodes(fs):
 
 
 #
-# Internal links are of the form '@link-name', which are references to the
+# Find all header links and create a dictionary out of them
+#
+def FindInternalLinks(fs):
+       linkDict = {}
+
+       for hdr in fs:
+               if 'link' in hdr:
+                       linkDict['@@' + hdr['link']] = '/' + hdr['filename'] + '/'
+
+       return linkDict
+
+
+#
+# Internal links are of the form '@@link-name', which are references to the
 # 'link:' field in the part header. We have to find all occurances and replace
 # them with the appropriate link.
 #
-def FixInternalLinks(content):
-#make a dictionary ahead of time of <link:, filename:> pairs
-       return content
+def FixInternalLinks(links, content):
+
+       # Make key1|key2|key3|... out of our links keys
+       pattern = re.compile('|'.join(links.keys()))
+
+       # Use a lambda callback to substitute each occurance found
+       result = pattern.sub(lambda x: links[x.group()], content)
+
+       return result
 
 
 #
@@ -237,11 +257,23 @@ def CreateLinkSidebar(fs, pos, childList):
 
 # Preliminaries
 
+# We have command line arguments now, so deal with them
+parser = argparse.ArgumentParser(description='A build script for the Ardour Manual')
+parser.add_argument('-v', '--verbose', action='store_true', help='Display the high-level structure of the manual')
+parser.add_argument('-q', '--quiet', action='store_true', help='Suppress all output (overrides -v)')
+args = parser.parse_args()
+verbose = args.verbose
+quiet = args.quiet
+
+if quiet:
+       verbose = False
+
+
 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' ]
 
-verbose = True
+#verbose = False
 level = 0
 fileCount = 0
 levelNums = [0]*6
@@ -253,7 +285,9 @@ pageNumber = 0
 siteDir = './website/'
 
 if os.access(siteDir, os.F_OK):
-       print('Removing stale HTML data...')
+       if not quiet:
+               print('Removing stale HTML data...')
+
        shutil.rmtree(siteDir)
 
 shutil.copytree('./source', siteDir)
@@ -275,20 +309,30 @@ template = template.replace('{{page.page_title}}', 'The Ardour Manual')
 
 # Parse out the master docuemnt's structure into a dictionary list
 fileStruct = GetFileStructure()
+
+# Build a quasi-tree structure listing children at level + 1 for each node
 nodeChildren = FindChildren(fileStruct)
 
-# Here we go!
+# Create a dictionary for translation of internal links to real links
+links = FindInternalLinks(fileStruct)
 
-master = open('master-doc.txt')
-firstLine = master.readline().rstrip('\r\n')
-master.close()
+if not quiet:
+       print('Found ' + str(len(links)) + ' internal link target', end='')
+       print('.') if len(links) == 1 else print('s.')
 
-if firstLine == '<!-- exploded -->':
-       print('Parsing exploded file...')
-elif firstLine == '<!-- imploded -->':
-       print('Parsing imploded file...')
-else:
-       print('Parsing unknown type...')
+if not quiet:
+       master = open('master-doc.txt')
+       firstLine = master.readline().rstrip('\r\n')
+       master.close()
+
+       if firstLine == '<!-- exploded -->':
+               print('Parsing exploded file...')
+       elif firstLine == '<!-- imploded -->':
+               print('Parsing imploded file...')
+       else:
+               print('Parsing unknown type...')
+
+# Here we go!
 
 for header in fileStruct:
        fileCount = fileCount + 1
@@ -312,7 +356,6 @@ for header in fileStruct:
 
        # This is totally unnecessary, but nice; besides which, you can capture
        # the output to a file to look at later if you like :-)
-       # [TODO: add command line switch, default to False]
        if verbose:
                for i in range(level):
                        print('\t', end='')
@@ -382,6 +425,9 @@ for header in fileStruct:
                        else:
                                content = '[something went wrong]'
 
+       # Fix up any internal links
+       content = FixInternalLinks(links, content)
+
        # Set up the actual page from the template
        if 'style' not in header:
                page = re.sub("{% if page.style %}.*\n.*\n{% endif %}.*\n", "", template)
@@ -421,5 +467,6 @@ tocFile = open(siteDir + 'toc/index.html', 'w')
 tocFile.write(page)
 tocFile.close()
 
-print('Processed ' + str(fileCount) + ' files.')
+if not quiet:
+       print('Processed ' + str(fileCount) + ' files.')