1 # -*- coding: utf-8 -*-
11 ENTITY_SUBSTITUTIONS = [
20 def substitute_entities(context, text):
21 """XPath extension function converting all entites in passed text."""
22 if isinstance(text, list):
24 for entity, substitutution in ENTITY_SUBSTITUTIONS:
25 text = text.replace(entity, substitutution)
29 # Register substitute_entities function with lxml
30 ns = etree.FunctionNamespace('http://wolnelektury.pl/functions')
31 ns['substitute_entities'] = substitute_entities
34 def transform(input_filename, output_filename):
35 """Transforms file input_filename in XML to output_filename in XHTML."""
37 style_filename = os.path.join(os.path.dirname(__file__), 'book2html.xslt')
38 style = etree.parse(style_filename)
40 doc_file = cStringIO.StringIO()
41 expr = re.compile(r'/\s', re.MULTILINE | re.UNICODE);
43 f = open(input_filename, 'r')
45 line = line.decode('utf-8')
46 line = expr.sub(u'<br/>\n', line)
47 doc_file.write(line.encode('utf-8'))
52 parser = etree.XMLParser(remove_blank_text=True)
53 doc = etree.parse(doc_file, parser)
55 result = doc.xslt(style)
56 result.write(output_filename, xml_declaration=True, pretty_print=True, encoding='utf-8')
59 class Fragment(object):
60 def __init__(self, id, themes):
61 super(Fragment, self).__init__()
66 def append(self, event, element):
67 self.events.append((event, element))
69 def closed_events(self):
71 for event, element in self.events:
73 stack.append(('end', element))
78 print 'CLOSED NON-OPEN TAG:', element
81 return self.events + stack
85 for event, element in self.closed_events():
87 result.append(u'<%s %s>' % (element.tag, ' '.join('%s="%s"' % (k, v) for k, v in element.attrib.items())))
89 result.append(element.text)
91 result.append(u'</%s>' % element.tag)
93 result.append(element.tail)
95 result.append(element)
97 return ''.join(result)
99 def __unicode__(self):
100 return self.to_string()
103 def extract_fragments(input_filename):
104 """Extracts theme fragments from input_filename."""
106 closed_fragments = {}
108 for event, element in etree.iterparse(input_filename, events=('start', 'end')):
109 # Process begin and end elements
110 if element.tag == 'span' and element.get('class', '') in ('theme-begin', 'theme-end'):
111 if not event == 'end': continue # Process elements only once, on end event
114 if element.get('class', '') == 'theme-begin':
115 fragment = Fragment(id=element.get('fid'), themes=element.text)
118 if element.getparent().tag != 'body':
119 parents = [element.getparent()]
120 while parents[-1].getparent().tag != 'body':
121 parents.append(parents[-1].getparent())
124 for parent in parents:
125 fragment.append('start', parent)
127 open_fragments[fragment.id] = fragment
129 # Close existing fragment
132 fragment = open_fragments[element.get('fid')]
134 print '%s:closed not open fragment #%s' % (input_filename, element.get('fid'))
136 closed_fragments[fragment.id] = fragment
137 del open_fragments[fragment.id]
139 # Append element tail to lost_text (we don't want to lose any text)
141 for fragment_id in open_fragments:
142 open_fragments[fragment_id].append('text', element.tail)
145 # Process all elements except begin and end
147 # Omit annotation tags
148 if len(element.get('name', '')) or element.get('class', '') == 'annotation':
149 if event == 'end' and element.tail:
150 for fragment_id in open_fragments:
151 open_fragments[fragment_id].append('text', element.tail)
153 for fragment_id in open_fragments:
154 open_fragments[fragment_id].append(event, copy.copy(element))
156 return closed_fragments, open_fragments