1 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
7 from librarian import dcparser, RDFNS
8 from librarian.util import get_translation
10 def last_words(text, n):
12 for w in reversed(text.split()):
20 return n, ' '.join(reversed(words))
23 class WLElement(etree.ElementBase):
24 SECTION_PRECEDENCE = None
39 EPUB_START_CHUNK = False
44 text_substitutions = [
47 #('...', '…'), # Temporary turnoff for epub
52 ("'", "\u2019"), # This was enabled for epub.
56 def meta_object(self):
57 if not hasattr(self, '_meta_object'):
58 elem = self.find(RDFNS('RDF'))
60 self._meta_object = dcparser.BookInfo.from_element(elem)
62 self._meta_object = None
63 return self._meta_object
67 if self.meta_object is not None:
68 return self.meta_object
70 if self.getparent() is not None:
71 return self.getparent().meta
73 return self.document.base_meta
77 return get_translation(self.meta.language).gettext
79 def in_context_of(self, setting):
80 parent = self.getparent()
84 return getattr(parent, setting)
85 except AttributeError:
86 return parent.in_context_of(setting)
88 def signal(self, signal):
89 parent = self.getparent()
90 if parent is not None:
93 def raw_printable_text(self, builder):
94 from librarian.html import raw_printable_text
96 # TODO: podtagi, wyroznienia, etc
98 t += self.normalize_text(self.text, builder)
100 if not isinstance(c, WLElement):
102 if c.tag not in ('pe', 'pa', 'pt', 'pr', 'motyw'):
103 t += c.raw_printable_text(builder)
104 t += self.normalize_text(c.tail, builder)
107 def normalize_text(self, text, builder):
109 for e, s in self.text_substitutions:
110 text = text.replace(e, s)
111 # FIXME: TEmporary turnoff
112 # text = re.sub(r'\s+', ' ', text)
113 ### TODO: Added now for epub
115 if getattr(builder, 'hyphenator', None) is not None:
117 wlist = re.compile(r'\w+|[^\w]', re.UNICODE).findall(text)
119 newt += builder.hyphenator.inserted(w, '\u00AD')
123 text = re.sub(r'(?<=\s\w)\s+', '\u00A0', text)
127 def _build_inner(self, builder, build_method):
128 child_count = len(self)
129 if self.CAN_HAVE_TEXT and self.text:
130 text = self.normalize_text(self.text, builder)
135 builder.push_text(text)
136 for i, child in enumerate(self):
137 if isinstance(child, WLElement):
138 getattr(child, build_method)(builder)
139 if self.CAN_HAVE_TEXT and child.tail:
140 text = self.normalize_text(child.tail, builder)
141 if self.STRIP and i == child_count - 1:
143 builder.push_text(text)
145 def _txt_build_inner(self, builder):
146 self._build_inner(builder, 'txt_build')
148 def txt_build(self, builder):
149 if hasattr(self, 'TXT_LEGACY_TOP_MARGIN'):
150 builder.push_legacy_margin(self.TXT_LEGACY_TOP_MARGIN)
152 builder.push_margin(self.TXT_TOP_MARGIN)
153 builder.push_text(self.TXT_PREFIX, True)
154 self._txt_build_inner(builder)
155 builder.push_text(self.TXT_SUFFIX, True)
156 if hasattr(self, 'TXT_LEGACY_BOTTOM_MARGIN'):
157 builder.push_legacy_margin(self.TXT_LEGACY_BOTTOM_MARGIN)
159 builder.push_margin(self.TXT_BOTTOM_MARGIN)
161 def _html_build_inner(self, builder):
162 self._build_inner(builder, 'html_build')
164 def get_html_attr(self, builder):
165 attr = self.HTML_ATTR.copy()
167 attr['class'] = self.HTML_CLASS
168 # always copy the id attribute (?)
169 if self.attrib.get('id'):
170 attr['id'] = self.attrib['id']
171 elif getattr(self, 'SHOULD_HAVE_ID', False) and '_compat_section_id' in self.attrib:
172 attr['id'] = self.attrib['_compat_section_id']
175 def html_build(self, builder):
177 builder.start_element(
179 self.get_html_attr(builder),
182 self._html_build_inner(builder)
184 builder.end_element()
186 def _epub_build_inner(self, builder):
187 self._build_inner(builder, 'epub_build')
189 def get_epub_attr(self, builder):
190 attr = self.EPUB_ATTR.copy()
192 attr['class'] = self.EPUB_CLASS
195 def epub_build(self, builder):
196 from librarian.elements.masters import Master
199 self.CAN_HAVE_TEXT = True
202 start_chunk = self.EPUB_START_CHUNK and isinstance(self.getparent(), Master)
205 builder.start_chunk()
208 if self.SECTION_PRECEDENCE and not self.in_context_of('NO_TOC'):
210 fragment = 'sub%d' % builder.assign_section_number()
211 self.attrib['id'] = fragment
213 builder.add_toc_entry(
215 self.raw_printable_text(builder),
216 self.SECTION_PRECEDENCE
220 attr = self.get_epub_attr(builder)
222 attr['id'] = fragment
223 builder.start_element(
228 self._epub_build_inner(builder)
230 builder.end_element()
233 from librarian.elements.masters import Master
234 from librarian.elements.blocks import DlugiCytat, PoezjaCyt
235 from librarian.elements.footnotes import Footnote
237 if self.SECTION_PRECEDENCE:
238 assert isinstance(self.getparent(), (Master, DlugiCytat, PoezjaCyt, Footnote)), \
239 'Header {} inside a <{}> instead of a master.'.format(
240 etree.tostring(self, encoding='unicode'), self.getparent().tag)
243 if isinstance(c, WLElement):
248 # TODO: Remove insanity here.
250 if isinstance(e, WLElement):
253 def snip(self, words, before=None, sub=False):
254 if sub and self.ASIDE:
258 if before is not None:
259 i = self.index(before)
267 words, text = last_words(self[i].tail, words)
268 snippet = [('text', text)] + snippet
271 words, subsnip = self[i].snip(words, sub=True)
272 snippet = subsnip + snippet
274 if words and self.text:
275 words, text = last_words(self.text, words)
276 snippet = [('text', text)] + snippet
278 snippet = [('start', self.tag, self.attrib)] + snippet + [('end',)]
280 if not sub and words and not self.ASIDE:
282 parent = self.getparent()
283 if parent is not None and parent.CAN_HAVE_TEXT:
284 words, parsnip = parent.snip(words, before=self)
285 return words, parsnip[:-1] + snippet + parsnip[-1:]
287 return words, snippet
289 def get_snippet(self, words=15):
290 from librarian.parser import parser
292 words, snippet = self.getparent().snip(words=words, before=self)
294 cursor = snipelem = parser.makeelement('snippet')
295 snipelem._meta_object = self.meta
298 elem = parser.makeelement(s[1], **s[2])
302 cursor = cursor.getparent()
305 cursor[-1].tail = (cursor[-1].tail or '') + s[1]
307 cursor.text = (cursor.text or '') + s[1]
312 sec = getattr(self, 'SHOULD_HAVE_ID', False) and self.attrib.get('_compat_section_id')
315 parent_index = self.getparent().index(self)
317 return self.getparent()[parent_index - 1].get_link()
319 return self.getparent().get_link()
322 class Snippet(WLElement):