class JekyllMetrics::Hook

Compile metrics template and inject it into the page code

Attributes

page[RW]

Public Class Methods

new(page) { |page, self| ... } click to toggle source
# File lib/jekyll-metrics/hook.rb, line 8
def initialize(page)
  @page = page
  yield(@page, self) if block_given? && injectable?
end

Public Instance Methods

inject_scripts() click to toggle source
# File lib/jekyll-metrics/hook.rb, line 13
def inject_scripts
  return unless injectable?

  document = Nokogiri::HTML(page.output)
  first_head_script = find_first_script(document)

  if first_head_script
    inject_after_first_script(first_head_script, document)
  else
    inject_before_closing_head(document)
  end

  page.output.replace(document.to_html)
end

Private Instance Methods

can_be_modified?() click to toggle source
# File lib/jekyll-metrics/hook.rb, line 42
def can_be_modified?
  page.output.match?('<html') && page.output.match('<\/head')
end
compiled_to_html_page?() click to toggle source
# File lib/jekyll-metrics/hook.rb, line 38
def compiled_to_html_page?
  page.output_ext == '.html' || page.permalink&.end_with?('/')
end
config() click to toggle source
# File lib/jekyll-metrics/hook.rb, line 78
def config
  @config ||= Config.instance(page.site)
end
find_first_script(document) click to toggle source
# File lib/jekyll-metrics/hook.rb, line 54
def find_first_script(document)
  document&.xpath('//head')&.xpath('script')&.first
end
inject_after_first_script(first_head_script, _document) click to toggle source
# File lib/jekyll-metrics/hook.rb, line 46
def inject_after_first_script(first_head_script, _document)
  first_head_script.add_previous_sibling(load_scripts)
end
inject_before_closing_head(document) click to toggle source
# File lib/jekyll-metrics/hook.rb, line 50
def inject_before_closing_head(document)
  document&.xpath('//head')&.first&.add_child(load_scripts)
end
injectable?() click to toggle source
# File lib/jekyll-metrics/hook.rb, line 30
def injectable?
  writable? && compiled_to_html_page? && can_be_modified?
end
load_scripts() click to toggle source
# File lib/jekyll-metrics/hook.rb, line 62
def load_scripts
  verify_path!(config.template_path)

  render_template(File.read(config.template_path))
end
prepare_scripts_for(closing_tag) click to toggle source
# File lib/jekyll-metrics/hook.rb, line 58
def prepare_scripts_for(closing_tag)
  [load_scripts, "</#{closing_tag}>"].compact.join("\n")
end
render_template(file) click to toggle source
# File lib/jekyll-metrics/hook.rb, line 74
def render_template(file)
  Liquid::Template.parse(file).render(config.plugin_vars)
end
verify_path!(path) click to toggle source
# File lib/jekyll-metrics/hook.rb, line 68
def verify_path!(path)
  return if path && File.exist?(path)

  raise ConfigurationError, "Template not found in path \"#{path}\""
end
writable?() click to toggle source
# File lib/jekyll-metrics/hook.rb, line 34
def writable?
  [Jekyll::Document, Jekyll::Page].include?(page.class) || page.write?
end