Text converter updated.
[librarian.git] / src / librarian / builders / txt.py
1 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 import io
5 from librarian import OutputFile, get_resource
6
7
8 with io.open(get_resource("res/text/template.txt")) as f:
9     TEMPLATE = f.read()
10
11
12 class TxtFragment:
13     def __init__(self):
14         self.pieces = []
15         self.current_margin = 0
16         self.starting_block = True
17
18     def push_margin(self, margin):
19         if margin:
20             if self.pieces:
21                 self.pieces[-1] = self.pieces[-1].rstrip(' ')
22             if margin > self.current_margin:
23                 self.pieces.append('\r\n' * (margin - self.current_margin))
24                 self.current_margin = margin
25                 self.starting_block = True
26
27     def push_text(self, text, prepared=False):
28         if text:
29             if self.starting_block and not prepared:
30                 text = text.lstrip()
31             self.pieces.append(text)
32             self.current_margin = 0
33             if not prepared:
34                 self.starting_block = False
35
36
37 class TxtBuilder:
38     """
39     """
40     file_extension = "txt"
41     identifier = "txt"
42     after_child_fn = 'txt_after_child'
43
44     debug = False
45     orphans = False
46     normalize_whitespace = True
47     
48     default_license_description = {
49         "pol": (
50             "Wszystkie zasoby Wolnych Lektur możesz swobodnie wykorzystywać, "
51             "publikować i rozpowszechniać pod warunkiem zachowania warunków "
52             "licencji i zgodnie z Zasadami wykorzystania Wolnych Lektur.\n"
53             "Ten utwór jest w domenie publicznej.\n"
54             "Wszystkie materiały dodatkowe (przypisy, motywy literackie) są "
55             "udostępnione na Licencji Wolnej Sztuki 1.3: "
56             "https://artlibre.org/licence/lal/pl/\n"
57             "Fundacja Wolne Lektury zastrzega sobie prawa do wydania "
58             "krytycznego zgodnie z art. Art.99(2) Ustawy o prawach autorskich "
59             "i prawach pokrewnych.\nWykorzystując zasoby z Wolnych Lektur, "
60             "należy pamiętać o zapisach licencji oraz zasadach, które "
61             "spisaliśmy w Zasadach wykorzystania Wolnych Lektur: "
62             "https://wolnelektury.pl/info/zasady-wykorzystania/\nZapoznaj "
63             "się z nimi, zanim udostępnisz dalej nasze książki."
64         )
65     }
66     license_description = {
67         "pol": (
68             #"Ten utwór jest udostępniony na licencji {meta.license_description}: \n{meta.license}",
69             "Wszystkie zasoby Wolnych Lektur możesz swobodnie wykorzystywać, "
70             "publikować i rozpowszechniać pod warunkiem zachowania warunków "
71             "licencji i zgodnie z Zasadami wykorzystania Wolnych Lektur.\n"
72             "Ten utwór jest jest udostępniony na licencji {meta.license_description} ({meta.license}). "
73             "Wszystkie materiały dodatkowe (przypisy, motywy literackie) są "
74             "udostępnione na Licencji Wolnej Sztuki 1.3: "
75             "https://artlibre.org/licence/lal/pl/\n"
76             "Fundacja Wolne Lektury zastrzega sobie prawa do wydania "
77             "krytycznego zgodnie z art. Art.99(2) Ustawy o prawach autorskich "
78             "i prawach pokrewnych.\nWykorzystując zasoby z Wolnych Lektur, "
79             "należy pamiętać o zapisach licencji oraz zasadach, które "
80             "spisaliśmy w Zasadach wykorzystania Wolnych Lektur: "
81             "https://wolnelektury.pl/info/zasady-wykorzystania/\nZapoznaj "
82             "się z nimi, zanim udostępnisz dalej nasze książki."
83         )
84     }
85
86     def __init__(self, base_url=None):
87         self.fragments = {
88             None: TxtFragment(),
89             'header': TxtFragment()
90         }
91         self.current_fragments = [self.fragments[None]]
92
93     def enter_fragment(self, fragment):
94         self.current_fragments.append(self.fragments[fragment])
95
96     def exit_fragment(self):
97         self.current_fragments.pop()
98         
99     def push_text(self, text, prepared=False):
100         self.current_fragments[-1].push_text(text, prepared=prepared)
101
102     def push_margin(self, margin):
103         self.current_fragments[-1].push_margin(margin)
104         
105     def build(self, document, raw_text=False, **kwargs):
106         document.tree.getroot().txt_build(self)
107         meta = document.meta
108
109         self.enter_fragment('header')
110         if meta.translators:
111             self.push_text("tłum. ")
112             self.push_text(
113                 ", ".join(
114                     translator.readable()
115                     for translator in meta.translators
116                 )
117             )
118             builder.push_margin(2)
119
120         if meta.isbn_txt:
121             self.push_margin(2)
122             isbn = meta.isbn_txt
123             if isbn.startswith(('ISBN-' , 'ISBN ')):
124                 isbn = isbn[5:]
125             self.push_text('ISBN {isbn}'.format(isbn=isbn))
126             #builder.push_margin(5)
127
128         self.push_margin(4)
129         self.exit_fragment()
130         
131         text = ''.join(self.fragments[None].pieces).lstrip()
132
133         if raw_text:
134             result = text
135         else:
136             text = ''.join(self.fragments['header'].pieces) +  text
137
138             if meta.license:
139                 license_description = self.license_description['pol'].format(meta=meta)
140             else:
141                 license_description = self.default_license_description['pol']
142
143             if meta.source_name:
144                 source = "\n\nTekst opracowany na podstawie: " + meta.source_name
145             else:
146                 source = ''
147
148             contributors = ', '.join(
149                 person.readable()
150                 for person in sorted(set(
151                     p for p in (
152                         meta.technical_editors + meta.editors
153                     ) if p))
154             )
155             if contributors:
156                 contributors = (
157                     "\n\nOpracowanie redakcyjne i przypisy: %s."
158                     % contributors
159                 )
160
161             funders = ', '.join(meta.funders)
162             if funders:
163                 funders = "\n\nPublikację wsparli i wsparły: %s." % funders
164
165             isbn = getattr(meta, 'isbn_txt', None)
166             if isbn:
167                 isbn = '\n\n' + isbn
168             else:
169                 isbn = ''
170                 
171             result = TEMPLATE % {
172                 "text": text,
173                 "description": meta.description,
174                 "url": meta.url,
175                 "license_description": license_description,
176                 "source": source,
177                 "contributors": contributors,
178                 "funders": funders,
179                 "publisher":  '\n\nWydawca: ' + ', '.join(meta.publisher),
180                 "isbn": isbn,
181             }
182
183         result = '\r\n'.join(result.splitlines()) + '\r\n'
184         return OutputFile.from_bytes(result.encode('utf-8'))