Getting the tests functional and getting the plugin into functional shape.
Now... time to try with a real application! git-svn-id: svn://hamptoncatlin.com/haml/branches/1.5dev@175 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
parent
fa5048ba40
commit
c94123be02
1
init.rb
1
init.rb
|
@ -1,3 +1,4 @@
|
|||
require 'haml/template'
|
||||
require 'sass/plugin'
|
||||
|
||||
ActionView::Base.register_template_handler('haml', Haml::Template)
|
|
@ -3,6 +3,12 @@ require File.dirname(__FILE__) + '/css/parser'
|
|||
|
||||
module Sass
|
||||
class Engine
|
||||
def render_file(location)
|
||||
@result = ""
|
||||
File.open(location).each_line { |l| @result += l }
|
||||
render(@result)
|
||||
end
|
||||
|
||||
def render(input)
|
||||
buffer, stack, last_level, first = "", [], 0, true
|
||||
input.each do |line|
|
||||
|
|
|
@ -1,27 +1,35 @@
|
|||
|
||||
require File.dirname(__FILE__) + "/engine"
|
||||
|
||||
#Rails plugin stuff. For use with action_view
|
||||
|
||||
module Sass
|
||||
module Plugin
|
||||
@@options = {}
|
||||
|
||||
def options
|
||||
@@options
|
||||
end
|
||||
class << self
|
||||
def options; @@options; end
|
||||
|
||||
def stylesheet_location
|
||||
def _stylesheet_location
|
||||
@@options[:stylesheet_location] || (RAILS_ROOT + "/public/stylesheets/")
|
||||
end
|
||||
|
||||
def sass_template(name)
|
||||
file_location = stylesheet_location + name
|
||||
if stylesheet_needs_update?(file_location)
|
||||
file = File.open(file_location + ".css")
|
||||
Sass::Engine.new.render(file_location + ".sass")
|
||||
end
|
||||
def _always_update
|
||||
@@options[:always_update] || false
|
||||
end
|
||||
|
||||
def stylesheet_needs_update?(file_location)
|
||||
!File.exists?(file_location + ".css") || (File.mtime("#{file_location}.sass") - 60) > File.mtime("#{file_location}.css")
|
||||
end
|
||||
end
|
||||
|
||||
def sass_template(name)
|
||||
file_location = Plugin._stylesheet_location + "/" + name.to_s
|
||||
if Plugin._always_update || Plugin.stylesheet_needs_update?(file_location)
|
||||
output_file = File.open(file_location + ".css", "w+")
|
||||
output_file << Sass::Engine.new.render_file(file_location + ".sass")
|
||||
output_file.close
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
|
||||
%body { font: Arial; background: blue; }
|
||||
|
||||
#page { width: 700px; height: 100%; }
|
||||
#page #header { height: 300px; }
|
||||
#page #header %h1 { font-size: 50px; color: blue; }
|
||||
#page #header #content.user.show { }
|
||||
#page #header #content.user.show #container.top { }
|
||||
#page #header #content.user.show #container.top #column.left { width: 100px; }
|
||||
#page #header #content.user.show #container.top #column.right { width: 600px; }
|
||||
#page #header #content.user.show #container.top #container.bottom { background: brown; }
|
|
@ -0,0 +1,6 @@
|
|||
require File.dirname(__FILE__) + '/../../lib/sass/engine'
|
||||
require File.dirname(__FILE__) + '/../../lib/sass/plugin'
|
||||
|
||||
class FakeController
|
||||
include Sass::Plugin
|
||||
end
|
|
@ -0,0 +1,23 @@
|
|||
|
||||
|
||||
%body
|
||||
:font Arial
|
||||
:background blue
|
||||
|
||||
#page
|
||||
:width 700px
|
||||
:height 100%
|
||||
#header
|
||||
:height 300px
|
||||
%h1
|
||||
:font-size 50px
|
||||
:color blue
|
||||
|
||||
#content.user.show
|
||||
#container.top
|
||||
#column.left
|
||||
:width 100px
|
||||
#column.right
|
||||
:width 600px
|
||||
#container.bottom
|
||||
:background brown
|
|
@ -0,0 +1,28 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require 'test/unit'
|
||||
require File.dirname(__FILE__) + '/../lib/sass/engine'
|
||||
|
||||
class SassEngineTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@engine = Sass::Engine.new
|
||||
end
|
||||
|
||||
def test_basic_render
|
||||
renders_correctly "basic"
|
||||
end
|
||||
|
||||
def renders_correctly(name)
|
||||
sass_file = load_file(name, "sass")
|
||||
css_file = load_file(name, "css")
|
||||
css_result = @engine.render(sass_file)
|
||||
#puts css_result.collect { |a| a.inspect }.join("\n ")
|
||||
assert_equal css_file, css_result
|
||||
end
|
||||
|
||||
def load_file(name, type = "sass")
|
||||
@result = ''
|
||||
File.new(File.dirname(__FILE__) + "/#{type}/#{name}.#{type}").each_line { |l| @result += l }
|
||||
@result
|
||||
end
|
||||
end
|
|
@ -0,0 +1,24 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require 'test/unit'
|
||||
require File.dirname(__FILE__) + '/../lib/sass/engine'
|
||||
require File.dirname(__FILE__) + '/../lib/sass/plugin'
|
||||
require File.dirname(__FILE__) + '/mocks/fake_controller'
|
||||
|
||||
class SassPluginTest < Test::Unit::TestCase
|
||||
def setup
|
||||
Sass::Plugin.options[:stylesheet_location] = File.dirname(__FILE__) + '/sass'
|
||||
Sass::Plugin.options[:always_update] = true
|
||||
@controller = FakeController.new
|
||||
end
|
||||
|
||||
def test_truth
|
||||
@controller.instance_eval do
|
||||
sass_template(:basic)
|
||||
end
|
||||
|
||||
assert FileUtils.compare_file(File.dirname(__FILE__) + "/sass/basic.css",
|
||||
File.dirname(__FILE__) + "/css/basic.css")
|
||||
FileUtils.rm(File.dirname(__FILE__) + "/sass/basic.css")
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue