Draft support
This commit is contained in:
parent
fd2c477173
commit
20e883cddf
1 changed files with 31 additions and 12 deletions
|
@ -35,11 +35,17 @@ def hash_file(filename: Path) -> str:
|
||||||
return file_hash.hexdigest()
|
return file_hash.hexdigest()
|
||||||
|
|
||||||
|
|
||||||
def perform_commit(filepath: Path, title: str=None) -> None:
|
def perform_commit(filepath: Path, title: str=None,
|
||||||
|
draft: bool=False) -> None:
|
||||||
|
|
||||||
repo = Repo("./") # FIXME
|
repo = Repo("./") # FIXME
|
||||||
repo.index.add(str(filepath))
|
repo.index.add(str(filepath))
|
||||||
message = "New post: {}".format(title)
|
|
||||||
|
if not draft:
|
||||||
|
message = "New post: {}".format(title)
|
||||||
|
else:
|
||||||
|
message = "New draft: {}".format(title)
|
||||||
|
|
||||||
message = fill(message, width=78)
|
message = fill(message, width=78)
|
||||||
|
|
||||||
repo.commit(message)
|
repo.commit(message)
|
||||||
|
@ -47,16 +53,21 @@ def perform_commit(filepath: Path, title: str=None) -> None:
|
||||||
|
|
||||||
|
|
||||||
def create_new_post(title: str, categories: list=None, tags: list=None,
|
def create_new_post(title: str, categories: list=None, tags: list=None,
|
||||||
post_metadata: dict=None, comments: bool=True) -> Path:
|
post_metadata: dict=None, comments: bool=True,
|
||||||
|
draft: bool=False) -> Path:
|
||||||
|
|
||||||
title_slug = slugify(title)
|
title_slug = slugify(title)
|
||||||
current_time = datetime.datetime.now(pytz.timezone("Europe/Rome"))
|
current_time = datetime.datetime.now(pytz.timezone("Europe/Rome"))
|
||||||
date_slug = current_time.date().isoformat()
|
date_slug = current_time.date().isoformat()
|
||||||
filename = "{0}-{1}.markdown".format(date_slug, title_slug)
|
|
||||||
formatted_date = current_time.strftime("%Y-%m-%d %H:%M:%S%z")
|
formatted_date = current_time.strftime("%Y-%m-%d %H:%M:%S%z")
|
||||||
|
|
||||||
metadata = dict(title=title, comments=comments,
|
metadata = dict(title=title, comments=comments, layout="page")
|
||||||
date=formatted_date)
|
|
||||||
|
if not draft:
|
||||||
|
metadata["date"] = formatted_date
|
||||||
|
filename = "{0}-{1}.markdown".format(date_slug, title_slug)
|
||||||
|
else:
|
||||||
|
filename = "{0}.markdown".format(title_slug)
|
||||||
|
|
||||||
if categories is not None:
|
if categories is not None:
|
||||||
metadata["categories"] = categories
|
metadata["categories"] = categories
|
||||||
|
@ -70,9 +81,12 @@ def create_new_post(title: str, categories: list=None, tags: list=None,
|
||||||
for key, value in post_metadata.items():
|
for key, value in post_metadata.items():
|
||||||
metadata[key] = value
|
metadata[key] = value
|
||||||
|
|
||||||
final_path = Path("_posts") / filename
|
if not draft:
|
||||||
|
final_path = Path("_posts") / filename
|
||||||
|
else:
|
||||||
|
final_path = Path("_drafts") / filename
|
||||||
|
|
||||||
with tempfile.NamedTemporaryFile() as temp:
|
with tempfile.NamedTemporaryFile(mode="wt") as temp:
|
||||||
|
|
||||||
temp_path = Path(temp.name)
|
temp_path = Path(temp.name)
|
||||||
|
|
||||||
|
@ -82,9 +96,10 @@ def create_new_post(title: str, categories: list=None, tags: list=None,
|
||||||
temp.flush()
|
temp.flush()
|
||||||
|
|
||||||
pre_edit_hash = hash_file(temp_path)
|
pre_edit_hash = hash_file(temp_path)
|
||||||
|
print("Save and close the editor after writing the post.")
|
||||||
subprocess.check_call("/usr/bin/kate {}".format(temp.name),
|
subprocess.check_call("/usr/bin/kate {}".format(temp.name),
|
||||||
shell=True)
|
shell=True)
|
||||||
post_edit_hash = hash_file(final_path)
|
post_edit_hash = hash_file(temp_path)
|
||||||
|
|
||||||
if pre_edit_hash == post_edit_hash:
|
if pre_edit_hash == post_edit_hash:
|
||||||
print("No post content. Aborting.")
|
print("No post content. Aborting.")
|
||||||
|
@ -106,6 +121,8 @@ def main():
|
||||||
help="Disable comments for the post")
|
help="Disable comments for the post")
|
||||||
parser.add_argument("--commit", action="store_true",
|
parser.add_argument("--commit", action="store_true",
|
||||||
help="Commit and push to the remote repository")
|
help="Commit and push to the remote repository")
|
||||||
|
parser.add_argument("--draft", action="store_true",
|
||||||
|
help="Create the post as draft")
|
||||||
parser.add_argument("title", help="Title of the new post")
|
parser.add_argument("title", help="Title of the new post")
|
||||||
|
|
||||||
options = parser.parse_args()
|
options = parser.parse_args()
|
||||||
|
@ -114,16 +131,18 @@ def main():
|
||||||
raise FileNotFoundError("Jekyll root not found.")
|
raise FileNotFoundError("Jekyll root not found.")
|
||||||
|
|
||||||
path = create_new_post(options.title, options.categories, options.tags,
|
path = create_new_post(options.title, options.categories, options.tags,
|
||||||
comments=options.disable_comments)
|
comments=options.disable_comments,
|
||||||
|
draft=options.draft)
|
||||||
|
|
||||||
if path is None:
|
if path is None:
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
print("Created new post {}".format(path.name))
|
print("Created new post {}.".format(path.name))
|
||||||
|
|
||||||
if options.commit:
|
if options.commit:
|
||||||
print("Committing to upstream repository...")
|
print("Committing to upstream repository...")
|
||||||
perform_commit(path, options.title)
|
perform_commit(path, options.title, options.draft)
|
||||||
|
|
||||||
print("Done.")
|
print("Done.")
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue