Class: JekyllMetrics::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-metrics/hook.rb

Overview

Compile metrics template and inject it into the page code

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page) {|@page, _self| ... } ⇒ Hook

Returns a new instance of Hook.

Yields:

Yield Parameters:



8
9
10
11
# File 'lib/jekyll-metrics/hook.rb', line 8

def initialize(page)
  @page = page
  yield(@page, self) if block_given? && injectable?
end

Instance Attribute Details

#pageObject

Returns the value of attribute page.



6
7
8
# File 'lib/jekyll-metrics/hook.rb', line 6

def page
  @page
end

Instance Method Details

#inject_scriptsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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