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-3.3.7
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'"@@[^#"]*[#"]')
26 githuburl = 'https://github.com/Ardour/manual/edit/master/include/'
29 # Create an all lowercase filename without special characters and with spaces
30 # replaced with dashes.
34 # Clean up the file name, removing all non letter/number or " .-_" chars.
35 # Also, convert to lower case and replace all spaces with dashes.
36 fn = cleanString.sub('', s).lower().replace(' ', '-')
37 # Double dashes can creep in from the above replacement, so we check for
39 fn = fn.replace('--', '-')
45 # Parse headers into a dictionary
47 def ParseHeader(fileObj):
51 hdrLine = fileObj.readline().rstrip('\r\n')
53 # Break out of the loop if we hit the end of header marker
54 if hdrLine.startswith('---'):
57 # Check to see that we have a well-formed header construct
58 match = re.findall(': ', hdrLine)
61 # Parse out foo: bar pairs & put into header dictionary
62 a = re.split(': ', hdrLine, 1)
69 # Turn a "part" name into an int
78 elif s == 'subchapter':
82 elif s == 'subsection':
88 # Converts a integer to a roman number
91 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')]
103 # Capture the master document's structure (and content, if any) in a list
105 def GetFileStructure():
110 mf = open('master-doc.txt')
113 if ln.startswith('---'):
114 # First, stuff any content that we may have read into the current
115 # header's dictionary
117 fs[-1]['content'] = content
121 # Then, get the new header and do things to it
122 hdr = ParseHeader(mf)
123 level = PartToLevel(hdr['part'])
125 fnames[level] = MakeFilename(hdr['title'])
127 # Ickyness--user specified URIs
129 hdr['filename'] = hdr['uri']
133 for i in range(level + 1):
134 fullName = fullName + fnames[i] + '/'
136 # Strip trailing '/' on filename
137 hdr['filename'] = fullName[:-1]
141 if ('include' not in hdr) and (level > 0):
145 content = content + ln
147 # Catch the last file, since it would be missed above
149 fs[-1]['content'] = content
156 # Determine if a particular node has child nodes
158 def HaveChildren(fs, pos):
159 # If we're at the end of the list, there can be no children
160 if pos == len(fs) - 1:
163 # If the next node is at a lower level than the current node, we have
165 if fs[pos]['level'] < fs[pos + 1]['level']:
168 # Otherwise, no children at this node.
173 # Get the children at this level, and return them in a list
175 def GetChildren(fs, pos):
178 childLevel = fs[pos]['level']
180 while fs[pos]['level'] >= childLevel:
181 if fs[pos]['level'] == childLevel:
194 # Get the parent at this level
196 def GetParent(fs, pos):
197 thisLevel = fs[pos]['level']
200 while pos >= 0 and fs[pos]['level'] >= thisLevel:
207 # Creates the BreadCrumbs
209 def GetBreadCrumbs(fs, pos):
210 # The <span class="divider">></span> is for Bootstrap pre-3.0
211 breadcrumbs = '<li class="active">'+ fs[pos]['title'] + '</li>'
214 pos = GetParent(fs, pos)
217 breadcrumbs='<li><a href="/' + fs[pos]['filename'] + '/">'+ fs[pos]['title'] + '</a></li>'+ breadcrumbs
219 breadcrumbs = '<ul class="breadcrumb"><li><a href="/toc/index.html">Home</a></li>' + breadcrumbs + '</ul>'
224 # Make an array of children attached to each node in the file structure
225 # (It's a quasi-tree structure, and can be traversed as such.)
227 def FindChildren(fs):
230 for i in range(len(fs)):
231 if HaveChildren(fs, i):
232 childArray.append(GetChildren(fs, i))
234 childArray.append([])
240 # Make an array of the top level nodes in the file structure
242 def FindTopLevelNodes(fs):
245 for i in range(len(fs)):
246 if fs[i]['level'] == 0:
253 # Find all header links and create a dictionary out of them
255 def FindInternalLinks(fs):
260 linkDict['"@@' + hdr['link'] + '"'] = '"/' + hdr['filename'] + '/"'
261 linkDict['"@@' + hdr['link'] + '#'] = '"/' + hdr['filename'] + '/index.html#'
267 # Same as above, but create anchors (for the one-page version)
269 def FindInternalAnchors(fs):
274 linkDict['"@@' + hdr['link'] + '"'] = '"#' + hdr['link'] + '"'
275 linkDict['"@@' + hdr['link'] + '#'] = '"#' + hdr['link'] + '"'
282 # Internal links are of the form '@@link-name', which are references to the
283 # 'link:' field in the part header. We have to find all occurrences and replace
284 # them with the appropriate link.
286 def FixInternalLinks(links, content, title):
288 match = findLinks.findall(content)
294 content = content.replace(s, links[s])
298 # Report missing link targets to the user (if any)
300 print('\nMissing link target' + ('s' if len(missing) > 1 else '') + ' in "' + title + '":')
311 # Recursively build a list of links based on the location of the page we're
312 # looking at currently
314 def BuildList(lst, fs, pagePos, cList):
315 content = '\n\n<ul>\n'
317 for i in range(len(lst)):
319 nextPos = lst[i + 1] if i + 1 < len(lst) else len(fs)
321 active = ' class=active' if curPos == pagePos else ''
322 menuTitle = fs[curPos]['menu_title'] if 'menu_title' in fs[curPos] else fs[curPos]['title']
323 content = content + '<li' + active + '><a href="/' + fs[curPos]['filename'] + '/">' + menuTitle + '</a></li>'
325 # If the current page is our page, and it has children, enumerate them
326 if curPos == pagePos:
327 if len(cList[curPos]) > 0:
328 content = content + BuildList(cList[curPos], fs, -1, cList)
330 # Otherwise, if our page lies between the current one and the next,
331 # build a list of links from those nodes one level down.
332 elif (pagePos > curPos) and (pagePos < nextPos):
333 content = content + BuildList(cList[curPos], fs, pagePos, cList)
335 content = content + '\n</ul>\n'
341 # Builds the sidebar for the one-page version
343 def BuildOnePageSidebar(fs):
345 content = '\n\n<ul style="white-space:nowrap;">\n'
349 for i in range(len(fs)):
350 # Handle Part/Chapter/subchapter/section/subsection numbering
351 level = fs[i]['level']
365 levelNums[level] = levelNums[level] + 1;
368 while j > 0: #level 0 is the part number which is not shown
369 txtlevel = str(levelNums[j]) + '.' + txtlevel
371 if len(txtlevel) > 0:
372 txtlevel = txtlevel[:-1] + ' - '
375 anchor = fs[i]['link']
377 anchor = fs[i]['filename']
380 content = content + '<ul style="white-space:nowrap;">\n'
383 content = content + '</ul>\n'
386 content = content + '<li><a href="#' + anchor + '">' + txtlevel + fs[i]['title'] + '</a></li>\n'
388 content = content + '</ul>\n'
394 # Create link sidebar given a position in the list.
396 def CreateLinkSidebar(fs, pos, childList):
398 # Build the list recursively from the top level nodes
399 content = BuildList(FindTopLevelNodes(fs), fs, pos, childList)
400 # Shove the TOC link and one file link at the top...
401 active = ' class=active' if pos<0 else ''
402 content = content.replace('<ul>','<ul><li' + active + '><a href="/toc/">Table of Contents</a></li>\n',1)
408 # We have command line arguments now, so deal with them
409 parser = argparse.ArgumentParser(description='A build script for the Ardour Manual')
410 parser.add_argument('-v', '--verbose', action='store_true', help='Display the high-level structure of the manual')
411 parser.add_argument('-q', '--quiet', action='store_true', help='Suppress all output (overrides -v)')
412 parser.add_argument('-d', '--devmode', action='store_true', help='Add content to pages to help developers debug them')
413 args = parser.parse_args()
414 verbose = args.verbose
416 devmode = args.devmode
430 siteDir = './website/'
432 if not quiet and devmode:
433 print('Devmode active: scribbling extra junk to the manual...')
435 if os.access(siteDir, os.F_OK):
437 print('Removing stale HTML data...')
439 shutil.rmtree(siteDir)
441 shutil.copytree('./source', siteDir)
444 # Read the template, and fix the stuff that's fixed for all pages
445 temp = open('page-template.txt')
446 template = temp.read()
449 template = template.replace('{{page.bootstrap_path}}', '/bootstrap-3.3.7')
450 template = template.replace('{{page.page_title}}', 'The Ardour Manual')
452 # Same as above, but for the One-page version
453 temp = open('onepage-template.txt')
454 onepage = temp.read()
457 onepage = onepage.replace('{{page.bootstrap_path}}', '/bootstrap-3.3.7')
458 onepage = onepage.replace('{{page.page_title}}', 'The Ardour Manual')
460 # Parse out the master docuemnt's structure into a dictionary list
461 fileStruct = GetFileStructure()
463 # Build a quasi-tree structure listing children at level + 1 for each node
464 nodeChildren = FindChildren(fileStruct)
466 # Create a dictionary for translation of internal links to real links
467 links = FindInternalLinks(fileStruct)
468 oplinks = FindInternalAnchors(fileStruct)
471 print('Found ' + str(len(links)) + ' internal link target', end='')
472 print('.') if len(links) == 1 else print('s.')
475 master = open('master-doc.txt')
476 firstLine = master.readline().rstrip('\r\n')
479 if firstLine == '<!-- exploded -->':
480 print('Parsing exploded file...')
481 elif firstLine == '<!-- imploded -->':
482 print('Parsing imploded file...')
484 print('Parsing unknown type...')
488 for header in fileStruct:
489 fileCount = fileCount + 1
494 level = header['level']
496 # Handle Part/Chapter/subchapter/section/subsection numbering
511 levelNums[level] = levelNums[level] + 1;
513 # This is totally unnecessary, but nice; besides which, you can capture
514 # the output to a file to look at later if you like :-)
516 for i in range(level):
520 print('\nPart ' + num2roman(levelNums[0]) + ': ', end='')
522 print('\n\tChapter ' + str(levelNums[1]) + ': ', end='')
524 print(header['title'])
526 # Handle TOC scriblings and one-page titles...
530 opl = ' id="' + header['link'] + '"'
532 opl = ' id="' + header['filename'] + '"'
535 toc = toc + '<h2>Part ' + num2roman(levelNums[level]) + ': ' + header['title'] + '</h2>\n';
536 oph = '<h1 class="clear"' + opl +'>Part ' + num2roman(levelNums[level]) + ' - ' + header['title'] + '</h1>\n';
538 toc = toc + '\t<p class="chapter">Ch. ' + str(levelNums[level]) + ': <a href="/' + header['filename'] + '/">' + header['title'] + '</a></p>\n'
539 oph = '<h1 class="clear"' + opl +'>' + str(levelNums[level]) + ' - ' + header['title'] + '</h1>\n';
541 toc = toc + '\t\t<p class="subchapter"><a href="/' + header['filename'] + '/">' + header['title'] + '</a></p>\n'
542 oph = '<h1 class="clear"' + opl +'>' + str(levelNums[level-1]) + '.' + str(levelNums[level]) + ' - ' + header['title'] + '</h1>\n';
544 toc = toc + '\t\t\t<p class="section"><a href="/' + header['filename'] + '/">' + header['title'] + '</a></p>\n'
545 oph = '<h1 class="clear"' + opl +'>' + str(levelNums[level-2]) + '.' + str(levelNums[level-1]) + '.' + str(levelNums[level]) + ' - ' + header['title'] + '</h1>\n';
547 toc = toc + '\t\t\t\t<p class="subsection"><a href="/' + header['filename'] + '/">' + header['title'] + '</a></p>\n'
548 oph = '<h1 class="clear"' + opl +'>' + str(levelNums[level-3]) + '.' + str(levelNums[level-2]) + '.' + str(levelNums[level-1]) + '.' + str(levelNums[level]) + ' - ' + header['title'] + '</h1>\n';
553 # Make the 'this thing contains...' stuff
554 if HaveChildren(fileStruct, pageNumber):
555 pages = GetChildren(fileStruct, pageNumber)
558 more = more + '<li>' + '<a href="/' + fileStruct[pg]['filename'] + '/">' + fileStruct[pg]['title'] + '</a>' + '</li>\n'
560 more = '<div id=subtopics>\n' + '<h2>This section contains the following topics:</h2>\n' + '<ul>\n' + more + '</ul>\n' + '</div>\n'
562 parent = GetParent(fileStruct, pageNumber)
564 # Make the 'Previous', 'Up' & 'Next' content
570 pLink = '<li class="previous"><a title="' + fileStruct[pageNumber - 1]['title'] + '" href="/' + fileStruct[pageNumber - 1]['filename'] + '/" class="previous"> ← Previous </a></li>'
572 if pageNumber < len(fileStruct) - 1:
573 nLink = '<li class="next"><a title="' + fileStruct[pageNumber + 1]['title'] + '" href="/' + fileStruct[pageNumber + 1]['filename'] + '/" class="next"> Next → </a></li>'
576 uLink = '<li><a title="' + fileStruct[parent]['title'] + '" href="/' + fileStruct[parent]['filename'] + '/" class="active"> ↑ Up </a></li>'
578 uLink = '<li><a title="Ardour Table of Contents" href="/toc/index.html" class="active"> ↑ Up </a></li>'
580 prevnext = '<ul class="pager">' + pLink + uLink + nLink + '</ul>'
582 # Make the BreadCrumbs
583 breadcrumbs = GetBreadCrumbs(fileStruct, pageNumber)
585 # Create the link sidebar
586 sidebar = CreateLinkSidebar(fileStruct, pageNumber, nodeChildren)
588 # Parts DO NOT have any content, they are ONLY an organizing construct!
589 # Chapters, subchapters, sections & subsections can all have content,
590 # but the basic fundamental organizing unit WRT content is still the
594 if 'include' in header:
595 srcFile = open('include/' + header['include'])
596 githubedit = '<span style="float:right;"><a title="Edit in GitHub" href="' + githuburl + header['include'] + '"><img src="/images/github.png" alt="Edit on GitHub"/></a></span>'
597 content = srcFile.read()
600 # Get rid of any extant header in the include file
601 # (once this is accepted, we can nuke this bit, as content files
602 # will not have any headers or footers in them)
603 content = re.sub('---.*\n(.*\n)*---.*\n', '', content)
604 content = content.replace('{% children %}', '')
607 if 'content' in header:
608 content = header['content']
610 content = '[something went wrong]'
612 # Add header information to the page if in dev mode
614 devnote ='<aside style="background-color:indigo; color:white;">'
615 if 'filename' in header:
616 devnote = devnote + 'filename: ' + header['filename'] + '<br>'
617 if 'include' in header:
618 devnote = devnote + 'include: ' + header['include'] + '<br>'
620 devnote = devnote + 'link: ' + header['link'] + '<br>'
621 content = devnote + '</aside>' + content
623 # ----- One page version -----
625 # Fix up any internal links
626 opcontent = FixInternalLinks(oplinks, content, header['title'])
628 # Create the link sidebar
629 opsidebar = BuildOnePageSidebar(fileStruct)
631 # Set up the actual page from the template
632 onepage = onepage.replace('{% tree %}', opsidebar)
633 onepage = onepage.replace('{{ content }}', oph + '\n' + opcontent + '{{ content }}')
635 # ----- Normal version -----
637 # Fix up any internal links
638 content = FixInternalLinks(links, content, header['title'])
640 # Set up the actual page from the template
641 if 'style' not in header:
642 page = re.sub("{% if page.style %}.*\n.*\n{% endif %}.*\n", "", template)
644 page = template.replace('{{page.style}}', header['style'])
645 page = page.replace('{% if page.style %}', '')
646 page = page.replace('{% endif %}', '')
648 page = page.replace('{{ page.title }}', header['title'])
649 page = page.replace('{% tree %}', sidebar)
650 page = page.replace('{% prevnext %}', prevnext)
651 page = page.replace('{% githubedit %}', githubedit)
652 page = page.replace('{% breadcrumbs %}', breadcrumbs)
653 page = page.replace('{{ content }}', content + more)
655 # Create the directory for the index.html file to go into (we use makedirs,
656 # because we have to in order to accomodate the 'uri' keyword)
657 os.makedirs(siteDir + header['filename'], 0o775, exist_ok=True)
659 # Finally, write the file!
660 destFile = open(siteDir + header['filename'] + '/index.html', 'w')
664 # Save filename for next header...
665 lastFile = header['filename']
666 pageNumber = pageNumber + 1
668 # Finally, create the TOC
669 sidebar = CreateLinkSidebar(fileStruct, -1, nodeChildren)
671 page = re.sub("{% if page.style %}.*\n.*\n{% endif %}.*\n", "", template)
672 page = page.replace('{{ page.title }}', 'Ardour Table of Contents')
673 page = page.replace('{% tree %}', sidebar)
674 page = page.replace('{{ content }}', toc)
675 page = page.replace('{% prevnext %}', '')
676 page = page.replace('{% githubedit %}', '')
677 page = page.replace('{% breadcrumbs %}', '')
679 os.mkdir(siteDir + 'toc', 0o775)
680 tocFile = open(siteDir + 'toc/index.html', 'w')
684 # Create the one-page version of the documentation
685 onepageFile = open(siteDir + 'ardourmanual.html', 'w')
686 onepage = onepage.replace('{{ content }}', '') # cleans up the last spaceholder
687 onepageFile.write(onepage)
692 print('Processed ' + str(fileCount) + ' files.')