예제 #1
0
def nb_detach_cells(path_nb, dest=None, replace=True, use_img=True):
    "Export cell attachments to `dest` and update references"
    path_nb = Path(path_nb)
    if not dest: dest = f'{path_nb.stem}_files'
    dest = Path(dest)
    dest.mkdir(exist_ok=True, parents=True)
    j = json.load(path_nb.open())
    atts = [o for o in j['cells'] if 'attachments' in o]
    for o in atts:
        o['source'] = _nb_detach_cell(o, dest, use_img)
    if atts and replace: json.dump(j, path_nb.open('w'))
    if not replace: return j
예제 #2
0
def rename_for_jekyll(nb_path: Path) -> str:
    """
    Return a Path's filename string appended with its modified time in YYYY-MM-DD format.
    """
    assert nb_path.exists(), f'{nb_path} could not be found.'

    # Checks if filename is compliant with Jekyll blog posts
    if _re_blog_date.match(nb_path.name):
        return nb_path.with_suffix('.md').name

    else:
        clean_name = _re_numdash.sub('', nb_path.with_suffix('.md').name)

        # Gets the file's last modified time and and append YYYY-MM-DD- to the beginning of the filename
        dtnm = datetime.fromtimestamp(
            os.path.getmtime(nb_path)).strftime("%Y-%m-%d-") + clean_name
        assert _re_blog_date.match(
            dtnm
        ), f'{dtnm} is not a valid name, filename must be pre-pended with YYYY-MM-DD-'
        # push this into a set b/c _nb2htmlfname gets called multiple times per conversion
        warnings.add((nb_path, dtnm))
        return dtnm
예제 #3
0
def _nb2htmlfname(nb_path, dest=None): 
    fname = rename_for_jekyll(nb_path, warnings=warnings)
    if dest is None: dest = Config().doc_path
    return Path(dest)/fname