3 # Script to take the master document and ancillary files and create the
4 # finished manual/website.
7 # (C) 2017 Underground Software
10 # Remnants (could go into the master document as the first header)
12 #bootstrap_path: /bootstrap-2.2.2
13 #page_title: The Ardour Manual
22 # This matches all *non* letter/number, ' ', '.', '-', and '_' chars
23 cleanString = re.compile(r'[^a-zA-Z0-9 \._-]+')
24 # This matches new 'unbreakable' links, up to the closing quote or anchor
25 findLinks = re.compile(r'@@[^#"]*')
28 # Create an all lowercase filename without special characters and with spaces
29 # replaced with dashes.
33 # Clean up the file name, removing all non letter/number or " .-_" chars.
34 # Also, convert to lower case and replace all spaces with dashes.
35 fn = cleanString.sub('', s).lower().replace(' ', '-')
36 # Double dashes can creep in from the above replacement, so we check for
38 fn = fn.replace('--', '-')
44 # Parse headers into a dictionary
46 def ParseHeader(fileObj):
50 hdrLine = fileObj.readline().rstrip('\r\n')
52 # Break out of the loop if we hit the end of header marker
53 if hdrLine.startswith('---'):
56 # Check to see that we have a well-formed header construct
57 match = re.findall(': ', hdrLine)
60 # Parse out foo: bar pairs & put into header dictionary
61 a = re.split(': ', hdrLine, 1)
68 # Turn a "part" name into an int
77 elif s == 'subchapter':
81 elif s == 'subsection':
87 # Converts a integer to a roman number
90 num_map = [(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'), (50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')]
102 # Capture the master document's structure (and content, if any) in a list
104 def GetFileStructure():
109 mf = open('master-doc.txt')
112 if ln.startswith('---'):
113 # First, stuff any content that we may have read into the current
114 # header's dictionary
116 fs[-1]['content'] = content
120 # Then, get the new header and do things to it
121 hdr = ParseHeader(mf)
122 level = PartToLevel(hdr['part'])
124 fnames[level] = MakeFilename(hdr['title'])
126 # Ickyness--user specified URIs
128 hdr['filename'] = hdr['uri']
132 for i in range(level + 1):
133 fullName = fullName + fnames[i] + '/'
135 # Strip trailing '/' on filename
136 hdr['filename'] = fullName[:-1]
140 if ('include' not in hdr) and (level > 0):
144 content = content + ln
146 # Catch the last file, since it would be missed above
148 fs[-1]['content'] = content
155 # Determine if a particular node has child nodes
157 def HaveChildren(fs, pos):
158 # If we're at the end of the list, there can be no children
159 if pos == len(fs) - 1:
162 # If the next node is at a lower level than the current node, we have
164 if fs[pos]['level'] < fs[pos + 1]['level']:
167 # Otherwise, no children at this node.
172 # Get the children at this level, and return them in a list
174 def GetChildren(fs, pos):
177 childLevel = fs[pos]['level']
179 while fs[pos]['level'] >= childLevel:
180 if fs[pos]['level'] == childLevel:
192 # Get the parent at this level
194 def GetParent(fs, pos):
195 thisLevel = fs[pos]['level']
198 while pos >= 0 and fs[pos]['level'] >= thisLevel:
204 # Creates the BreadCrumbs
206 def GetBreadCrumbs(fs, pos):
207 breadcrumbs = ' <span class="divider">></span> <li class="active">'+ fs[pos]['title'] + '</li>'
208 # The <span class="divider">></span> is for Bootstrap pre-3.0
210 pos = GetParent(fs,pos)
212 breadcrumbs=' <span class="divider">></span> <li><a href="/' + fs[pos]['filename'] + '/">'+ fs[pos]['title'] + '</a></li>'+ breadcrumbs
214 breadcrumbs = '<ol class="breadcrumb"><li><a href="/toc/index.html">Home</a></li>' + breadcrumbs + '</ol>'
218 # Make an array of children attached to each node in the file structure
219 # (It's a quasi-tree structure, and can be traversed as such.)
221 def FindChildren(fs):
224 for i in range(len(fs)):
225 if HaveChildren(fs, i):
226 childArray.append(GetChildren(fs, i))
228 childArray.append([])
234 # Make an array of the top level nodes in the file structure
236 def FindTopLevelNodes(fs):
239 for i in range(len(fs)):
240 if fs[i]['level'] == 0:
247 # Find all header links and create a dictionary out of them
249 def FindInternalLinks(fs):
254 linkDict['@@' + hdr['link']] = '/' + hdr['filename'] + '/'
260 # Internal links are of the form '@@link-name', which are references to the
261 # 'link:' field in the part header. We have to find all occurances and replace
262 # them with the appropriate link.
264 def FixInternalLinks(links, content, title):
266 match = findLinks.findall(content)
272 content = content.replace(s, links[s])
276 # Report missing link targets to the user (if any)
278 print('\nMissing link target' + ('s' if len(missing) > 1 else '') + ' in "' + title + '":')
289 # Recursively build a list of links based on the location of the page we're
290 # looking at currently
292 def BuildList(lst, fs, pagePos, cList):
293 content = '\n\n<dl>\n'
295 for i in range(len(lst)):
297 nextPos = lst[i + 1] if i + 1 < len(lst) else len(fs)
299 active = ' class=active' if curPos == pagePos else ''
300 menuTitle = fs[curPos]['menu_title'] if 'menu_title' in fs[curPos] else fs[curPos]['title']
301 content = content + '<dt' + active + '><a href="/' + fs[curPos]['filename'] + '/">' + menuTitle + '</a></dt><dd' + active + '>'
303 # If the current page is our page, and it has children, enumerate them
304 if curPos == pagePos:
305 if len(cList[curPos]) > 0:
306 content = content + BuildList(cList[curPos], fs, -1, cList)
308 # Otherwise, if our page lies between the current one and the next,
309 # build a list of links from those nodes one level down.
310 elif (pagePos > curPos) and (pagePos < nextPos):
311 content = content + BuildList(cList[curPos], fs, pagePos, cList)
313 content = content + '</dd>\n'
315 content = content + '</dl>\n'
320 # Create link sidebar given a position in the list.
322 def CreateLinkSidebar(fs, pos, childList):
324 # Build the list recursively from the top level nodes
325 content = BuildList(FindTopLevelNodes(fs), fs, pos, childList)
326 # Shove the TOC link in the top...
327 content = content[:7] + '<dt><a href="/toc/">Table of Contents</a></dt><dd></dd>\n' + content[7:]
334 # We have command line arguments now, so deal with them
335 parser = argparse.ArgumentParser(description='A build script for the Ardour Manual')
336 parser.add_argument('-v', '--verbose', action='store_true', help='Display the high-level structure of the manual')
337 parser.add_argument('-q', '--quiet', action='store_true', help='Suppress all output (overrides -v)')
338 parser.add_argument('-d', '--devmode', action='store_true', help='Add content to pages to help developers debug them')
339 args = parser.parse_args()
340 verbose = args.verbose
342 devmode = args.devmode
355 siteDir = './website/'
357 if not quiet and devmode:
358 print('Devmode active: scribbling extra junk to the manual...')
360 if os.access(siteDir, os.F_OK):
362 print('Removing stale HTML data...')
364 shutil.rmtree(siteDir)
366 shutil.copytree('./source', siteDir)
369 # Read the template, and fix the stuff that's fixed for all pages
370 temp = open('page-template.txt')
371 template = temp.read()
374 template = template.replace('{{page.bootstrap_path}}', '/bootstrap-2.2.2')
375 template = template.replace('{{page.page_title}}', 'The Ardour Manual')
378 # Parse out the master docuemnt's structure into a dictionary list
379 fileStruct = GetFileStructure()
381 # Build a quasi-tree structure listing children at level + 1 for each node
382 nodeChildren = FindChildren(fileStruct)
384 # Create a dictionary for translation of internal links to real links
385 links = FindInternalLinks(fileStruct)
388 print('Found ' + str(len(links)) + ' internal link target', end='')
389 print('.') if len(links) == 1 else print('s.')
392 master = open('master-doc.txt')
393 firstLine = master.readline().rstrip('\r\n')
396 if firstLine == '<!-- exploded -->':
397 print('Parsing exploded file...')
398 elif firstLine == '<!-- imploded -->':
399 print('Parsing imploded file...')
401 print('Parsing unknown type...')
405 for header in fileStruct:
406 fileCount = fileCount + 1
411 level = header['level']
413 # Handle Part/Chapter/subchapter/section/subsection numbering
423 levelNums[level] = levelNums[level] + 1;
425 # This is totally unnecessary, but nice; besides which, you can capture
426 # the output to a file to look at later if you like :-)
428 for i in range(level):
432 print('\nPart ' + num2roman(levelNums[0]) + ': ', end='')
434 print('\n\tChapter ' + str(levelNums[1]) + ': ', end='')
436 print(header['title'])
438 # Handle TOC scriblings...
440 toc = toc + '<h2>Part ' + num2roman(levelNums[level]) + ': ' + header['title'] + '</h2>\n';
442 toc = toc + ' <p id=chapter>Ch. ' + str(levelNums[level]) + ': <a href="/' + header['filename'] + '/">' + header['title'] + '</a></p>\n'
444 toc = toc + ' <a id=subchapter href="/' + header['filename'] + '/">' + header['title'] + '</a><br>\n'
446 toc = toc + ' <a id=subchapter href="/' + header['filename'] + '/">' + header['title'] + '</a><br>\n'
448 toc = toc + ' <a id=subchapter href="/' + header['filename'] + '/">' + header['title'] + '</a><br>\n'
450 # Make the 'this thing contains...' stuff
451 if HaveChildren(fileStruct, pageNumber):
452 pages = GetChildren(fileStruct, pageNumber)
455 more = more + '<li>' + '<a href="/' + fileStruct[pg]['filename'] + '/">' + fileStruct[pg]['title'] + '</a>' + '</li>\n'
457 more = '<div id=subtopics>\n' + '<h2>This section contains the following topics:</h2>\n' + '<ul>\n' + more + '</ul>\n' + '</div>\n'
459 parent = GetParent(fileStruct, pageNumber)
461 # Make the 'Previous' & 'Next' content
467 pLink = '<li><a title="' + fileStruct[pageNumber - 1]['title'] + '" href="/' + fileStruct[pageNumber - 1]['filename'] + '/" class="previous"> ← Previous </a></li>'
469 if pageNumber < len(fileStruct) - 1:
470 nLink = '<li><a title="' + fileStruct[pageNumber + 1]['title'] + '" href="/' + fileStruct[pageNumber + 1]['filename'] + '/" class="next"> Next → </a></li>'
473 uLink = '<li><a title="' + fileStruct[parent]['title'] + '" href="/' + fileStruct[parent]['filename'] + '/" class="active"> ↑ Up </a></li>'
475 uLink = '<li><a title="Ardour Table of Contents" href="/toc/index.html" class="active"> ↑ Up </a></li>'
477 prevnext = '<ul class=pager>' + pLink + uLink + nLink + '</ul>'
479 # Make the BreadCrumbs
480 breadcrumbs = GetBreadCrumbs(fileStruct, pageNumber)
482 # Create the link sidebar
483 sidebar = CreateLinkSidebar(fileStruct, pageNumber, nodeChildren)
485 # Parts DO NOT have any content, they are ONLY an organizing construct!
486 # Chapters, subchapters, sections & subsections can all have content,
487 # but the basic fundamental organizing unit WRT content is still the
490 if 'include' in header:
491 srcFile = open('include/' + header['include'])
492 content = srcFile.read()
495 # Get rid of any extant header in the include file
496 # (once this is accepted, we can nuke this bit, as content files
497 # will not have any headers or footers in them)
498 content = re.sub('---.*\n(.*\n)*---.*\n', '', content)
499 content = content.replace('{% children %}', '')
502 if 'content' in header:
503 content = header['content']
505 content = '[something went wrong]'
507 # Fix up any internal links
508 content = FixInternalLinks(links, content, header['title'])
510 # Add header information to the page if in dev mode
511 if devmode and 'link' in header:
512 content = '<h1>link: ' + header['link'] + '</h1>\n<br><br>\n' + content
514 # Set up the actual page from the template
515 if 'style' not in header:
516 page = re.sub("{% if page.style %}.*\n.*\n{% endif %}.*\n", "", template)
518 page = template.replace('{{page.style}}', header['style'])
519 page = page.replace('{% if page.style %}', '')
520 page = page.replace('{% endif %}', '')
522 page = page.replace('{{ page.title }}', header['title'])
523 page = page.replace('{% tree %}', sidebar)
524 page = page.replace('{% prevnext %}', prevnext)
525 page = page.replace('{% breadcrumbs %}', breadcrumbs)
526 page = page.replace('{{ content }}', content + more)
528 # Create the directory for the index.html file to go into (we use makedirs,
529 # because we have to in order to accomodate the 'uri' keyword)
530 os.makedirs(siteDir + header['filename'], 0o775, exist_ok=True)
532 # Finally, write the file!
533 destFile = open(siteDir + header['filename'] + '/index.html', 'w')
537 # Save filename for next header...
538 lastFile = header['filename']
539 pageNumber = pageNumber + 1
541 # Finally, create the TOC
542 sidebar = CreateLinkSidebar(fileStruct, -1, nodeChildren)
544 page = re.sub("{% if page.style %}.*\n.*\n{% endif %}.*\n", "", template)
545 page = page.replace('{{ page.title }}', 'Ardour Table of Contents')
546 page = page.replace('{% tree %}', sidebar)
547 page = page.replace('{{ content }}', toc)
548 page = page.replace('{% prevnext %}', '')
549 page = page.replace('{% breadcrumbs %}', '')
551 os.mkdir(siteDir + 'toc', 0o775)
552 tocFile = open(siteDir + 'toc/index.html', 'w')
557 print('Processed ' + str(fileCount) + ' files.')