Sha256: 5e4a72453a67295289beef8ce9f30a93f1c17e0211cfd05012300a7faf7bea8f

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

require 'listen'

module Softcover::Commands::Server
  include Softcover::Output
  include Softcover::Utils
  attr_accessor :no_listener
  extend self

  # Listens for changes to the book's source files.
  def listen_for_changes
    return if defined?(@no_listener) && @no_listener
    server_pid = Process.pid
    directories = ['.', 'chapters']
    @listener = Listen.to(*directories)
    @listener.filter(/(\.tex|\.md|custom\.sty)$/)
    @listener.change do |modified|
      rebuild modified.try(:first)
      Process.kill("HUP", server_pid)
    end
    @listener.start
  end

  def rebuild(modified=nil)
    printf modified ? "=> #{File.basename modified} changed, rebuilding... " :
                      'Building...'
    t = Time.now
    Softcover::Builders::Html.new.build
    puts "Done. (#{(Time.now - t).round(2)}s)"

  rescue Softcover::BookManifest::NotFound => e
    puts e.message
  end

  def start_server(port)
    require 'softcover/server/app'
    rebuild
    puts "Running Softcover server on http://localhost:#{port}"
    Softcover::App.set :port, port
    Softcover::App.run!
  end

  def run(port)
    listen_for_changes
    start_server port
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
softcover-0.5.0 lib/softcover/commands/server.rb