Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## CREATE JEKYLL POSTS FROM THE COMMAND LINE
- I got tired on creating new files manually for each new post a write so I put together this little command line task with Thor.
- It creates a new file in the _posts directory with today’s date, parses the parameters to command as the post’s title and adds that as a slug to the new file. It then writes a default yaml template to the file (as specified in the script).
- Running `thor jekyll:new New and shiny post` will for example create the file `_posts/2012-12-28-new-and-shiny-post.markdown`, populate it with an yaml template and finally open the file in my favorite editor.
- #### HOW TO
- Add the following to your Gemfile:
- ```ruby
- gem 'thor'
- gem 'stringex'
- ```
- Run `bundle install` and create a `jekyll.thor` file with the following contents:
- ```ruby
- require "stringex"
- class Jekyll < Thor
- desc "new", "create a new post"
- method_option :editor, :default => "subl"
- def new(*title)
- title = title.join(" ")
- date = Time.now.strftime('%Y-%m-%d')
- filename = "_posts/#{date}-#{title.to_url}.markdown"
- if File.exist?(filename)
- abort("#{filename} already exists!")
- end
- puts "Creating new post: #{filename}"
- open(filename, 'w') do |post|
- post.puts "---"
- post.puts "layout: post"
- post.puts "title: \"#{title.gsub(/&/,'&')}\""
- post.puts "tags:"
- post.puts " -"
- post.puts "---"
- end
- system(options[:editor], filename)
- end
- end
- ```
- Use the new command:
- ```console
- $ thor jekyll:new The title of the new post
- ```
- You can even specify which editor to open the new file with:
- ```console
- $ thor jekyll:new The title of the new post --editor=vim
- ```
- The default editor is Sublime Text 2, just change on line 4 in `jekyll.thor` if an other editor is preferred.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement