Sha256: deb7b7c9bcadbae5985cd9c11b5897f19e9cdac4fe8357c90786afe567fd4f10

Contents?: true

Size: 1.31 KB

Versions: 5

Compression:

Stored size: 1.31 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)
    file_pattern = markdown? ? '\.md|\.txt' : '\.tex'
    @listener.filter(/(#{file_pattern}|custom\.sty)$/)

    @listener.change do |modified|
      rebuild modified.try(:first)
      Process.kill("HUP", server_pid)
    end
    @listener.start
  end

  def markdown?
    !Dir.glob(path('chapters/*.md')).empty?
  end

  def rebuild(modified=nil)
    printf modified ? "=> #{File.basename modified} changed, rebuilding... " :
                      'Building...'
    t = Time.now
    builder = Softcover::Builders::Html.new
    builder.build(preserve_tex: true)
    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

5 entries across 5 versions & 1 rubygems

Version Path
softcover-0.6.6 lib/softcover/commands/server.rb
softcover-0.6.5 lib/softcover/commands/server.rb
softcover-0.6.4 lib/softcover/commands/server.rb
softcover-0.6.3 lib/softcover/commands/server.rb
softcover-0.6.2 lib/softcover/commands/server.rb