]> Shamusworld >> Repos - ardour-manual-diverged/blob - build.py
Move content from _manual/ to include/.
[ardour-manual-diverged] / 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-2.2.2
13 #page_title: The Ardour Manual
14
15 import os
16 import re
17 import shutil
18 import argparse
19
20
21 #
22 # Create an all lowercase filename without special characters and with spaces
23 # replaced with dashes.
24 #
25 def MakeFilename(s):
26         # Cleans up the file name, removing all non ASCII or .-_ chars
27         fn = re.sub(r'[^.\-_a-zA-Z0-9 ]', '', s)
28         fn = fn.lower()
29         fn = fn.replace(' ', '-')
30         return fn
31
32
33 #
34 # Parse headers into a dictionary
35 #
36 def ParseHeader(fileObj):
37         header = {}
38
39         while (True):
40                 hdrLine = fileObj.readline().rstrip('\r\n')
41
42                 # Break out of the loop if we hit the end of header marker
43                 if hdrLine.startswith('---'):
44                         break
45
46                 # Check to see that we have a well-formed header construct
47                 match = re.findall(': ', hdrLine)
48
49                 if match:
50                         # Parse out foo: bar pairs & put into header dictionary
51                         a = re.split(': ', hdrLine, 1)
52                         header[a[0]] = a[1]
53
54         return header
55
56
57 #
58 # Turn a "part" name into an int
59 #
60 def PartToLevel(s):
61         level = -1
62
63         if s == 'part':
64                 level = 0
65         elif s == 'chapter':
66                 level = 1
67         elif s == 'subchapter':
68                 level = 2
69         elif s == 'section':
70                 level = 3
71         elif s == 'subsection':
72                 level = 4
73
74         return level
75
76 #
77 # Converts a integer to a roman number
78 #
79 def num2roman(num):
80         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')]
81         roman = ''
82
83         while num > 0:
84                 for i, r in num_map:
85                         while num >= i:
86                                 roman += r
87                                 num -= i
88
89         return roman
90
91 #
92 # Capture the master document's structure (and content, if any) in a list
93 #
94 def GetFileStructure():
95         fs = []
96         fnames = [None]*6
97         content = ''
98         grab = False
99         mf = open('master-doc.txt')
100
101         for ln in mf:
102                 if ln.startswith('---'):
103                         # First, stuff any content that we may have read into the current
104                         # header's dictionary
105                         if grab:
106                                 fs[-1]['content'] = content
107                                 grab = False
108                                 content = ''
109
110                         # Then, get the new header and do things to it
111                         hdr = ParseHeader(mf)
112                         level = PartToLevel(hdr['part'])
113                         hdr['level'] = level
114                         fnames[level] = MakeFilename(hdr['title'])
115
116                         # Ickyness--user specified URIs
117                         if 'uri' in hdr:
118                                 hdr['filename'] = hdr['uri']
119                         else:
120                                 fullName = ''
121
122                                 for i in range(level + 1):
123                                         fullName = fullName + fnames[i] + '/'
124
125                                 hdr['filename'] = fullName.rstrip('/')
126
127                         fs.append(hdr)
128
129                         if ('include' not in hdr) and (level > 0):
130                                 grab = True
131                 else:
132                         if grab:
133                                 content = content + ln
134
135         # Catch the last file, since it would be missed above
136         if grab:
137                 fs[-1]['content'] = content
138
139         mf.close()
140         return fs
141
142
143 #
144 # Determine if a particular node has child nodes
145 #
146 def HaveChildren(fs, pos):
147         # If we're at the end of the list, there can be no children
148         if pos == len(fs) - 1:
149                 return False
150
151         # If the next node is at a lower level than the current node, we have
152         # children.
153         if fs[pos]['level'] < fs[pos + 1]['level']:
154                 return True
155
156         # Otherwise, no children at this node.
157         return False
158
159
160 #
161 # Get the children at this level, and return them in a list
162 #
163 def GetChildren(fs, pos):
164         children = []
165         pos = pos + 1
166         childLevel =  fs[pos]['level']
167
168         while fs[pos]['level'] >= childLevel:
169                 if fs[pos]['level'] == childLevel:
170                         children.append(pos)
171
172                 pos = pos + 1
173
174                 # Sanity check
175                 if pos == len(fs):
176                         break
177
178         return children
179
180
181 #
182 # Make an array of children attached to each node in the file structure
183 # (It's a quasi-tree structure, and can be traversed as such.)
184 #
185 def FindChildren(fs):
186         childArray = []
187
188         for i in range(len(fs)):
189                 if HaveChildren(fs, i):
190                         childArray.append(GetChildren(fs, i))
191                 else:
192                         childArray.append([])
193
194         return childArray
195
196
197 #
198 # Make an array of the top level nodes in the file structure
199 #
200 def FindTopLevelNodes(fs):
201         level0 = []
202
203         for i in range(len(fs)):
204                 if fs[i]['level'] == 0:
205                         level0.append(i)
206
207         return level0
208
209
210 #
211 # Find all header links and create a dictionary out of them
212 #
213 def FindInternalLinks(fs):
214         linkDict = {}
215
216         for hdr in fs:
217                 if 'link' in hdr:
218                         linkDict['@@' + hdr['link']] = '/' + hdr['filename'] + '/'
219
220         return linkDict
221
222
223 #
224 # Internal links are of the form '@@link-name', which are references to the
225 # 'link:' field in the part header. We have to find all occurances and replace
226 # them with the appropriate link.
227 #
228 def FixInternalLinks(links, content, title):
229
230         # Make key1|key2|key3|... out of our links keys
231         pattern = re.compile('|'.join(links.keys()))
232
233         # Use a lambda callback to substitute each occurance found
234         result = pattern.sub(lambda x: links[x.group()], content)
235
236         # Check for missing link targets, and report them to the user
237         match = re.findall('"@@.*"', result)
238
239         if len(match) > 0:
240                 print('\nMissing link target' + ('s' if len(match) > 1 else '') + ' in "' + title + '":')
241
242                 for s in match:
243                         print('  ' + s[3:-1])
244
245                 print()
246
247         return result
248
249
250 #
251 # Recursively build a list of links based on the location of the page we're
252 # looking at currently
253 #
254 def BuildList(lst, fs, pagePos, cList):
255         content = '\n\n<dl>\n'
256
257         for i in range(len(lst)):
258                 curPos = lst[i]
259                 nextPos = lst[i + 1] if i + 1 < len(lst)  else len(fs)
260
261                 active = ' class=active' if curPos == pagePos else ''
262                 content = content + '<dt' + active + '><a href="/' + fs[curPos]['filename'] + '/">' + fs[curPos]['title'] + '</a></dt><dd' + active + '>'
263
264                 # If the current page is our page, and it has children, enumerate them
265                 if curPos == pagePos:
266                         if len(cList[curPos]) > 0:
267                                 content = content + BuildList(cList[curPos], fs, -1, cList)
268
269                 # Otherwise, if our page lies between the current one and the next,
270                 # build a list of links from those nodes one level down.
271                 elif (pagePos > curPos) and (pagePos < nextPos):
272                         content = content + BuildList(cList[curPos], fs, pagePos, cList)
273
274                 content = content + '</dd>\n'
275
276         content = content + '</dl>\n'
277
278         return content
279
280 #
281 # Create link sidebar given a position in the list.
282 #
283 def CreateLinkSidebar(fs, pos, childList):
284
285         # Build the list recursively from the top level nodes
286         content = BuildList(FindTopLevelNodes(fs), fs, pos, childList)
287         # Shove the TOC link in the top...
288         content = content[:7] + '<dt><a href="/toc/">Table of Contents</a></dt><dd></dd>\n' + content[7:]
289
290         return content
291
292
293 # Preliminaries
294
295 # We have command line arguments now, so deal with them
296 parser = argparse.ArgumentParser(description='A build script for the Ardour Manual')
297 parser.add_argument('-v', '--verbose', action='store_true', help='Display the high-level structure of the manual')
298 parser.add_argument('-q', '--quiet', action='store_true', help='Suppress all output (overrides -v)')
299 args = parser.parse_args()
300 verbose = args.verbose
301 quiet = args.quiet
302
303 if quiet:
304         verbose = False
305
306 #verbose = False
307 level = 0
308 fileCount = 0
309 levelNums = [0]*6
310 lastFile = ''
311 page = ''
312 toc = ''
313 pageNumber = 0
314
315 siteDir = './website/'
316
317 if os.access(siteDir, os.F_OK):
318         if not quiet:
319                 print('Removing stale HTML data...')
320
321         shutil.rmtree(siteDir)
322
323 shutil.copytree('./source', siteDir)
324
325
326 # Read the template, and fix the stuff that's fixed for all pages
327 temp = open('page-template.txt')
328 template = temp.read()
329 temp.close()
330
331 template = template.replace('{{page.bootstrap_path}}', '/bootstrap-2.2.2')
332 template = template.replace('{{page.page_title}}', 'The Ardour Manual')
333
334
335 # Parse out the master docuemnt's structure into a dictionary list
336 fileStruct = GetFileStructure()
337
338 # Build a quasi-tree structure listing children at level + 1 for each node
339 nodeChildren = FindChildren(fileStruct)
340
341 # Create a dictionary for translation of internal links to real links
342 links = FindInternalLinks(fileStruct)
343
344 if not quiet:
345         print('Found ' + str(len(links)) + ' internal link target', end='')
346         print('.') if len(links) == 1 else print('s.')
347
348 if not quiet:
349         master = open('master-doc.txt')
350         firstLine = master.readline().rstrip('\r\n')
351         master.close()
352
353         if firstLine == '<!-- exploded -->':
354                 print('Parsing exploded file...')
355         elif firstLine == '<!-- imploded -->':
356                 print('Parsing imploded file...')
357         else:
358                 print('Parsing unknown type...')
359
360 # Here we go!
361
362 for header in fileStruct:
363         fileCount = fileCount + 1
364         content = ''
365         more = ''
366
367         lastLevel = level
368         level = header['level']
369
370         # Handle Part/Chapter/subchapter/section/subsection numbering
371         if level == 0:
372                 levelNums[2] = 0
373         elif level == 1:
374                 levelNums[2] = 0
375         elif level == 2:
376                 levelNums[3] = 0
377         elif level == 3:
378                 levelNums[4] = 0
379
380         levelNums[level] = levelNums[level] + 1;
381
382         # This is totally unnecessary, but nice; besides which, you can capture
383         # the output to a file to look at later if you like :-)
384         if verbose:
385                 for i in range(level):
386                         print('\t', end='')
387
388                 if (level == 0):
389                         print('\nPart ' + num2roman(levelNums[0]) + ': ', end='')
390                 elif (level == 1):
391                         print('\n\tChapter ' + str(levelNums[1]) + ': ', end='')
392
393                 print(header['title'])
394
395         # Handle TOC scriblings...
396         if level == 0:
397                 toc = toc + '<h2>Part ' + num2roman(levelNums[level]) + ': ' + header['title'] + '</h2>\n';
398         elif level == 1:
399                 toc = toc + '  <p id=chapter>Ch. ' + str(levelNums[level]) + ':&nbsp;&nbsp;<a href="/' + header['filename'] + '/">' + header['title'] + '</a></p>\n'
400         elif level == 2:
401                 toc = toc + '    <a id=subchapter href="/' + header['filename'] + '/">' + header['title'] + '</a><br>\n'
402         elif level == 3:
403                 toc = toc + '      <a id=subchapter href="/' + header['filename'] + '/">' + header['title'] + '</a><br>\n'
404         elif level == 4:
405                 toc = toc + '      <a id=subchapter href="/' + header['filename'] + '/">' + header['title'] + '</a><br>\n'
406
407         # Make the 'this thing contains...' stuff
408         if HaveChildren(fileStruct, pageNumber):
409                 pages = GetChildren(fileStruct, pageNumber)
410
411                 for pg in pages:
412                         more = more + '<li>' + '<a href="/' + fileStruct[pg]['filename'] + '/">' + fileStruct[pg]['title'] + '</a>' + '</li>\n'
413
414                 more = '<div id=subtopics>\n' + '<h2>This section contains the following topics:</h2>\n' + '<ul>\n' + more + '</ul>\n' + '</div>\n'
415
416         # Make the 'Previous' & 'Next' content
417         nLink = ''
418         pLink = ''
419
420         if pageNumber > 0:
421                 pLink = '<li><a title="' + fileStruct[pageNumber - 1]['title'] + '" href="/' + fileStruct[pageNumber - 1]['filename'] + '/" class="previous"> &lt; Previous </a></li>'
422
423         if pageNumber < len(fileStruct) - 1:
424                 nLink = '<li><a title="' + fileStruct[pageNumber + 1]['title'] + '" href="/' + fileStruct[pageNumber + 1]['filename'] + '/" class="next"> Next &gt; </a></li>'
425
426         prevnext = '<ul class=pager>' + pLink + nLink + '</ul>'
427
428         # Create the link sidebar
429         sidebar = CreateLinkSidebar(fileStruct, pageNumber, nodeChildren)
430
431         # Parts DO NOT have any content, they are ONLY an organizing construct!
432         # Chapters, subchapters, sections & subsections can all have content,
433         # but the basic fundamental organizing unit WRT content is still the
434         # chapter.
435         if level > 0:
436                 if 'include' in header:
437                         srcFile = open('include/' + header['include'])
438                         content = srcFile.read()
439                         srcFile.close()
440
441                         # Get rid of any extant header in the include file
442                         # (once this is accepted, we can nuke this bit, as content files
443                         # will not have any headers or footers in them)
444                         content = re.sub('---.*\n(.*\n)*---.*\n', '', content)
445                         content = content.replace('{% children %}', '')
446
447                 else:
448                         if 'content' in header:
449                                 content = header['content']
450                         else:
451                                 content = '[something went wrong]'
452
453         # Fix up any internal links
454         content = FixInternalLinks(links, content, header['title'])
455
456         # Set up the actual page from the template
457         if 'style' not in header:
458                 page = re.sub("{% if page.style %}.*\n.*\n{% endif %}.*\n", "", template)
459         else:
460                 page = template.replace('{{page.style}}', header['style'])
461                 page = page.replace('{% if page.style %}', '')
462                 page = page.replace('{% endif %}', '')
463
464         page = page.replace('{{ page.title }}', header['title'])
465         page = page.replace('{% tree %}', sidebar)
466         page = page.replace('{% prevnext %}', prevnext)
467         page = page.replace('{{ content }}', content + more)
468
469         # Create the directory for the index.html file to go into
470         # (we use makedirs, because we have to in order to accomadate the 'uri'
471         # keyword
472 #       os.mkdir(siteDir + header['filename'], 0o775)
473         os.makedirs(siteDir + header['filename'], 0o775, exist_ok=True)
474
475         # Finally, write the file!
476         destFile = open(siteDir + header['filename'] + '/index.html', 'w')
477         destFile.write(page)
478         destFile.close()
479
480         # Save filename for next header...
481         lastFile = header['filename']
482         pageNumber = pageNumber + 1
483
484 # Finally, create the TOC
485 sidebar = CreateLinkSidebar(fileStruct, -1, nodeChildren)
486
487 page = re.sub("{% if page.style %}.*\n.*\n{% endif %}.*\n", "", template)
488 page = page.replace('{{ page.title }}', 'Ardour Table of Contents')
489 page = page.replace('{% tree %}', sidebar)
490 page = page.replace('{{ content }}', toc)
491 page = page.replace('{% prevnext %}', '')
492
493 os.mkdir(siteDir + 'toc', 0o775)
494 tocFile = open(siteDir + 'toc/index.html', 'w')
495 tocFile.write(page)
496 tocFile.close()
497
498 if not quiet:
499         print('Processed ' + str(fileCount) + ' files.')