Class: Hawkins::Commands::LiveServe::BodyProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/hawkins/servlet.rb

Constant Summary collapse

HEAD_TAG_REGEX =
/<head>|<head[^(er)][^<]*>/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, options) ⇒ BodyProcessor

Returns a new instance of BodyProcessor.



51
52
53
54
55
# File 'lib/hawkins/servlet.rb', line 51

def initialize(body, options)
  @body = body
  @options = options
  @processed = false
end

Instance Attribute Details

#content_lengthObject (readonly)

Returns the value of attribute content_length.



49
50
51
# File 'lib/hawkins/servlet.rb', line 49

def content_length
  @content_length
end

#livereload_addedObject (readonly)

Returns the value of attribute livereload_added.



49
50
51
# File 'lib/hawkins/servlet.rb', line 49

def livereload_added
  @livereload_added
end

#new_bodyObject (readonly)

Returns the value of attribute new_body.



49
50
51
# File 'lib/hawkins/servlet.rb', line 49

def new_body
  @new_body
end

Instance Method Details

#host_to_useObject



90
91
92
# File 'lib/hawkins/servlet.rb', line 90

def host_to_use
  (@options["host"] || 'localhost').gsub(%r{:.*}, '')
end

#livereload_protocolObject



113
114
115
116
# File 'lib/hawkins/servlet.rb', line 113

def livereload_protocol
  use_ssl = @options["ssl_cert"] && @options["ssl_key"]
  use_ssl ? '"wss://"' : '"ws://"'
end

#livereload_sourceObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/hawkins/servlet.rb', line 118

def livereload_source
  use_ssl = @options["ssl_cert"] && @options["ssl_key"]
  protocol = use_ssl ? "https" : "http"

  # Unclear what "snipver" does.  https://github.com/livereload/livereload-js states
  # that the recommended setting is 1.
  src = "#{protocol}://#{host_to_use}:#{@options['reload_port']}/livereload.js?snipver=1"

  # XHTML standard requires ampersands to be encoded as entities when in attributes
  # See http://stackoverflow.com/a/2190292
  src << "&amp;mindelay=#{@options['min_delay']}" if @options["min_delay"]
  src << "&amp;maxdelay=#{@options['max_delay']}" if @options["max_delay"]
  src << "&amp;port=#{@options['reload_port']}" if @options["reload_port"]
  src
end

#process!Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/hawkins/servlet.rb', line 65

def process!
  @new_body = []
  begin
    @body.each { |line| @new_body << line.to_s }
  ensure
    # @body will be a File object
    @body.close
  end

  @content_length = 0
  @livereload_added = false

  @new_body.each do |line|
    if !@livereload_added && line['<head']
      line.gsub!(HEAD_TAG_REGEX) { |match| %(#{match}#{template.result(binding)}) }

      @livereload_added = true
    end

    @content_length += line.bytesize
    @processed = true
  end
  @new_body = @new_body.join
end

#processed?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/hawkins/servlet.rb', line 61

def processed?
  @processed
end

#templateObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/hawkins/servlet.rb', line 94

def template
  template = <<-TEMPLATE
  <% if with_swf? %>
    <script type="text/javascript">
      WEB_SOCKET_SWF_LOCATION = "<%= @options["baseurl"] %>/__livereload/WebSocketMain.swf";
      WEB_SOCKET_FORCE_FLASH = false;
    </script>
    <script type="text/javascript" src="<%= @options["baseurl"] %>/__livereload/swfobject.js"></script>
    <script type="text/javascript" src="<%= @options["baseurl"] %>/__livereload/web_socket.js"></script>
  <% end %>
  <script type="text/javascript">
    HAWKINS_LIVERELOAD_PORT = <%= @options["reload_port"] %>;
    HAWKINS_LIVERELOAD_PROTOCOL = <%= livereload_protocol %>;
  </script>
  <script type="text/javascript" src="<%= livereload_source %>"></script>
  TEMPLATE
  ERB.new(Jekyll::Utils.strip_heredoc(template))
end

#with_swf?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/hawkins/servlet.rb', line 57

def with_swf?
  @options["swf"]
end