Last active
October 21, 2023 16:15
-
-
Save justin808/5550381 to your computer and use it in GitHub Desktop.
Rake tasks to rename Octopress posts to correspond to metadata, for org-mode as well. In Octopress, the meta is used for the blog post date.
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
org_posts_dir = "org_posts" # directory under source | |
# From http://www.ewal.net/2012/09/08/octopress-customizations/ | |
# Modified to have default flag of true to include drafts | |
desc "Rename files in the posts directory if the filename does not match the post date in the YAML front matter" | |
task :rename_posts do | |
rename_posts_in_dir "#{source_dir}/#{posts_dir}", "markdown" | |
# remove next line if you're you're not using org-mode | |
rename_posts_in_dir "#{source_dir}/#{org_posts_dir}", "org" | |
end | |
def rename_posts_in_dir dir, ext, include_drafts = true | |
Dir.chdir(dir) do | |
Dir["*.#{ext}"].each do |post| | |
post_date = "" | |
File.open( post ) do |f| | |
f.grep( /^date: / ) do |line| | |
post_date = line.gsub(/date: /, "").gsub(/\s.*$/, "") | |
break | |
end | |
end | |
post_title = post.to_s.gsub(/\d{4}-\d{2}-\d{2}/, "") # Get the post title from the currently processed post | |
new_post_name = post_date + post_title # determing the correct filename | |
rename = post != new_post_name | |
next unless rename | |
unless include_drafts | |
is_draft = false | |
File.open( post ) do |f| | |
f.grep( /^published: false/ ) do |line| | |
is_draft = true | |
break | |
end | |
end | |
next if is_draft | |
end | |
puts "renaming #{post} to #{new_post_name}" | |
FileUtils.mv(post, new_post_name) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment