X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/db91f942ce46e3af1420f3469a83257ef5aca4c2..7805246da7d634d97dfa9e864c2502bd56a3a919:/src/librarian/elements/base.py?ds=inline diff --git a/src/librarian/elements/base.py b/src/librarian/elements/base.py index 5317268..46ae29f 100644 --- a/src/librarian/elements/base.py +++ b/src/librarian/elements/base.py @@ -3,7 +3,6 @@ import re from lxml import etree from librarian import dcparser, RDFNS -from librarian.html import raw_printable_text from librarian.util import get_translation @@ -62,23 +61,49 @@ class WLElement(etree.ElementBase): def gettext(self): return get_translation(self.meta.language).gettext - def raw_printable_text(self): + def in_context_of(self, setting): + parent = self.getparent() + if parent is None: + return False + try: + return getattr(parent, setting) + except AttributeError: + return parent.in_context_of(setting) + + def signal(self, signal): + parent = self.getparent() + if parent is not None: + parent.signal(signal) + + def raw_printable_text(self, builder): + from librarian.html import raw_printable_text + # TODO: podtagi, wyroznienia, etc t = '' - t += self.normalize_text(self.text) + t += self.normalize_text(self.text, builder) for c in self: + if not isinstance(c, WLElement): + continue if c.tag not in ('pe', 'pa', 'pt', 'pr', 'motyw'): - t += c.raw_printable_text() - t += self.normalize_text(c.tail) + t += c.raw_printable_text(builder) + t += self.normalize_text(c.tail, builder) return t - def normalize_text(self, text): + def normalize_text(self, text, builder): text = text or '' for e, s in self.text_substitutions: text = text.replace(e, s) # FIXME: TEmporary turnoff # text = re.sub(r'\s+', ' ', text) ### TODO: Added now for epub + + if getattr(builder, 'hyphenator', None) is not None: + newt = '' + wlist = re.compile(r'\w+|[^\w]', re.UNICODE).findall(text) + for w in wlist: + newt += builder.hyphenator.inserted(w, u'\u00AD') + text = newt + text = re.sub(r'(?<=\s\w)\s+', u'\u00A0', text) return text @@ -86,7 +111,7 @@ class WLElement(etree.ElementBase): def _build_inner(self, builder, build_method): child_count = len(self) if self.CAN_HAVE_TEXT and self.text: - text = self.normalize_text(self.text) + text = self.normalize_text(self.text, builder) if self.STRIP: text = text.lstrip() if not child_count: @@ -96,7 +121,7 @@ class WLElement(etree.ElementBase): if isinstance(child, WLElement): getattr(child, build_method)(builder) if self.CAN_HAVE_TEXT and child.tail: - text = self.normalize_text(child.tail) + text = self.normalize_text(child.tail, builder) if self.STRIP and i == child_count - 1: text = text.rstrip() builder.push_text(text) @@ -152,22 +177,26 @@ class WLElement(etree.ElementBase): return attr def epub_build(self, builder): + from librarian.elements.masters import Master + # TEMPORARY self.CAN_HAVE_TEXT = True self.STRIP = False - - if self.EPUB_START_CHUNK: + + start_chunk = self.EPUB_START_CHUNK and isinstance(self.getparent(), Master) + + if start_chunk: builder.start_chunk() fragment = None - if self.SECTION_PRECEDENCE: - if not self.EPUB_START_CHUNK: + if self.SECTION_PRECEDENCE and not self.in_context_of('NO_TOC'): + if not start_chunk: fragment = 'sub%d' % builder.assign_section_number() self.attrib['id'] = fragment builder.add_toc_entry( fragment, - self.raw_printable_text(), + self.raw_printable_text(builder), self.SECTION_PRECEDENCE ) @@ -183,7 +212,22 @@ class WLElement(etree.ElementBase): self._epub_build_inner(builder) if self.EPUB_TAG: builder.end_element() - + + def validate(self): + from librarian.elements.masters import Master + from librarian.elements.blocks import DlugiCytat, PoezjaCyt + from librarian.elements.footnotes import Footnote + + if self.SECTION_PRECEDENCE: + assert isinstance(self.getparent(), (Master, DlugiCytat, PoezjaCyt, Footnote)), \ + 'Header {} inside a <{}> instead of a master.'.format( + etree.tostring(self), self.getparent().tag) + + for c in self: + if isinstance(c, WLElement): + c.validate() + + def sanitize(self): # TODO: Remove insanity here. for e in self: