Skip to content

Instantly share code, notes, and snippets.

@robshep
Last active April 24, 2019 16:01
Show Gist options
  • Save robshep/a0bb2ae622aefde682beace9e42886f9 to your computer and use it in GitHub Desktop.
Save robshep/a0bb2ae622aefde682beace9e42886f9 to your computer and use it in GitHub Desktop.
Hugo Blog: Generate weekly blog content file
"""
Generate a year's worth of \"Week notes\" blog content files using ISO week format.
Usage: cd <hugo content sub-folder> && python3 <year>
E.g. $ cd ~/hugo/content/blog
$ python3 ~/hugo/scripts/gen_weeknotes.py 2019
Produces a folder for each week for publication on a Sunday:
20190106-weeknotes-2019W01 to 20191229-weeknotes-2019W52
Each with an index.md having suitable frontmatter: E.g. week 31 of 2019 looks like:
---
title: Week Notes - 2019 week 31
date: 2019-08-04
draft: true
slug: weeknotes-2019W31
categories: [week notes]
tags: [ ]
summary: Weekly dump of things of interest and happenings from the last week
resources:
---
## Jul 29th to Aug 4th
"""
import arrow
from isoweek import Week
import sys
from string import Template
import os
this_year = int(sys.argv[1])
week = Week(this_year, 1)
while week.year == this_year:
print(week.isoformat())
dt = arrow.get(week.sunday())
print(dt.format("YYYYMMDD"))
content_folder = dt.format("YYYYMMDD") + "-weeknotes-" + week.isoformat()
tpl = Template("""
---
title: Week Notes - $WEEK_YEAR week $WEEK_NUM
date: $WEEK_DATE_SUNDAY
draft: true
slug: weeknotes-$WEEK_ISOFORMAT
categories: [week notes]
tags: [ ]
summary: Weekly dump of things of interest and happenings from the last week
resources:
---
## $WEEK_DESC_MON to $WEEK_DESC_SUN
### Monday
### Tuesday
### Wednesday
### Thursday
### Friday
### Saturday
### Sunday
""")
content_index = tpl.substitute({
'WEEK_YEAR': week.year,
'WEEK_NUM': week.week,
'WEEK_DATE_SUNDAY': dt.format("YYYY-MM-DD"),
'WEEK_ISOFORMAT': week.isoformat(),
'WEEK_DESC_MON': arrow.get(week.monday()).format("MMM Do"),
'WEEK_DESC_SUN': arrow.get(week.sunday()).format("MMM Do")
})
# print(content_index)
os.makedirs("./" + content_folder)
file = open("./" + content_folder + "/index.md", 'w')
file.write(content_index)
file.close()
week = week + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment