]> Shamusworld >> Repos - ardour-manual-diverged/blob - build.py
Version 1.0 of the final Python build script.
[ardour-manual-diverged] / build.py
1 #!/usr/bin/python
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
19
20 #
21 # Create an all lowercase filename without special characters and with spaces
22 # replaced with dashes.
23 #
24 def MakeFilename(s):
25         # This RE is shitty, but I can't think of a better one right now
26         fn = re.sub("[?!'&#:;_*()/\\,.]+", "", s)
27         fn = fn.lower()
28         fn = fn.replace(' ', '-')
29         return fn
30
31
32 #
33 # Parse headers into a dictionary
34 #
35 def ParseHeader(fileObj):
36         header = {}
37
38         while (True):
39                 hdrLine = fileObj.readline().rstrip('\r\n')
40
41                 # Break out of the loop if we hit the end of header marker
42                 if hdrLine.startswith('---'):
43                         break
44
45                 # Parse out foo: bar pairs & put into header dictionary
46                 a = re.split(': ', hdrLine, 1)
47                 header[a[0]] = a[1]
48
49         return header
50
51
52 #
53 # Turn a "part" name into an int
54 #
55 def PartToLevel(s):
56         level = -1
57
58         if s == 'part':
59                 level = 0
60         elif s == 'chapter':
61                 level = 1
62         elif s == 'subchapter':
63                 level = 2
64         elif s == 'section':
65                 level = 3
66         elif s == 'subsection':
67                 level = 4
68
69         return level
70
71
72 #
73 # Capture the master document's structure (and content, if any) in a list
74 #
75 def GetFileStructure():
76         fs = []
77         fnames = [None]*6
78         content = ''
79         grab = False
80         mf = open('master-doc.txt')
81
82         for ln in mf:
83                 if ln.startswith('---'):
84                         # First, stuff any content that we may have read into the current
85                         # header's dictionary
86                         if grab:
87                                 fs[-1]['content'] = content
88                                 grab = False
89                                 content = ''
90
91                         # Then, get the new header and do things to it
92                         hdr = ParseHeader(mf)
93                         level = PartToLevel(hdr['part'])
94                         hdr['level'] = level
95                         fnames[level] = MakeFilename(hdr['title'])
96                         fullName = ''
97
98                         for i in range(level + 1):
99                                 fullName = fullName + fnames[i] + '/'
100
101                         hdr['filename'] = fullName.rstrip('/')
102                         fs.append(hdr)
103
104                         if ('include' not in hdr) and (level > 0):
105                                 grab = True
106                 else:
107                         if grab:
108                                 content = content + ln
109
110         # Catch the last file, since it would be missed above
111         if grab:
112                 fs[-1]['content'] = content
113
114         mf.close()
115         return fs
116
117
118 #
119 # Determine if a particular node has child nodes
120 #
121 def HaveChildren(fs, pos):
122         # If we're at the end of the list, there can be no children
123         if pos == len(fs) - 1:
124                 return False
125
126         # If the next node is at a lower level than the current node, we have
127         # children.
128         if fs[pos]['level'] < fs[pos + 1]['level']:
129                 return True
130
131         # Otherwise, no children at this node.
132         return False
133
134
135 #
136 # Get the children at this level, and return them in a list
137 #
138 def GetChildren(fs, pos):
139         children = []
140         pos = pos + 1
141         childLevel =  fs[pos]['level']
142
143         while fs[pos]['level'] >= childLevel:
144                 if fs[pos]['level'] == childLevel:
145                         children.append(pos)
146
147                 pos = pos + 1
148
149                 # Sanity check
150                 if pos == len(fs):
151                         break
152
153         return children
154
155
156 #
157 # Make an array of children attached to each node in the file structure
158 # (It's a quasi-tree structure, and can be traversed as such.)
159 #
160 def FindChildren(fs):
161         childArray = []
162
163         for i in range(len(fs)):
164                 if HaveChildren(fs, i):
165                         childArray.append(GetChildren(fs, i))
166                 else:
167                         childArray.append([])
168
169         return childArray
170
171
172 #
173 # Make an array of the top level nodes in the file structure
174 #
175 def FindTopLevelNodes(fs):
176         level0 = []
177
178         for i in range(len(fs)):
179                 if fs[i]['level'] == 0:
180                         level0.append(i)
181
182         return level0
183
184
185 #
186 # Internal links are of the form '@link-name', which are references to the
187 # 'link:' field in the part header. We have to find all occurances and replace
188 # them with the appropriate link.
189 #
190 def FixInternalLinks(content):
191 #make a dictionary ahead of time of <link:, filename:> pairs
192         return content
193
194
195 #
196 # Recursively build a list of links based on the location of the page we're
197 # looking at currently
198 #
199 def BuildList(lst, fs, pagePos, cList):
200         content = '\n\n<dl>\n'
201
202         for i in range(len(lst)):
203                 curPos = lst[i]
204                 nextPos = lst[i + 1] if i + 1 < len(lst)  else len(fs)
205
206                 active = ' class=active' if curPos == pagePos else ''
207                 content = content + '<dt' + active + '><a href="/' + fs[curPos]['filename'] + '/">' + fs[curPos]['title'] + '</a></dt><dd' + active + '>'
208
209                 # If the current page is our page, and it has children, enumerate them
210                 if curPos == pagePos:
211                         if len(cList[curPos]) > 0:
212                                 content = content + BuildList(cList[curPos], fs, -1, cList)
213
214                 # Otherwise, if our page lies between the current one and the next,
215                 # build a list of links from those nodes one level down.
216                 elif (pagePos > curPos) and (pagePos < nextPos):
217                         content = content + BuildList(cList[curPos], fs, pagePos, cList)
218
219                 content = content + '</dd>\n'
220
221         content = content + '</dl>\n'
222
223         return content
224
225 #
226 # Create link sidebar given a position in the list.
227 #
228 def CreateLinkSidebar(fs, pos, childList):
229
230         # Build the list recursively from the top level nodes
231         content = BuildList(FindTopLevelNodes(fs), fs, pos, childList)
232         # Shove the TOC link in the top...
233         content = content[:7] + '<dt><a href="/toc/">Table of Contents</a></dt><dd></dd>\n' + content[7:]
234
235         return content
236
237
238 # Preliminaries
239
240 roman = [ '0', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X',
241         'XI', 'XII', 'XIII', 'XIV', 'XV', 'XVI', 'XVII', 'XVIII', 'XIX', 'XX',
242         'XXI', 'XXII', 'XXIII', 'XXIV', 'XXV', 'XXVI', 'XXVII', 'XXVIII', 'XXIX', 'XXX' ]
243
244 verbose = True
245 level = 0
246 fileCount = 0
247 levelNums = [0]*6
248 lastFile = ''
249 page = ''
250 toc = ''
251 pageNumber = 0
252
253 siteDir = './website/'
254
255 if os.access(siteDir, os.F_OK):
256         print('Removing stale HTML data...')
257         shutil.rmtree(siteDir)
258
259 shutil.copytree('./source', siteDir)
260
261 # Yeah, need to make a symlink in include/ too :-P
262 # [this will go away when the rewrite happens]
263 if (os.access('include/_manual', os.F_OK) == False):
264         os.symlink('../_manual/', 'include/_manual')
265
266
267 # Read the template, and fix the stuff that's fixed for all pages
268 temp = open('page-template.txt')
269 template = temp.read()
270 temp.close()
271
272 template = template.replace('{{page.bootstrap_path}}', '/bootstrap-2.2.2')
273 template = template.replace('{{page.page_title}}', 'The Ardour Manual')
274
275
276 # Parse out the master docuemnt's structure into a dictionary list
277 fileStruct = GetFileStructure()
278 nodeChildren = FindChildren(fileStruct)
279
280 # Here we go!
281
282 master = open('master-doc.txt')
283 firstLine = master.readline().rstrip('\r\n')
284 master.close()
285
286 if firstLine == '<!-- exploded -->':
287         print('Parsing exploded file...')
288 elif firstLine == '<!-- imploded -->':
289         print('Parsing imploded file...')
290 else:
291         print('Parsing unknown type...')
292
293 for header in fileStruct:
294         fileCount = fileCount + 1
295         content = ''
296         more = ''
297
298         lastLevel = level
299         level = header['level']
300
301         # Handle Part/Chapter/subchapter/section/subsection numbering
302         if level == 0:
303                 levelNums[2] = 0
304         elif level == 1:
305                 levelNums[2] = 0
306         elif level == 2:
307                 levelNums[3] = 0
308         elif level == 3:
309                 levelNums[4] = 0
310
311         levelNums[level] = levelNums[level] + 1;
312
313         # This is totally unnecessary, but nice; besides which, you can capture
314         # the output to a file to look at later if you like :-)
315         # [TODO: add command line switch, default to False]
316         if verbose:
317                 for i in range(level):
318                         print('\t', end='')
319
320                 if (level == 0):
321                         print('\nPart ' + roman[levelNums[0]] + ': ', end='')
322                 elif (level == 1):
323                         print('\n\tChapter ' + str(levelNums[1]) + ': ', end='')
324
325                 print(header['title'])
326
327         # Handle TOC scriblings...
328         if level == 0:
329                 toc = toc + '<h2>Part ' + roman[levelNums[level]] + ': ' + header['title'] + '</h2>\n';
330         elif level == 1:
331                 toc = toc + '  <p id=chapter>Ch. ' + str(levelNums[level]) + ':&nbsp;&nbsp;<a href="/' + header['filename'] + '/">' + header['title'] + '</a></p>\n'
332         elif level == 2:
333                 toc = toc + '    <a id=subchapter href="/' + header['filename'] + '/">' + header['title'] + '</a><br>\n'
334         elif level == 3:
335                 toc = toc + '      <a id=subchapter href="/' + header['filename'] + '/">' + header['title'] + '</a><br>\n'
336         elif level == 4:
337                 toc = toc + '      <a id=subchapter href="/' + header['filename'] + '/">' + header['title'] + '</a><br>\n'
338
339         # Make the 'this thing contains...' stuff
340         if HaveChildren(fileStruct, pageNumber):
341                 pages = GetChildren(fileStruct, pageNumber)
342
343                 for pg in pages:
344                         more = more + '<li>' + '<a href="/' + fileStruct[pg]['filename'] + '/">' + fileStruct[pg]['title'] + '</a>' + '</li>\n'
345
346                 more = '<div id=subtopics>\n' + '<h2>This section contains the following topics:</h2>\n' + '<ul>\n' + more + '</ul>\n' + '</div>\n'
347
348         # Make the 'Previous' & 'Next' content
349         nLink = ''
350         pLink = ''
351
352         if pageNumber > 0:
353                 pLink = '<li><a title="' + fileStruct[pageNumber - 1]['title'] + '" href="/' + fileStruct[pageNumber - 1]['filename'] + '" class="previous"> &lt; Previous </a></li>'
354
355         if pageNumber < len(fileStruct) - 1:
356                 nLink = '<li><a title="' + fileStruct[pageNumber + 1]['title'] + '" href="/' + fileStruct[pageNumber + 1]['filename'] + '" class="next"> Next &gt; </a></li>'
357
358         prevnext = '<ul class=pager>' + pLink + nLink + '</ul>'
359
360         # Create the link sidebar
361         sidebar = CreateLinkSidebar(fileStruct, pageNumber, nodeChildren)
362
363         # Parts DO NOT have any content, they are ONLY an organizing construct!
364         # Chapters, subchapters, sections & subsections can all have content,
365         # but the basic fundamental organizing unit WRT content is still the
366         # chapter.
367         if level > 0:
368                 if 'include' in header:
369                         srcFile = open('include/' + header['include'])
370                         content = srcFile.read()
371                         srcFile.close()
372
373                         # Get rid of any extant header in the include file
374                         # (once this is accepted, we can nuke this bit, as content files
375                         # will not have any headers or footers in them)
376                         content = re.sub('---.*\n(.*\n)*---.*\n', '', content)
377                         content = content.replace('{% children %}', '')
378
379                 else:
380                         if 'content' in header:
381                                 content = header['content']
382                         else:
383                                 content = '[something went wrong]'
384
385         # Set up the actual page from the template
386         if 'style' not in header:
387                 page = re.sub("{% if page.style %}.*\n.*\n{% endif %}.*\n", "", template)
388         else:
389                 page = template.replace('{{page.style}}', header['style'])
390                 page = page.replace('{% if page.style %}', '')
391                 page = page.replace('{% endif %}', '')
392
393         page = page.replace('{{ page.title }}', header['title'])
394         page = page.replace('{% tree %}', sidebar)
395         page = page.replace('{% prevnext %}', prevnext)
396         page = page.replace('{{ content }}', content + more)
397
398         # Create the directory for the index.html file to go into
399         os.mkdir(siteDir + header['filename'], 0o775)
400
401         # Finally, write the file!
402         destFile = open(siteDir + header['filename'] + '/index.html', 'w')
403         destFile.write(page)
404         destFile.close()
405
406         # Save filename for next header...
407         lastFile = header['filename']
408         pageNumber = pageNumber + 1
409
410 # Finally, create the TOC
411 sidebar = CreateLinkSidebar(fileStruct, -1, nodeChildren)
412
413 page = re.sub("{% if page.style %}.*\n.*\n{% endif %}.*\n", "", template)
414 page = page.replace('{{ page.title }}', 'Ardour Table of Contents')
415 page = page.replace('{% tree %}', sidebar)
416 page = page.replace('{{ content }}', toc)
417 page = page.replace('{% prevnext %}', '')
418
419 os.mkdir(siteDir + 'toc', 0o775)
420 tocFile = open(siteDir + 'toc/index.html', 'w')
421 tocFile.write(page)
422 tocFile.close()
423
424 print('Processed ' + str(fileCount) + ' files.')
425