]> Shamusworld >> Repos - ardour-manual-diverged/blob - implode.py
Created a function for roman numbers
[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
15 def CheckForHeader(fn):
16         check = open(fn)
17
18         for line in check:
19                 if line.startswith('---'):
20                         check.close()
21                         return True
22
23         check.close()
24         return False
25
26
27 lineCount = 0
28 fileCount = 0
29 delList = []
30
31 master = open('master-doc.txt')
32 firstLine = master.readline().rstrip('\r\n')
33 master.close()
34
35 if firstLine == '<!-- imploded -->':
36         print('Master file has already been imploded.')
37         exit(0)
38
39 if os.rename('master-doc.txt', 'master-doc.bak') == False:
40         print('Could not rename master-doc.txt!')
41         exit(-1)
42
43 master = open('master-doc.bak', 'r')
44 implode = open('master-doc.txt', 'w')
45
46 implode.write('<!-- imploded -->\n')
47
48 for line in master:
49         lineCount = lineCount + 1
50
51         # Do any header parsing if needed...
52         if line.startswith('---'):
53
54                 header = {}
55                 noMove = False
56
57                 while (True):
58                         hdrLine = master.readline().rstrip('\r\n')
59                         lineCount = lineCount + 1
60
61                         # Break out of the loop if we hit the end of header marker
62                         if hdrLine.startswith('---'):
63                                 break
64
65                         # Parse out foo: bar pairs & put into header dictionary
66                         a = re.split(': ', hdrLine, 1)
67                         header[a[0]] = a[1]
68
69                 # Pull in files and write the result to the master file
70                 implode.write('\n---\n' + 'title: ' + header['title'] + '\n')
71
72                 if header['part'] != 'part':
73                         if 'menu_title' in header:
74                                 implode.write('menu_title: ' + header['menu_title'] + '\n')
75
76                         if 'link' in header:
77                                 implode.write('link: ' + header['link'] + '\n')
78
79                         if 'style' in header:
80                                 implode.write('style: ' + header['style'] + '\n')
81
82                         if ('exclude' in header) and ('include' in header):
83                                 noMove = True
84                                 implode.write('include: ' + header['include'] + '\n')
85                                 implode.write('exclude: yes\n')
86
87                 implode.write('part: ' + header['part'] + '\n' + '---\n')
88
89                 # Only parts have no content...
90                 if header['part'] != 'part':
91                         if noMove:
92                                 implode.write('\n')
93                         else:
94                                 fileCount = fileCount + 1
95                                 inclFile = 'include/' + header['include']
96
97                                 try:
98                                         fromFile = open(inclFile)
99                                 except (FileNotFoundError):
100                                         print('Could not find include file "include/' + header['include'] + '"; aborting!')
101                                         os.remove('master-doc.txt')
102                                         os.rename('master-doc.bak', 'master-doc.txt')
103                                         exit(-1)
104
105 #eventually this will go away, as this will never happen again...
106                                 if CheckForHeader(inclFile) == True:
107
108                                         # Skip the header
109                                         while fromFile.readline().startswith('---') == False:
110                                                 pass
111
112                                         ln = fromFile.readline()
113
114                                         while fromFile.readline().startswith('---') == False:
115                                                 pass
116
117                                 shutil.copyfileobj(fromFile, implode)
118                                 fromFile.close()
119                                 delList.append(inclFile)
120
121 master.close()
122 implode.close()
123
124 print('Processed ' + str(lineCount) + ' lines.')
125 print('Imploded master document from ' + str(fileCount) + ' files.')
126
127 os.remove('master-doc.bak')
128
129 for name in delList:
130         os.remove(name)
131