require "set"
module Jekyll
# REF: https://github.com/rpalo/personal-blog
# Syncs the catpages with the cats in all of my posts
Jekyll::Hooks.register :site, :post_read do |site|
cats = Set.new(site.categories.keys)
catpage_filenames = Dir.entries("./cats/")
.grep(/\.md/)
.map do |filename|
filename.chomp(".md")
end
catpages = Set.new(catpage_filenames)
cats_without_pages = cats - catpages
pages_without_cats = catpages - cats
puts "==== Create a catpage for any cat without a page.===="
# Create a catpage for any cat without a page
cats_without_pages.each do |cat|
puts "Creating new catpage for: #{cat}"
content = <<~HEREDOC
---
layout: catpage
cat: #{cat}
permalink: /cats/#{cat}/
---
HEREDOC
File.open("cats/#{cat}.md", "w") do |f|
f.write(content)
end
end
# Delete the catpage for any page that doesn't have any cats
pages_without_cats.each do |page|
puts "Removing catpage for nonexistent cat: #{page}. Ignore any Ruby errors reported."
File.delete("src/cats/#{page}.md")
end
end
end