2 # -*- coding: utf-8 -*-
11 usage = """Usage: %prog [options] SOURCE [SOURCE...]
12 Extract theme fragments from SOURCE."""
14 parser = optparse.OptionParser(usage=usage)
16 parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
17 help='print status messages to stdout')
19 options, input_filenames = parser.parse_args()
21 if len(input_filenames) < 1:
26 class Fragment(object):
27 def __init__(self, id, themes):
28 super(Fragment, self).__init__()
33 def append(self, event, element):
34 self.events.append((event, element))
36 def closed_events(self):
38 for event, element in self.events:
40 stack.append(('end', element))
45 print 'CLOSED NON-OPEN TAG:', element
48 return self.events + stack
52 for event, element in self.closed_events():
54 result.append(u'<%s %s>' % (element.tag, ' '.join('%s="%s"' % (k, v) for k, v in element.attrib.items())))
56 result.append(element.text)
58 result.append(u'</%s>' % element.tag)
60 result.append(element.tail)
62 result.append(element)
64 return ''.join(result)
66 def __unicode__(self):
67 return self.to_string()
71 for input_filename in input_filenames:
75 output_filename = os.path.splitext(input_filename)[0] + '.fragments.html'
81 for event, element in etree.iterparse(input_filename, events=('start', 'end')):
83 # Process begin and end elements
84 if element.tag == 'span' and element.get('class', '') in ('theme-begin', 'theme-end'):
85 if not event == 'end': continue # Process elements only once, on end event
88 if element.get('class', '') == 'theme-begin':
89 fragment = Fragment(id=element.get('fid'), themes=element.text)
92 if element.getparent().tag != 'body':
93 parents = [element.getparent()]
94 while parents[-1].getparent().tag != 'body':
95 parents.append(parents[-1].getparent())
98 for parent in parents:
99 fragment.append('start', parent)
101 open_fragments[fragment.id] = fragment
103 # Close existing fragment
106 fragment = open_fragments[element.get('fid')]
108 print '%s:closed not open fragment #%s' % (input_filename, element.get('fid'))
110 closed_fragments[fragment.id] = fragment
111 del open_fragments[fragment.id]
113 # Append element tail to lost_text (we don't want to lose any text)
115 for fragment_id in open_fragments:
116 open_fragments[fragment_id].append('text', element.tail)
119 # Process all elements except begin and end
121 # Omit annotation tags
122 if len(element.get('name', '')) or element.get('class', '') == 'annotation':
123 if event == 'end' and element.tail:
124 for fragment_id in open_fragments:
125 open_fragments[fragment_id].append('text', element.tail)
127 for fragment_id in open_fragments:
128 open_fragments[fragment_id].append(event, copy.copy(element))
131 for fragment_id in open_fragments:
132 print '%s:warning:unclosed fragment #%s' % (input_filename, fragment_id)
134 output_file = open(output_filename, 'w')
135 output_file.write("""
136 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
138 <title>bookfragments output</title>
139 <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
140 <link rel="stylesheet" href="master.css" type="text/css" media="screen" charset="utf-8" />
143 for fragment in closed_fragments.values():
144 html = u'<div class="fragment"><h3>[#%s] %s</h3>%s</div>' % (fragment.id, fragment.themes, fragment)
145 output_file.write(html.encode('utf-8'))
146 output_file.write('</body></html>')