def remove_default(path): """Remove all subfolders in _build except .jupyter_cache.""" to_remove = [ dd for dd in path.iterdir() if dd.is_dir() and dd.name != ".jupyter_cache" ] for dd in to_remove: sh.rmtree(path.joinpath(dd.name)) _message_box( "Your _build directory has been emptied except for .jupyter_cache")
def remove_html_latex(path): """Remove both html and latex folders.""" print_msg = False for opt in ["html", "latex"]: if path.joinpath(opt).is_dir(): print_msg = True remove_option(path, opt, True) if print_msg: _message_box("Your html and latex directories have been removed")
def remove_option(path, option, rm_both=False): """Remove folder specified under option. If rm_both is True, remove folder and skip message_box.""" option_path = path.joinpath(option) if not option_path.is_dir(): return sh.rmtree(option_path) if not rm_both: _message_box(f"Your {option} directory has been removed")
def create(path_book, cookiecutter): """Create a Jupyter Book template that you can customize.""" book = Path(path_book) if not cookiecutter: # this will be the more common option template_path = Path(__file__).parent.parent.joinpath("book_template") sh.copytree(template_path, book) else: cc_url = "gh:executablebooks/cookiecutter-jupyter-book" try: from cookiecutter.main import cookiecutter except ModuleNotFoundError as e: _error( f"{e}. To install, run\n\n\tpip install cookiecutter", kind=e.__class__, ) book = cookiecutter(cc_url, output_dir=Path(path_book)) _message_box(f"Your book template can be found at\n\n {book}{os.sep}")
def builder_specific_actions(result, builder, output_path, cmd_type, page_name=None, print_func=print): """Run post-sphinx-build actions. :param result: the result of the build execution; a status code or and exception """ from sphinx.util.osutil import cd from jupyter_book.pdf import html_to_pdf from jupyter_book.sphinx import REDIRECT_TEXT if isinstance(result, Exception): msg = (f"There was an error in building your {cmd_type}. " "Look above for the cause.") # TODO ideally we probably only want the original traceback here raise RuntimeError(_message_box(msg, color="red", doprint=False)) from result elif result: msg = ( f"Building your {cmd_type}, returns a non-zero exit code ({result}). " "Look above for the cause.") _message_box(msg, color="red", print_func=click.echo) sys.exit(result) # Builder-specific options if builder == "html": path_output_rel = Path(op.relpath(output_path, Path())) if cmd_type == "page": path_page = path_output_rel.joinpath(f"{page_name}.html") # Write an index file if it doesn't exist so we get redirects path_index = path_output_rel.joinpath("index.html") if not path_index.exists(): path_index.write_text( REDIRECT_TEXT.format(first_page=path_page.name)) _message_box( dedent(f""" Page build finished. Your page folder is: {path_page.parent}{os.sep} Open your page at: {path_page} """)) elif cmd_type == "book": path_output_rel = Path(op.relpath(output_path, Path())) path_index = path_output_rel.joinpath("index.html") _message_box(f"""\ Finished generating HTML for {cmd_type}. Your book's HTML pages are here: {path_output_rel}{os.sep} You can look at your book by opening this file in a browser: {path_index} Or paste this line directly into your browser bar: file://{path_index.resolve()}\ """) if builder == "pdfhtml": print_func(f"Finished generating HTML for {cmd_type}...") print_func(f"Converting {cmd_type} HTML into PDF...") path_pdf_output = output_path.parent.joinpath("pdf") path_pdf_output.mkdir(exist_ok=True) if cmd_type == "book": path_pdf_output = path_pdf_output.joinpath("book.pdf") html_to_pdf(output_path.joinpath("index.html"), path_pdf_output) elif cmd_type == "page": path_pdf_output = path_pdf_output.joinpath(page_name + ".pdf") html_to_pdf(output_path.joinpath(page_name + ".html"), path_pdf_output) path_pdf_output_rel = Path(op.relpath(path_pdf_output, Path())) _message_box(f"""\ Finished generating PDF via HTML for {cmd_type}. Your PDF is here: {path_pdf_output_rel}\ """) if builder == "pdflatex": print_func(f"Finished generating latex for {cmd_type}...") print_func(f"Converting {cmd_type} latex into PDF...") # Convert to PDF via tex and template built Makefile and make.bat if sys.platform == "win32": makecmd = os.environ.get("MAKE", "make.bat") else: makecmd = os.environ.get("MAKE", "make") try: with cd(output_path): output = subprocess.run([makecmd, "all-pdf"]) if output.returncode != 0: _error("Error: Failed to build pdf") return output.returncode _message_box(f"""\ A PDF of your {cmd_type} can be found at: {output_path} """) except OSError: _error("Error: Failed to run: %s" % makecmd) return 1
def remove_all(path): """Remove _build directory entirely.""" sh.rmtree(path) _message_box("Your _build directory has been removed")