def save_user_document(data: Data, user_slug: str, doc_slug: str): """Saves a stored document into a directory as text files.""" dst_dir = fixtures_dir(doc_slug) dst_dict = data.userDocument_get(user_slug, doc_slug) save_dir(dst_dir, dst_dict) print("Saved: {} ({:d} files)".format(doc_slug, len(dst_dict)))
def write_epub(user_slug, doc_slug, file_path): # Get all the data config = load_env_config() data = Data(config) user = data.user_get(user_slug) # or None if not user: raise RuntimeError("User not found: %s", user_slug) document = data.userDocument_get(user_slug, doc_slug) # or Noen if not document: raise RuntimeError("Document not found: %s" % doc_slug) # ------------------------- # 0. Create book # 1. Create cover # 2. Create title page # 3. Create chapter (which basically is the book) # ... This upgrades to multiple chapters when compiling books. # Pre-processing... settings = Settings({ 'config:user': user_slug, 'config:document': doc_slug, }) wiki = Wiki(settings) xhtml = wiki.process(user_slug, doc_slug, document) metadata = wiki.compile_metadata(config['TIME_ZONE'], user_slug, doc_slug) metadata['url'] = '/read/{:s}/{:s}'.format(user_slug, doc_slug), title = metadata.get('title', 'Untitled') summary = metadata.get('summary', '') author = metadata.get('author', 'Anonymous') date = metadata.get('date', '') # ------------------------- # 0. CREATE BOOK book = epub.EpubBook() # set metadata book.set_identifier(user_slug + '+' + doc_slug) book.set_title(title) book.set_language('en') book.add_author(author) # define CSS style with open('static/epub.css') as f: style = f.read() global_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style) book.add_item(global_css) # ------------------------- # 1. Create Cover tmp_cover_file = "/tmp/%s-%s-cover.png" % (user_slug, doc_slug) image = make_background((1600, 2200), (160, 184, 160)) cover = make_cover(image, [title, summary, author, date], [COLOR_TEXT, COLOR_SHADOW]) cover.save(tmp_cover_file, "JPEG") chapter_file_name = doc_slug + '.xhtml' assert os.path.exists(tmp_cover_file) cover_image = open(tmp_cover_file, 'rb').read() book.set_cover("image.jpg", cover_image) # ------------------------- # 2. Create Title Page date_string = datetime.now().strftime("%Y-%m-%d %H:%M:%S") title_xhtml = """ <html> <body> <div>Generated by <i>Article Wiki</i>:</div> <div>%s</div> <div> </div> <div>Permanent URL:</div> <div>http://chapman.wiki/read/%s/%s</div> </body> </html> """ % (date_string, user_slug, doc_slug) c1 = epub.EpubHtml(title="About this book", file_name="title.xhtml", lang='en') c1.content = title_xhtml c1.add_item(global_css) book.add_item(c1) # ------------------------- # 3. Create Chapter c2 = epub.EpubHtml(title=title, file_name=chapter_file_name, lang='en') c2.content = xhtml c2.add_item(global_css) book.add_item(c2) # Define Table Of Contents book.toc = ( epub.Link(chapter_file_name, title, doc_slug), # (epub.Section(user_slug), (c2)) ) # add default NCX and Nav file book.add_item(epub.EpubNcx()) book.add_item(epub.EpubNav()) # basic spine book.spine = ['nav', c1, c2] # write to the file epub.write_epub(file_path, book, {})