New draft wrapper to write posts
This commit is contained in:
parent
75aee8e46c
commit
ad2f6f72c8
1 changed files with 55 additions and 0 deletions
55
dennogumi_new_post.py
Executable file
55
dennogumi_new_post.py
Executable file
|
@ -0,0 +1,55 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# SPDX-FileCopyrightText: 2021 Luca Beltrame <lbeltrame@kde.org>
|
||||||
|
# SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
from datetime import datetime
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
import frontmatter
|
||||||
|
import sarge
|
||||||
|
from slugify import slugify
|
||||||
|
|
||||||
|
POST_PATH = "content/post"
|
||||||
|
|
||||||
|
|
||||||
|
def new_post_title(title: str) -> str:
|
||||||
|
suffix = "md"
|
||||||
|
current_date = datetime.now().date().isoformat()
|
||||||
|
slugified_title = slugify(title)
|
||||||
|
post_title = f"{current_date}-{slugified_title}.{suffix}"
|
||||||
|
|
||||||
|
return post_title
|
||||||
|
|
||||||
|
|
||||||
|
def create_post(post_title: str, tags: List[str] = None,
|
||||||
|
categories: List[str] = None) -> bool:
|
||||||
|
cmd = ["hugo", "new", "-k", "post"]
|
||||||
|
title = new_post_title(post_title)
|
||||||
|
path = Path(POST_PATH) / title
|
||||||
|
cmd.append(str(path))
|
||||||
|
|
||||||
|
pid = sarge.run(cmd)
|
||||||
|
if pid.returncode != 0:
|
||||||
|
return False
|
||||||
|
|
||||||
|
header = frontmatter.load(path)
|
||||||
|
|
||||||
|
if categories is not None:
|
||||||
|
header["categories"] = categories
|
||||||
|
if tags is not None:
|
||||||
|
header["tags"] = tags
|
||||||
|
|
||||||
|
with path.open("w") as handle:
|
||||||
|
frontmatter.dump(header, handle)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Add table
Reference in a new issue