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
21 # Create an all lowercase filename without special characters and with spaces
22 # replaced with dashes.
25 # This RE is shitty, but I can't think of a better one right now
26 fn = re.sub("[?!'&#:;_*()/\\,.]+", "", s)
28 fn = fn.replace(' ', '-')
33 # Parse headers into a dictionary
35 def ParseHeader(fileObj):
39 hdrLine = fileObj.readline().rstrip('\r\n')
41 # Break out of the loop if we hit the end of header marker
42 if hdrLine.startswith('---'):
45 # Parse out foo: bar pairs & put into header dictionary
46 a = re.split(': ', hdrLine, 1)
53 # Turn a "part" name into an int
62 elif s == 'subchapter':
66 elif s == 'subsection':
73 # Capture the master document's structure (and content, if any) in a list
75 def GetFileStructure():
80 mf = open('master-doc.txt')
83 if ln.startswith('---'):
84 # First, stuff any content that we may have read into the current
87 fs[-1]['content'] = content
91 # Then, get the new header and do things to it
93 level = PartToLevel(hdr['part'])
95 fnames[level] = MakeFilename(hdr['title'])
98 for i in range(level + 1):
99 fullName = fullName + fnames[i] + '/'
101 hdr['filename'] = fullName.rstrip('/')
104 if ('include' not in hdr) and (level > 0):
108 content = content + ln
110 # Catch the last file, since it would be missed above
112 fs[-1]['content'] = content
119 # Determine if a particular node has child nodes
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:
126 # If the next node is at a lower level than the current node, we have
128 if fs[pos]['level'] < fs[pos + 1]['level']:
131 # Otherwise, no children at this node.
136 # Get the children at this level, and return them in a list
138 def GetChildren(fs, pos):
141 childLevel = fs[pos]['level']
143 while fs[pos]['level'] >= childLevel:
144 if fs[pos]['level'] == childLevel:
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.)
160 def FindChildren(fs):
163 for i in range(len(fs)):
164 if HaveChildren(fs, i):
165 childArray.append(GetChildren(fs, i))
167 childArray.append([])
173 # Make an array of the top level nodes in the file structure
175 def FindTopLevelNodes(fs):
178 for i in range(len(fs)):
179 if fs[i]['level'] == 0:
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.
190 def FixInternalLinks(content):
191 #make a dictionary ahead of time of <link:, filename:> pairs
196 # Recursively build a list of links based on the location of the page we're
197 # looking at currently
199 def BuildList(lst, fs, pagePos, cList):
200 content = '\n\n<dl>\n'
202 for i in range(len(lst)):
204 nextPos = lst[i + 1] if i + 1 < len(lst) else len(fs)
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 + '>'
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)
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)
219 content = content + '</dd>\n'
221 content = content + '</dl>\n'
226 # Create link sidebar given a position in the list.
228 def CreateLinkSidebar(fs, pos, childList):
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:]
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' ]
253 siteDir = './website/'
255 if os.access(siteDir, os.F_OK):
256 print('Removing stale HTML data...')
257 shutil.rmtree(siteDir)
259 shutil.copytree('./source', siteDir)
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')
267 # Read the template, and fix the stuff that's fixed for all pages
268 temp = open('page-template.txt')
269 template = temp.read()
272 template = template.replace('{{page.bootstrap_path}}', '/bootstrap-2.2.2')
273 template = template.replace('{{page.page_title}}', 'The Ardour Manual')
276 # Parse out the master docuemnt's structure into a dictionary list
277 fileStruct = GetFileStructure()
278 nodeChildren = FindChildren(fileStruct)
282 master = open('master-doc.txt')
283 firstLine = master.readline().rstrip('\r\n')
286 if firstLine == '<!-- exploded -->':
287 print('Parsing exploded file...')
288 elif firstLine == '<!-- imploded -->':
289 print('Parsing imploded file...')
291 print('Parsing unknown type...')
293 for header in fileStruct:
294 fileCount = fileCount + 1
299 level = header['level']
301 # Handle Part/Chapter/subchapter/section/subsection numbering
311 levelNums[level] = levelNums[level] + 1;
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]
317 for i in range(level):
321 print('\nPart ' + roman[levelNums[0]] + ': ', end='')
323 print('\n\tChapter ' + str(levelNums[1]) + ': ', end='')
325 print(header['title'])
327 # Handle TOC scriblings...
329 toc = toc + '<h2>Part ' + roman[levelNums[level]] + ': ' + header['title'] + '</h2>\n';
331 toc = toc + ' <p id=chapter>Ch. ' + str(levelNums[level]) + ': <a href="/' + header['filename'] + '/">' + header['title'] + '</a></p>\n'
333 toc = toc + ' <a id=subchapter href="/' + header['filename'] + '/">' + header['title'] + '</a><br>\n'
335 toc = toc + ' <a id=subchapter href="/' + header['filename'] + '/">' + header['title'] + '</a><br>\n'
337 toc = toc + ' <a id=subchapter href="/' + header['filename'] + '/">' + header['title'] + '</a><br>\n'
339 # Make the 'this thing contains...' stuff
340 if HaveChildren(fileStruct, pageNumber):
341 pages = GetChildren(fileStruct, pageNumber)
344 more = more + '<li>' + '<a href="/' + fileStruct[pg]['filename'] + '/">' + fileStruct[pg]['title'] + '</a>' + '</li>\n'
346 more = '<div id=subtopics>\n' + '<h2>This section contains the following topics:</h2>\n' + '<ul>\n' + more + '</ul>\n' + '</div>\n'
348 # Make the 'Previous' & 'Next' content
353 pLink = '<li><a title="' + fileStruct[pageNumber - 1]['title'] + '" href="/' + fileStruct[pageNumber - 1]['filename'] + '" class="previous"> < Previous </a></li>'
355 if pageNumber < len(fileStruct) - 1:
356 nLink = '<li><a title="' + fileStruct[pageNumber + 1]['title'] + '" href="/' + fileStruct[pageNumber + 1]['filename'] + '" class="next"> Next > </a></li>'
358 prevnext = '<ul class=pager>' + pLink + nLink + '</ul>'
360 # Create the link sidebar
361 sidebar = CreateLinkSidebar(fileStruct, pageNumber, nodeChildren)
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
368 if 'include' in header:
369 srcFile = open('include/' + header['include'])
370 content = srcFile.read()
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 %}', '')
380 if 'content' in header:
381 content = header['content']
383 content = '[something went wrong]'
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)
389 page = template.replace('{{page.style}}', header['style'])
390 page = page.replace('{% if page.style %}', '')
391 page = page.replace('{% endif %}', '')
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)
398 # Create the directory for the index.html file to go into
399 os.mkdir(siteDir + header['filename'], 0o775)
401 # Finally, write the file!
402 destFile = open(siteDir + header['filename'] + '/index.html', 'w')
406 # Save filename for next header...
407 lastFile = header['filename']
408 pageNumber = pageNumber + 1
410 # Finally, create the TOC
411 sidebar = CreateLinkSidebar(fileStruct, -1, nodeChildren)
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 %}', '')
419 os.mkdir(siteDir + 'toc', 0o775)
420 tocFile = open(siteDir + 'toc/index.html', 'w')
424 print('Processed ' + str(fileCount) + ' files.')