From d5e4d179e990a54437eab2f16ec4468f165fb0cd Mon Sep 17 00:00:00 2001 From: Jake Vanderplas Date: Sat, 4 May 2013 07:21:10 -0700 Subject: [PATCH] add ablility to specify notebook cells --- liquid_tags/notebook.py | 61 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/liquid_tags/notebook.py b/liquid_tags/notebook.py index 79bb6e9..2d27751 100644 --- a/liquid_tags/notebook.py +++ b/liquid_tags/notebook.py @@ -6,7 +6,7 @@ notebook in a blog post. Syntax ------ -{% notebook filename.ipynb %} +{% notebook filename.ipynb [ cells[start:end] ]%} The file should be specified relative to the ``notebooks`` subdirectory of the content directory. Optionally, this subdirectory can be specified in the @@ -14,6 +14,9 @@ config file: NOTEBOOK_DIR = 'notebooks' +The cells[start:end] statement is optional, and can be used to specify which +block of cells from the notebook to include. + Details ------- Because the conversion and formatting of notebooks is rather involved, there @@ -52,8 +55,8 @@ except ImportError: from converters import ConverterBloggerHTML # requires nbconvert package separate_available = False -SYNTAX = "{% notebook /path/to/notebook.ipynb %}" -FORMAT = re.compile(r"""^(?:\s+)?(?P\S+)(?:\s+)?$""") +SYNTAX = "{% notebook /path/to/notebook.ipynb [ cells[start:end] ] %}" +FORMAT = re.compile(r"""^(\s+)?(?P\S+)(\s+)?((cells\[)(?P-?[0-9]*):(?P-?[0-9]*)(\]))?(\s+)?$""") def process_body(body): @@ -108,16 +111,66 @@ def process_header(header): return header.split('\n') +def strip_divs(body, start=None, end=None): + """Strip divs from the body for partial notebook insertion + + If L represents the list of parsed main divs, then this returns + the document corresponding to the divs L[start:end]. + + body should be a list of lines in the body of the html file. + """ + # TODO: this is a bit hackish. It would be better to add a PR to + # nbconvert which does this at the source. + DIV = re.compile('