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 raw_printable_text(self):
66 # TODO: podtagi, wyroznienia, etc
68 t += self.normalize_text(self.text)
70 if c.tag not in ('pe', 'pa', 'pt', 'pr', 'motyw'):
71 t += c.raw_printable_text()
72 t += self.normalize_text(c.tail)
75 def normalize_text(self, text):
77 for e, s in self.text_substitutions:
78 text = text.replace(e, s)
79 # FIXME: TEmporary turnoff
80 # text = re.sub(r'\s+', ' ', text)
81 ### TODO: Added now for epub
82 text = re.sub(r'(?<=\s\w)\s+', u'\u00A0', text)
86 def _build_inner(self, builder, build_method):
87 child_count = len(self)
88 if self.CAN_HAVE_TEXT and self.text:
89 text = self.normalize_text(self.text)
94 builder.push_text(text)
95 for i, child in enumerate(self):
96 if isinstance(child, WLElement):
97 getattr(child, build_method)(builder)
98 if self.CAN_HAVE_TEXT and child.tail:
99 text = self.normalize_text(child.tail)
100 if self.STRIP and i == child_count - 1:
102 builder.push_text(text)
104 def _txt_build_inner(self, builder):
105 self._build_inner(builder, 'txt_build')
107 def txt_build(self, builder):
108 if hasattr(self, 'TXT_LEGACY_TOP_MARGIN'):
109 builder.push_legacy_margin(self.TXT_LEGACY_TOP_MARGIN)
111 builder.push_margin(self.TXT_TOP_MARGIN)
112 builder.push_text(self.TXT_PREFIX, True)
113 self._txt_build_inner(builder)
114 builder.push_text(self.TXT_SUFFIX, True)
115 if hasattr(self, 'TXT_LEGACY_BOTTOM_MARGIN'):
116 builder.push_legacy_margin(self.TXT_LEGACY_BOTTOM_MARGIN)
118 builder.push_margin(self.TXT_BOTTOM_MARGIN)
120 def _html_build_inner(self, builder):
121 self._build_inner(builder, 'html_build')
123 def get_html_attr(self, builder):
124 attr = self.HTML_ATTR.copy()
126 attr['class'] = self.HTML_CLASS
127 # always copy the id attribute (?)
128 if self.attrib.get('id'):
129 attr['id'] = self.attrib['id']
130 elif '_compat_section_id' in self.attrib:
131 attr['id'] = self.attrib['_compat_section_id']
134 def html_build(self, builder):
136 builder.start_element(
138 self.get_html_attr(builder),
141 self._html_build_inner(builder)
143 builder.end_element()
145 def _epub_build_inner(self, builder):
146 self._build_inner(builder, 'epub_build')
148 def get_epub_attr(self, builder):
149 attr = self.EPUB_ATTR.copy()
151 attr['class'] = self.EPUB_CLASS
154 def epub_build(self, builder):
156 self.CAN_HAVE_TEXT = True
159 if self.EPUB_START_CHUNK:
160 builder.start_chunk()
163 if self.SECTION_PRECEDENCE:
164 if not self.EPUB_START_CHUNK:
165 fragment = 'sub%d' % builder.assign_section_number()
166 self.attrib['id'] = fragment
168 builder.add_toc_entry(
170 self.raw_printable_text(),
171 self.SECTION_PRECEDENCE
175 attr = self.get_epub_attr(builder)
177 attr['id'] = fragment
178 builder.start_element(
183 self._epub_build_inner(builder)
185 builder.end_element()
188 # TODO: Remove insanity here.
190 if isinstance(e, WLElement):