Class: Guard::Toc

Inherits:
Guard
  • Object
show all
Defined in:
lib/guard/toc.rb

Instance Method Summary collapse

Constructor Details

#initialize(watchers = [], options = {}) ⇒ Toc

Returns a new instance of Toc.



9
10
11
12
13
14
15
# File 'lib/guard/toc.rb', line 9

def initialize(watchers=[], options={})
  super              
  @options = {
    :convert_on_start => true,
    :dry_run          => false
  }.update(options)
end

Instance Method Details

#run_allObject



22
23
24
25
26
# File 'lib/guard/toc.rb', line 22

def run_all
  files = Dir.glob("**/*.*")
  targets = Watcher.match_files(self, files)
  run_on_change targets
end

#run_on_change(paths) ⇒ Object



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/guard/toc.rb', line 28

def run_on_change(paths)
  paths.uniq.each do |path|
    input, output, outfile, template = path.split("|")
    info = "Created '#{output}/#{outfile}' from the directory '#{input}'"
    info = "#{info} via '#{template}'" unless template.nil? 
    UI.info info 
    unless @options[:dry_run]
      # make sure directory path exists
      reg = /(.+\/).+\.\w+/i
      target_path = output.gsub(reg,"\\1")
      FileUtils.mkpath target_path unless target_path.empty?

      full_file_list = Dir.entries(input)
      
      # Trim "." and ".." off of the file list
      file_list = full_file_list[2,full_file_list.length-2]
      
      # Don't list the index.html file (or whatever it is named) in itself
      if input==output
        file_list = file_list - ["#{outfile}"]
      end
      
      source = "<ul class='toc_links'>\n"
      if file_list.length > 0
        file_list.each do |file|
          file_name = file.gsub(/.html/ix,'').gsub(/_/ix,' ').capitalize
          source += "      <li><a href='#{output}/#{file}'>#{file_name}</a></li>\n"
        end
      end
      source += "    </ul>"
                                        
      kram_ops = { :input => "kramdown", :output => "html" }
      kram_ops.update({ :template => template }) unless template.nil?
      
      doc = Kramdown::Document.new(source, kram_ops).to_html
    
      File.open("#{output}/#{outfile}", "w") do |f|
        f.write(doc)
      end
    end
  end
  true
end

#startObject



17
18
19
20
# File 'lib/guard/toc.rb', line 17

def start
  UI.info("Guard::Toc is building a list of links\n")
  run_all if @options[:convert_on_start]
end