1
0
Fork 0

Add date and featured image

This commit is contained in:
Luca Beltrame 2021-08-28 22:29:18 +02:00
parent 200fbb12a6
commit 3cad10d133
Signed by: einar
GPG key ID: 4707F46E9EC72DEC

View file

@ -9,13 +9,18 @@ from subprocess import call
import shutil import shutil
from typing import List from typing import List
from atomicwrites import atomic_write, AtomicWriter from atomicwrites import atomic_write
import frontmatter import frontmatter
from git import Repo from git import Repo
import sarge import pytz
from slugify import slugify from slugify import slugify
import toml
POST_PATH = "content/post" POST_PATH = "content/post"
TEMPLATE_PATH = "archetypes/post.blank.md"
CONFIG = "config.toml"
TIMEZONE = pytz.timezone("Europe/Rome")
def new_post_title(title: str) -> str: def new_post_title(title: str) -> str:
@ -59,16 +64,14 @@ def create_post(post_title: str, tags: List[str] = None,
print("hugo executable not found in PATH.") print("hugo executable not found in PATH.")
return return
cmd = [hugo_cmd, "new", "-k", "posts", "--quiet"]
title = new_post_title(post_title) title = new_post_title(post_title)
path = Path(POST_PATH) / title path = Path(POST_PATH) / title
cmd.append(str(path)) shutil.copy(TEMPLATE_PATH, path)
print(" ".join(cmd))
pid = sarge.run(cmd) with open(CONFIG) as handle:
site_config = toml.load(handle)
if pid.returncode != 0: featured_image = site_config["params"]["featured_image"]
return
header = frontmatter.load(path) header = frontmatter.load(path)
@ -80,6 +83,10 @@ def create_post(post_title: str, tags: List[str] = None,
header["draft"] = draft header["draft"] = draft
header["comments"] = comments header["comments"] = comments
header["title"] = post_title header["title"] = post_title
header["featured_image"] = featured_image
date = datetime.now(tz=TIMEZONE)
# '2021-01-09 02:14:57+01:00'
header["date"] = date.isoformat(timespec="seconds", sep=" ")
frontmatter.dump(header, path) frontmatter.dump(header, path)