Sha256: 39e37e158ec5897828cc3d1e6b6c33268138c40b431639194890fe89642f8a2f

Contents?: true

Size: 1.77 KB

Versions: 76

Compression:

Stored size: 1.77 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
    filter_regex = /(\.md|\.tex|custom\.sty|custom\.css|Book\.txt|book\.yml)$/
    @listener = Listen.to('.')
    @listener.filter(filter_regex)

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

  # Returns a regex for files to be ignored by the listener.
  def ignore_regex
    ignores = ['generated_polytex', '\.tmp\.tex']
    # Ignore <book>.tex, which gets overwritten each time PolyTeXnic runs,
    # unless there's no Book.txt, which means the author is using raw LaTeX.
    if File.exist?(Softcover::BookManifest::TXT_PATH)
      ignores << Regexp.escape(Dir.glob('*.tex').first)
    end
    /(#{ignores.join('|')})/
  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
    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'
    puts "Running Softcover server on http://localhost:#{port}"
    Softcover::App.set :port, port
    Softcover::App.run!
  end

  def run(port)
    rebuild
    listen_for_changes
    start_server port
  end
end

Version data entries

76 entries across 76 versions & 2 rubygems

Version Path
softcover-1.1.17 lib/softcover/commands/server.rb
softcover-1.1.16 lib/softcover/commands/server.rb
softcover-1.1.15 lib/softcover/commands/server.rb
softcover-1.1.14 lib/softcover/commands/server.rb
softcover-1.1.13 lib/softcover/commands/server.rb
softcover-1.1.12 lib/softcover/commands/server.rb
softcover-1.1.11 lib/softcover/commands/server.rb
softcover-1.1.10 lib/softcover/commands/server.rb
softcover-1.1.9 lib/softcover/commands/server.rb
softcover-1.1.8 lib/softcover/commands/server.rb
softcover-1.1.7 lib/softcover/commands/server.rb
softcover-1.1.6 lib/softcover/commands/server.rb
softcover-1.1.4 lib/softcover/commands/server.rb
softcover-1.1.3 lib/softcover/commands/server.rb
softcover-1.1.2 lib/softcover/commands/server.rb
softcover-1.1.1 lib/softcover/commands/server.rb
softcover-1.1.0 lib/softcover/commands/server.rb
softcover-1.1.beta2 lib/softcover/commands/server.rb
softcover-1.0.5 lib/softcover/commands/server.rb
softcover-1.1.beta1 lib/softcover/commands/server.rb