Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. ## CREATE JEKYLL POSTS FROM THE COMMAND LINE
  2.  
  3. 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.
  4.  
  5. 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).
  6.  
  7. 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.
  8.  
  9. #### HOW TO
  10. Add the following to your Gemfile:
  11. ```ruby
  12. gem 'thor'
  13. gem 'stringex'
  14. ```
  15. Run `bundle install` and create a `jekyll.thor` file with the following contents:
  16. ```ruby
  17. require "stringex"
  18. class Jekyll < Thor
  19. desc "new", "create a new post"
  20. method_option :editor, :default => "subl"
  21. def new(*title)
  22. title = title.join(" ")
  23. date = Time.now.strftime('%Y-%m-%d')
  24. filename = "_posts/#{date}-#{title.to_url}.markdown"
  25.  
  26. if File.exist?(filename)
  27. abort("#{filename} already exists!")
  28. end
  29.  
  30. puts "Creating new post: #{filename}"
  31. open(filename, 'w') do |post|
  32. post.puts "---"
  33. post.puts "layout: post"
  34. post.puts "title: \"#{title.gsub(/&/,'&')}\""
  35. post.puts "tags:"
  36. post.puts " -"
  37. post.puts "---"
  38. end
  39.  
  40. system(options[:editor], filename)
  41. end
  42. end
  43. ```
  44. Use the new command:
  45. ```console
  46. $ thor jekyll:new The title of the new post
  47. ```
  48. You can even specify which editor to open the new file with:
  49. ```console
  50. $ thor jekyll:new The title of the new post --editor=vim
  51. ```
  52. 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