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