def create_chapter(self): self.sections.append(self.body) self.body = [] body = ''.join(self.sections[-1]) if smartypants: #body = smartypants.smartyPants(body) # pass need to ignore pre contents... pass css = '' if self.css: css = ''.join([ '<link rel="stylesheet" href="{0}" type="text/css" media="all" />' .format(os.path.basename(item)) for item in self.css ]) for item in self.css: if os.path.exists(item): self.book.add_css(item, os.path.basename(item)) else: self.book.add_css( os.path.join(os.path.dirname(epub.__file__), 'templates', 'main.css'), 'main.css') title = '' if self.section_title: title = striptags(self.section_title) html = XHTML_WRAPPER.format(body=body, title=title, css=css) if self.is_title_page: self.book.add_title_page(html) # clear out toc_map_node self.book.last_node_at_depth = {0: self.book.toc_map_root} self.is_title_page = False elif self.toc_page: self.book.add_toc_page(order=self.book.next_order()) self.toc_page = False else: item = self.book.add_html('', '{0}.html'.format(len(self.sections)), html) self.book.add_spine_item(item) parent = self.toc_parents[-1] if self.toc_parents else None node = self.book.add_toc_map_node( item.dest_path, striptags(self.section_title), parent=parent) #''.join(self.html_subtitle)) if self.parent_level == 1: self.toc_parents = [node] elif self.parent_level == 2: self.toc_parents = self.toc_parents[:1] + [node] self.reset_chapter()
def depart_title(self, node): # print "--TITLE", node.text if not self._title: self._title = striptags(''.join(self.body[1:])) print "\n\n**title", self.section_level, ''.join(self.body[1:])[:150] low_level = self.section_level \ <= int(self.builder.config.mobi_chapter_level) if node.parent.getattr('ids', False) and low_level: print "TITLE", self.section_level, node.parent['ids'][0] self._title = striptags(''.join(self.body[1:])) print "\t", str(node)[:50] print "\tBODY", self.body[:4] else: print "FALSE" HTMLTranslator.depart_title(self, node)
def striptags(self): """Return a copy of the text with all XML/HTML tags removed. :return: a `Markup` instance with all tags removed :rtype: `Markup` :see: `genshi.util.striptags` """ return Markup(striptags(self))
def create_chapter(self): self.sections.append(self.body) self.body = [] body = ''.join(self.sections[-1]) if smartypants: #body = smartypants.smartyPants(body) # pass need to ignore pre contents... pass css = '' if self.css: css = ''.join(['<link rel="stylesheet" href="{0}" type="text/css" media="all" />'.format(os.path.basename(item)) for item in self.css]) for item in self.css: if os.path.exists(item): self.book.add_css(item, os.path.basename(item)) else: self.book.add_css(os.path.join(os.path.dirname(epub.__file__), 'templates', 'main.css'),'main.css') title = '' if self.section_title: title=striptags(self.section_title) html = XHTML_WRAPPER.format(body=body, title=title, css=css) if self.is_title_page: self.book.add_title_page(html) # clear out toc_map_node self.book.last_node_at_depth = {0:self.book.toc_map_root} self.is_title_page = False elif self.toc_page: self.book.add_toc_page(order=self.book.next_order()) self.toc_page = False else: item = self.book.add_html('', '{0}.html'.format(len(self.sections)), html) self.book.add_spine_item(item) parent = self.toc_parents[-1] if self.toc_parents else None node = self.book.add_toc_map_node(item.dest_path, striptags(self.section_title), parent=parent) #''.join(self.html_subtitle)) if self.parent_level == 1: self.toc_parents = [node] elif self.parent_level == 2: self.toc_parents = self.toc_parents[:1] + [node] self.reset_chapter()
def depart_title(self, node): if node.parent.hasattr('ids') and node.parent['ids'] and \ self.section_level == 2: print "TITLE",self.section_level,node.parent['ids'][0] #self._title = node.parent['ids'][0] self._title = striptags(''.join(self.body[1:])) print "\t",str(node)[:50] print "\tBODY", self.body[:4] HTMLTranslator.depart_title(self, node)
def to_text(fragment): if isinstance(fragment, (Element, Fragment, Markup)): return striptags(unicode(fragment)) else: return unicode(fragment)
def create_chapter(self): self.sections.append(self.body) self.body = [] body = ''.join(self.sections[-1]) if smartypants: #body = smartypants.smartyPants(body) # pass need to ignore pre contents... pass css = '' if self.css: css = ''.join(['<link rel="stylesheet" href="{0}" type="text/css" media="all" />'.format(os.path.basename(item)) for item in self.css]) for item in self.css: if os.path.exists(item): self.book.add_css(item, os.path.basename(item)) else: self.book.add_css(os.path.join(os.path.dirname(epub.__file__), 'templates', 'main.css'),'main.css') if self.font: for item in self.font: if os.path.exists(item): self.book.add_font(item, os.path.basename(item)) else: raise KeyError js = '' if self.js: js = ''.join(['<script src="{0}" type="text/javascript"></script>'.format(os.path.basename(item)) for item in self.js]) for item in self.js: if os.path.exists(item): self.book.add_js(item, os.path.basename(item)) else: raise KeyError title = '' if 'title' in self.fields and self.is_title_page: title = self.fields['title'] elif self.section_title: title=striptags(self.section_title) header = css+js html = XHTML_WRAPPER.format(body=body, title=title, header=header) if self.is_title_page: self.book.add_title_page(html) # clear out toc_map_node self.book.last_node_at_depth = {0:self.book.toc_map_root} self.is_title_page = False elif self.toc_page: self.book.add_toc_page(order=self.book.next_order()) self.toc_page = False else: dst = '{0}.html'.format(len(self.sections)) item = self.book.add_html('', dst, html) if self.guide_type: self.book.add_guide_item(dst, self.section_title, self.guide_type) self.book.add_spine_item(item) parent = self.toc_parents[-1] if self.toc_parents else None if self.toc_entry: node = self.book.add_toc_map_node(item.dest_path, striptags(self.section_title), parent=parent) #''.join(self.html_subtitle)) if self.parent_level == 1: self.toc_parents = [node] elif self.parent_level == 2: self.toc_parents = self.toc_parents[:1] + [node] self.reset_chapter()
def create_chapter(self): self.sections.append(self.body) self.body = [] body = ''.join(self.sections[-1]) if self.footnotes: # add footnotes to end of chapter body += '<br/>' body += ''.join(self.footnotes) self.footnotes = [] if smartypants: #body = smartypants.smartyPants(body) # pass need to ignore pre contents... pass css = '' if self.css: css = ''.join([ '<link rel="stylesheet" href="{0}" type="text/css" media="all" />' .format(os.path.basename(item)) for item in self.css ]) for item in self.css: if os.path.exists(item): self.book.add_css(item, os.path.basename(item)) else: self.book.add_css( os.path.join(os.path.dirname(epub.__file__), 'templates', 'main.css'), 'main.css') if self.font: for item in self.font: if os.path.exists(item): self.book.add_font(item, os.path.basename(item)) else: raise KeyError(item) js = '' if self.js: js = ''.join([ '<script src="{0}" type="text/javascript"></script>'.format( os.path.basename(item)) for item in self.js ]) for item in self.js: if os.path.exists(item): self.book.add_js(item, os.path.basename(item)) else: raise KeyError(item) title = '' if 'title' in self.fields and self.is_title_page: title = self.fields['title'] elif self.section_title: title = striptags(self.section_title) header = css + js html = XHTML_WRAPPER.format(body=body, title=title, header=header) if self.is_title_page: self.book.add_title_page(html) # clear out toc_map_node self.book.last_node_at_depth = {0: self.book.toc_map_root} self.is_title_page = False elif self.toc_page: self.book.add_toc_page(order=self.book.next_order()) self.toc_page = False else: dst = '{0}.html'.format(len(self.sections)) item = self.book.add_html('', dst, html) if self.guide_type: self.book.add_guide_item(dst, self.section_title, self.guide_type) self.book.add_spine_item(item) parent = self.toc_parents[-1] if self.toc_parents else None if self.toc_entry: node = self.book.add_toc_map_node( item.dest_path, striptags(self.section_title), parent=parent) #''.join(self.html_subtitle)) if self.parent_level == 1: self.toc_parents = [node] elif self.parent_level == 2: self.toc_parents = self.toc_parents[:1] + [node] self.reset_chapter()
def striptags(self): """Return a copy of the text with all XML/HTML tags removed. :see: `genshi.util.striptags` """ return Markup(striptags(self))