Class: Guard::Redcarpet

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Redcarpet.



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

def initialize(watchers = [], options = {})
  super(watchers, {
    :notifications => true
  }.merge(options))
  @watchers, @options = watchers, options
end

Instance Method Details

#compile_file(file) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/guard/redcarpet.rb', line 16

def compile_file(file)
  begin
    compile_markdown(File.new(file).read)
  rescue StandardError => e
    ::Guard::UI.info "Redcarpet File Error: #{e}"
  end
end

#compile_markdown(content) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/guard/redcarpet.rb', line 24

def compile_markdown(content)
  begin
    render = ::Redcarpet::Render::HTML.new(@options[:render_options] || {})
    markdown = ::Redcarpet::Markdown.new(render, (@options[:markdown_options] || {}))
    markdown.render content
  rescue StandardError => e
    ::Guard::UI.info "Redcarpet Error: #{e}"
  end
end

#get_output(file) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/guard/redcarpet.rb', line 34

def get_output(file)
  file_dir = File.dirname(file)
  file_dir = File.join(@options[:output], file_dir) if @options[:output]
  file_name = File.basename(file).split(".")[0..-2].join(".") # remove extension

  file_dir == '' ? file_name : File.join(file_dir, file_name)
end

#notify(changed_files) ⇒ Object



56
57
58
59
60
61
# File 'lib/guard/redcarpet.rb', line 56

def notify(changed_files)
  ::Guard.guards.reject{ |guard| guard == self }.each do |guard|
    paths = Watcher.match_files(guard, changed_files)
    guard.run_on_change paths unless paths.empty?
  end
end

#run_allObject



42
43
44
# File 'lib/guard/redcarpet.rb', line 42

def run_all
  run_on_change Watcher.match_files(self, Dir.glob(File.join("**", "*.*")))
end

#run_on_change(paths) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/guard/redcarpet.rb', line 46

def run_on_change(paths)
  paths.each do |file|
    output_file = get_output file
    FileUtils.mkdir_p File.dirname(output_file)
    File.open(output_file, 'w') { |f| f.write(compile_file(file)) }
    ::Guard::UI.info "# compiled markdown in '#{file}' to html in '#{output_file}'"
    ::Guard::Notifier.notify("# compiled markdown in #{file}", :title => "Guard::Redcarpet", :image => :success) if @options[:notifications]
  end
end