Sha256: 32e6725492695395be8d4283c63981c90539cafb8626951605a9d546ce0d874b

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

module GhostInThePost
  
  class GhostJSError < StandardError; end

  class PhantomTransform
    PHANTOMJS_SCRIPT = File.expand_path('../phantom/staticize.js', __FILE__)
    ERROR_TAG = "[GHOSTINTHEPOST-STATICIZE-ERROR]"

    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

    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
      @inliner.remove_all_script if GhostInThePost.remove_js_tags
      @inliner.html(true)
    end

    private

    def command html_file
      [
        GhostInThePost.phantomjs_path, 
        PHANTOMJS_SCRIPT, 
        html_file.path,
        @timeout,
        @wait_event,
      ].map(&:to_s)
    end

    #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
    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

    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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ghost_in_the_post-0.0.12 lib/ghost_in_the_post/phantom_transform.rb