Sha256: 2c1b7e53de825cea040cdaeb1f0c48041056c0703d2b5e40ce37a8a7b0e86ab8

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require 'listen'

module Marksman
  class Watcher

    def initialize(file, output_directory, theme_name = nil)
      @file = Pathname.new file
      @output_directory = output_directory
      @theme_name = theme_name
    end

    def watch
      update(true)
      listen
    end

    def update(start_listener = false)
      puts 'Updating files...'
      presentation = Marksman::Writer.new(@file, @output_directory, @theme_name).generate
      start_theme_listener(presentation.theme) unless @theme_listener
    end

    def listen
      puts 'Listening to changes...'
      @markdown_listener = Listen.to(Pathname.new(@file).dirname) do |modified, added, removed|
        if modified.include? @file.realpath.to_s
          update
        end
      end
      @markdown_listener.start
      sleep
    end

    def start_theme_listener(theme)
      @theme_listener = Listen.to(theme.path) do |modified, added, removed|
        update
      end
      @theme_listener.start
    end

    def stop
      @markdown_listener.stop
      @theme_listener.stop if @theme_listener
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
marksman-0.1 lib/marksman/watcher.rb