Fix some errors in scripts.
[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
28
29 %(description)s%(contributors)s%(funders)s
30 """
31
32 def transform(wldoc, flags=None, **options):
33     """
34     Transforms input_file in XML to output_file in TXT.
35     possible flags: raw-text,
36     """
37     # Parse XSLT
38     style_filename = os.path.join(os.path.dirname(__file__), 'xslt/book2txt.xslt')
39     style = etree.parse(style_filename)
40
41     document = copy.deepcopy(wldoc)
42     del wldoc
43     document.swap_endlines()
44
45     if flags:
46         for flag in flags:
47             document.edoc.getroot().set(flag, 'yes')
48     if 'wrapping' in options:
49         options['wrapping'] = str(options['wrapping'])
50
51     result = document.transform(style, **options)
52
53     if not flags or 'raw-text' not in flags:
54         if document.book_info:
55             parsed_dc = document.book_info
56             description = parsed_dc.description
57             url = document.book_info.url
58     
59             license_description = parsed_dc.license_description
60             license = parsed_dc.license
61             if license:
62                 license_description = u"Ten utwór jest udostepniony na licencji %s: \n%s" % (license_description, license)        
63             else:
64                 license_description = u"Ten utwór nie jest objęty majątkowym prawem autorskim i znajduje się w domenie publicznej, co oznacza że możesz go swobodnie wykorzystywać, publikować i rozpowszechniać. Jeśli utwór opatrzony jest dodatkowymi materiałami (przypisy, motywy literackie etc.), które podlegają prawu autorskiemu, to te dodatkowe materiały udostępnione są na licencji Creative Commons Uznanie Autorstwa – Na Tych Samych Warunkach 3.0 PL (http://creativecommons.org/licenses/by-sa/3.0/)"
65     
66             source = parsed_dc.source_name
67             if source:
68                 source = "\n\nTekst opracowany na podstawie: " + source
69             else:
70                 source = ''
71     
72             contributors = ', '.join(person.readable() for person in 
73                                      sorted(set(p for p in (parsed_dc.technical_editors + parsed_dc.editors) if p)))
74             if contributors:
75                 contributors = "\n\nOpracowanie redakcyjne i przypisy: %s." % contributors
76             funders = ', '.join(parsed_dc.funders)
77             if funders:
78                 funders = u"\n\nPublikację ufundowali i ufundowały: %s." % funders
79         else:
80             description = 'Publikacja zrealizowana w ramach projektu Wolne Lektury (http://wolnelektury.pl).'
81             url = '*' * 10
82             license = ""
83             license_description = ""
84             source = ""
85             contributors = ""
86             funders = ""
87         return OutputFile.from_string((TEMPLATE % {
88             'description': description,
89             'url': url,
90             'license_description': license_description,
91             'text': unicode(result),
92             'source': source,
93             'contributors': contributors,
94             'funders': funders,
95         }).encode('utf-8'))
96     else:
97         return OutputFile.from_string(unicode(result).encode('utf-8'))
98