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')
108 text = re.sub(r'(?<=\s\w)\s+', u'\u00A0', text)
112 def _build_inner(self, builder, build_method):
113 child_count = len(self)
114 if self.CAN_HAVE_TEXT and self.text:
115 text = self.normalize_text(self.text, builder)
120 builder.push_text(text)
121 for i, child in enumerate(self):
122 if isinstance(child, WLElement):
123 getattr(child, build_method)(builder)
124 if self.CAN_HAVE_TEXT and child.tail:
125 text = self.normalize_text(child.tail, builder)
126 if self.STRIP and i == child_count - 1:
128 builder.push_text(text)
130 def _txt_build_inner(self, builder):
131 self._build_inner(builder, 'txt_build')
133 def txt_build(self, builder):
134 if hasattr(self, 'TXT_LEGACY_TOP_MARGIN'):
135 builder.push_legacy_margin(self.TXT_LEGACY_TOP_MARGIN)
137 builder.push_margin(self.TXT_TOP_MARGIN)
138 builder.push_text(self.TXT_PREFIX, True)
139 self._txt_build_inner(builder)
140 builder.push_text(self.TXT_SUFFIX, True)
141 if hasattr(self, 'TXT_LEGACY_BOTTOM_MARGIN'):
142 builder.push_legacy_margin(self.TXT_LEGACY_BOTTOM_MARGIN)
144 builder.push_margin(self.TXT_BOTTOM_MARGIN)
146 def _html_build_inner(self, builder):
147 self._build_inner(builder, 'html_build')
149 def get_html_attr(self, builder):
150 attr = self.HTML_ATTR.copy()
152 attr['class'] = self.HTML_CLASS
153 # always copy the id attribute (?)
154 if self.attrib.get('id'):
155 attr['id'] = self.attrib['id']
156 elif '_compat_section_id' in self.attrib:
157 attr['id'] = self.attrib['_compat_section_id']
160 def html_build(self, builder):
162 builder.start_element(
164 self.get_html_attr(builder),
167 self._html_build_inner(builder)
169 builder.end_element()
171 def _epub_build_inner(self, builder):
172 self._build_inner(builder, 'epub_build')
174 def get_epub_attr(self, builder):
175 attr = self.EPUB_ATTR.copy()
177 attr['class'] = self.EPUB_CLASS
180 def epub_build(self, builder):
181 from librarian.elements.masters import Master
184 self.CAN_HAVE_TEXT = True
187 start_chunk = self.EPUB_START_CHUNK and isinstance(self.getparent(), Master)
190 builder.start_chunk()
193 if self.SECTION_PRECEDENCE and not self.in_context_of('NO_TOC'):
195 fragment = 'sub%d' % builder.assign_section_number()
196 self.attrib['id'] = fragment
198 builder.add_toc_entry(
200 self.raw_printable_text(builder),
201 self.SECTION_PRECEDENCE
205 attr = self.get_epub_attr(builder)
207 attr['id'] = fragment
208 builder.start_element(
213 self._epub_build_inner(builder)
215 builder.end_element()
218 from librarian.elements.masters import Master
219 from librarian.elements.blocks import DlugiCytat, PoezjaCyt
220 from librarian.elements.footnotes import Footnote
222 if self.SECTION_PRECEDENCE:
223 assert isinstance(self.getparent(), (Master, DlugiCytat, PoezjaCyt, Footnote)), \
224 'Header {} inside a <{}> instead of a master.'.format(
225 etree.tostring(self, encoding='unicode'), self.getparent().tag)
228 if isinstance(c, WLElement):
233 # TODO: Remove insanity here.
235 if isinstance(e, WLElement):