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.



26
27
28
29
30
31
32
33
34
# 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(is_pdf = false, overfull = false) ⇒ Object

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(is_pdf=false, overfull=false)
  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(is_pdf, modified: first_modified, overfull: overfull)
      Process.kill("HUP", server_pid)
    end
  end

  @listener.start
end

#markdown?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/softcover/commands/server.rb', line 36

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

#rebuild(is_pdf, modified: nil, overfull: nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/softcover/commands/server.rb', line 40

def rebuild(is_pdf, modified: nil, overfull: nil)
  printf modified ? "=> #{File.basename modified} changed, rebuilding... " :
                    'Building...'
  t = Time.now

  if is_pdf
    if overfull
      options = "--find-overfull"
    else
      options = "--once --nonstop --quiet"
    end
    system "softcover build:pdf #{options}"
  else
    builder = Softcover::Builders::Html.new
    builder.build
  end
  puts "Done. (#{(Time.now - t).round(2)}s)"

rescue Softcover::BookManifest::NotFound => e
  puts e.message
end

#run(port, bind, is_pdf, overfull) ⇒ Object



72
73
74
75
76
# File 'lib/softcover/commands/server.rb', line 72

def run(port, bind, is_pdf, overfull)
  rebuild(is_pdf, overfull: overfull)
  listen_for_changes(is_pdf, overfull)
  start_server port, bind, is_pdf
end

#start_server(port, bind, is_pdf) ⇒ Object



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

def start_server(port, bind, is_pdf)
  require 'softcover/server/app'
  Softcover::App.set :port, port
  unless is_pdf
    puts "Running Softcover server on http://#{bind}:#{port}"
    Softcover::App.set :bind, bind
  end
  Softcover::App.run!
end