]> Shamusworld >> Repos - ardour-manual/blob - explode.py
Ruler/markers rewrite part 6 of N
[ardour-manual] / 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 lineCount = 0
15 cleanString = re.compile(r'[^a-zA-Z0-9 \._-]+')
16
17
18 #
19 # Create an all lowercase filename without special characters and with spaces
20 # replaced with dashes.
21 #
22 def MakeFilename(s):
23         global cleanString
24         # Clean up the file name, removing all non letter/number or " .-_" chars.
25         # Also, convert to lower case and replace all spaces with dashes.
26         fn = cleanString.sub('', s).lower().replace(' ', '-')
27         # Double dashes can creep in from the above replacement, so we check for
28         # that here.
29         fn = fn.replace('--', '-')
30
31         return fn
32
33
34 #
35 # Parse headers into a dictionary
36 #
37 def ParseHeader(fileObj):
38         global lineCount
39         header = {}
40
41         while (True):
42                 hdrLine = fileObj.readline().rstrip('\r\n')
43                 lineCount = lineCount + 1
44
45                 # Break out of the loop if we hit the end of header marker
46                 if hdrLine.startswith('---'):
47                         break
48
49                 # Check to see that we have a well-formed header construct
50                 match = re.findall(': ', hdrLine)
51
52                 if match:
53                         # Parse out foo: bar pairs & put into header dictionary
54                         a = re.split(': ', hdrLine, 1)
55                         header[a[0]] = a[1]
56
57         return header
58
59
60 fileCount = 0
61 writingFile = False
62 toFile = open('master-doc.txt')
63 toFile.close()
64 filenames = []
65
66
67 master = open('master-doc.txt')
68 firstLine = master.readline().rstrip('\r\n')
69 master.close()
70
71 if firstLine == '<!-- exploded -->':
72         print('Master file has already been exploded.')
73         exit(0)
74
75 if os.rename('master-doc.txt', 'master-doc.bak') == False:
76         print('Could not rename master-doc.txt!')
77         exit(-1)
78
79 master = open('master-doc.bak', 'r')
80 explode = open('master-doc.txt', 'w')
81
82 explode.write('<!-- exploded -->\n')
83
84 for line in master:
85         lineCount = lineCount + 1
86
87         # Do any header parsing if needed...
88         if line.startswith('---'):
89
90                 # Close any open file from the previous header
91                 if (writingFile):
92                         toFile.close()
93                         writingFile = False
94
95                 noMove = False
96                 header = ParseHeader(master)
97
98                 # Make sure the filename we're making is unique...
99                 basename = MakeFilename(header['title'])
100                 inclFile = basename + '.html'
101
102                 if 'file' in header:
103                         inclFile = header['file']
104                 else:
105                         suffix = 1
106
107                         while inclFile in filenames:
108                                 suffix = suffix + 1
109                                 inclFile = basename + '_' + str(suffix) + '.html'
110
111                 # Find all files in the master file and write them out to include/,
112                 # while removing it from the master file.
113                 explode.write('\n---\n' + 'title: ' + header['title'] + '\n')
114
115                 if header['part'] != 'part':
116                         if 'menu_title' in header:
117                                 explode.write('menu_title: ' + header['menu_title'] + '\n')
118
119                         if 'style' in header:
120                                 explode.write('style: ' + header['style'] + '\n')
121
122                         if 'include' in header:
123                                 noMove = True
124                                 explode.write('include: ' + header['include'] + '\n')
125                                 explode.write('exclude: yes\n')
126                                 filenames.append(header['include'])
127                         else:
128                                 explode.write('include: ' + inclFile + '\n')
129                                 filenames.append(inclFile)
130
131                 if 'link' in header:
132                         explode.write('link: ' + header['link'] + '\n')
133
134                 if 'uri' in header:
135                         explode.write('uri: ' + header['uri'] + '\n')
136
137                 explode.write('part: ' + header['part'] + '\n' + '---\n')
138
139                 # Only parts have no content...
140                 if header['part'] != 'part':
141                         if noMove:
142                                 explode.write('\n')
143                         else:
144                                 fileCount = fileCount + 1
145
146                                 toFile = open('include/' + inclFile, 'w')
147                                 writingFile = True
148
149         else:
150                 if writingFile:
151                         toFile.write(line)
152
153 master.close()
154 explode.close()
155
156 print('Processed ' + str(lineCount) + ' lines.')
157 print('Exploded master document into ' + str(fileCount) + ' files.')
158
159 os.remove('master-doc.bak')
160