diff --git a/lib/simplecov-html.rb b/lib/simplecov-html.rb index bd6efa6..aa195ff 100644 --- a/lib/simplecov-html.rb +++ b/lib/simplecov-html.rb @@ -5,6 +5,7 @@ require "fileutils" require "digest/sha1" require "time" +require "base64" # Ensure we are using a compatible version of SimpleCov major, minor, patch = SimpleCov::VERSION.scan(/\d+/).first(3).map(&:to_i) @@ -19,11 +20,15 @@ class HTMLFormatter def initialize @branchable_result = SimpleCov.branch_coverage? @templates = {} + @inline_assets = !ENV["SIMPLECOV_INLINE_ASSETS"].nil? + @public_assets_dir = File.join(File.dirname(__FILE__), "../public/") end def format(result) - Dir[File.join(File.dirname(__FILE__), "../public/*")].each do |path| - FileUtils.cp_r(path, asset_output_path) + unless @inline_assets + Dir[File.join(@public_assets_dir, "*")].each do |path| + FileUtils.cp_r(path, asset_output_path) + end end File.open(File.join(output_path, "index.html"), "wb") do |file| @@ -71,9 +76,26 @@ def asset_output_path end def assets_path(name) + return asset_inline(name) if @inline_assets + File.join("./assets", SimpleCov::Formatter::HTMLFormatter::VERSION, name) end + def asset_inline(name) + path = File.join(@public_assets_dir, name) + + # Only have a few content types, just hardcode them + content_type = { + ".js" => "text/javascript", + ".png" => "image/png", + ".gif" => "image/gif", + ".css" => "text/css", + }[File.extname(name)] + + base64_content = Base64.strict_encode64 File.open(path).read + "data:#{content_type};base64,#{base64_content}" + end + # Returns the html for the given source_file def formatted_source_file(source_file) template("source_file").result(binding)