Example #1
0
 def epub(self):
     """
     Converts html to epub and returns the char stream
     """
     self._epub = EpubBook()
     self._epub.set_title(self.parser.get_title())
     self._epub.add_creator(self.parser.get_author())
     
     chapters = self.parser.get_chapters()
     self._epub.add_title_page()
     
     if len(chapters) > 1:
         self._epub.add_toc_page()
     
     if self.cover:
         self._epub.add_cover(self.cover)
     
     # add chapters
     for chapter in chapters:
         item = self._epub.add_html('{0}.html'.format(chapter.slug), chapter.html)
         self._epub.add_spine_item(item)
         
         if len(chapters) > 1:
             self._epub.add_toc_map_node(item.dest_path, chapter.title)
     
     stream = StringIO()
     self._epub.create_book(stream)
     return stream.getvalue()
Example #2
0
class Md2Ebook(object):
    """
    Converts from markdown to a variety of ebook formats: 
        html, pdf, epub and mobi
    Usage:
        >>> book = Md2Ebook(md_content)
        >>> open(path, 'w').write(book.html)
        >>> open(path, 'w').write(book.pdf)
        ...
    """
    def __init__(self, md_content, cover=None):
        self.markdown = md_content
        self.cover = cover
        self.parser = HtmlParser(self.html)
        self._epub = None
    
    @LazyProperty
    def html(self):
        """
        Converts markdown to html and returns the char stream
        """
        html = markdown(self.markdown)
        parser = HtmlParser(html)
        return HTMLWRAP.wrap_html(parser.get_title(), html)
    
    @LazyProperty
    def pdf(self):
        """
        Converts html to pdf and returns the char stream
        """
        result = StringIO()
        # h3 and down shouldn't make chapters
        html = self.html.encode("UTF-8")
        html = html.replace('<h3>', '<h3 class="not_outline">')
        html = html.replace('<h4>', '<h4 class="not_outline">')
        html = html.replace('<h5>', '<h5 class="not_outline">')
        html = html.replace('<h6>', '<h6 class="not_outline">')
        # add page breaks before each h2
        html = html.replace('<h2>', '<h2 class="break">')
        # generate the pdf
        pisa.pisaDocument(StringIO(html), result)
        return result.getvalue()
    
    @LazyProperty
    def epub(self):
        """
        Converts html to epub and returns the char stream
        """
        self._epub = EpubBook()
        self._epub.set_title(self.parser.get_title())
        self._epub.add_creator(self.parser.get_author())
        
        chapters = self.parser.get_chapters()
        self._epub.add_title_page()
        
        if len(chapters) > 1:
            self._epub.add_toc_page()
        
        if self.cover:
            self._epub.add_cover(self.cover)
        
        # add chapters
        for chapter in chapters:
            item = self._epub.add_html('{0}.html'.format(chapter.slug), chapter.html)
            self._epub.add_spine_item(item)
            
            if len(chapters) > 1:
                self._epub.add_toc_map_node(item.dest_path, chapter.title)
        
        stream = StringIO()
        self._epub.create_book(stream)
        return stream.getvalue()
    
    @LazyProperty
    def mobi(self):
        """
        Converts epub to mobi and returns the char stream
        """
        # save epub to temp file
        (_, epub_name) = tempfile.mkstemp(suffix='.epub')
        (_, mobi_name) = tempfile.mkstemp(suffix='.mobi')
        (_, mobi_file) = os.path.split(mobi_name)
        
        try:
            with open(epub_name, 'w') as _file:
                _file.write(self.epub)
            
            kindlegen_path = os.path.join(os.path.split(__file__)[0], 
                'kindlegen_linux_2.6_i386_v2_4/kindlegen')
            with open(os.devnull) as fnull:
                subprocess.call(
                    [kindlegen_path, epub_name, '-c1', '-o', mobi_file],
                    stdout = fnull, stderr = fnull)
            with open(mobi_name) as _file:
                return _file.read()
        finally:
            os.remove(epub_name)
            os.remove(mobi_name)