class Jekyll::Commands::Serve::BodyProcessor
This class inserts the LiveReload script tags into HTML as it is served
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/jekyll/commands/serve/servlet.rb, line 50 def initialize(body, options) @body = body @options = options @processed = false end
Public Instance Methods
livereload_args()
click to toggle source
# File lib/jekyll/commands/serve/servlet.rb, line 113 def livereload_args # XHTML standard requires ampersands to be encoded as entities when in # attributes. See http://stackoverflow.com/a/2190292 src = "" if @options["livereload_min_delay"] src += "&mindelay=#{@options["livereload_min_delay"]}" end if @options["livereload_max_delay"] src += "&maxdelay=#{@options["livereload_max_delay"]}" end src += "&port=#{@options["livereload_port"]}" if @options["livereload_port"] src end
process!()
click to toggle source
rubocop:disable Metrics/MethodLength
# File lib/jekyll/commands/serve/servlet.rb, line 61 def process! @new_body = [] # @body will usually be a File object but Strings occur in rare cases 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) do |match| %(#{match}#{template.result(binding)}) end @livereload_added = true end @content_length += line.bytesize @processed = true end @new_body = @new_body.join end
processed?()
click to toggle source
# File lib/jekyll/commands/serve/servlet.rb, line 56 def processed? @processed end
template()
click to toggle source
rubocop:enable Metrics/MethodLength
# File lib/jekyll/commands/serve/servlet.rb, line 93 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 @template ||= ERB.new(<<~TEMPLATE) <script> document.write( '<script src="' + location.protocol + '//' + (location.host || 'localhost').split(':')[0] + ':<%=@options["livereload_port"] %>/livereload.js?snipver=1<%= livereload_args %>"' + '></' + 'script>'); </script> TEMPLATE end