Created
July 26, 2013 10:55
-
-
Save starmer/6088014 to your computer and use it in GitHub Desktop.
Rake task to create a new post for a jekyll blog
This file contains hidden or 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
| task :new, :title do |t, args| | |
| if args.to_hash.empty? | |
| puts 'usage: rake new["New Post Title"]' | |
| next | |
| end | |
| title = args.title | |
| slug = title.downcase.gsub(/\s/, '-') | |
| file_name = "_posts/" + Time.new().strftime("%Y-%m-%d") + "-#{slug}.md" | |
| if Pathname.new(file_name).exist? | |
| puts "Error: post already exsists" | |
| next | |
| end | |
| file_content = <<-eos | |
| --- | |
| layout: post | |
| title: "#{title}" | |
| date: #{Time.new().strftime("%Y-%m-%d %H:%M:%S")} | |
| slug: #{slug} | |
| --- | |
| eos | |
| post = File.open(file_name, "w") do |f| | |
| f.write(file_content) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment