5 from librarian import dcparser, RDFNS
6 from librarian.html import raw_printable_text
7 from librarian.util import get_translation
10 class WLElement(etree.ElementBase):
11 SECTION_PRECEDENCE = None
25 EPUB_START_CHUNK = False
30 text_substitutions = [
33 #(u'...', u'…'), # Temporary turnoff for epub
38 ("'", "\u2019"), # This was enabled for epub.
42 def meta_object(self):
43 if not hasattr(self, '_meta_object'):
44 elem = self.find(RDFNS('RDF'))
46 self._meta_object = dcparser.BookInfo.from_element(elem)
48 self._meta_object = None
49 return self._meta_object
53 if self.meta_object is not None:
54 return self.meta_object
56 if self.getparent() is not None:
57 return self.getparent().meta
59 return self.document.base_meta
63 return get_translation(self.meta.language).gettext
65 def in_context_of(self, setting):
66 parent = self.getparent()
70 return getattr(parent, setting)
71 except AttributeError:
72 return parent.in_context_of(setting)
74 def signal(self, signal):
75 parent = self.getparent()
76 if parent is not None:
79 def raw_printable_text(self):
80 # TODO: podtagi, wyroznienia, etc
82 t += self.normalize_text(self.text)
84 if not isinstance(c, WLElement):
86 if c.tag not in ('pe', 'pa', 'pt', 'pr', 'motyw'):
87 t += c.raw_printable_text()
88 t += self.normalize_text(c.tail)
91 def normalize_text(self, text):
93 for e, s in self.text_substitutions:
94 text = text.replace(e, s)
95 # FIXME: TEmporary turnoff
96 # text = re.sub(r'\s+', ' ', text)
97 ### TODO: Added now for epub
98 text = re.sub(r'(?<=\s\w)\s+', u'\u00A0', text)
102 def _build_inner(self, builder, build_method):
103 child_count = len(self)
104 if self.CAN_HAVE_TEXT and self.text:
105 text = self.normalize_text(self.text)
110 builder.push_text(text)
111 for i, child in enumerate(self):
112 if isinstance(child, WLElement):
113 getattr(child, build_method)(builder)
114 if self.CAN_HAVE_TEXT and child.tail:
115 text = self.normalize_text(child.tail)
116 if self.STRIP and i == child_count - 1:
118 builder.push_text(text)
120 def _txt_build_inner(self, builder):
121 self._build_inner(builder, 'txt_build')
123 def txt_build(self, builder):
124 if hasattr(self, 'TXT_LEGACY_TOP_MARGIN'):
125 builder.push_legacy_margin(self.TXT_LEGACY_TOP_MARGIN)
127 builder.push_margin(self.TXT_TOP_MARGIN)
128 builder.push_text(self.TXT_PREFIX, True)
129 self._txt_build_inner(builder)
130 builder.push_text(self.TXT_SUFFIX, True)
131 if hasattr(self, 'TXT_LEGACY_BOTTOM_MARGIN'):
132 builder.push_legacy_margin(self.TXT_LEGACY_BOTTOM_MARGIN)
134 builder.push_margin(self.TXT_BOTTOM_MARGIN)
136 def _html_build_inner(self, builder):
137 self._build_inner(builder, 'html_build')
139 def get_html_attr(self, builder):
140 attr = self.HTML_ATTR.copy()
142 attr['class'] = self.HTML_CLASS
143 # always copy the id attribute (?)
144 if self.attrib.get('id'):
145 attr['id'] = self.attrib['id']
146 elif '_compat_section_id' in self.attrib:
147 attr['id'] = self.attrib['_compat_section_id']
150 def html_build(self, builder):
152 builder.start_element(
154 self.get_html_attr(builder),
157 self._html_build_inner(builder)
159 builder.end_element()
161 def _epub_build_inner(self, builder):
162 self._build_inner(builder, 'epub_build')
164 def get_epub_attr(self, builder):
165 attr = self.EPUB_ATTR.copy()
167 attr['class'] = self.EPUB_CLASS
170 def epub_build(self, builder):
171 from librarian.elements.masters import Master
174 self.CAN_HAVE_TEXT = True
177 start_chunk = self.EPUB_START_CHUNK and isinstance(self.getparent(), Master)
180 builder.start_chunk()
183 if self.SECTION_PRECEDENCE and not self.in_context_of('NO_TOC'):
185 fragment = 'sub%d' % builder.assign_section_number()
186 self.attrib['id'] = fragment
188 builder.add_toc_entry(
190 self.raw_printable_text(),
191 self.SECTION_PRECEDENCE
195 attr = self.get_epub_attr(builder)
197 attr['id'] = fragment
198 builder.start_element(
203 self._epub_build_inner(builder)
205 builder.end_element()
208 from librarian.elements.masters import Master
209 from librarian.elements.blocks import DlugiCytat, PoezjaCyt
210 from librarian.elements.footnotes import Footnote
212 if self.SECTION_PRECEDENCE:
213 assert isinstance(self.getparent(), (Master, DlugiCytat, PoezjaCyt, Footnote)), \
214 'Header {} inside a <{}> instead of a master.'.format(
215 etree.tostring(self), self.getparent().tag)
218 if isinstance(c, WLElement):
223 # TODO: Remove insanity here.
225 if isinstance(e, WLElement):