Caspershire Meta (csmeta
) has been running for almost 10 years now since July 2014.
It went through several static content generators (SCGs), the latest one being Hugo.
Now, it is time for another migration. I decided to retire csmeta
, replacing it with Caspershire Aurora (auroracs
, located at aurora.caspershire.net
).
Eleventy has been the default option for new simple projects because I found out that I spent less time setting it up compared to Hugo.
For a slightly complicated project, I would choose AstroJS[1] as it gives a much more superior control at the expense of longer dev time.
Initially, I planned to migrate contents from the hugo
-based SCGs to eleventy
, but the plan changed and I wished to start anew.
When the migration plan was formulated, I thought of amending the frontmatter in all of the *.md
markdown files.
It used to look like this:
---
date: 2023-08-19T17:40:00-04:00
title: "Plotting for Fragment Length from Nanopore FASTQ Data"
url: checking-for-fragment-length
description: "a comparison of speed between programs written in Python vs. Golang"
---
... and now I want it to look like this:
---
date: 2023-08-19T17:40:00-04:00
title: "Plotting for Fragment Length from Nanopore FASTQ Data"
tags: "post"
permalink: "checking-for-fragment-length/"
---
Three fields needed modification:
url
is nowpermalink
, with a compulsory trailing slash/
and quotes pair".."
.description
field is removed.tags
field is added.
I could use tools like sed
and yq
to automate this process on 144 markdown *.md
files, but I decided to write a python script.
At first I thought it would be simple.
It was not, and the process took me roughly an hour to get the script working.
Why? RegEx and the behavior or Python's fileinput
.
Inside the src/modify.py
:
import glob
import fileinput
import re
def modify() -> None:
for f in glob.glob("*.md"):
with fileinput.input(f, inplace=True, backup=".bak") as f:
for line in f:
modify_permalink(line)
for f in glob.glob("*.md"):
with fileinput.input(f, inplace=True, backup=".bak") as f:
for line in f:
add_tag(line)
for f in glob.glob("*.md"):
with fileinput.input(f, inplace=True, backup=".bak") as f:
for line in f:
remove_description(line)
def modify_permalink(l: str):
"""
Modify from
url: post-slug
To
permalink: 'post-slug/'
"""
url = re.search(r"^url: (.*)", l.strip())
if url:
change_link = "permalink: " + "\"" + url.group(1) + "/\""
print(change_link)
else:
print(l, end="")
def add_tag(l: str):
"""
Adding the 'tags' field underneath permalink
"""
tag_location = re.search(r"^permalink: (.*)", l.strip())
if tag_location:
tag = tag_location.group(0) + "\ntags: \"posts\""
print(tag)
else:
print(l, end="")
def remove_description(l: str):
l = re.sub(r"^description: (.*)", "", l.strip())
print(l)
I stopped it halfway because there would be cases where special handling(s) might be required. It would not be fun checking on them one-by-one, especially when there are more than 100 posts to go over.
Caspershire Main (caspershire.net) runs on AstroJS. I was sold to the idea of an extensible markdown format, the MDX. ↩︎
Published on 2024/Sep/10 // Aizan Fahri
aixnr[at]outlook[dot]my