示例#1
0
def new_post(title, tag, category, edit, image):
    """Creates an empty markdown post with the given title for Jekyll in the 
    directory specified by the configs. It will include a front-matter
    with the default options and optional tags or categories.
    """

    if image:
        contents = _get_contents_img()
    else:
        contents = _get_contents_no_img()

    slug = slugify(title)
    date = datetime.now()
    date_str = date.strftime("%Y-%m-%d %H:%M:%S %z")
    tags = ",".join(list(map(lambda el: '"' + str(el).strip() + '"', tag)))
    categories = ",".join(
            list(map(lambda el: '"' + str(el).strip() + '"', category)))

    file_name = date.strftime("%Y-%m-%d") + "-" + slug + ".markdown"

    path_to_file = get_path_to_posts_dir().rstrip("/") + "/" + file_name

    with open(path_to_file, "w") as f:
        f.write(textwrap
                .dedent(contents)
                .lstrip()
                .format(title, date_str, tags, categories))

    if edit:
        editor = get_editor_name()
        editor_executable = get_executable_from_name(editor)
        call([editor_executable, path_to_file])
        time.sleep(2)  # just to give the os time for the editor to load

    click.echo(wrap_success("Post created at: {0}".format(path_to_file)))
示例#2
0
def edit_post(keywords):
    if len(keywords) == 0:
        raise click.UsageError("Please supply at least one keyword as argument")

    path_to_posts_directory = resolve_path(get_path_to_posts_dir())
    post_files = list_files(path_to_posts_directory, keywords)

    if len(post_files) == 0:
        raise click.UsageError("No files match the given keywords")
    elif len(post_files) > 1:

        exception = click.UsageError("Multiple files match the given keywords")
        exception.show()

        for file in post_files[:10]:
            click.echo(file)

        raise exception

    else:
        path_to_file = path_to_posts_directory + "/" + post_files[0]
        editor = get_editor_name()
        editor_executable = get_executable_from_name(editor)
        call([editor_executable, path_to_file])
        time.sleep(2)  # just to give the os time for the editor to load