class GhostInThePost::JsInline

Constants

SCRIPT_ID
STRATEGIES

Public Class Methods

new(html, included_scripts=[]) click to toggle source
# File lib/ghost_in_the_post/js_inline.rb, line 17
def initialize(html, included_scripts=[])
  self.html = html
  @included_scripts = Array(included_scripts).compact
end

Public Instance Methods

html() click to toggle source
# File lib/ghost_in_the_post/js_inline.rb, line 38
def html
  @dom.dup.to_html
end
html=(html) click to toggle source
# File lib/ghost_in_the_post/js_inline.rb, line 34
def html=(html)
  @dom = Nokogiri::HTML.parse html
end
inline() click to toggle source
# File lib/ghost_in_the_post/js_inline.rb, line 22
def inline
  @dom.at_xpath('html/body').add_child("<script id='#{SCRIPT_ID}'>#{generate_flat_js}</script>")
end
remove_all_script() click to toggle source
# File lib/ghost_in_the_post/js_inline.rb, line 26
def remove_all_script
  @dom.css('script').map(&:remove)
end
remove_inlined() click to toggle source
# File lib/ghost_in_the_post/js_inline.rb, line 30
def remove_inlined
  @dom.css("##{SCRIPT_ID}").map(&:remove)
end

Private Instance Methods

find_js(url) click to toggle source
# File lib/ghost_in_the_post/js_inline.rb, line 66
def find_js(url)
  STRATEGIES.each do |strategy|
    js = strategy.load(url)
    return js.force_encoding('UTF-8') if js
  end
end
generate_flat_js() click to toggle source
# File lib/ghost_in_the_post/js_inline.rb, line 44
def generate_flat_js
  injectable_scripts.map do |script|
    asset = find_js(script)
    if GhostInThePost.raise_asset_errors and asset.nil?
      raise AssetNotFoundError.new("cannot find asset #{normalize_asset_name(script)}")
    end
    unless asset.nil?
      GhostInThePost::JSLoaders::CacheLoader.store(script, asset.to_s)
      asset.to_s
    end
  end.compact.join("\n")
end
injectable_scripts() click to toggle source
# File lib/ghost_in_the_post/js_inline.rb, line 57
def injectable_scripts
  doc_scripts = @dom.css('script').map do |script_node|
    src = script_node['src']
    script_node.remove unless src.nil?
    src
  end.compact
  doc_scripts + GhostInThePost.includes + @included_scripts
end