Class: Guard::Markdown

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Markdown.



10
11
12
13
14
15
16
17
18
# File 'lib/guard/markdown.rb', line 10

def initialize(watchers=[], options={})
  super              
  @options = {
    :convert_on_start => true,
    :dry_run          => false
  }.update(options)
  @kram_ops = { :input => "kramdown", :output => "html" }
  @kram_ops.update(@options[:kram_ops]) if @options[:kram_ops]
end

Instance Attribute Details

#kram_opsObject (readonly)

Your code goes here…



9
10
11
# File 'lib/guard/markdown.rb', line 9

def kram_ops
  @kram_ops
end

Instance Method Details

#run_allObject



25
26
27
28
29
# File 'lib/guard/markdown.rb', line 25

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

#run_on_change(paths) ⇒ Object

Called on file(s) modifications TODO - this method does far too much. Must refactor to allow

- for better testing


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
# File 'lib/guard/markdown.rb', line 34

def run_on_change(paths)
  paths.each do |path|
    input, output, template = path.split("|")
    info = "#{input} >> #{output}"
    info = "#{info} via #{template}" unless template.nil? 
    UI.info info 
    unless @options[:dry_run]
      source = File.open(input,"rb").read                        
                                        
      # make sure directory path exists
      reg = /(.+\/).+\.\w+/i
      target_path = output.gsub(reg,"\\1")
      FileUtils.mkpath target_path unless target_path.empty?
      
      @kram_ops.update({ :template => template }) unless template.nil?
      
      doc = Kramdown::Document.new(source, @kram_ops).to_html
               
    
      File.open(output, "w") do |f|
        f.write(doc)
      end
    end
  end
  true
end

#startObject



20
21
22
23
# File 'lib/guard/markdown.rb', line 20

def start
  UI.info("Guard::Markdown has started watching your files")
  run_all if @options[:convert_on_start]
end