Module: Jekyll::Watcher
Instance Method Summary collapse
- #build_listener(options) ⇒ Object
- #listen_handler(options) ⇒ Object
-
#listen_ignore_paths(options) ⇒ Object
Paths to ignore for the watch option.
- #watch(options) ⇒ Object
Instance Method Details
#build_listener(options) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/jekyll/watcher.rb', line 24 def build_listener() require 'listen' Listen.to( ['source'], :ignore => listen_ignore_paths(), :force_polling => ['force_polling'], &(listen_handler()) ) end |
#listen_handler(options) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/jekyll/watcher.rb', line 34 def listen_handler() proc { |modified, added, removed| t = Time.now.strftime("%Y-%m-%d %H:%M:%S") c = modified + added + removed n = c.length print Jekyll.logger.("Regenerating:", "#{n} files at #{t} ") begin Jekyll::Command.process_site(Jekyll::Site.new()) puts "...done." rescue => e puts "...error:" Jekyll.logger.warn "Error:", e. Jekyll.logger.warn "Error:", "Run jekyll build --trace for more information." end } end |
#listen_ignore_paths(options) ⇒ Object
Paths to ignore for the watch option
options - A Hash of options passed to the command
Returns a list of relative paths from source that should be ignored
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/jekyll/watcher.rb', line 56 def listen_ignore_paths() source = ['source'] destination = ['destination'] config_files = Jekyll.sanitized_path(source, "_config.yml") paths = ( Array(config_files) \ + Array(destination) \ + Array(['exclude']).map { |e| Jekyll.sanitized_path(source, e) } ) ignored = [] source_abs = Pathname.new(source). paths.each do |p| path_abs = Pathname.new(p). begin rel_path = path_abs.relative_path_from(source_abs).to_s ignored << Regexp.new(Regexp.escape(rel_path)) unless rel_path.start_with?('../') rescue ArgumentError # Could not find a relative path end end Jekyll.logger.debug "Watcher:", "Ignoring #{ignored.map(&:inspect).join(', ')}" ignored end |
#watch(options) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/jekyll/watcher.rb', line 5 def watch() listener = build_listener() listener.start Jekyll.logger.info "Auto-regeneration:", "enabled for '#{['source']}'" unless ['serving'] trap("INT") do listener.stop puts " Halting auto-regeneration." exit 0 end loop { sleep 1000 } end rescue ThreadError => e # You pressed Ctrl-C, oh my! end |