def stylizer(self, item): ans = self.stylizer_cache.get(item, None) if ans is None: ans = Stylizer(item.data, item.href, self.oeb, self.opts, self.profile, base_css=self.base_css) self.stylizer_cache[item] = ans return ans
def mlize_spine(self, oeb_book): output = [''] for item in oeb_book.spine: self.log.debug('Converting %s to Markdown formatted TXT...' % item.href) self.rewrite_ids(item.data, item) rewrite_links(item.data, partial(self.rewrite_link, page=item)) stylizer = Stylizer(item.data, item.href, oeb_book, self.opts, self.opts.output_profile) output += self.dump_text(item.data.find(XHTML('body')), stylizer) output.append('\n\n') return ''.join(output)
def mobimlize_spine(self): 'Iterate over the spine and convert it to MOBIML' for item in self.oeb.spine: stylizer = Stylizer(item.data, item.href, self.oeb, self.opts, self.profile) body = item.data.find(base.tag('xhtml', 'body')) nroot = etree.Element(base.tag('xhtml', 'html'), nsmap=MOBI_NSMAP) nbody = etree.SubElement(nroot, base.tag('xhtml', 'body')) self.current_spine_item = item self.mobimlize_elem(body, stylizer, BlockState(nbody), [FormatState()]) item.data = nroot
def mangle_spine(self): id, href = self.oeb.manifest.generate('manglecase', 'manglecase.css') self.oeb.manifest.add(id, href, base.CSS_MIME, data=CASE_MANGLER_CSS) for item in self.oeb.spine: html = item.data relhref = item.relhref(href) etree.SubElement(html.find(base.tag('xhtml', 'head')), base.tag('xhtml', 'link'), rel='stylesheet', href=relhref, type=base.CSS_MIME) stylizer = Stylizer(html, item.href, self.oeb, self.opts, self.profile) self.mangle_elem(html.find(base.tag('xhtml', 'body')), stylizer)
def mlize_spine(self, oeb_book): output = [ u'<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><title>%s</title></head><body>' % (prepare_string_for_xml(self.book_title)) ] for item in oeb_book.spine: self.log.debug('Converting %s to HTML...' % item.href) self.rewrite_ids(item.data, item) base.rewrite_links(item.data, partial(self.rewrite_link, page=item)) stylizer = Stylizer(item.data, item.href, oeb_book, self.opts) output += self.dump_text(item.data.find(base.tag('xhtml', 'body')), stylizer, item) output.append('\n\n') output.append('</body></html>') return ''.join(output)
def mlize_spine(self, oeb_book): output = [] for item in oeb_book.spine: self.log.debug('Converting %s to HTML...' % item.href) self.rewrite_ids(item.data, item) base.rewrite_links(item.data, partial(self.rewrite_link, page=item)) stylizer = Stylizer(item.data, item.href, oeb_book, self.opts) output += self.dump_text(item.data.find(base.tag('xhtml', 'body')), stylizer, item) output.append('\n\n') if self.opts.htmlz_class_style == 'external': css = u'<link href="style.css" rel="stylesheet" type="text/css" />' else: css = u'<style type="text/css">' + self.get_css( oeb_book) + u'</style>' title = u'<title>%s</title>' % prepare_string_for_xml(self.book_title) output = [u'<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" />'] + \ [css] + [title, u'</head><body>'] + output + [u'</body></html>'] return ''.join(output)
def mlize_spine(self): output = [u''] output.append(self.get_toc()) for item in self.oeb_book.spine: self.log.debug('Converting %s to TXT...' % item.href) for x in item.data.iterdescendants(etree.Comment): if x.text and '--' in x.text: x.text = x.text.replace('--', '__') content = etree.tostring(item.data, encoding='unicode') content = self.remove_newlines(content) content = etree.fromstring(content) stylizer = Stylizer(content, item.href, self.oeb_book, self.opts, self.opts.output_profile) output += self.dump_text(content.find(base.tag('xhtml', 'body')), stylizer, item) output += '\n\n\n\n\n\n' output = ''.join(output) output = '\n'.join(l.rstrip() for l in output.splitlines()) output = self.cleanup_text(output) return output
def get_text(self): from ebook_converter.ebooks.oeb.stylizer import Stylizer text = ['<body>'] # Create main section if there are no others to create if self.opts.sectionize == 'nothing': text.append('<section>') self.section_level += 1 for item in self.oeb_book.spine: self.log.debug('Converting %s to FictionBook2 XML' % item.href) stylizer = Stylizer(item.data, item.href, self.oeb_book, self.opts, self.opts.output_profile) # Start a <section> if we must sectionize each file or if the TOC # references this page page_section_open = False if (self.opts.sectionize == 'files' or None in self.toc.get(item.href, ())): text.append('<section>') page_section_open = True self.section_level += 1 text += self.dump_text(item.data.find(base.tag('xhtml', 'body')), stylizer, item) if page_section_open: text.append('</section>') self.section_level -= 1 # Close any open sections while self.section_level > 0: text.append('</section>') self.section_level -= 1 text.append('</body>') return ''.join(text)
def stylize_spine(self): self.stylizers = {} profile = self.context.source css = '' for item in self.items: html = item.data body = html.find(base.tag('xhtml', 'body')) if 'style' in html.attrib: b = body.attrib.get('style', '') body.set('style', html.get('style') + ';' + b) del html.attrib['style'] bs = body.get('style', '').split(';') bs.append('margin-top: 0pt') bs.append('margin-bottom: 0pt') if float(self.context.margin_left) >= 0: bs.append('margin-left : %gpt' % float(self.context.margin_left)) if float(self.context.margin_right) >= 0: bs.append('margin-right : %gpt' % float(self.context.margin_right)) bs.extend(['padding-left: 0pt', 'padding-right: 0pt']) if self.page_break_on_body: bs.extend(['page-break-before: always']) if self.context.change_justification != 'original': bs.append('text-align: ' + self.context.change_justification) if self.body_font_family: bs.append('font-family: ' + self.body_font_family) body.set('style', '; '.join(bs)) stylizer = Stylizer(html, item.href, self.oeb, self.context, profile, user_css=self.context.extra_css, extra_css=css) self.stylizers[item] = stylizer