lib/flamegraph/renderer.rb in flamegraph-0.0.3 vs lib/flamegraph/renderer.rb in flamegraph-0.0.4

- old
+ new

@@ -1,15 +1,25 @@ # inspired by https://github.com/brendangregg/FlameGraph +require 'base64' class Flamegraph::Renderer def initialize(stacks) @stacks = stacks end - def graph_html - body = IO.read(::File.expand_path('flamegraph.html', ::File.dirname(__FILE__))) - body.sub!("/*DATA*/", ::JSON.generate(graph_data)); + def graph_html(embed_resources) + body = read('flamegraph.html') + body.sub! "/**INCLUDES**/", + if embed_resources + embed("jquery.min.js","d3.min.js","lodash.min.js") + else + '<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> +<script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.0.8/d3.min.js"></script> +<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/1.3.1/lodash.min.js"></script>' + end + + body.sub!("/**DATA**/", ::JSON.generate(graph_data)); body end def graph_data height = 0 @@ -53,9 +63,24 @@ } end end data + end + + private + + def embed(*files) + out = "" + files.each do |file| + body = read(file) + out << "<script src='data:text/javascript;base64," << Base64.encode64(body) << "'></script>" + end + out + end + + def read(file) + body = IO.read(::File.expand_path(file, ::File.dirname(__FILE__))) end end