#!/usr/bin/env ruby require "fileutils" Dir.glob("../../danbarber.me/_posts/*.md").each do |file| name = file.split("/").last.split(".")[0..-2].join FileUtils.mkdir_p("blog/#{name}") # Read the file and scan for images content = File.read(file) content.scan(/blog\/(.*\.(?:jpg|jpeg|png))/) do |image| image = image[0] if File.exists?("../../danbarber.me/_pictures/blog/#{image}") FileUtils.cp("../../danbarber.me/_pictures/blog/#{image}", "blog/#{name}") end end newcontent = content.gsub( /{% picture [a-z\-]+\s(?:\S+\/)+([^\/]+\.(?:jpg|jpeg|png))\s+(?:alt="([^"]*)"\s+)?%}/m, ) do |match| image, alt, caption = match.match(/{% picture [a-z\-]+\s(?:\S+\/)+([^\/]+\.(?:jpg|jpeg|png))\s+(?:alt="([^"]*)"\s+)?%}/m).captures <<~TAG {{< img src="#{image}" alt="#{alt}" caption="#{caption}" >}} TAG end File.open("blog/#{name}/index.md", "w") do |f| f.write(newcontent) end end