5 from librarian import dcparser, RDFNS
6 from librarian.util import get_translation
9 class WLElement(etree.ElementBase):
10 SECTION_PRECEDENCE = None
24 EPUB_START_CHUNK = False
29 text_substitutions = [
32 #(u'...', u'…'), # Temporary turnoff for epub
37 ("'", "\u2019"), # This was enabled for epub.
41 def meta_object(self):
42 if not hasattr(self, '_meta_object'):
43 elem = self.find(RDFNS('RDF'))
45 self._meta_object = dcparser.BookInfo.from_element(elem)
47 self._meta_object = None
48 return self._meta_object
52 if self.meta_object is not None:
53 return self.meta_object
55 if self.getparent() is not None:
56 return self.getparent().meta
58 return self.document.base_meta
62 return get_translation(self.meta.language).gettext
64 def in_context_of(self, setting):
65 parent = self.getparent()
69 return getattr(parent, setting)
70 except AttributeError:
71 return parent.in_context_of(setting)
73 def signal(self, signal):
74 parent = self.getparent()
75 if parent is not None:
78 def raw_printable_text(self, builder):
79 from librarian.html import raw_printable_text
81 # TODO: podtagi, wyroznienia, etc
83 t += self.normalize_text(self.text, builder)
85 if not isinstance(c, WLElement):
87 if c.tag not in ('pe', 'pa', 'pt', 'pr', 'motyw'):
88 t += c.raw_printable_text(builder)
89 t += self.normalize_text(c.tail, builder)
92 def normalize_text(self, text, builder):
94 for e, s in self.text_substitutions:
95 text = text.replace(e, s)
96 # FIXME: TEmporary turnoff
97 # text = re.sub(r'\s+', ' ', text)
98 ### TODO: Added now for epub
100 if getattr(builder, 'hyphenator', None) is not None:
102 wlist = re.compile(r'\w+|[^\w]', re.UNICODE).findall(text)
104 newt += builder.hyphenator.inserted(w, u'\u00AD')
107 text = re.sub(r'(?<=\s\w)\s+', u'\u00A0', text)
111 def _build_inner(self, builder, build_method):
112 child_count = len(self)
113 if self.CAN_HAVE_TEXT and self.text:
114 text = self.normalize_text(self.text, builder)
119 builder.push_text(text)
120 for i, child in enumerate(self):
121 if isinstance(child, WLElement):
122 getattr(child, build_method)(builder)
123 if self.CAN_HAVE_TEXT and child.tail:
124 text = self.normalize_text(child.tail, builder)
125 if self.STRIP and i == child_count - 1:
127 builder.push_text(text)
129 def _txt_build_inner(self, builder):
130 self._build_inner(builder, 'txt_build')
132 def txt_build(self, builder):
133 if hasattr(self, 'TXT_LEGACY_TOP_MARGIN'):
134 builder.push_legacy_margin(self.TXT_LEGACY_TOP_MARGIN)
136 builder.push_margin(self.TXT_TOP_MARGIN)
137 builder.push_text(self.TXT_PREFIX, True)
138 self._txt_build_inner(builder)
139 builder.push_text(self.TXT_SUFFIX, True)
140 if hasattr(self, 'TXT_LEGACY_BOTTOM_MARGIN'):
141 builder.push_legacy_margin(self.TXT_LEGACY_BOTTOM_MARGIN)
143 builder.push_margin(self.TXT_BOTTOM_MARGIN)
145 def _html_build_inner(self, builder):
146 self._build_inner(builder, 'html_build')
148 def get_html_attr(self, builder):
149 attr = self.HTML_ATTR.copy()
151 attr['class'] = self.HTML_CLASS
152 # always copy the id attribute (?)
153 if self.attrib.get('id'):
154 attr['id'] = self.attrib['id']
155 elif '_compat_section_id' in self.attrib:
156 attr['id'] = self.attrib['_compat_section_id']
159 def html_build(self, builder):
161 builder.start_element(
163 self.get_html_attr(builder),
166 self._html_build_inner(builder)
168 builder.end_element()
170 def _epub_build_inner(self, builder):
171 self._build_inner(builder, 'epub_build')
173 def get_epub_attr(self, builder):
174 attr = self.EPUB_ATTR.copy()
176 attr['class'] = self.EPUB_CLASS
179 def epub_build(self, builder):
180 from librarian.elements.masters import Master
183 self.CAN_HAVE_TEXT = True
186 start_chunk = self.EPUB_START_CHUNK and isinstance(self.getparent(), Master)
189 builder.start_chunk()
192 if self.SECTION_PRECEDENCE and not self.in_context_of('NO_TOC'):
194 fragment = 'sub%d' % builder.assign_section_number()
195 self.attrib['id'] = fragment
197 builder.add_toc_entry(
199 self.raw_printable_text(builder),
200 self.SECTION_PRECEDENCE
204 attr = self.get_epub_attr(builder)
206 attr['id'] = fragment
207 builder.start_element(
212 self._epub_build_inner(builder)
214 builder.end_element()
217 from librarian.elements.masters import Master
218 from librarian.elements.blocks import DlugiCytat, PoezjaCyt
219 from librarian.elements.footnotes import Footnote
221 if self.SECTION_PRECEDENCE:
222 assert isinstance(self.getparent(), (Master, DlugiCytat, PoezjaCyt, Footnote)), \
223 'Header {} inside a <{}> instead of a master.'.format(
224 etree.tostring(self), self.getparent().tag)
227 if isinstance(c, WLElement):
232 # TODO: Remove insanity here.
234 if isinstance(e, WLElement):