3 # Script to take the master document and create something that build.rb wants.
4 # Eventually, this will replace build.rb
6 # Ultimately, we will write directly to the finished web site structure instead
7 # of creating this half-assed thing that we then rely on the other ruby script
11 # (C) 2017 Underground Software
19 # Create an all lowercase filename without special characters and with spaces
20 # replaced with dashes.
23 # This RE is shitty, but I can't think of a better one right now
24 fn = re.sub("[?!'&#:;_*()/\\,.]+", "", s)
26 fn = fn.replace(' ', '-')
32 buildDir = './_manual.munge/'
34 if os.access(buildDir, os.F_OK):
35 print('Removing stale manual data...')
36 shutil.rmtree(buildDir)
38 os.mkdir(buildDir, 0o774)
40 # Yeah, need to make a symlink in include/ too :-P
41 # [this will go away when the rewrite happens]
42 if (os.access('include/_manual', os.F_OK) == False):
43 os.symlink('../_manual/', 'include/_manual')
48 master = open('master-doc.txt')
50 toc = open(buildDir + '00_toc.html', 'w')
51 toc.write('---\n' + 'title: Ardour Table of Contents\n' + '---\n\n')
54 roman = [ '0', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X',
55 'XI', 'XII', 'XIII', 'XIV', 'XV', 'XVI', 'XVII', 'XVIII', 'XIX', 'XX',
56 'XXI', 'XXII', 'XXIII', 'XXIV', 'XXV', 'XXVI', 'XXVII', 'XXVIII', 'XXIX', 'XXX' ]
63 # This is a shitty way of getting a filehandle...
64 htmlFile = open('master-doc.txt')
68 firstLine = master.readline().rstrip('\r\n')
70 if firstLine == '<!-- exploded -->':
71 print('Parsing exploded file...\n')
72 elif firstLine == '<!-- imploded -->':
73 print('Parsing imploded file...\n')
75 print('Parsing unknown type...\n')
78 lineCount = lineCount + 1
80 # Do any header parsing if needed...
81 if line.startswith('---'):
83 # Close any open files that may have been opened from the last header...
91 hdrLine = master.readline().rstrip('\r\n')
92 lineCount = lineCount + 1
94 # Break out of the loop if we hit the end of header marker
95 if hdrLine.startswith('---'):
98 # Parse out foo: bar pairs & put into header dictionary
99 a = re.split(': ', hdrLine, 1)
102 # Header has been parsed, now do something about it...
105 # Handle Part/Chapter/subchapter/section/subsection numbering
106 if (header['part'] == 'part'):
109 elif (header['part'] == 'chapter'):
112 elif (header['part'] == 'subchapter'):
115 elif (header['part'] == 'section'):
118 elif (header['part'] == 'subsection'):
121 levelNums[level] = levelNums[level] + 1;
123 # This is totally unnecessary, but nice; besides which, you can capture
124 # the output to a file to look at later if you like :-)
125 for i in range(level):
129 print('\nPart ' + roman[levelNums[0]] + ': ', end='')
131 print('\n\tChapter ' + str(levelNums[1]) + ': ', end='')
133 print(header['title'])
135 # Make a filename from the title...
136 levelNames[level] = MakeFilename(header['title'])
139 for i in range(level):
140 path = path + str(levelNums[i]).zfill(2) + '_' + levelNames[i] + '/'
142 path = path + str(levelNums[level]).zfill(2) + '_' + levelNames[level]
144 # Append the appropriate footer to the last file, if the current file
145 # is one level down from the previous...
146 if ((level > 0) and (level > lastLevel)):
147 nfile = open(buildDir + lastFile + '.html', 'a')
148 nfile.write('\n{% children %}\n\n')
151 # Handle TOC scriblings...
153 toc.write('<h2>Part ' + roman[levelNums[level]] + ': ' + header['title'] + '</h2>\n');
155 toc.write(' <p id=chapter>Ch. ' + str(levelNums[level]) + ': <a href="/' + levelNames[0] + '/' + levelNames[1] + '/">' + header['title'] + '</a></p>\n')
157 toc.write(' <a id=subchapter href="/' + levelNames[0] + '/' + levelNames[1] + '/' + levelNames[2] + '/">' + header['title'] + '</a><br>\n')
159 toc.write(' <a id=subchapter href="/' + levelNames[0] + '/' + levelNames[1] + '/' + levelNames[2] + '/' + levelNames[3] + '/">' + header['title'] + '</a><br>\n')
161 toc.write(' <a id=subchapter href="/' + levelNames[0] + '/' + levelNames[1] + '/' + levelNames[2] + '/' + levelNames[3] + '/' + levelNames[4] + '/">' + header['title'] + '</a><br>\n')
163 # Parts DO NOT have any content, they are ONLY an organizing construct!
165 os.mkdir(buildDir + path, 0o774)
166 nfile = open(buildDir + path + '.html', 'w')
167 nfile.write('---\n' + 'title: ' + header['title'] + '\n')
169 if ('menu_title' in header):
170 nfile.write('menu_title: ' + header['menu_title'] + '\n')
172 nfile.write('---\n' + '\n{% children %}\n\n')
175 # Chapters, subchapters, sections & subsections all can have content.
176 # But the basic fundamental organizing unit WRT content is still the
179 os.mkdir(buildDir + path, 0o774)
181 if ('include' in header):
182 shutil.copy('include/' + header['include'], buildDir + path + '.html')
184 htmlFile = open(buildDir + path + '.html', 'w')
186 htmlFile.write('---\n' + 'title: ' + header['title'] + '\n')
188 if ('menu_title' in header):
189 htmlFile.write('menu_title: ' + header['menu_title'] + '\n')
191 if ('style' in header):
192 htmlFile.write('style: ' + header['style'] + '\n')
194 htmlFile.write('---\n\n')
196 # Save filename for next header...
199 # No header, in that case, just dump the lines into the currently open file
201 if (writingToFile and (level > 0)):
211 print('\nProcessed ' + str(lineCount) + ' lines.')