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 add_anchors(result.getroot())
57 result.write(output_filename, xml_declaration=True, pretty_print=True, encoding='utf-8')
60 class Fragment(object):
61 def __init__(self, id, themes):
62 super(Fragment, self).__init__()
67 def append(self, event, element):
68 self.events.append((event, element))
70 def closed_events(self):
72 for event, element in self.events:
74 stack.append(('end', element))
79 print 'CLOSED NON-OPEN TAG:', element
82 return self.events + stack
86 for event, element in self.closed_events():
88 result.append(u'<%s %s>' % (element.tag, ' '.join('%s="%s"' % (k, v) for k, v in element.attrib.items())))
90 result.append(element.text)
92 result.append(u'</%s>' % element.tag)
94 result.append(element.tail)
96 result.append(element)
98 return ''.join(result)
100 def __unicode__(self):
101 return self.to_string()
104 def extract_fragments(input_filename):
105 """Extracts theme fragments from input_filename."""
107 closed_fragments = {}
109 for event, element in etree.iterparse(input_filename, events=('start', 'end')):
110 # Process begin and end elements
111 if element.tag == 'span' and element.get('class', '') in ('theme-begin', 'theme-end'):
112 if not event == 'end': continue # Process elements only once, on end event
115 if element.get('class', '') == 'theme-begin':
116 fragment = Fragment(id=element.get('fid'), themes=element.text)
119 if element.getparent().tag != 'body':
120 parents = [element.getparent()]
121 while parents[-1].getparent().tag != 'body':
122 parents.append(parents[-1].getparent())
125 for parent in parents:
126 fragment.append('start', parent)
128 open_fragments[fragment.id] = fragment
130 # Close existing fragment
133 fragment = open_fragments[element.get('fid')]
135 print '%s:closed not open fragment #%s' % (input_filename, element.get('fid'))
137 closed_fragments[fragment.id] = fragment
138 del open_fragments[fragment.id]
140 # Append element tail to lost_text (we don't want to lose any text)
142 for fragment_id in open_fragments:
143 open_fragments[fragment_id].append('text', element.tail)
146 # Process all elements except begin and end
148 # Omit annotation tags
149 if len(element.get('name', '')) or element.get('class', '') == 'annotation':
150 if event == 'end' and element.tail:
151 for fragment_id in open_fragments:
152 open_fragments[fragment_id].append('text', element.tail)
154 for fragment_id in open_fragments:
155 open_fragments[fragment_id].append(event, copy.copy(element))
157 return closed_fragments, open_fragments
160 def add_anchor(element, number):
161 anchor = etree.Element('a', href='#f%d' % number)
162 anchor.set('class', 'anchor')
163 anchor.text = str(number)
165 anchor.tail = element.text
167 element.insert(0, anchor)
169 anchor_target = etree.Element('a', name='f%d' % number)
170 element.insert(0, anchor_target)
173 def add_anchors(root):
175 for element in root.iterdescendants():
176 if element.getparent().tag in 'div' and 'note' in element.getparent().get('class', ''):
178 if element.getparent().tag in 'blockquote':
181 if element.tag == 'p' and 'verse' in element.get('class', ''):
182 if counter == 1 or counter % 5 == 0:
183 add_anchor(element, counter)
185 elif 'paragraph' in element.get('class', ''):
186 add_anchor(element, counter)