3 # Small program to 'implode' the master document automagically from separate
4 # files in the include/ directory.
7 # (C) 2017 Underground Software
15 def CheckForHeader(fn):
19 if line.startswith('---'):
31 master = open('master-doc.txt')
32 firstLine = master.readline().rstrip('\r\n')
35 if firstLine == '<!-- imploded -->':
36 print('Master file has already been imploded.')
39 if os.rename('master-doc.txt', 'master-doc.bak') == False:
40 print('Could not rename master-doc.txt!')
43 master = open('master-doc.bak', 'r')
44 implode = open('master-doc.txt', 'w')
46 implode.write('<!-- imploded -->\n')
49 lineCount = lineCount + 1
51 # Do any header parsing if needed...
52 if line.startswith('---'):
58 hdrLine = master.readline().rstrip('\r\n')
59 lineCount = lineCount + 1
61 # Break out of the loop if we hit the end of header marker
62 if hdrLine.startswith('---'):
65 # Parse out foo: bar pairs & put into header dictionary
66 a = re.split(': ', hdrLine, 1)
69 # Pull in files and write the result to the master file
70 implode.write('\n---\n' + 'title: ' + header['title'] + '\n')
72 if header['part'] != 'part':
73 if 'menu_title' in header:
74 implode.write('menu_title: ' + header['menu_title'] + '\n')
77 implode.write('link: ' + header['link'] + '\n')
80 implode.write('style: ' + header['style'] + '\n')
82 if ('exclude' in header) and ('include' in header):
84 implode.write('include: ' + header['include'] + '\n')
85 implode.write('exclude: yes\n')
87 implode.write('part: ' + header['part'] + '\n' + '---\n')
89 # Only parts have no content...
90 if header['part'] != 'part':
94 fileCount = fileCount + 1
95 inclFile = 'include/' + header['include']
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')
105 #eventually this will go away, as this will never happen again...
106 if CheckForHeader(inclFile) == True:
109 while fromFile.readline().startswith('---') == False:
112 ln = fromFile.readline()
114 while fromFile.readline().startswith('---') == False:
117 shutil.copyfileobj(fromFile, implode)
119 delList.append(inclFile)
124 print('Processed ' + str(lineCount) + ' lines.')
125 print('Imploded master document from ' + str(fileCount) + ' files.')
127 os.remove('master-doc.bak')