Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fix posts metadata to migrate from Jekyll to Blogdown
#!/usr/bin/env python
import os
from pathlib import Path
import datetime
import frontmatter
posts_root = os.environ['HOME'] / Path('dev/brainblog/content/post')
for post in posts_root.iterdir():
fname_date = post.name[0:10] # capture the "2018-02-08" "timestamp" from the post filename
tstamp = datetime.datetime.strptime(fname_date, "%Y-%m-%d").timestamp()
utc_time = datetime.datetime.utcfromtimestamp(tstamp)
utc_string = utc_time.strftime("%Y-%m-%dT%H:%M:%S.%f+00:00")
with post.open('r') as f:
post_fm = frontmatter.load(f)
if not post_fm.get('date'):
post_fm.__setitem__('date', utc_string)
post_fm.__setitem__('modified', utc_string)
post_str = frontmatter.dumps(post_fm)
f.close()
with post.open('w') as f:
f.write(post_str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.