module Softcover::Commands::Server

Attributes

no_listener[RW]

Public Instance Methods

ignore_regex() click to toggle source

Returns a regex for files to be ignored by the listener.

# File lib/softcover/commands/server.rb, line 26
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
listen_for_changes() click to toggle source

Listens for changes to the book's source files.

# File lib/softcover/commands/server.rb, line 10
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('.', only: filter_regex, ignore: /html\//) 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
markdown?() click to toggle source
# File lib/softcover/commands/server.rb, line 36
def markdown?
  !Dir.glob(path('chapters/*.md')).empty?
end
rebuild(modified=nil) click to toggle source
# File lib/softcover/commands/server.rb, line 40
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
run(port, bind) click to toggle source
# File lib/softcover/commands/server.rb, line 60
def run(port, bind)
  rebuild
  listen_for_changes
  start_server port, bind
end
start_server(port, bind) click to toggle source
# File lib/softcover/commands/server.rb, line 52
def start_server(port, bind)
  require 'softcover/server/app'
  puts "Running Softcover server on http://#{bind}:#{port}"
  Softcover::App.set :port, port
  Softcover::App.set :bind, bind
  Softcover::App.run!
end