Poprawienie ilości światła pomiędzy częściami utworu w plikach TXT wypluwanych przez...
[wolnelektury.git] / lib / librarian / text.py
1 # -*- coding: utf-8 -*-
2 import os
3 import cStringIO
4 import re
5 import codecs
6
7 from lxml import etree
8
9 from librarian import dcparser
10
11
12 ENTITY_SUBSTITUTIONS = [
13     (u'---', u'—'),
14     (u'--', u'–'),
15     (u'...', u'…'),
16     (u',,', u'„'),
17     (u'"', u'”'),
18 ]
19
20
21 def substitute_entities(context, text):
22     """XPath extension function converting all entites in passed text."""
23     if isinstance(text, list):
24         text = ''.join(text)
25     for entity, substitutution in ENTITY_SUBSTITUTIONS:
26         text = text.replace(entity, substitutution)
27     return text
28
29
30 # Register substitute_entities function with lxml
31 ns = etree.FunctionNamespace('http://wolnelektury.pl/functions')
32 ns['substitute_entities'] = substitute_entities
33
34
35 def transform(input_filename, output_filename):
36     """Transforms file input_filename in XML to output_filename in TXT."""
37     # Parse XSLT
38     style_filename = os.path.join(os.path.dirname(__file__), 'book2txt.xslt')
39     style = etree.parse(style_filename)
40
41     doc_file = cStringIO.StringIO()
42     expr = re.compile(r'/\s', re.MULTILINE | re.UNICODE);
43     
44     f = open(input_filename, 'r')
45     for line in f:
46         line = line.decode('utf-8')
47         line = expr.sub(u'<br/>\n', line)
48         doc_file.write(line.encode('utf-8'))
49     f.close()
50
51     doc_file.seek(0)
52
53     parser = etree.XMLParser(remove_blank_text=True)
54     doc = etree.parse(doc_file, parser)
55     
56     result = doc.xslt(style)
57     output_file = codecs.open(output_filename, 'wb', encoding='utf-8')
58     output_file.write(unicode(result) % dcparser.parse(input_filename).url)
59