The code behind my Jekyll-powered blog.

Rakefile.rb 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. require "rubygems"
  2. require "bundler/setup"
  3. require "stringex"
  4. ## -- Config -- ##
  5. public_dir = "public" # compiled site directory
  6. posts_dir = "_posts" # directory for blog files
  7. new_post_ext = "md" # default new post file extension when using the new_post task
  8. new_page_ext = "md" # default new page file extension when using the new_page task
  9. #############################
  10. # Create a new Post or Page #
  11. #############################
  12. # usage rake new_post
  13. desc "Create a new post in #{posts_dir}"
  14. task :new_post, :title do |t, args|
  15. if args.title
  16. title = args.title
  17. else
  18. title = get_stdin("Enter a title for your post: ")
  19. end
  20. filename = "#{posts_dir}/#{Time.now.strftime('%Y-%m-%d')}-#{title.to_url}.#{new_post_ext}"
  21. if File.exist?(filename)
  22. abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
  23. end
  24. tags = get_stdin("Enter tags to classify your post (comma separated): ")
  25. puts "Creating new post: #{filename}"
  26. open(filename, 'w') do |post|
  27. post.puts "---"
  28. post.puts "layout: post"
  29. post.puts "title: \"#{title.gsub(/&/,'&')}\""
  30. post.puts "modified: #{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}"
  31. post.puts "tags: [#{tags}]"
  32. post.puts "image:"
  33. post.puts " feature: "
  34. post.puts " credit: "
  35. post.puts " creditlink: "
  36. post.puts "comments: "
  37. post.puts "share: "
  38. post.puts "---"
  39. end
  40. end
  41. # usage rake new_page
  42. desc "Create a new page"
  43. task :new_page, :title do |t, args|
  44. if args.title
  45. title = args.title
  46. else
  47. title = get_stdin("Enter a title for your page: ")
  48. end
  49. filename = "#{title.to_url}.#{new_page_ext}"
  50. if File.exist?(filename)
  51. abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
  52. end
  53. tags = get_stdin("Enter tags to classify your page (comma separated): ")
  54. puts "Creating new page: #{filename}"
  55. open(filename, 'w') do |page|
  56. page.puts "---"
  57. page.puts "layout: page"
  58. page.puts "permalink: /#{title.to_url}/"
  59. page.puts "title: \"#{title}\""
  60. page.puts "modified: #{Time.now.strftime('%Y-%m-%d %H:%M')}"
  61. page.puts "tags: [#{tags}]"
  62. page.puts "image:"
  63. page.puts " feature: "
  64. page.puts " credit: "
  65. page.puts " creditlink: "
  66. page.puts "share: "
  67. page.puts "---"
  68. end
  69. end
  70. def get_stdin(message)
  71. print message
  72. STDIN.gets.chomp
  73. end
  74. def ask(message, valid_options)
  75. if valid_options
  76. answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer)
  77. else
  78. answer = get_stdin(message)
  79. end
  80. answer
  81. end