Exemplo n.º 1
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)
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
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)