]> Shamusworld >> Repos - ardour-manual/blob - build.py
Better numbering for one page version
[ardour-manual] / build.py
1 #!/usr/bin/python3
2 #
3 # Script to take the master document and ancillary files and create the
4 # finished manual/website.
5 #
6 # by James Hammons
7 # (C) 2017 Underground Software
8 #
9
10 # Remnants (could go into the master document as the first header)
11
12 #bootstrap_path: /bootstrap-3.3.7
13 #page_title: The Ardour Manual
14
15 import os
16 import re
17 import shutil
18 import argparse
19
20
21 # Global vars
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/'
27
28 #
29 # Create an all lowercase filename without special characters and with spaces
30 # replaced with dashes.
31 #
32 def MakeFilename(s):
33         global cleanString
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
38         # that here.
39         fn = fn.replace('--', '-')
40
41         return fn
42
43
44 #
45 # Parse headers into a dictionary
46 #
47 def ParseHeader(fileObj):
48         header = {}
49
50         while (True):
51                 hdrLine = fileObj.readline().rstrip('\r\n')
52
53                 # Break out of the loop if we hit the end of header marker
54                 if hdrLine.startswith('---'):
55                         break
56
57                 # Check to see that we have a well-formed header construct
58                 match = re.findall(': ', hdrLine)
59
60                 if match:
61                         # Parse out foo: bar pairs & put into header dictionary
62                         a = re.split(': ', hdrLine, 1)
63                         header[a[0]] = a[1]
64
65         return header
66
67
68 #
69 # Turn a "part" name into an int
70 #
71 def PartToLevel(s):
72         level = -1
73
74         if s == 'part':
75                 level = 0
76         elif s == 'chapter':
77                 level = 1
78         elif s == 'subchapter':
79                 level = 2
80         elif s == 'section':
81                 level = 3
82         elif s == 'subsection':
83                 level = 4
84
85         return level
86
87 #
88 # Converts a integer to a roman number
89 #
90 def num2roman(num):
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')]
92         roman = ''
93
94         while num > 0:
95                 for i, r in num_map:
96                         while num >= i:
97                                 roman += r
98                                 num -= i
99
100         return roman
101
102 #
103 # Capture the master document's structure (and content, if any) in a list
104 #
105 def GetFileStructure():
106         fs = []
107         fnames = [None]*6
108         content = ''
109         grab = False
110         mf = open('master-doc.txt')
111
112         for ln in mf:
113                 if ln.startswith('---'):
114                         # First, stuff any content that we may have read into the current
115                         # header's dictionary
116                         if grab:
117                                 fs[-1]['content'] = content
118                                 grab = False
119                                 content = ''
120
121                         # Then, get the new header and do things to it
122                         hdr = ParseHeader(mf)
123                         level = PartToLevel(hdr['part'])
124                         hdr['level'] = level
125                         fnames[level] = MakeFilename(hdr['title'])
126
127                         # Ickyness--user specified URIs
128                         if 'uri' in hdr:
129                                 hdr['filename'] = hdr['uri']
130                         else:
131                                 fullName = ''
132
133                                 for i in range(level + 1):
134                                         fullName = fullName + fnames[i] + '/'
135
136                                 # Strip trailing '/' on filename
137                                 hdr['filename'] = fullName[:-1]
138
139                         fs.append(hdr)
140
141                         if ('include' not in hdr) and (level > 0):
142                                 grab = True
143                 else:
144                         if grab:
145                                 content = content + ln
146
147         # Catch the last file, since it would be missed above
148         if grab:
149                 fs[-1]['content'] = content
150
151         mf.close()
152         return fs
153
154
155 #
156 # Determine if a particular node has child nodes
157 #
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:
161                 return False
162
163         # If the next node is at a lower level than the current node, we have
164         # children.
165         if fs[pos]['level'] < fs[pos + 1]['level']:
166                 return True
167
168         # Otherwise, no children at this node.
169         return False
170
171
172 #
173 # Get the children at this level, and return them in a list
174 #
175 def GetChildren(fs, pos):
176         children = []
177         pos = pos + 1
178         childLevel =  fs[pos]['level']
179
180         while fs[pos]['level'] >= childLevel:
181                 if fs[pos]['level'] == childLevel:
182                         children.append(pos)
183
184                 pos = pos + 1
185
186                 # Sanity check
187                 if pos == len(fs):
188                         break
189
190         return children
191
192
193 #
194 # Get the parent at this level
195 #
196 def GetParent(fs, pos):
197         thisLevel =  fs[pos]['level']
198         pos = pos - 1
199
200         while pos >= 0 and fs[pos]['level'] >= thisLevel:
201                 pos = pos - 1
202
203         return pos
204
205
206 #
207 # Creates the BreadCrumbs
208 #
209 def GetBreadCrumbs(fs, pos):
210         # The <span class="divider">&gt;</span> is for Bootstrap pre-3.0
211         breadcrumbs = '<li class="active">'+ fs[pos]['title'] + '</li>'
212
213         while pos >= 0:
214                 pos = GetParent(fs, pos)
215
216                 if pos >= 0:
217                         breadcrumbs='<li><a href="/' + fs[pos]['filename'] + '/">'+ fs[pos]['title'] + '</a></li>'+ breadcrumbs
218
219         breadcrumbs = '<ul class="breadcrumb"><li><a href="/toc/index.html">Home</a></li>' + breadcrumbs + '</ul>'
220         return breadcrumbs
221
222
223 #
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.)
226 #
227 def FindChildren(fs):
228         childArray = []
229
230         for i in range(len(fs)):
231                 if HaveChildren(fs, i):
232                         childArray.append(GetChildren(fs, i))
233                 else:
234                         childArray.append([])
235
236         return childArray
237
238
239 #
240 # Make an array of the top level nodes in the file structure
241 #
242 def FindTopLevelNodes(fs):
243         level0 = []
244
245         for i in range(len(fs)):
246                 if fs[i]['level'] == 0:
247                         level0.append(i)
248
249         return level0
250
251
252 #
253 # Find all header links and create a dictionary out of them
254 #
255 def FindInternalLinks(fs):
256         linkDict = {}
257
258         for hdr in fs:
259                 if 'link' in hdr:
260                         linkDict['"@@' + hdr['link'] + '"'] = '"/' + hdr['filename'] + '/"'
261                         linkDict['"@@' + hdr['link'] + '#'] = '"/' + hdr['filename'] + '/index.html#'
262
263
264         return linkDict
265
266 #
267 # Same as above, but create anchors (for the one-page version)
268 #
269 def FindInternalAnchors(fs):
270         linkDict = {}
271
272         for hdr in fs:
273                 if 'link' in hdr:
274                         linkDict['"@@' + hdr['link'] + '"'] = '"#' + hdr['link'] + '"'
275                         linkDict['"@@' + hdr['link'] + '#'] = '"#' + hdr['link'] + '"'
276
277
278         return linkDict
279
280
281 #
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.
285 #
286 def FixInternalLinks(links, content, title):
287         global findLinks
288         match = findLinks.findall(content)
289         missing = []
290
291         if len(match) > 0:
292                 for s in match:
293                         if s in links:
294                                 content = content.replace(s, links[s])
295                         else:
296                                 missing.append(s)
297
298         # Report missing link targets to the user (if any)
299         if len(missing) > 0:
300                 print('\nMissing link target' + ('s' if len(missing) > 1 else '') + ' in "' + title + '":')
301
302                 for s in missing:
303                         print('  ' + s)
304
305                 print()
306
307         return content
308
309
310 #
311 # Recursively build a list of links based on the location of the page we're
312 # looking at currently
313 #
314 def BuildList(lst, fs, pagePos, cList):
315         content = '\n\n<dl>\n'
316
317         for i in range(len(lst)):
318                 curPos = lst[i]
319                 nextPos = lst[i + 1] if i + 1 < len(lst)  else len(fs)
320
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 + '<dt' + active + '><a href="/' + fs[curPos]['filename'] + '/">' + menuTitle + '</a></dt><dd' + active + '>'
324
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)
329
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)
334
335                 content = content + '</dd>\n'
336
337         content = content + '</dl>\n'
338
339         return content
340
341
342 #
343 # Builds the sidebar for the one-page version
344 #
345 def BuildOnePageSidebar(fs):
346
347         content = '\n\n<ul style="white-space:nowrap;">\n'
348         lvl = 0
349         levelNums = [0]*6
350
351         for i in range(len(fs)):
352                 # Handle Part/Chapter/subchapter/section/subsection numbering
353                 level = fs[i]['level']
354                 if level == 0:
355                         levelNums[2] = 0
356                         levelNums[3] = 0
357                         levelNums[4] = 0
358                 elif level == 1:
359                         levelNums[2] = 0
360                         levelNums[3] = 0
361                         levelNums[4] = 0
362                 elif level == 2:
363                         levelNums[3] = 0
364                         levelNums[4] = 0
365                 elif level == 3:
366                         levelNums[4] = 0
367                 levelNums[level] = levelNums[level] + 1;
368                 j = level
369                 txtlevel = ''
370                 while j > 0:  #level 0 is the part number which is not shown
371                         txtlevel = str(levelNums[j]) + '.' + txtlevel
372                         j = j-1
373                 if len(txtlevel) > 0:
374                         txtlevel = txtlevel[:-1] + ' - '
375
376                 if 'link' in fs[i]:
377                         anchor = fs[i]['link']
378                 else:
379                         anchor = fs[i]['filename']
380
381                 while lvl < level:
382                         content = content + '<ul style="white-space:nowrap;">\n'
383                         lvl = lvl + 1
384                 while lvl > level:
385                         content = content + '</ul>\n'
386                         lvl = lvl - 1
387
388                 content = content + '<li><a href="#' + anchor + '">' + txtlevel + fs[i]['title'] + '</a></li>\n'
389
390         content = content + '</ul>\n'
391
392         return content
393
394
395 #
396 # Create link sidebar given a position in the list.
397 #
398 def CreateLinkSidebar(fs, pos, childList):
399
400         # Build the list recursively from the top level nodes
401         #content = BuildList(FindTopLevelNodes(fs), fs, pos, childList)
402         content = BuildList(FindTopLevelNodes(fs), fs, pos, childList)
403         # Shove the TOC link and one file link at the top...
404         content = content[:7] + '<dt><dt><a href="/toc/">Table of Contents</a></dt><dd></dd>\n' + content[7:]
405
406         return content
407
408 # Preliminaries
409
410 # We have command line arguments now, so deal with them
411 parser = argparse.ArgumentParser(description='A build script for the Ardour Manual')
412 parser.add_argument('-v', '--verbose', action='store_true', help='Display the high-level structure of the manual')
413 parser.add_argument('-q', '--quiet', action='store_true', help='Suppress all output (overrides -v)')
414 parser.add_argument('-d', '--devmode', action='store_true', help='Add content to pages to help developers debug them')
415 args = parser.parse_args()
416 verbose = args.verbose
417 quiet = args.quiet
418 devmode = args.devmode
419
420 if quiet:
421         verbose = False
422
423 level = 0
424 fileCount = 0
425 levelNums = [0]*6
426 lastFile = ''
427 page = ''
428 onepage = ''
429 toc = ''
430 pageNumber = 0
431
432 siteDir = './website/'
433
434 if not quiet and devmode:
435         print('Devmode active: scribbling extra junk to the manual...')
436
437 if os.access(siteDir, os.F_OK):
438         if not quiet:
439                 print('Removing stale HTML data...')
440
441         shutil.rmtree(siteDir)
442
443 shutil.copytree('./source', siteDir)
444
445
446 # Read the template, and fix the stuff that's fixed for all pages
447 temp = open('page-template.txt')
448 template = temp.read()
449 temp.close()
450
451 template = template.replace('{{page.bootstrap_path}}', '/bootstrap-3.3.7')
452 template = template.replace('{{page.page_title}}', 'The Ardour Manual')
453
454 # Same as above, but for the One-page version
455 temp = open('onepage-template.txt')
456 onepage = temp.read()
457 temp.close()
458
459 onepage = onepage.replace('{{page.bootstrap_path}}', '/bootstrap-3.3.7')
460 onepage = onepage.replace('{{page.page_title}}', 'The Ardour Manual')
461
462 # Parse out the master docuemnt's structure into a dictionary list
463 fileStruct = GetFileStructure()
464
465 # Build a quasi-tree structure listing children at level + 1 for each node
466 nodeChildren = FindChildren(fileStruct)
467
468 # Create a dictionary for translation of internal links to real links
469 links = FindInternalLinks(fileStruct)
470 oplinks = FindInternalAnchors(fileStruct)
471
472 if not quiet:
473         print('Found ' + str(len(links)) + ' internal link target', end='')
474         print('.') if len(links) == 1 else print('s.')
475
476 if not quiet:
477         master = open('master-doc.txt')
478         firstLine = master.readline().rstrip('\r\n')
479         master.close()
480
481         if firstLine == '<!-- exploded -->':
482                 print('Parsing exploded file...')
483         elif firstLine == '<!-- imploded -->':
484                 print('Parsing imploded file...')
485         else:
486                 print('Parsing unknown type...')
487
488 # Here we go!
489
490 for header in fileStruct:
491         fileCount = fileCount + 1
492         content = ''
493         more = ''
494
495         lastLevel = level
496         level = header['level']
497
498         # Handle Part/Chapter/subchapter/section/subsection numbering
499         if level == 0:
500                 levelNums[2] = 0
501                 levelNums[3] = 0
502                 levelNums[4] = 0
503         elif level == 1:
504                 levelNums[2] = 0
505                 levelNums[3] = 0
506                 levelNums[4] = 0
507         elif level == 2:
508                 levelNums[3] = 0
509                 levelNums[4] = 0
510         elif level == 3:
511                 levelNums[4] = 0
512
513         levelNums[level] = levelNums[level] + 1;
514
515         # This is totally unnecessary, but nice; besides which, you can capture
516         # the output to a file to look at later if you like :-)
517         if verbose:
518                 for i in range(level):
519                         print('\t', end='')
520
521                 if (level == 0):
522                         print('\nPart ' + num2roman(levelNums[0]) + ': ', end='')
523                 elif (level == 1):
524                         print('\n\tChapter ' + str(levelNums[1]) + ': ', end='')
525
526                 print(header['title'])
527
528         # Handle TOC scriblings and one-page titles...
529         opl = ''
530
531         if 'link' in header:
532                 opl = ' id="' + header['link'] + '"'
533         else:
534                 opl = ' id="' + header['filename'] + '"'
535
536         if level == 0:
537                 toc = toc + '<h2>Part ' + num2roman(levelNums[level]) + ': ' + header['title'] + '</h2>\n';
538                 oph = '<h1 class="clear"' + opl +'>Part ' + num2roman(levelNums[level]) + ' - ' + header['title'] + '</h1>\n';
539         elif level == 1:
540                 toc = toc + '\t<p class="chapter">Ch. ' + str(levelNums[level]) + ':&nbsp;&nbsp;<a href="/' + header['filename'] + '/">' + header['title'] + '</a></p>\n'
541                 oph = '<h1 class="clear"' + opl +'>' + str(levelNums[level]) + ' - ' + header['title'] + '</h1>\n';
542         elif level == 2:
543                 toc = toc + '\t\t<p class="subchapter"><a href="/' + header['filename'] + '/">' + header['title'] + '</a></p>\n'
544                 oph = '<h1 class="clear"' + opl +'>' + str(levelNums[level-1]) + '.' + str(levelNums[level]) + ' - ' + header['title'] + '</h1>\n';
545         elif level == 3:
546                 toc = toc + '\t\t\t<p class="section"><a href="/' + header['filename'] + '/">' + header['title'] + '</a></p>\n'
547                 oph = '<h1 class="clear"' + opl +'>' + str(levelNums[level-2]) + '.' + str(levelNums[level-1]) + '.' + str(levelNums[level]) + ' - ' + header['title'] + '</h1>\n';
548         elif level == 4:
549                 toc = toc + '\t\t\t\t<p class="subsection"><a href="/' + header['filename'] + '/">' + header['title'] + '</a></p>\n'
550                 oph = '<h1 class="clear"' + opl +'>' + str(levelNums[level-3]) + '.'  + str(levelNums[level-2]) + '.'  + str(levelNums[level-1]) + '.' + str(levelNums[level]) + ' - ' + header['title'] + '</h1>\n';
551
552
553
554
555         # Make the 'this thing contains...' stuff
556         if HaveChildren(fileStruct, pageNumber):
557                 pages = GetChildren(fileStruct, pageNumber)
558
559                 for pg in pages:
560                         more = more + '<li>' + '<a href="/' + fileStruct[pg]['filename'] + '/">' + fileStruct[pg]['title'] + '</a>' + '</li>\n'
561
562                 more = '<div id=subtopics>\n' + '<h2>This section contains the following topics:</h2>\n' + '<ul>\n' + more + '</ul>\n' + '</div>\n'
563
564         parent = GetParent(fileStruct, pageNumber)
565
566         # Make the 'Previous', 'Up' & 'Next' content
567         nLink = ''
568         pLink = ''
569         uLink = ''
570
571         if pageNumber > 0:
572                 pLink = '<li class="previous"><a title="' + fileStruct[pageNumber - 1]['title'] + '" href="/' + fileStruct[pageNumber - 1]['filename'] + '/" class="previous"> &larr; Previous </a></li>'
573
574         if pageNumber < len(fileStruct) - 1:
575                 nLink = '<li class="next"><a title="' + fileStruct[pageNumber + 1]['title'] + '" href="/' + fileStruct[pageNumber + 1]['filename'] + '/" class="next"> Next &rarr; </a></li>'
576
577         if level > 0:
578                 uLink = '<li><a title="' + fileStruct[parent]['title'] + '" href="/' + fileStruct[parent]['filename'] + '/" class="active"> &uarr; Up </a></li>'
579         else:
580                 uLink = '<li><a title="Ardour Table of Contents" href="/toc/index.html" class="active"> &uarr; Up </a></li>'
581
582         prevnext = '<ul class="pager">' + pLink + uLink + nLink + '</ul>'
583
584         # Make the BreadCrumbs
585         breadcrumbs = GetBreadCrumbs(fileStruct, pageNumber)
586
587         # Create the link sidebar
588         sidebar = CreateLinkSidebar(fileStruct, pageNumber, nodeChildren)
589
590         # Parts DO NOT have any content, they are ONLY an organizing construct!
591         # Chapters, subchapters, sections & subsections can all have content,
592         # but the basic fundamental organizing unit WRT content is still the
593         # chapter.
594         githubedit = ''
595         if level > 0:
596                 if 'include' in header:
597                         srcFile = open('include/' + header['include'])
598                         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>'
599                         content = srcFile.read()
600                         srcFile.close()
601
602                         # Get rid of any extant header in the include file
603                         # (once this is accepted, we can nuke this bit, as content files
604                         # will not have any headers or footers in them)
605                         content = re.sub('---.*\n(.*\n)*---.*\n', '', content)
606                         content = content.replace('{% children %}', '')
607
608                 else:
609                         if 'content' in header:
610                                 content = header['content']
611                         else:
612                                 content = '[something went wrong]'
613
614         # Add header information to the page if in dev mode
615         if devmode:
616                 devnote ='<aside style="background-color:indigo; color:white;">'
617                 if 'filename' in header:
618                         devnote = devnote + 'filename: ' + header['filename'] + '<br>'
619                 if 'include' in header:
620                         devnote = devnote + 'include: ' + header['include'] + '<br>'
621                 if 'link' in header:
622                         devnote = devnote + 'link: ' + header['link'] + '<br>'
623                 content = devnote + '</aside>' + content
624
625         # ----- One page version -----
626
627         # Fix up any internal links
628         opcontent = FixInternalLinks(oplinks, content, header['title'])
629
630         # Create the link sidebar
631         opsidebar = BuildOnePageSidebar(fileStruct)
632
633         # Set up the actual page from the template
634         onepage = onepage.replace('{% tree %}', opsidebar)
635         onepage = onepage.replace('{{ content }}', oph + '\n' + opcontent + '{{ content }}')
636
637         # ----- Normal version -----
638
639         # Fix up any internal links
640         content = FixInternalLinks(links, content, header['title'])
641
642         # Set up the actual page from the template
643         if 'style' not in header:
644                 page = re.sub("{% if page.style %}.*\n.*\n{% endif %}.*\n", "", template)
645         else:
646                 page = template.replace('{{page.style}}', header['style'])
647                 page = page.replace('{% if page.style %}', '')
648                 page = page.replace('{% endif %}', '')
649
650         page = page.replace('{{ page.title }}', header['title'])
651         page = page.replace('{% tree %}', sidebar)
652         page = page.replace('{% prevnext %}', prevnext)
653         page = page.replace('{% githubedit %}', githubedit)
654         page = page.replace('{% breadcrumbs %}', breadcrumbs)
655         page = page.replace('{{ content }}', content + more)
656
657         # Create the directory for the index.html file to go into (we use makedirs,
658         # because we have to in order to accomodate the 'uri' keyword)
659         os.makedirs(siteDir + header['filename'], 0o775, exist_ok=True)
660
661         # Finally, write the file!
662         destFile = open(siteDir + header['filename'] + '/index.html', 'w')
663         destFile.write(page)
664         destFile.close()
665
666         # Save filename for next header...
667         lastFile = header['filename']
668         pageNumber = pageNumber + 1
669
670 # Finally, create the TOC
671 sidebar = CreateLinkSidebar(fileStruct, -1, nodeChildren)
672
673 page = re.sub("{% if page.style %}.*\n.*\n{% endif %}.*\n", "", template)
674 page = page.replace('{{ page.title }}', 'Ardour Table of Contents')
675 page = page.replace('{% tree %}', sidebar)
676 page = page.replace('{{ content }}', toc)
677 page = page.replace('{% prevnext %}', '')
678 page = page.replace('{% githubedit %}', '')
679 page = page.replace('{% breadcrumbs %}', '')
680
681 os.mkdir(siteDir + 'toc', 0o775)
682 tocFile = open(siteDir + 'toc/index.html', 'w')
683 tocFile.write(page)
684 tocFile.close()
685
686 # Create the one-page version of the documentation
687 onepageFile = open(siteDir + 'ardourmanual.html', 'w')
688 onepage = onepage.replace('{{ content }}', '') # cleans up the last spaceholder
689 onepageFile.write(onepage)
690 onepageFile.close()
691
692
693 if not quiet:
694         print('Processed ' + str(fileCount) + ' files.')