]> Shamusworld >> Repos - ardour-manual/blob - implode.py
Be a bit more specific at least.
[ardour-manual] / implode.py
1 #!/usr/bin/python
2 #
3 # Small program to 'implode' the master document automagically from 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
16
17 #
18 # Parse headers into a dictionary
19 #
20 def ParseHeader(fileObj):
21         global lineCount
22         header = {}
23
24         while (True):
25                 hdrLine = fileObj.readline().rstrip('\r\n')
26                 lineCount = lineCount + 1
27
28                 # Break out of the loop if we hit the end of header marker
29                 if hdrLine.startswith('---'):
30                         break
31
32                 # Check to see that we have a well-formed header construct
33                 match = re.findall(': ', hdrLine)
34
35                 if match:
36                         # Parse out foo: bar pairs & put into header dictionary
37                         a = re.split(': ', hdrLine, 1)
38                         header[a[0]] = a[1]
39
40         return header
41
42
43 #
44 # Check to see if a given file has a header (it shouldn't)
45 #
46 def CheckForHeader(fn):
47         check = open(fn)
48
49         for line in check:
50                 if line.startswith('---'):
51                         check.close()
52                         return True
53
54         check.close()
55         return False
56
57
58 fileCount = 0
59 delList = []
60
61 master = open('master-doc.txt')
62 firstLine = master.readline().rstrip('\r\n')
63 master.close()
64
65 if firstLine == '<!-- imploded -->':
66         print('Master file has already been imploded.')
67         exit(0)
68
69 if os.rename('master-doc.txt', 'master-doc.bak') == False:
70         print('Could not rename master-doc.txt!')
71         exit(-1)
72
73 master = open('master-doc.bak', 'r')
74 implode = open('master-doc.txt', 'w')
75
76 implode.write('<!-- imploded -->\n')
77
78 for line in master:
79         lineCount = lineCount + 1
80
81         # Do any header parsing if needed...
82         if line.startswith('---'):
83
84                 noMove = False
85                 header = ParseHeader(master)
86
87                 # Pull in files and write the result to the master file
88                 implode.write('\n---\n' + 'title: ' + header['title'] + '\n')
89
90                 if header['part'] != 'part':
91                         if 'menu_title' in header:
92                                 implode.write('menu_title: ' + header['menu_title'] + '\n')
93
94                         if 'style' in header:
95                                 implode.write('style: ' + header['style'] + '\n')
96
97                         implode.write('file: ' + header['include'] + '\n')
98
99                         if ('exclude' in header) and ('include' in header):
100                                 noMove = True
101                                 implode.write('include: ' + header['include'] + '\n')
102                                 implode.write('exclude: yes\n')
103
104                 if 'link' in header:
105                         implode.write('link: ' + header['link'] + '\n')
106
107                 if 'uri' in header:
108                         implode.write('uri: ' + header['uri'] + '\n')
109
110                 implode.write('part: ' + header['part'] + '\n' + '---\n')
111
112                 # Only parts have no content...
113                 if header['part'] != 'part':
114                         if noMove:
115                                 implode.write('\n')
116                         else:
117                                 fileCount = fileCount + 1
118                                 inclFile = 'include/' + header['include']
119
120                                 try:
121                                         fromFile = open(inclFile)
122                                 except (FileNotFoundError):
123                                         print('Could not find include file "include/' + header['include'] + '"; aborting!')
124                                         os.remove('master-doc.txt')
125                                         os.rename('master-doc.bak', 'master-doc.txt')
126                                         exit(-1)
127
128 #eventually this will go away, as this will never happen again...
129                                 if CheckForHeader(inclFile) == True:
130
131                                         # Skip the header
132                                         while fromFile.readline().startswith('---') == False:
133                                                 pass
134
135                                         ln = fromFile.readline()
136
137                                         while fromFile.readline().startswith('---') == False:
138                                                 pass
139
140 #                               shutil.copyfileobj(fromFile, implode)
141                                 # Strip trailing newlines from content...
142                                 tempContent = fromFile.read().rstrip('\r\n')
143                                 implode.write(tempContent + '\n')
144                                 fromFile.close()
145                                 delList.append(inclFile)
146
147 master.close()
148 implode.close()
149
150 print('Processed ' + str(lineCount) + ' lines.')
151 print('Imploded master document from ' + str(fileCount) + ' files.')
152
153 # Cleanup after successful implode
154 os.remove('master-doc.bak')
155
156 for name in delList:
157         os.remove(name)
158