Module: Softcover::Commands::Server

Extended by:
Server
Includes:
Output, Utils
Included in:
Server
Defined in:
lib/softcover/commands/server.rb

Constant Summary

Constants included from Utils

Utils::UNITS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#add_highlight_class!, #as_size, #current_book, #digest, #executable, #execute, #in_book_directory?, #logged_in?, #mkdir, #path, #reset_current_book!, #rm, #silence, #source, #tmpify, #write_pygments_file

Methods included from Output

should_output?, silence!, silent?, #system, unsilence!

Instance Attribute Details

#no_listenerObject

Returns the value of attribute no_listener.



6
7
8
# File 'lib/softcover/commands/server.rb', line 6

def no_listener
  @no_listener
end

Instance Method Details

#listen_for_changesObject

Listens for changes to the book’s source files.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/softcover/commands/server.rb', line 10

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

#markdown?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/softcover/commands/server.rb', line 25

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

#rebuild(modified = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/softcover/commands/server.rb', line 29

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

#run(port) ⇒ Object



49
50
51
52
# File 'lib/softcover/commands/server.rb', line 49

def run(port)
  listen_for_changes
  start_server port
end

#start_server(port) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/softcover/commands/server.rb', line 41

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