PKtJUH,QQbookbook/__init__.py"""Tools to use a collection of notebooks as 'chapters' """ __version__ = '0.2' PKsJrqbookbook/filter_links.py#!/usr/bin/env python3 """A pandoc filter used in converting notebooks to Latex. Converts links between notebooks to Latex cross-references. """ import re from pandocfilters import toJSONFilter, applyJSONFilters, RawInline def convert_link(key, val, fmt, meta): if key == 'Link': target = val[2][0] # Links to other notebooks m = re.match(r'(\d+\-.+)\.ipynb$', target) if m: return RawInline('tex', 'Section \\ref{sec:%s}' % m.group(1)) # Links to sections of this or other notebooks m = re.match(r'(\d+\-.+\.ipynb)?#(.+)$', target) if m: # pandoc automatically makes labels for headings. label = m.group(2).lower() label = re.sub(r'[^\w-]+', '', label) # Strip HTML entities return RawInline('tex', 'Section \\ref{%s}' % label) # Other elements will be returned unchanged. def convert_links(source): return applyJSONFilters([convert_link], source) if __name__ == "__main__": toJSONFilter(convert_link) PKrJa a bookbook/html.py"""Converts a collection of notebooks to HTML files.""" import argparse import logging from pathlib import Path import re import os import nbformat from nbconvert.exporters import HTMLExporter from nbconvert.writers import FilesWriter from nbconvert.filters.markdown_mistune import MarkdownWithMath, IPythonRenderer from jinja2 import Environment, FileSystemLoader _PKGDIR = Path(__file__).parent log = logging.getLogger(__name__) class MyMarkdownRenderer(IPythonRenderer): def link(self, link, title, text): m = re.match(r'(\d+\-.+)\.ipynb(#.+)?$', link) if m: link = m.expand(r'\1.html\2') return super().link(link, title, text) def markdown2html_custom(source): """Convert a markdown string to HTML using mistune""" return MarkdownWithMath(renderer=MyMarkdownRenderer(escape=False)).render(source) class MyHTMLExporter(HTMLExporter): def default_filters(self): yield from super().default_filters() yield ('markdown2html', markdown2html_custom) class IndexEntry: def __init__(self, chapter_no, title, filename): self.chapter_no = chapter_no self.title = title self.filename = filename @classmethod def from_notebook_file(cls, path: Path): chapter_no = int(re.match('(\d+)\-', path.stem).group(1)) nb = nbformat.read(str(path), as_version=4) assert nb.cells[0].cell_type == 'markdown', nb.cells[0].cell_type lines = nb.cells[0].source.splitlines() if lines[0].startswith('# '): header = lines[0][2:] elif len(lines) > 1 and lines[1].startswith('==='): header = lines[0] else: assert False, "No heading found in %s" % str(path) assert path.suffix=='.ipynb', path html_filename = path.stem + '.html' return cls(chapter_no, header, html_filename) def convert(source_path: Path, output_dir: Path): exporter = MyHTMLExporter() writer = FilesWriter(build_directory=str(output_dir)) output, resources = exporter.from_filename(str(source_path)) notebook_name = source_path.stem writer.write(output, resources, notebook_name=notebook_name) def write_index(index_entries, output_dir): env = Environment(loader=FileSystemLoader(str(_PKGDIR))) template = env.get_template('html_index.tpl') index_entries.sort(key=lambda e: e.chapter_no) log.info('Writing table of contents') with (output_dir / 'index.html').open('w') as f: f.write(template.render(index_entries=index_entries)) def convert_directory(source_dir: Path, output_dir: Path): index_entries = [] for nbfile in source_dir.glob('*-*.ipynb'): convert(nbfile, output_dir) index_entries.append(IndexEntry.from_notebook_file(nbfile)) output_dir.mkdir(exist_ok=True, parents=True) write_index(index_entries, output_dir) def main(argv=None): ap = argparse.ArgumentParser(description='Convert a set of notebooks to HTML') ap.add_argument('source_dir', nargs='?', type=Path, default='.', help='Directory containing the .ipynb files') ap.add_argument('--output-dir', type=Path, default='html', help='Directory where output files will be written') args = ap.parse_args(argv) logging.basicConfig(level=logging.INFO) convert_directory(args.source_dir, args.output_dir) if __name__ == '__main__': main() PKqJ=KPPbookbook/html_index.tpl Table of Contents

Table of Contents

PKyJۋbookbook/latex.py"""Convert a collection of notebooks to a single PDF, via Latex. - Combines notebooks into one document - Inserts Latex labels for each document - Converts links between notebooks to Latex \\ref{} - Runs pdflatex to make a PDF (actually, nbconvert does this) Requirements: - nbconvert pandocfilters (pip installable) - pandoc - pdflatex """ import argparse import logging import os from pathlib import Path from tempfile import mkdtemp from typing import Sequence import nbformat from nbformat import NotebookNode from nbformat.v4 import new_notebook, new_markdown_cell from nbconvert.exporters import PDFExporter, LatexExporter from nbconvert.writers import FilesWriter from nbconvert.utils.pandoc import pandoc from .filter_links import convert_links log = logging.getLogger(__name__) def new_latex_cell(source=''): return NotebookNode( cell_type='raw', metadata=NotebookNode(raw_mimetype='text/latex'), source=source, ) class NoHeader(Exception): pass def add_sec_label(cell: NotebookNode, nbname) -> Sequence[NotebookNode]: """Adds a Latex \\label{} under the chapter heading. This takes the first cell of a notebook, and expects it to be a Markdown cell starting with a level 1 heading. It inserts a label with the notebook name just underneath this heading. """ assert cell.cell_type == 'markdown', cell.cell_type lines = cell.source.splitlines() if lines[0].startswith('# '): header_lines = 1 elif len(lines) > 1 and lines[1].startswith('==='): header_lines = 2 else: raise NoHeader header = '\n'.join(lines[:header_lines]) intro_remainder = '\n'.join(lines[header_lines:]).strip() res = [ new_markdown_cell(header), new_latex_cell('\label{sec:%s}' % nbname) ] res[0].metadata = cell.metadata if intro_remainder: res.append(new_markdown_cell(intro_remainder)) return res def combine_notebooks(notebook_files: Sequence[Path]) -> NotebookNode: combined_nb = new_notebook() count = 0 for filename in notebook_files: count += 1 log.debug('Adding notebook: %s', filename) nbname = filename.stem nb = nbformat.read(str(filename), as_version=4) try: combined_nb.cells.extend(add_sec_label(nb.cells[0], nbname)) except NoHeader: raise NoHeader("Failed to find header in " + filename) combined_nb.cells.extend(nb.cells[1:]) if not combined_nb.metadata: combined_nb.metadata = nb.metadata.copy() log.info('Combined %d files' % count) return combined_nb mydir = os.path.dirname(os.path.abspath(__file__)) filter_links = os.path.join(mydir, 'filter_links.py') def pandoc_convert_links(source): return pandoc(source, 'markdown', 'latex', extra_args=['--filter', filter_links]) class MyLatexExporter(LatexExporter): def default_filters(self): yield from super().default_filters() yield ('resolve_references', convert_links) class MyLatexPDFExporter(PDFExporter): def default_filters(self): yield from super().default_filters() yield ('resolve_references', convert_links) def add_preamble(extra_preamble_file, exporter): if extra_preamble_file is None: return with extra_preamble_file.open() as f: extra_preamble = f.read() td = mkdtemp() print(td) template_path = Path(td, 'with_extra_preamble.tplx') with template_path.open('w') as f: f.write("((* extends 'article.tplx' *))\n" '((* block header *))\n' '((( super() )))\n' ) f.write(extra_preamble) f.write('((* endblock header *))\n' ) # Not using append, because we need an assignment to trigger traitlet change exporter.template_path = exporter.template_path + [td] exporter.template_file = 'with_extra_preamble' def export(combined_nb: NotebookNode, output_file: Path, pdf=False, template_file=None): resources = {} resources['unique_key'] = 'combined' resources['output_files_dir'] = 'combined_files' log.info('Converting to %s', 'pdf' if pdf else 'latex') exporter = MyLatexPDFExporter() if pdf else MyLatexExporter() if template_file is not None: exporter.template_file = str(template_file) writer = FilesWriter(build_directory=str(output_file.parent)) output, resources = exporter.from_notebook_node(combined_nb, resources) writer.write(output, resources, notebook_name=output_file.stem) def combine_and_convert(source_dir: Path, output_file: Path, pdf=False, template_file=None): notebook_files = sorted(source_dir.glob('*-*.ipynb')) combined_nb = combine_notebooks(notebook_files) export(combined_nb, output_file, pdf=pdf, template_file=template_file) def main(argv=None): ap = argparse.ArgumentParser(description='Convert a set of notebooks to PDF via Latex') ap.add_argument('source_dir', nargs='?', type=Path, default='.', help='Directory containing the .ipynb files') ap.add_argument('--output-file', type=Path, default='combined', help='Base name of the output file.') ap.add_argument('--pdf', action='store_true', help='Run Latex to convert to PDF.') ap.add_argument('--template', type=Path, help='Latex template file to use for nbconvert.') args = ap.parse_args(argv) logging.basicConfig(level=logging.INFO) combine_and_convert(args.source_dir, args.output_file, args.pdf, args.template) if __name__ == '__main__': main() PKvIm 599bookbook-0.2.dist-info/LICENSEThe MIT License (MIT) Copyright (c) 2016 Thomas Kluyver Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!H;@QPbookbook-0.2.dist-info/WHEEL1 0 RZq+D-Dv;_[*7Fp ܦpv/fݞoL(*IPK!H)bookbook-0.2.dist-info/METADATASj@}߯CZ\苠iCmJAݱ޺#w֖8!i.gfΙ%O!*g+8+ل`2?l;%Ck ]St䠍E8ps.a΍#D ',<_|pPigpfR p_{H_'7v٦*8C7ۄ\JW6=Zil jUXwq޿(߲OǨ Uw#>;G)-7F]4\_GS+'Z6١+Sv&KV:1W:#?gc DL(KVI"e!cp>'X <.F8ǀV`5Iӭ5=MDP2ȃ@ yڨOP֝"k߸<~0#7s'Wt:(0t| xrw%y)3T4$ A6ҿN|=>ܷ-2{`*Xѡ*IܵAPKtJUH,QQbookbook/__init__.pyPKsJrqbookbook/filter_links.pyPKrJa a bookbook/html.pyPKqJ=KPPfbookbook/html_index.tplPKyJۋbookbook/latex.pyPKvIm 599)*bookbook-0.2.dist-info/LICENSEPK!H;@QP.bookbook-0.2.dist-info/WHEELPK!H))/bookbook-0.2.dist-info/METADATAPK!HB1bookbook-0.2.dist-info/RECORDPK x3