Rick Cogley's Tech Logr

Short Technical Laser Bursts %%

RC Logr 20190307 172838

Thursday, 7 Mar, 2019

Python program «remarshal» is a useful utility to convert between TOML, YAML and JSON. My friend Bjørn wrote this one-liner to use it to convert the frontmatter of content markdown files, from TOML to YAML, because github renders them nicely. Here is his solution:

1
 $> find content -name "*.md" | while read i; do YAML=`sed -n '/+++/,/+++/p' $i | sed '1d; $d' | remarshal -if toml -of yaml`; perl -pi -e "BEGIN{undef $/;} s/\+\+\+.*\+\+\+/---\n$YAML\n---/gs" $i; done

Run from the root of your Hugo project, this command finds any markdown files in the content folder ending in .md, then reads each line by line into a pipeline of commands. The YAML var is set, by extracting the lines between the +++ delimiters and using remarshal to convert that TOML to YAML. Then, perl is used to find the whole frontmatter section including the +++ lines, and replace that with the $YAML variable contents, between --- delimiters expected for YAML frontmatter. (Why doesn’t Github render TOML nicely, given its author?) 🤖

RC Logr 20190307 172838 - Python program «remarshal» is … Rick Cogley
Back to Home Tweet Link