Skip to content

Instantly share code, notes, and snippets.

@caleorourke
Created March 7, 2014 20:19
Show Gist options
  • Save caleorourke/9419203 to your computer and use it in GitHub Desktop.
Save caleorourke/9419203 to your computer and use it in GitHub Desktop.
Porting a Rakefile for creating markdown (.md) pages with canned front-matter to Grunt's mkdirp plug-in
require "rubygems"
require "rake"
require "safe_yaml"
SOURCE = "."
# Creates a new markdown (.md) page in the root with predefined front-matter
# Spaces, duplicate names, and file extensions other than .md are not supported
# Run: $ rake new page=about
desc "Create a new page"
task :new do
name = ENV["page"] || "new-page.md"
filename = File.join(SOURCE, "#{name}\.md")
title = File.basename(filename, File.extname(filename)).gsub(/\b\w/){$&.capitalize}
tag = File.basename(filename, File.extname(filename)).gsub(/\b\w/){$&.downcase}
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ["y", "n"]) == "n"
end
mkdir_p File.dirname(filename)
puts "Creating new page: #{filename}"
open(filename, "w") do |post|
post.puts "---"
post.puts "layout: default"
post.puts "title: #{title}"
post.puts "intro: ""Add a short intro to describe this page"
post.puts "tag: #{tag}"
post.puts "baseurl: \"../\""
post.puts "---"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment