2006-10-14 19:50:07 -04:00
|
|
|
require 'rubygems'
|
|
|
|
require 'active_support'
|
2007-11-18 06:14:12 -05:00
|
|
|
require 'action_controller'
|
2006-10-14 19:50:07 -04:00
|
|
|
require 'action_view'
|
2007-11-18 06:14:12 -05:00
|
|
|
|
|
|
|
require File.dirname(__FILE__) + '/../lib/haml'
|
|
|
|
require 'haml/template'
|
|
|
|
|
2006-10-14 19:50:07 -04:00
|
|
|
require 'profiler'
|
|
|
|
require 'stringio'
|
|
|
|
|
|
|
|
module Haml
|
2007-01-07 01:29:12 -05:00
|
|
|
# Used by both Haml::Profiler and Sass::Profiler.
|
|
|
|
# Encapsulates profiling behavior.
|
|
|
|
module AbstractProfiler
|
|
|
|
def self.profile(times, &block)
|
|
|
|
# Runs the profiler, collects information
|
|
|
|
Profiler__::start_profile
|
|
|
|
times.times &block
|
|
|
|
Profiler__::stop_profile
|
|
|
|
|
|
|
|
# Outputs information to a StringIO, returns result
|
|
|
|
io = StringIO.new
|
|
|
|
Profiler__::print_profile(io)
|
|
|
|
io.pos = 0
|
|
|
|
result = io.read
|
|
|
|
io.close
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# A profiler for Haml, mostly for development use. This simply implements
|
2006-12-16 18:13:01 -05:00
|
|
|
# the Ruby profiler for profiling haml code.
|
2006-10-14 19:50:07 -04:00
|
|
|
class Profiler
|
|
|
|
|
|
|
|
# Creates a new profiler that looks for templates in the base
|
|
|
|
# directory.
|
2006-12-03 17:43:19 -05:00
|
|
|
def initialize(base = File.join(File.dirname(__FILE__), 'haml', 'templates'))
|
2006-10-14 19:50:07 -04:00
|
|
|
unless base.class == ActionView::Base
|
|
|
|
@base = ActionView::Base.new(base)
|
|
|
|
else
|
|
|
|
@base = base
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-12-16 18:13:01 -05:00
|
|
|
# Profiles haml on the given template with the given number of runs.
|
2006-10-14 19:50:07 -04:00
|
|
|
# The template name shouldn't have a file extension; this will
|
2006-12-16 18:13:01 -05:00
|
|
|
# automatically look for a haml template.
|
2006-10-14 19:50:07 -04:00
|
|
|
#
|
|
|
|
# Returns the results of the profiling as a string.
|
|
|
|
def profile(runs = 100, template_name = 'standard')
|
2007-01-07 01:29:12 -05:00
|
|
|
AbstractProfiler.profile(runs) { @base.render template_name }
|
2006-10-14 19:50:07 -04:00
|
|
|
end
|
2007-01-07 01:29:12 -05:00
|
|
|
end
|
|
|
|
end
|
2006-10-14 19:50:07 -04:00
|
|
|
|
2007-01-07 01:29:12 -05:00
|
|
|
module Sass
|
|
|
|
class Profiler
|
|
|
|
def profile(runs = 100, template_name = 'complex')
|
|
|
|
Haml::AbstractProfiler.profile(runs) do
|
|
|
|
Sass::Engine.new("#{File.dirname(__FILE__)}/sass/templates/#{template_name}.sass").render
|
|
|
|
end
|
|
|
|
end
|
2006-10-14 19:50:07 -04:00
|
|
|
end
|
|
|
|
end
|