def run(): """Run this action.""" options = mdepub.options project_path = mdepub.project_path log.debug("run()") mdepub.require_opts_file() os.chdir(project_path) css_file = getFN("css") args = ["pandoc", "--standalone", "--email-obfuscation=none"] src = getFN("md") if not os.path.exists(src): log.fatal("\"%s\" not found.", src) sys.exit(1) if os.path.exists(css_file): args.extend(["-c", css_file]) if options['smart quotes']: args.append("--smart") args.append(src) html = shell.pipe(args, None) checkForBadLinks(html) with file(getFN("html"), 'w') as f: f.write(html)
def run(): """Run this action.""" log.debug("run()") mdepub.require_opts_file() os.chdir(mdepub.project_path) for ext in ['epub', 'html', 'zip']: f = getFN(ext) if os.path.exists(f): os.remove(f)
def run(): """Run this action.""" options = mdepub.options project_path = mdepub.project_path log.debug("run()") mdepub.require_opts_file() os.chdir(project_path) skip = ["./" + getFN("html"), "./" + getFN("epub"), "./" + getFN("zip")] with ZipFile(getFN("zip"), 'w') as zip: for root, dirs, files in os.walk("."): for file in files: fpath = os.path.join(root, file) if fpath not in skip: zip.write(fpath)
def run(): """Run this action.""" options = mdepub.options project_path = mdepub.project_path log.debug("run()") mdepub.require_opts_file() os.chdir(project_path) if 'uuid' in options: # replace existing uuid with open("options.yaml", "r") as f: txt = f.read() txt = r_uuid.sub(r'\g<1>uuid:\g<2>' + str(new_id()) + r'\g<4>', txt) #print txt with open("options.yaml", "w") as f: f.write(txt) else: # add new uuid field to end of file with open("options.yaml", "a") as f: f.write("\nuuid: {0}\n".format(new_id()))
def run(): """Run this action.""" options = mdepub.options project_path = mdepub.project_path log.debug("run()") mdepub.require_opts_file() os.chdir(project_path) skip = [ "./" + getFN("html"), "./" + getFN("epub"), "./" + getFN("zip") ] with ZipFile(getFN("zip"), 'w') as zip: for root, dirs, files in os.walk("."): for file in files: fpath = os.path.join(root, file) if fpath not in skip: zip.write(fpath)
def run(): """Run this action.""" options = mdepub.options project_path = mdepub.project_path log.debug("run()") mdepub.require_opts_file() os.chdir(project_path) if 'uuid' in options: # replace existing uuid with open("options.yaml", "r") as f: txt = f.read() txt = r_uuid.sub( r'\g<1>uuid:\g<2>' + str(new_id()) + r'\g<4>', txt ) #print txt with open("options.yaml", "w") as f: f.write(txt) else: # add new uuid field to end of file with open("options.yaml", "a") as f: f.write("\nuuid: {0}\n".format(new_id()))
def run(): """Run this action.""" options = mdepub.options project_path = mdepub.project_path arguments = mdepub.arguments log.debug("run()") mdepub.require_opts_file() os.chdir(project_path) # Get source and dest file times time = {} for ext in ['md', 'css', 'html', 'zip']: f = getFN(ext) if os.path.exists(f): time[ext] = os.path.getmtime(f) else: time[ext] = None if time['html'] < time['md'] or time['html'] < time['css']: mdepub.actions.html.run() time['html'] = os.path.getmtime(getFN("html")) if time['zip'] < time['html']: mdepub.actions.archive.run() # Get description html from markdown if 'description' in options: description = shell.pipe(["pandoc"], options['description']) else: description = "" # Setup ebook-convert args if not 'title' in options: log.fatal("No title given in options.yaml.") sys.exit(1) args = [ "ebook-convert", '"' + getFN("html") + '"', '"' + getFN("epub") + '"', "--title=\"{}\"".format(quote(options['title'])) ] for a, b in ( ('author-sort', 'author sort'), ('authors', 'authors'), ('book-producer', 'book producer'), ('isbn', 'isbn'), ('language', 'language'), ('pubdate', 'publication date'), ('publisher', 'publisher'), ('rating', 'rating'), ('series', 'series'), ('series-index', 'series index'), ('title-sort', 'title sort'), ): if options.get(b): args.append("--{}=\"{}\"".format(a, quote(options[b]))) if description: args.append("--comments=\"{}\"".format(quote(description))) if options.get("tags"): args.append("--tags=\"{}\"".format(quote(','.join(options['tags'])))) args.append("--chapter=\"//h:h{}\"".format( options.get('chapter head level') or 2)) tmp = [] for i in range(options.get('chapter head level') or 2): if len(tmp) > 0: tmp.append("or") tmp.append("name()='h{}'".format(str(i + 1))) args.append("--level1-toc=\"//*[{}]\"".format(" ".join(tmp))) for ext in ["png", "jpg", "svg"]: cover_filename = "cover.{}".format(ext) if os.path.exists(cover_filename): break else: cover_filename = None if cover_filename: args.append("--cover=\"{}\"".format(cover_filename)) if not options.get('stretch cover image'): args.append("--preserve-cover-aspect-ratio") if options.get('margin'): for i in ['top', 'left', 'right', 'bottom']: if options['margin'].get(i) is not None: args.append("--margin-{}".format(i)) args.append(str(options['margin'][i])) # Run ebook-convert shell.run(" ".join(args), shell=True) # Add source zip file to epub and update content.opf with correct uuid metadata = None with ZipFile(getFN("epub"), 'a') as zip: if arguments.source: zip.write(getFN("zip"), "META-INF/source/mdepub_source.zip") metadata = zip.read("content.opf") shell.run(["zip", "-d", getFN("epub"), "content.opf"]) with ZipFile(getFN("epub"), 'a') as zip: #metadata = zip.read("content.opf") soup = BeautifulSoup(metadata) id = soup.find(id="uuid_id") id.contents[0].replaceWith(options.get('uuid') or mdepub.new_id()) #print soup zip.writestr("content.opf", str(soup))
def run(): """Run this action.""" options = mdepub.options project_path = mdepub.project_path arguments = mdepub.arguments log.debug("run()") mdepub.require_opts_file() os.chdir(project_path) # Get source and dest file times time = {} for ext in ["md", "css", "html", "zip"]: f = getFN(ext) if os.path.exists(f): time[ext] = os.path.getmtime(f) else: time[ext] = None if time["html"] < time["md"] or time["html"] < time["css"]: mdepub.actions.html.run() time["html"] = os.path.getmtime(getFN("html")) if time["zip"] < time["html"]: mdepub.actions.archive.run() # Get description html from markdown if "description" in options: description = shell.pipe(["pandoc"], options["description"]) else: description = "" # Setup ebook-convert args if not "title" in options: log.fatal("No title given in options.yaml.") sys.exit(1) args = [ "ebook-convert", '"' + getFN("html") + '"', '"' + getFN("epub") + '"', '--title="{}"'.format(quote(options["title"])), ] for a, b in ( ("author-sort", "author sort"), ("authors", "authors"), ("book-producer", "book producer"), ("isbn", "isbn"), ("language", "language"), ("pubdate", "publication date"), ("publisher", "publisher"), ("rating", "rating"), ("series", "series"), ("series-index", "series index"), ("title-sort", "title sort"), ): if options.get(b): args.append('--{}="{}"'.format(a, quote(options[b]))) if description: args.append('--comments="{}"'.format(quote(description))) if options.get("tags"): args.append('--tags="{}"'.format(quote(",".join(options["tags"])))) args.append('--chapter="//h:h{}"'.format(options.get("chapter head level") or 2)) tmp = [] for i in range(options.get("chapter head level") or 2): if len(tmp) > 0: tmp.append("or") tmp.append("name()='h{}'".format(str(i + 1))) args.append('--level1-toc="//*[{}]"'.format(" ".join(tmp))) for ext in ["png", "jpg", "svg"]: cover_filename = "cover.{}".format(ext) if os.path.exists(cover_filename): break else: cover_filename = None if cover_filename: args.append('--cover="{}"'.format(cover_filename)) if not options.get("stretch cover image"): args.append("--preserve-cover-aspect-ratio") if options.get("margin"): for i in ["top", "left", "right", "bottom"]: if options["margin"].get(i) is not None: args.append("--margin-{}".format(i)) args.append(str(options["margin"][i])) # Run ebook-convert shell.run(" ".join(args), shell=True) # Add source zip file to epub and update content.opf with correct uuid metadata = None with ZipFile(getFN("epub"), "a") as zip: if arguments.source: zip.write(getFN("zip"), "META-INF/source/mdepub_source.zip") metadata = zip.read("content.opf") shell.run(["zip", "-d", getFN("epub"), "content.opf"]) with ZipFile(getFN("epub"), "a") as zip: # metadata = zip.read("content.opf") soup = BeautifulSoup(metadata) id = soup.find(id="uuid_id") id.contents[0].replaceWith(options.get("uuid") or mdepub.new_id()) # print soup zip.writestr("content.opf", str(soup))