Created
August 29, 2021 20:03
-
-
Save fuzyll/bda6685eba524bc80484e06a3ce163ec to your computer and use it in GitHub Desktop.
Short Ruby script to convert blog posts with Jekyll frontmatter into blog posts with Zola frontmatter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "yaml" | |
# | |
# We want to convert this: | |
# | |
#--- | |
#layout: post | |
#published: true | |
#title: "Fabric Art History" | |
#date: 2017-07-10 | |
#categories: [self] | |
#tags: [fabric] | |
#--- | |
# | |
# Into this: | |
# | |
#+++ | |
#title = "Fabric Art History" | |
#date = 2017-07-10 | |
# | |
#[taxonomies] | |
#categories = ["self"] | |
#tags = ["fabric"] | |
#+++ | |
# | |
data = nil | |
File.open(ARGV[0], "r") do |f| | |
# read in data from file | |
data = f.read().split("\n") | |
end | |
# find the start and end of the jekyll frontmatter | |
fm_start = -1 | |
fm_end = -1 | |
data.each_with_index() do |line, i| | |
if line.start_with?("---") and fm_start == -1 | |
fm_start = i | |
elsif line.start_with?("---") | |
fm_end = i | |
break | |
end | |
end | |
# bail if we couldn't find it | |
if fm_start == -1 or fm_end == -1 | |
abort "Could not find start and end of Jekyll frontmatter!" | |
end | |
# load the jekyll frontmatter | |
jfm = YAML.load_file(ARGV[0]) | |
# build the tera frontmatter | |
tfm = "+++\n" | |
tfm += "title = \"#{jfm["title"]}\"\n" | |
tfm += "date = #{jfm["date"]}\n" | |
tfm += "\n[taxonomies]\n" | |
tfm += "categories = #{jfm["categories"]}\n" | |
tfm += "tags = #{jfm["tags"]}\n" | |
tfm += "+++\n\n" | |
File.open(ARGV[0], "w") do |f| | |
f.write(tfm + data[fm_end+1..-1].join("\n")) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment