Esempio n. 1
0
def extract_main_and_meta(ctx, ipynb_file_path):
    blog = ctx.current_blog
    metadata = OrderedDict()
    delete_ipynb_meta = ctx.config.read(key=blog + ':delete_ipynb_meta')
    ctx.written_ipynb = False
    with open(ipynb_file_path, 'r', encoding='utf-8') as rf:
        ipynb_data = rf.read()

    nbdata_file_path = replace_ext(ipynb_file_path, '.nbdata')
    if os.path.exists(nbdata_file_path):
        ctx.vlog(":: Reading nbdata file", nbdata_file_path)
        with open(nbdata_file_path, 'r', encoding='utf-8') as rf:
            nbdata_content = rf.read()

        __, metadata = extract_main_and_meta_from_file_content(
                                            ctx, nbdata_content)
    if not metadata:
        ctx.vlog(":: Extracting meta from file", ipynb_file_path)
        ipynb_data, metadata = extract_main_and_meta_from_ipynb(
                                                ctx, ipynb_data)

        if metadata and not delete_ipynb_meta in ['false', 'False']:
            ipynb_filename = os.path.basename(ipynb_file_path)
            topic = extract_topic(ctx, metadata)
            blog_post_dir = get_blog_post_dir(ctx, topic)
            new_ipynb_file_path = os.path.join(blog_post_dir, ipynb_filename)

            ctx.log(":: Deleting meta from ipynb_file", new_ipynb_file_path )
            with open(new_ipynb_file_path, 'w', encoding='utf-8') as wf:
                ipynb_dict = json.loads(ipynb_data)
                json.dump(ipynb_dict, wf, indent=2)
            ctx.written_ipynb = True

    return ipynb_data, metadata
Esempio n. 2
0
def write_html_and_md(ctx, html_body, md_file_path, meta):
    md_filename = os.path.basename(md_file_path)
    destination_dir = ctx.conversion['destination_dir']
    override_meta = ctx.conversion['override_meta']
    topic = extract_topic(ctx, meta)

    md_filename = os.path.join(topic, md_filename)
    html_filename = replace_ext(md_filename, '.html')
    html_file_path = os.path.join(destination_dir, html_filename)
    new_md_file_path = os.path.join(destination_dir, md_filename)
    new_blog_post_dir = os.path.dirname(html_file_path)
    ctx.vlog(":: New blog_posts_dir finalized", new_blog_post_dir)

    if not os.path.exists(new_blog_post_dir):
        os.mkdir(new_blog_post_dir)

    extract_static = ctx.conversion['extract_static']
    if extract_static:
        html_body = extract_and_write_static(ctx, html_body, new_blog_post_dir,
                                             md_filename)

    with open(html_file_path, 'w', encoding='utf8') as wf:
        wf.write(html_body)
        ctx.log(":: Converted basic html to", html_file_path)

    try:
        copyfile(md_file_path, new_md_file_path)
        ctx.log(":: Copied md file to", new_md_file_path)
    except SameFileError:
        os.remove(new_md_file_path)
        copyfile(md_file_path, new_md_file_path)
        ctx.log(":: Overwriting md file", new_md_file_path)

    return (html_filename, meta)
Esempio n. 3
0
def extract_main_and_meta(ctx, ipynb_file_path):
    blog = ctx.current_blog
    metadata = OrderedDict()
    delete_ipynb_meta = ctx.config.read(key=blog + ":delete_ipynb_meta")
    ctx.written_ipynb = False
    with open(ipynb_file_path, "r", encoding="utf-8") as rf:
        ipynb_data = rf.read()

    nbdata_file_path = replace_ext(ipynb_file_path, ".nbdata")
    if os.path.exists(nbdata_file_path):
        ctx.vlog(":: Reading nbdata file", nbdata_file_path)
        try:
            metadata = extract_meta_from_nbdata(nbdata_file_path)
        except Exception as E:
            ctx.log(":: Error reading nbdata file", nbdata_file_path, E)

    if not metadata:
        ctx.vlog(":: Extracting meta from file", ipynb_file_path)
        ipynb_data, metadata = extract_main_and_meta_from_ipynb(ctx, ipynb_data)

        # Rewrite only ipynb_data to ipynb file if delete metadata is true
        if metadata and not delete_ipynb_meta in ["false", "False"]:
            ipynb_filename = os.path.basename(ipynb_file_path)
            topic = extract_topic(ctx, metadata)
            blog_post_dir = ensure_and_get_blog_post_dir(ctx, topic)
            new_ipynb_file_path = os.path.join(blog_post_dir, ipynb_filename)

            ctx.log(":: Deleting meta from ipynb_file", new_ipynb_file_path)
            with open(new_ipynb_file_path, "w", encoding="utf-8") as wf:
                ipynb_dict = json.loads(ipynb_data)
                json.dump(ipynb_dict, wf, indent=2)
            ctx.written_ipynb = True

    metadata["_summary_"] = get_ipynb_summary(ctx, ipynb_data)
    return ipynb_data, metadata
Esempio n. 4
0
def write_html_and_ipynb(ctx, ipynb_file_path,  html_body, meta):
    blog = ctx.current_blog
    extract_static = ctx.conversion['extract_static']
    create_nbdata_file = ctx.config.read(key=blog + ':create_nbdata_file')
    topic = extract_topic(ctx, meta)
    ctx.log(":: Got topic,", topic)

    blog_post_dir = get_blog_post_dir(ctx, topic)
    ipynb_filename = os.path.basename(ipynb_file_path)
    html_filename = replace_ext(ipynb_filename, '.html')
    html_file_path = os.path.join(blog_post_dir, html_filename)
    new_ipynb_file_path = os.path.join(blog_post_dir, ipynb_filename)
    ctx.vlog(":: New blog_posts_dir finalized::", blog_post_dir)

    html_topic_filename = os.path.join(topic, html_filename)
    ipynb_topic_filename = replace_ext(html_topic_filename, '.ipynb')
    if not os.path.exists(blog_post_dir):
        os.mkdir(blog_post_dir)

    if extract_static:
        html_body = extract_and_write_static(ctx, html_body, blog_post_dir,
                                            ipynb_topic_filename)

    if meta and not create_nbdata_file in ['false', 'False']:
        create_nbdata_file_in_blog_dir(ctx, meta, new_ipynb_file_path)

    with open(html_file_path, 'w', encoding='utf8') as wf:
        wf.write(html_body)
        ctx.log(":: Converted basic html to", html_file_path)

    if (not ctx.written_ipynb) and (ipynb_file_path != new_ipynb_file_path):
        try:
            copyfile(ipynb_file_path, new_ipynb_file_path)
            ctx.log(":: Copied ipynb file to", new_ipynb_file_path)
        except SameFileError:
            os.remove(new_ipynb_file_path)
            copyfile(ipynb_file_path, new_ipynb_file_path)
            ctx.log(":: Overwriting ipynb file", new_ipynb_file_path)

    return (html_topic_filename, meta)
Esempio n. 5
0
def write_html_and_ipynb(ctx, ipynb_file_path, html_body, meta):
    blog = ctx.current_blog
    extract_static = ctx.conversion["extract_static"]
    create_nbdata_file = ctx.config.read(key=blog + ":create_nbdata_file")
    topic = extract_topic(ctx, meta)
    ctx.log(":: Got topic,", topic)

    blog_post_dir = ensure_and_get_blog_post_dir(ctx, topic)
    ipynb_filename = os.path.basename(ipynb_file_path)
    html_filename = replace_ext(ipynb_filename, ".html")
    html_file_path = os.path.join(blog_post_dir, html_filename)
    new_ipynb_file_path = os.path.join(blog_post_dir, ipynb_filename)
    ctx.vlog(":: New blog_posts_dir finalized::", blog_post_dir)

    html_topic_filename = os.path.join(topic, html_filename)
    ipynb_topic_filename = replace_ext(html_topic_filename, ".ipynb")
    if extract_static:
        html_body = extract_and_write_static(
            ctx, html_body, blog_post_dir, ipynb_topic_filename
        )

    if meta and not create_nbdata_file in ["false", "False"]:
        create_nbdata_file_in_blog_dir(ctx, meta, new_ipynb_file_path)

    with open(html_file_path, "w", encoding="utf8") as wf:
        wf.write(html_body)
        ctx.log(":: Converted basic html to", html_file_path)

    if (not ctx.written_ipynb) and (ipynb_file_path != new_ipynb_file_path):
        try:
            copyfile(ipynb_file_path, new_ipynb_file_path)
            ctx.log(":: Copied ipynb file to", new_ipynb_file_path)
        except Exception as E:
            os.remove(new_ipynb_file_path)
            copyfile(ipynb_file_path, new_ipynb_file_path)
            ctx.log(":: ERROR", E, "Overwriting ipynb file", new_ipynb_file_path)

    return (html_topic_filename, meta)
Esempio n. 6
0
def write_html_and_md(ctx, html_body, md_file_path, meta):
    md_filename = os.path.basename(md_file_path)
    destination_dir = ctx.conversion["destination_dir"]
    topic = extract_topic(ctx, meta)

    md_filename = os.path.join(topic, md_filename)
    html_filename = replace_ext(md_filename, ".html")
    html_file_path = os.path.join(destination_dir, html_filename)
    new_md_file_path = os.path.join(destination_dir, md_filename)
    new_blog_post_dir = os.path.dirname(html_file_path)
    ctx.vlog(":: New blog_posts_dir finalized", new_blog_post_dir)

    if not os.path.exists(new_blog_post_dir):
        os.mkdir(new_blog_post_dir)

    extract_static = ctx.conversion["extract_static"]
    if extract_static:
        html_body = extract_and_write_static(
            ctx, html_body, new_blog_post_dir, md_filename
        )

    with open(html_file_path, "w", encoding="utf8") as wf:
        wf.write(html_body)
        ctx.log(":: Converted basic html to", html_file_path)

    # skip copying md file if converting to and from same folder.
    if md_file_path != new_md_file_path:
        try:
            copyfile(md_file_path, new_md_file_path)
            ctx.log(":: Copied md file to", new_md_file_path)
        except Exception as E:
            os.remove(new_md_file_path)
            copyfile(md_file_path, new_md_file_path)
            ctx.log(":: ERROR", E, "Overwriting md file", new_md_file_path)

    return (html_filename, meta)