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!, #article?, #as_size, #book_file_lines, #chapter_label, #commands, #current_book, #dependency_filename, #digest, #executable, #execute, #filename_or_default, #first_path, #get_filename, #html_extension, #in_book_directory?, #language_labels, #linux?, #logged_in?, #master_content, #master_filename, #master_latex_header, #mkdir, #non_comment_lines, #os_x?, #path, #polytexnic_html, #raw_lines, #reset_current_book!, #rm, #rm_r, #silence, #silence_stream, #source, #template_dir, #tmpify, #unpublish_slug, #write_master_latex_file, #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

#ignore_regexObject

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



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

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_changesObject

Listens for changes to the book’s source files.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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('.')
  @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

#markdown?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/softcover/commands/server.rb', line 38

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

#rebuild(modified = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/softcover/commands/server.rb', line 42

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) ⇒ Object



62
63
64
65
66
# File 'lib/softcover/commands/server.rb', line 62

def run(port, bind)
  rebuild
  listen_for_changes
  start_server port, bind
end

#start_server(port, bind) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/softcover/commands/server.rb', line 54

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