class Hawkins::Commands::LiveServe::BodyProcessor

Constants

HEAD_TAG_REGEX

Attributes

content_length[R]
livereload_added[R]
new_body[R]

Public Class Methods

new(body, options) click to toggle source
# File lib/hawkins/servlet.rb, line 46
def initialize(body, options)
  @body = body
  @options = options
  @processed = false
end

Public Instance Methods

livereload_args() click to toggle source
# File lib/hawkins/servlet.rb, line 121
def livereload_args
  src = ''
  # 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!() click to toggle source
# File lib/hawkins/servlet.rb, line 60
def process!
  # @body will usually be a File object but Strings occur in rare cases
  # that occur for reasons unknown to me.
  @new_body = []
  if @body.respond_to?(:each)
    begin
      @body.each { |line| @new_body << line.to_s }
    ensure
      @body.close
    end
  else
    @new_body = @body.lines
  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?() click to toggle source
# File lib/hawkins/servlet.rb, line 56
def processed?
  @processed
end
template() click to toggle source
# File lib/hawkins/servlet.rb, line 90
        def template
          # Unclear what "snipver" does. Doc at
          # https://github.com/livereload/livereload-js states that the recommended
          # setting is 1.

          # Complicated JavaScript to ensure that livereload.js is loaded from the
          # same origin as the page.  Mostly useful for dealing with the browser's
          # distinction between 'localhost' and 127.0.0.1

          # Use 'src="//..."' to mirror the protocol used to load the page itself.
          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>
            document.write(
              '<script src="//' +
              (location.host || 'localhost').split(':')[0] +
              ':<%=@options["reload_port"] %>/livereload.js?snipver=1<%= livereload_args %>"' +
              '></' +
              'script>');
          </script>
          TEMPLATE
          ERB.new(Jekyll::Utils.strip_heredoc(template))
        end
with_swf?() click to toggle source
# File lib/hawkins/servlet.rb, line 52
def with_swf?
  @options["swf"]
end