Rakefile 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. require "bundler/gem_tasks"
  2. require "jekyll"
  3. require "listen"
  4. def listen_ignore_paths(base, options)
  5. [
  6. /_config\.ya?ml/,
  7. /_site/,
  8. /\.jekyll-metadata/
  9. ]
  10. end
  11. def listen_handler(base, options)
  12. site = Jekyll::Site.new(options)
  13. Jekyll::Command.process_site(site)
  14. proc do |modified, added, removed|
  15. t = Time.now
  16. c = modified + added + removed
  17. n = c.length
  18. relative_paths = c.map{ |p| Pathname.new(p).relative_path_from(base).to_s }
  19. print Jekyll.logger.message("Regenerating:", "#{relative_paths.join(", ")} changed... ")
  20. begin
  21. Jekyll::Command.process_site(site)
  22. puts "regenerated in #{Time.now - t} seconds."
  23. rescue => e
  24. puts "error:"
  25. Jekyll.logger.warn "Error:", e.message
  26. Jekyll.logger.warn "Error:", "Run jekyll build --trace for more information."
  27. end
  28. end
  29. end
  30. task :preview do
  31. base = Pathname.new('.').expand_path
  32. options = {
  33. "source" => base.join('test').to_s,
  34. "destination" => base.join('test/_site').to_s,
  35. "force_polling" => false,
  36. "serving" => true,
  37. "theme" => "minimal-mistakes-jekyll"
  38. }
  39. options = Jekyll.configuration(options)
  40. ENV["LISTEN_GEM_DEBUGGING"] = "1"
  41. listener = Listen.to(
  42. base.join("_data"),
  43. base.join("_includes"),
  44. base.join("_layouts"),
  45. base.join("_sass"),
  46. base.join("assets"),
  47. options["source"],
  48. :ignore => listen_ignore_paths(base, options),
  49. :force_polling => options['force_polling'],
  50. &(listen_handler(base, options))
  51. )
  52. begin
  53. listener.start
  54. Jekyll.logger.info "Auto-regeneration:", "enabled for '#{options["source"]}'"
  55. unless options['serving']
  56. trap("INT") do
  57. listener.stop
  58. puts " Halting auto-regeneration."
  59. exit 0
  60. end
  61. loop { sleep 1000 }
  62. end
  63. rescue ThreadError
  64. # You pressed Ctrl-C, oh my!
  65. end
  66. Jekyll::Commands::Serve.process(options)
  67. end