]> Shamusworld >> Repos - ardour-manual-diverged/blob - implode.py
Update to minimal build.
[ardour-manual-diverged] / 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 'link' in header:
95                                 implode.write('link: ' + header['link'] + '\n')
96
97                         if 'style' in header:
98                                 implode.write('style: ' + header['style'] + '\n')
99
100                         implode.write('file: ' + header['include'] + '\n')
101
102                         if ('exclude' in header) and ('include' in header):
103                                 noMove = True
104                                 implode.write('include: ' + header['include'] + '\n')
105                                 implode.write('exclude: yes\n')
106
107                 implode.write('part: ' + header['part'] + '\n' + '---\n')
108
109                 # Only parts have no content...
110                 if header['part'] != 'part':
111                         if noMove:
112                                 implode.write('\n')
113                         else:
114                                 fileCount = fileCount + 1
115                                 inclFile = 'include/' + header['include']
116
117                                 try:
118                                         fromFile = open(inclFile)
119                                 except (FileNotFoundError):
120                                         print('Could not find include file "include/' + header['include'] + '"; aborting!')
121                                         os.remove('master-doc.txt')
122                                         os.rename('master-doc.bak', 'master-doc.txt')
123                                         exit(-1)
124
125 #eventually this will go away, as this will never happen again...
126                                 if CheckForHeader(inclFile) == True:
127
128                                         # Skip the header
129                                         while fromFile.readline().startswith('---') == False:
130                                                 pass
131
132                                         ln = fromFile.readline()
133
134                                         while fromFile.readline().startswith('---') == False:
135                                                 pass
136
137                                 shutil.copyfileobj(fromFile, implode)
138                                 fromFile.close()
139                                 delList.append(inclFile)
140
141 master.close()
142 implode.close()
143
144 print('Processed ' + str(lineCount) + ' lines.')
145 print('Imploded master document from ' + str(fileCount) + ' files.')
146
147 # Cleanup after successful implode
148 os.remove('master-doc.bak')
149
150 for name in delList:
151         os.remove(name)
152