mirror of
https://github.com/danbee/danbarber.me.hugo.git
synced 2025-03-04 08:59:18 +00:00
41 lines
1.1 KiB
Ruby
Executable File
41 lines
1.1 KiB
Ruby
Executable File
#!/usr/bin/env ruby
|
|
|
|
require "fileutils"
|
|
|
|
Dir.glob("posts/*.md").each do |file|
|
|
name = file.split("/").last.split(".")[0..-2].join
|
|
|
|
parts = name.split("-")
|
|
date_parts = parts.shift(3)
|
|
|
|
dir = date_parts.join("/").concat("/", parts.join("-"))
|
|
|
|
FileUtils.mkdir_p(dir)
|
|
|
|
FileUtils.mv(file, "#{dir}/index.md")
|
|
end
|
|
|
|
Dir.glob("**/index.md").each do |file|
|
|
dir = file.split("/")[0..-2].join("/")
|
|
content = File.read(file)
|
|
content.match(/blog\/(.*\.(jpg|jpeg|png))/) do |image|
|
|
if File.exists?("pictures/#{image}")
|
|
FileUtils.mv("pictures/#{image}", dir)
|
|
end
|
|
end
|
|
|
|
newcontent = content.gsub(
|
|
/(?:<figure[^\<\>]*>)?\s+{% picture [a-z-]+\s\S+\/([^\/]+\.(?:jpg|jpeg|png))\s+(?:alt="([^"]*)")?\s+%}\s+(?:<figcaption>([^<>]+)<\/figcaption>\s+)?(?:<\/figure>)?/m,
|
|
) do |match|
|
|
image, alt, caption = match.match(/(?:<figure[^\<\>]*>)?\s+{% picture [a-z-]+\s\S+\/([^\/]+\.(?:jpg|jpeg|png))\s+(?:alt="([^"]*)")?\s+%}\s+(?:<figcaption>([^<>]+)<\/figcaption>\s+)?(?:<\/figure>)?/m).captures
|
|
|
|
<<~TAG
|
|
{{< img src="#{image}" alt="#{alt}" caption="#{caption}" >}}
|
|
TAG
|
|
end
|
|
|
|
File.open(file, "w") do |f|
|
|
f.write(newcontent)
|
|
end
|
|
end
|