Class: Hawkins::Commands::LiveServe::BodyProcessor
- Inherits:
-
Object
- Object
- Hawkins::Commands::LiveServe::BodyProcessor
- Defined in:
- lib/hawkins/servlet.rb
Constant Summary collapse
- HEAD_TAG_REGEX =
/<head>|<head[^(er)][^<]*>/
Instance Attribute Summary collapse
-
#content_length ⇒ Object
readonly
Returns the value of attribute content_length.
-
#livereload_added ⇒ Object
readonly
Returns the value of attribute livereload_added.
-
#new_body ⇒ Object
readonly
Returns the value of attribute new_body.
Instance Method Summary collapse
-
#initialize(body, options) ⇒ BodyProcessor
constructor
A new instance of BodyProcessor.
- #livereload_args ⇒ Object
- #process! ⇒ Object
- #processed? ⇒ Boolean
- #template ⇒ Object
- #with_swf? ⇒ Boolean
Constructor Details
#initialize(body, options) ⇒ BodyProcessor
Returns a new instance of BodyProcessor.
46 47 48 49 50 |
# File 'lib/hawkins/servlet.rb', line 46 def initialize(body, ) @body = body @options = @processed = false end |
Instance Attribute Details
#content_length ⇒ Object (readonly)
Returns the value of attribute content_length.
44 45 46 |
# File 'lib/hawkins/servlet.rb', line 44 def content_length @content_length end |
#livereload_added ⇒ Object (readonly)
Returns the value of attribute livereload_added.
44 45 46 |
# File 'lib/hawkins/servlet.rb', line 44 def livereload_added @livereload_added end |
#new_body ⇒ Object (readonly)
Returns the value of attribute new_body.
44 45 46 |
# File 'lib/hawkins/servlet.rb', line 44 def new_body @new_body end |
Instance Method Details
#livereload_args ⇒ Object
121 122 123 124 125 126 127 128 129 |
# 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 << "&mindelay=#{@options['min_delay']}" if @options["min_delay"] src << "&maxdelay=#{@options['max_delay']}" if @options["max_delay"] src << "&port=#{@options['reload_port']}" if @options["reload_port"] src end |
#process! ⇒ Object
60 61 62 63 64 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 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? ⇒ Boolean
56 57 58 |
# File 'lib/hawkins/servlet.rb', line 56 def processed? @processed end |
#template ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# 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? ⇒ Boolean
52 53 54 |
# File 'lib/hawkins/servlet.rb', line 52 def with_swf? @options["swf"] end |