]> Shamusworld >> Repos - ardour-manual-diverged/blob - explode.py
Minimal commit for Paul.
[ardour-manual-diverged] / explode.py
1 #!/usr/bin/python
2 #
3 # Small program to 'explode' the master document automagically into separate
4 # files in the include/ directory.
5 #
6 # by James Hammons
7 # (C) 2017 Underground Software
8 #
9
10 import os
11 import re
12 import shutil
13
14
15 #
16 # Create an all lowercase filename without special characters and with spaces
17 # replaced with dashes.
18 #
19 def MakeFilename(s):
20         # This RE is shitty, but I can't think of a better one right now
21         fn = re.sub("[?!'&#:;_*()/\\,.]+", "", s)
22         fn = fn.lower()
23         fn = fn.replace(' ', '-')
24         return fn
25
26
27 lineCount = 0
28 fileCount = 0
29 writingFile = False
30 toFile = open('master-doc.txt')
31 toFile.close()
32 filenames = []
33
34
35 master = open('master-doc.txt')
36 firstLine = master.readline().rstrip('\r\n')
37 master.close()
38
39 if firstLine == '<!-- exploded -->':
40         print('Master file has already been exploded.')
41         exit(0)
42
43 if os.rename('master-doc.txt', 'master-doc.bak') == False:
44         print('Could not rename master-doc.txt!')
45         exit(-1)
46
47 master = open('master-doc.bak', 'r')
48 explode = open('master-doc.txt', 'w')
49
50 explode.write('<!-- exploded -->\n')
51
52 for line in master:
53         lineCount = lineCount + 1
54
55         # Do any header parsing if needed...
56         if line.startswith('---'):
57
58                 # Close any open file from the previous header
59                 if (writingFile):
60                         toFile.close()
61                         writingFile = False
62
63                 header = {}
64                 noMove = False
65
66                 while (True):
67                         hdrLine = master.readline().rstrip('\r\n')
68                         lineCount = lineCount + 1
69
70                         # Break out of the loop if we hit the end of header marker
71                         if hdrLine.startswith('---'):
72                                 break
73
74                         # Parse out foo: bar pairs & put into header dictionary
75                         a = re.split(': ', hdrLine, 1)
76                         header[a[0]] = a[1]
77
78                 # Make sure the filename we're making is unique...
79                 inclFile = MakeFilename(header['title']) + '.html'
80
81                 if inclFile in filenames:
82                         suffix = 2
83                         inclFile = MakeFilename(header['title']) + str(suffix) + '.html'
84
85                         while inclFile in filenames:
86                                 suffix = suffix + 1
87                                 inclFile = MakeFilename(header['title']) + str(suffix) + '.html'
88
89                 # Pull in files and write the result to the master file
90                 explode.write('\n---\n' + 'title: ' + header['title'] + '\n')
91
92                 if header['part'] != 'part':
93                         if 'menu_title' in header:
94                                 explode.write('menu_title: ' + header['menu_title'] + '\n')
95
96                         if 'link' in header:
97                                 explode.write('link: ' + header['link'] + '\n')
98
99                         if 'style' in header:
100                                 explode.write('style: ' + header['style'] + '\n')
101
102                         if 'include' in header:
103                                 noMove = True
104                                 explode.write('include: ' + header['include'] + '\n')
105                                 explode.write('exclude: yes\n')
106                                 filenames.append(header['include'])
107                         else:
108                                 explode.write('include: ' + inclFile + '\n')
109                                 filenames.append(inclFile)
110
111                 explode.write('part: ' + header['part'] + '\n' + '---\n')
112
113                 # Only parts have no content...
114                 if header['part'] != 'part':
115                         if noMove:
116                                 explode.write('\n')
117                         else:
118                                 fileCount = fileCount + 1
119
120                                 toFile = open('include/' + inclFile, 'w')
121                                 writingFile = True
122                                 toFile.write('---\n' + 'title: ' + header['title'] + '\n')
123
124                                 if 'menu_title' in header:
125                                         toFile.write('menu_title: ' + header['menu_title'] + '\n')
126
127                                 if 'style' in header:
128                                         toFile.write('style: ' + header['style'] + '\n')
129
130                                 toFile.write('---\n')
131         else:
132                 if writingFile:
133                         toFile.write(line)
134
135 master.close()
136 explode.close()
137
138 print('Processed ' + str(lineCount) + ' lines.')
139 print('Exploded master document into ' + str(fileCount) + ' files.')
140
141 os.remove('master-doc.bak')
142