-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathhtmlcombine.py
executable file
·87 lines (71 loc) · 2.53 KB
/
htmlcombine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
#
import re
import sys
from optparse import OptionParser
###############################################################################
parser = OptionParser( 'Usage: %prog [OPTIONS]' )
parser.add_option( '--header', action='store', default=None, help='header file' )
parser.add_option( '--footer', action='store', default=None, help='footer file' )
parser.add_option( '--body', action='store', default=None, help='file to extract contents of body from' )
parser.add_option( '-v', '--verbose', action='count', default=False, help='increase verbosity' )
(options, args) = parser.parse_args()
if( len(args) != 0 ):
parser.error( 'incorrect number of arguments' )
if not options.header:
parser.error( '--header must be specified' )
if not options.footer:
parser.error( '--footer must be specified' )
if not options.body:
parser.error( '--body must be specified' )
###############################################################################
title = 'Unknown'
# parse majorheading
with open( options.body, 'r' ) as f:
rx = re.compile( '^.*class="majorheading".*>([^>]+)</.+>' )
for line in f:
m = rx.match( line )
if not m:
continue;
title = m.group( 1 )
break
m = re.compile( '(\S+)\s+(\S+)\s+(.+)' ).match( title )
if m:
shortTitle = m.group(3)
else:
shortTitle = title
menu = ''
if not shortTitle == 'Documentation':
menu += '<li><a href="index.html">Documentation</a></li>\n'
menu += '<li class="active">%s</li>' % (shortTitle)
else:
menu += '<li class="active">Documentation</li>'
with open( options.header, 'r' ) as f:
rxTitle = re.compile( '__TITLE__' )
rxShortTitle = re.compile( '__SHORT_TITLE__' )
rxMenu = re.compile( '__MENU__' )
for line in f:
line = re.sub( rxTitle, title, line )
line = re.sub( rxShortTitle, shortTitle, line )
line = re.sub( rxMenu, menu, line )
sys.stdout.write( line )
# out everything *after* first <body> and *before* </body>
with open( options.body, 'r' ) as f:
rxBegin = re.compile( '<body>' )
rxEnd = re.compile( '</body>' )
inside = False
lstrip = False
for line in f:
if rxBegin.match( line ):
inside = True
continue
if rxEnd.match( line ):
outside = False
continue
if lstrip:
line = line.lstrip()
if inside:
sys.stdout.write( line )
with open( options.footer, 'r' ) as f:
for line in f:
sys.stdout.write( line )