3 from __future__ import with_statement
9 sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
11 from StringIO import StringIO
12 from lxml import etree
16 (u'---', u'\u2014'), # mdash
17 (u'--', u'\u2013'), # ndash
18 (u'...', u'\u2026'), # ldots
19 (u',,', u'\u201E'), # lower double back-quote
20 (u'"', u'\u201D'), # upper double quote
23 DIALOG_EXPR = re.compile(r"\s*---\s(.*)")
25 def wl_normalize_text(context, text):
26 """XPath extension function converting all entites in passed text."""
27 if isinstance(text, list):
30 for code, ucode in REPLACEMENTS:
31 text = text.replace(code, ucode)
35 def wl_fix_dialog(context, data):
37 if isinstance(data, list):
42 m = DIALOG_EXPR.match(text)
50 def filter_verse_ends(data):
51 return data.replace('/\n', '<br />')
53 ns = etree.FunctionNamespace('http://wolnelektury.pl/functions')
54 ns['normalize-text'] = wl_normalize_text
55 ns['fix-dialog-line'] = wl_fix_dialog
57 def normalize_stylesheet():
58 return etree.XSLT(etree.parse(os.path.join(os.path.dirname(librarian.__file__), 'xslt', 'normalize.xslt')))
60 if __name__ == '__main__':
61 tran = normalize_stylesheet()
63 doc = trans( etree.parse(input) )
64 print etree.tostring(doc, pretty_print=True, encoding=unicode).encode('utf-8')
66 for err in trans.error_log:
67 sys.stderr.write( (u"%s\n" % err).encode('utf-8') )