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