Class: JekyllTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/jekylltask.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :jekyll) {|_self| ... } ⇒ JekyllTask

:yield: self

Yields:

  • (_self)

Yield Parameters:

  • _self (JekyllTask)

    the object that the method was called on



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jekylltask.rb', line 21

def initialize(name=:jekyll)  # :yield: self
  @name = name
  @source = name
  @target = name
  yield self if block_given?
  task name, :auto, :needs=>[@source] do |task, args|
    generate args.auto
  end
  if @source != @target
    file @target=>FileList["#{@source}/**/*"] do
      generate
    end
    task 'clobber' do
      rm_rf @target
    end
  end
end

Instance Attribute Details

#sourceObject

Returns the value of attribute source.



39
40
41
# File 'lib/jekylltask.rb', line 39

def source
  @source
end

#targetObject

Returns the value of attribute target.



40
41
42
# File 'lib/jekylltask.rb', line 40

def target
  @target
end

Instance Method Details

#generate(auto = false) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/jekylltask.rb', line 42

def generate(auto = false)
  options = { 'source'=>source, 'destination'=>target }
  options = Jekyll.configuration(options)
  site = Jekyll::Site.new(options)

  if auto
    require 'directory_watcher'
    puts "Auto generating: just edit a page and save, watch the console to see when we're done regenerating pages"
    dw = DirectoryWatcher.new(source)
    dw.interval = 1
    dw.glob = Dir.chdir(source) do
      dirs = Dir['*'].select { |x| File.directory?(x) }
      dirs -= [target]
      dirs = dirs.map { |x| "#{x}/**/*" }
      dirs += ['*']
    end
    dw.start
    dw.add_observer do |*args|
      t = Time.now.strftime("%Y-%m-%d %H:%M:%S")
      puts "[#{t}] regeneration: #{args.size} files changed"
      site.process
      puts "Done"
    end
    loop { sleep 1 }
  else
    puts "Generating documentation in #{target}"
    site.process
    touch target
  end
end