Python 3.4-3.7 support;
[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 __future__ import unicode_literals
7
8 import copy
9 from librarian import functions, OutputFile
10 from lxml import etree
11 import os
12 import six
13
14
15 functions.reg_substitute_entities()
16 functions.reg_wrap_words()
17 functions.reg_strip()
18 functions.reg_person_name()
19
20 TEMPLATE = u"""\
21 %(text)s
22
23
24 -----
25 Ta lektura, podobnie jak tysiące innych, dostępna jest na stronie wolnelektury.pl.
26 Wersja lektury w opracowaniu merytorycznym i krytycznym (przypisy i motywy) dostępna jest na stronie %(url)s.
27
28 Utwór opracowany został w ramach projektu Wolne Lektury przez fundację Nowoczesna Polska.
29
30 %(license_description)s.%(source)s%(publisher)s
31
32 %(description)s%(contributors)s%(funders)s%(isbn)s
33 """
34
35
36 def transform(wldoc, flags=None, **options):
37     """
38     Transforms input_file in XML to output_file in TXT.
39     possible flags: raw-text,
40     """
41     # Parse XSLT
42     style_filename = os.path.join(os.path.dirname(__file__), 'xslt/book2txt.xslt')
43     style = etree.parse(style_filename)
44
45     document = copy.deepcopy(wldoc)
46     del wldoc
47     document.swap_endlines()
48
49     if flags:
50         for flag in flags:
51             document.edoc.getroot().set(flag, 'yes')
52     if 'wrapping' in options:
53         options['wrapping'] = str(options['wrapping'])
54
55     result = document.transform(style, **options)
56
57     if not flags or 'raw-text' not in flags:
58         if document.book_info:
59             parsed_dc = document.book_info
60             description = parsed_dc.description
61             url = document.book_info.url
62     
63             license_description = parsed_dc.license_description
64             license = parsed_dc.license
65             if license:
66                 license_description = u"Ten utwór jest udostępniony na licencji %s: \n%s" % (
67                     license_description, license)
68             else:
69                 license_description = u"Ten utwór nie jest objęty majątkowym prawem autorskim i znajduje się " \
70                                       u"w domenie publicznej, co oznacza że możesz go swobodnie wykorzystywać, " \
71                                       u"publikować i rozpowszechniać. Jeśli utwór opatrzony jest dodatkowymi " \
72                                       u"materiałami (przypisy, motywy literackie etc.), które podlegają prawu " \
73                                       u"autorskiemu, to te dodatkowe materiały udostępnione są na licencji " \
74                                       u"Creative Commons Uznanie Autorstwa – Na Tych Samych Warunkach 3.0 PL " \
75                                       u"(http://creativecommons.org/licenses/by-sa/3.0/)"
76
77             source = parsed_dc.source_name
78             if source:
79                 source = "\n\nTekst opracowany na podstawie: " + source
80             else:
81                 source = ''
82     
83             contributors = ', '.join(person.readable() for person in 
84                                      sorted(set(p for p in (parsed_dc.technical_editors + parsed_dc.editors) if p)))
85             if contributors:
86                 contributors = "\n\nOpracowanie redakcyjne i przypisy: %s." % contributors
87             funders = ', '.join(parsed_dc.funders)
88             if funders:
89                 funders = u"\n\nPublikację wsparli i wsparły: %s." % funders
90             publisher = '\n\nWydawca: ' + ', '.join(parsed_dc.publisher)
91             isbn = getattr(parsed_dc, 'isbn_txt', None)
92             if isbn:
93                 isbn = '\n\n' + isbn
94             else:
95                 isbn = ''
96         else:
97             description = 'Publikacja zrealizowana w ramach projektu Wolne Lektury (http://wolnelektury.pl).'
98             url = '*' * 10
99             license_description = ""
100             source = ""
101             contributors = ""
102             funders = ""
103             publisher = ""
104             isbn = ""
105         result = (TEMPLATE % {
106             'description': description,
107             'url': url,
108             'license_description': license_description,
109             'text': six.text_type(result),
110             'source': source,
111             'contributors': contributors,
112             'funders': funders,
113             'publisher': publisher,
114             'isbn': isbn,
115         }).encode('utf-8')
116     else:
117         result = six.text_type(result).encode('utf-8')
118     return OutputFile.from_bytes(b"\r\n".join(result.splitlines()) + b"\r\n")