def convert_to_rst(notebook, cache=True):
    notebook_mtime = path.getmtime(notebook)
    path_dir = path.dirname(notebook)
    cache_file = path.join(path_dir, '.{}_{}'.format(
        notebook_mtime, path.basename(notebook)))

    if path.exists(cache_file):
        print('Reading from cache: ', cache_file)
        with open(cache_file) as f:
            body = f.read()
        if body == '':
            remove(cache_file)
            return convert_to_rst(notebook, cache)
    else:
        # Instantiate it
        rst_exporter = RSTExporter()
        # Convert the notebook to RST format
        (body, resources) = rst_exporter.from_filename(notebook)

        print('Writing to cache: ', cache_file)
        with open(cache_file, 'w') as f:
            f.write(body)
    return body
Beispiel #2
0
def parse_notebook_index(ntbkpth):
    """
    Parse the top-level notebook index file at `ntbkpth`.  Returns a
    list of subdirectories in order of appearance in the index file,
    and a dict mapping subdirectory name to a description.
    """

    # Convert notebook to RST text in string
    rex = RSTExporter()
    rsttxt = rex.from_filename(ntbkpth)[0]
    # Clean up trailing whitespace
    rsttxt = re.sub(r'\n  ', r'', rsttxt, re.M | re.S)
    pthidx = {}
    pthlst = []
    lines = rsttxt.split('\n')
    for l in lines:
        m = re.match(r'^-\s+`([^<]+)\s+<([^>]+).ipynb>`__', l)
        if m:
            # List of subdirectories in order of appearance in index.rst
            pthlst.append(m.group(2))
            # Dict mapping subdirectory name to description
            pthidx[m.group(2)] = m.group(1)
    return pthlst, pthidx
Beispiel #3
0
def parse_notebook_index(ntbkpth):
    """
    Parse the top-level notebook index file at `ntbkpth`.  Returns a
    list of subdirectories in order of appearance in the index file,
    and a dict mapping subdirectory name to a description.
    """

    # Convert notebook to RST text in string
    rex = RSTExporter()
    rsttxt = rex.from_filename(ntbkpth)[0]
    # Clean up trailing whitespace
    rsttxt = re.sub(r'\n  ', r'', rsttxt, re.M | re.S)
    pthidx = {}
    pthlst = []
    lines = rsttxt.split('\n')
    for l in lines:
        m = re.match(r'^-\s+`([^<]+)\s+<([^>]+).ipynb>`__', l)
        if m:
            # List of subdirectories in order of appearance in index.rst
            pthlst.append(m.group(2))
            # Dict mapping subdirectory name to description
            pthidx[m.group(2)] = m.group(1)
    return pthlst, pthidx