class GhostInThePost::PhantomTransform

Constants

ERROR_TAG
PHANTOMJS_SCRIPT

Public Class Methods

new(html, timeout=nil, wait_event=nil, included_scripts=nil) click to toggle source
# File lib/ghost_in_the_post/phantom_transform.rb, line 9
def initialize(html, timeout=nil, wait_event=nil, included_scripts=nil)
  @inliner = JsInline.new(html, included_scripts)
  @timeout = timeout || GhostInThePost.timeout
  @wait_event = wait_event || GhostInThePost.wait_event
end

Public Instance Methods

transform() click to toggle source
# File lib/ghost_in_the_post/phantom_transform.rb, line 15
def transform
  @inliner.inline
  begin
    htmlfile = html_file(@inliner.html)
    @inliner.html = checkError(IO.popen(command(htmlfile)){|io| io.read})
  ensure
    if GhostInThePost.debug and @has_error
      p "Generated html can be found at #{htmlfile.path}"
    elsif !htmlfile.nil?
      htmlfile.unlink
    end
  end
  if GhostInThePost.remove_js_tags
    @inliner.remove_all_script 
  else
    @inliner.remove_inlined
  end
  @inliner.html
end

Private Instance Methods

checkError(output) click to toggle source
# File lib/ghost_in_the_post/phantom_transform.rb, line 56
def checkError output
  if output.start_with?(ERROR_TAG) and GhostInThePost.raise_js_errors
    @has_error = true
    raise GhostJSError.new(output.gsub(ERROR_TAG, "")) 
  end
  output
end
command(html_file) click to toggle source
# File lib/ghost_in_the_post/phantom_transform.rb, line 37
def command html_file
  [
    GhostInThePost.phantomjs_path, 
    PHANTOMJS_SCRIPT, 
    html_file.path,
    @timeout,
    @wait_event,
  ].map(&:to_s)
end
html_file(html) click to toggle source

generate a tempfile with all the html that we need so that phantom can inject easily and not have to max out a single command

# File lib/ghost_in_the_post/phantom_transform.rb, line 49
def html_file(html)
  file = Tempfile.new(['ghost_in_the_post', '.html'], encoding: Encoding::UTF_8)
  file.write(html)
  file.close #closing the file makes it accessible by phantom
  file
end