class EpubBuilder(Builder):
+ build_method_fn = 'epub_build'
file_extension = 'epub'
isbn_field = 'isbn_epub'
orphans = True
from librarian import OutputFile
-class HtmlBuilder:
+class TreeBuilder:
+ @property
+ def cursor(self):
+ return self.current_cursors[-1]
+
+ def enter_fragment(self, fragment):
+ cursor = self.cursors.get(fragment, self.cursor)
+ self.current_cursors.append(cursor)
+
+ def exit_fragment(self):
+ self.current_cursors.pop()
+
+ def create_fragment(self, name, element):
+ assert name not in self.cursors
+ self.cursors[name] = element
+
+ def forget_fragment(self, name):
+ del self.cursors[name]
+
+ def start_element(self, tag, attrib=None):
+ self.current_cursors.append(etree.SubElement(
+ self.cursor,
+ tag,
+ **(attrib or {})
+ ))
+
+ def end_element(self):
+ self.current_cursors.pop()
+
+ def push_text(self, text):
+ cursor = self.cursor
+ if len(cursor):
+ cursor[-1].tail = (cursor[-1].tail or '') + text
+ else:
+ cursor.text = (cursor.text or '') + text
+
+
+class HtmlBuilder(TreeBuilder):
+ build_method_fn = 'html_build'
file_extension = "html"
with_themes = True
with_toc = True
else:
return 'https://wolnelektury.pl/media/book/pictures/{}/'.format(self.document.meta.url.slug)
- @property
- def cursor(self):
- return self.current_cursors[-1]
-
- def enter_fragment(self, fragment):
- cursor = self.cursors.get(fragment, self.cursor)
- self.current_cursors.append(cursor)
-
- def exit_fragment(self):
- self.current_cursors.pop()
-
- def create_fragment(self, name, element):
- assert name not in self.cursors
- self.cursors[name] = element
-
- def forget_fragment(self, name):
- del self.cursors[name]
-
def build(self, document, element=None, **kwargs):
self.document = document
self.document.assign_ids()
self.footnotes.insert(0, fnheader)
self.tree.append(self.footnotes)
- def start_element(self, tag, attrib=None):
- self.current_cursors.append(etree.SubElement(
- self.cursor,
- tag,
- **(attrib or {})
- ))
-
- def end_element(self):
- self.current_cursors.pop()
-
- def push_text(self, text):
- cursor = self.cursor
- if len(cursor):
- cursor[-1].tail = (cursor[-1].tail or '') + text
- else:
- cursor.text = (cursor.text or '') + text
-
def add_visible_number(self, element):
assert '_id' in element.attrib, etree.tostring(element)
self.start_element('a', {
class TxtBuilder:
"""
"""
+ build_method_fn = 'txt_build'
file_extension = "txt"
identifier = "txt"
after_child_fn = 'txt_after_child'
return text
- def _build_inner(self, builder, build_method):
+ def build_inner(self, builder):
+ build_method = builder.build_method_fn
child_count = len(self)
if self.CAN_HAVE_TEXT and self.text:
text = self.normalize_text(self.text, builder)
def txt_after_child(self, builder, child_count):
pass
- def _txt_build_inner(self, builder):
- self._build_inner(builder, 'txt_build')
+ def txt_build_inner(self, builder):
+ self.build_inner(builder)
def txt_build(self, builder):
builder.push_margin(self.TXT_TOP_MARGIN)
builder.push_text(self.TXT_PREFIX, True)
- self._txt_build_inner(builder)
+ self.txt_build_inner(builder)
builder.push_text(self.TXT_SUFFIX, True)
builder.push_margin(self.TXT_BOTTOM_MARGIN)
- def _html_build_inner(self, builder):
- self._build_inner(builder, 'html_build')
+ def html_build_inner(self, builder):
+ self.build_inner(builder)
def get_html_attr(self, builder):
attr = self.HTML_ATTR.copy()
self.get_html_attr(builder),
)
- self._html_build_inner(builder)
+ self.html_build_inner(builder)
if self.HTML_TAG:
builder.end_element()
- def _epub_build_inner(self, builder):
- self._build_inner(builder, 'epub_build')
+ def epub_build_inner(self, builder):
+ self.build_inner(builder)
def get_epub_attr(self, builder):
attr = self.EPUB_ATTR.copy()
attr
)
- self._epub_build_inner(builder)
+ self.epub_build_inner(builder)
if self.EPUB_TAG:
builder.end_element()
HTML_TAG = "div"
HTML_CLASS = "person-list"
- def _html_build_inner(self, builder):
+ def html_build_inner(self, builder):
ol = etree.Element('ol')
builder.create_fragment('list', ol)
- super(ListaOsob, self)._html_build_inner(builder)
+ super().html_build_inner(builder)
builder.cursor.append(ol)
builder.forget_fragment('list')
- def _epub_build_inner(self, builder):
+ def epub_build_inner(self, builder):
ol = etree.Element('ol')
builder.create_fragment('list', ol)
- super(ListaOsob, self)._epub_build_inner(builder)
+ super().epub_build_inner(builder)
builder.cursor.append(ol)
builder.forget_fragment('list')
EPUB_TAG = "h2"
EPUB_CLASS = "h2"
- def _epub_build_inner(self, builder):
+ def epub_build_inner(self, builder):
builder.start_element('small', {})
- super()._epub_build_inner(builder)
+ super().epub_build_inner(builder)
builder.end_element()
EPUB_TAG = "h2"
EPUB_CLASS = "h4"
- def _epub_build_inner(self, builder):
+ def epub_build_inner(self, builder):
builder.start_element('small', {})
- super()._epub_build_inner(builder)
+ super().epub_build_inner(builder)
builder.end_element()
EPUB_TAG = "h2"
EPUB_CLASS = "h3"
- def _epub_build_inner(self, builder):
+ def epub_build_inner(self, builder):
builder.start_element('small', {})
- super()._epub_build_inner(builder)
+ super().epub_build_inner(builder)
builder.end_element()
return self.stanza.meta
return super(Wers, self).meta
- def _epub_build_inner(self, builder):
- super()._epub_build_inner(builder)
+ def epub_build_inner(self, builder):
+ super().epub_build_inner(builder)
builder.push_text('''\u00a0''')
@property
attr['class'] += ' verse-stretched'
return attr
- def _html_build_inner(self, builder):
+ def html_build_inner(self, builder):
if self.is_stretched:
builder.start_element('span')
- super()._html_build_inner(builder)
+ super().html_build_inner(builder)
if self.is_stretched:
builder.end_element()
class WersCd(Wers):
HTML_CLASS = Wers.HTML_CLASS + ' verse-cont'
- def _txt_build_inner(self, builder):
+ def txt_build_inner(self, builder):
builder.push_text(' ' * 24, prepared=True)
- super(WersCd, self)._txt_build_inner(builder)
+ super().txt_build_inner(builder)
EPUB_ATTR = {
"style": "margin-left: 12em",
v = self.attrib.get('typ')
return int(v) if v else 1
- def _txt_build_inner(self, builder):
+ def txt_build_inner(self, builder):
## Temporary legacy compatibility fix.
typ = min(self.typ, 2)
builder.push_text(' ' * self.typ, prepared=True)
- super(WersWciety, self)._txt_build_inner(builder)
+ super().txt_build_inner(builder)
def get_html_attr(self, builder):
- attr = super(WersWciety, self).get_html_attr(builder)
+ attr = super().get_html_attr(builder)
attr['class'] += f" verse-indent-{self.typ}"
return attr
def get_epub_attr(self, builder):
- attr = super(WersWciety, self).get_html_attr(builder)
+ attr = super().get_html_attr(builder)
attr['style'] = "margin-left: {}em".format(self.typ)
return attr
EPUB_TAG = HTML_TAG = "p"
HTML_CLASS = HTML_CLASS = "spacer-asterisk"
- def _txt_build_inner(self, builder):
+ def txt_build_inner(self, builder):
builder.push_text('*')
- def _html_build_inner(self, builder):
+ def html_build_inner(self, builder):
builder.push_text("*")
- _epub_build_inner = _html_build_inner
+ epub_build_inner = html_build_inner
EPUB_TAG = 'p'
EPUB_CLASS = 'spacer'
- def _epub_build_inner(self, builder):
+ def epub_build_inner(self, builder):
builder.push_text("\u00a0")
EPUB_TAG = HTML_TAG = "hr"
EPUB_CLASS = HTML_CLASS = "spacer-line"
- def _txt_build_inner(self, builder):
+ def txt_build_inner(self, builder):
builder.push_text('-' * 48)