import os
import re
import shutil
+import argparse
#
#
-# 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
#
# 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
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)
# 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
# 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='')
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)
tocFile.write(page)
tocFile.close()
-print('Processed ' + str(fileCount) + ' files.')
+if not quiet:
+ print('Processed ' + str(fileCount) + ' files.')
suffix = suffix + 1
inclFile = MakeFilename(header['title']) + 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')
+#no more headers in separate files ffs...
+# 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)