Class: Nopoint::Commands::Build

Inherits:
Nopoint::Command show all
Defined in:
lib/nopoint/commands/build.rb

Class Method Summary collapse

Class Method Details

.build(options) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
# File 'lib/nopoint/commands/build.rb', line 13

def self.build(options)
				config = YAML.safe_load_file('config.yml')
  template = config['template'] # Error handling

				chapters = []
				source = []
  destination = 'public'
  
				config['chapters'].each_with_index do |chapter, index|
    name = File.join(Dir.pwd, "slides/#{chapter}")
					chapters.push Nopoint::Chapter.new(index, name)
				end
  
  # Cleanup and rebuild the Public folder
  if File.directory? destination
    FileUtils.rm_rf(Dir.glob("#{destination}/*"))
  else
    FileUtils.mkdir(destination)
  end
  
  # Insert vendor template
  FileUtils.mkdir destination + '/assets'
  FileUtils.mkdir destination + '/slides'
  FileUtils.mkdir destination + '/images'
  
			  FileUtils.cp 'vendor/index.html', destination
  FileUtils.cp Dir.glob('vendor/*.js'), destination + '/assets'
  FileUtils.cp_r Dir.glob('images/*'), destination + '/images' if File.directory? 'images'
  FileUtils.cp Dir.glob("templates/#{template}/*.{css,js}"), destination + '/assets'
  
  # Build Stitic slides
  @slide_index = 0
  @slide_max = chapters.inject(0) {|sum, ch| sum + ch.slides.count}
  @chapter_max = chapters.count
  
  chapters.each_with_index do |chapter, index|
    @chapter_index = index
    
    if chapter.data.has_key? 'title'
    	@title = chapter.data['title']
    else
      @title = ''
    end
    
    chapter.slides.each do |slide|
  path = "public/slides/#{@slide_index}.html"
  file = File.new(path, 'w')
  # slides.count - 1
  @content = slide.content
 erb = ERB.new(File.read("templates/#{template}/slide.erb"))
      
 file.puts erb.result(binding)
 file.close
      
      @slide_index += 1
end
  end
end

.process(options) ⇒ Object



7
8
9
10
11
# File 'lib/nopoint/commands/build.rb', line 7

def self.process(options)
  puts 'Let’s go!'
  self.build(options)
  self.watch(options) if options.watch
end

.watch(options) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/nopoint/commands/build.rb', line 72

def self.watch(options)
  require 'directory_watcher'

  source = File.join(Dir.pwd, 'slides/')

  glob = './{slides/*, templates/*, config.yml, images/*}'
  
  puts "watching #{source} for changes"
  
  dw = DirectoryWatcher.new(Dir.pwd, :glob => glob, :pre_load => true)
  dw.interval = 1

  dw.add_observer do |*args|
    t = Time.now.strftime("%Y-%m-%d %H:%M:%S")
    print "Regenerating: #{args.size} files at #{t}"
    self.build(options)
    puts  "...done."
  end

  dw.start

  unless options.serving
    trap("INT") do
      puts "     Halting auto-regeneration."
      exit 0
    end
    loop { sleep 1000 }
  end
end