text: use files, not filenames
[librarian.git] / librarian / text.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 from librarian import dcparser, parser, functions
7 from lxml import etree
8 import os
9
10
11 functions.reg_substitute_entities()
12 functions.reg_wrap_words()
13 functions.reg_strip()
14
15 TEMPLATE = u"""\
16 Kodowanie znaków w dokumencie: UTF-8.
17 -----
18 %(description)s 
19
20 %(license_description)s.%(source)s
21
22 Wersja lektury w opracowaniu merytorycznym i krytycznym (przypisy i motywy) dostępna jest na stronie %(url)s.
23 -----
24
25
26
27 %(text)s
28 """
29
30 def transform(input_file, output_file, parse_dublincore=True, **options):
31     """Transforms input_file in XML to output_file in TXT."""
32     # Parse XSLT
33     style_filename = os.path.join(os.path.dirname(__file__), 'xslt/book2txt.xslt')
34     style = etree.parse(style_filename)
35
36     document = parser.WLDocument.from_file(input_file, True, parse_dublincore=parse_dublincore)
37     result = document.transform(style, **options)
38
39     if parse_dublincore:
40         parsed_dc = dcparser.BookInfo.from_element(document.edoc)
41         description = parsed_dc.description
42         url = parsed_dc.url
43         license_description = parsed_dc.license_description
44         license = parsed_dc.license
45         if license:
46             license_description = u"Ten utwór jest udostepniony na licencji %s: \n%s" % (license_description, license)        
47         else:
48             license_description = u"Ten utwór nie jest chroniony prawem autorskim i znajduje się w domenie publicznej, co oznacza, że możesz go swobodnie wykorzystywać, publikować i rozpowszechniać"
49         source = parsed_dc.source_name
50         if source:
51             source = "\n\nNa podstawie: " + source
52         else:
53             source = ''
54     else:
55         description = 'Publikacja zrealizowana w ramach projektu Wolne Lektury (http://wolnelektury.pl).'
56         url = '*' * 10
57         license = ""
58         license_description = ""
59         source = ""
60     output_file.write((TEMPLATE % {
61         'description': description,
62         'url': url,
63         'license_description': license_description,
64         'text': unicode(result),
65         'source': source,
66     }).encode('utf-8'))
67