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