1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Add a Haml::Util class for nifty utility functions.

This commit is contained in:
Nathan Weizenbaum 2008-11-22 19:17:06 -08:00
parent 9ebbe62230
commit 24bd863573
7 changed files with 51 additions and 32 deletions

View file

@ -1044,4 +1044,5 @@ module Haml
end
end
require 'haml/util'
require 'haml/engine'

View file

@ -5,6 +5,7 @@ module Haml
# processing done within instance_eval'd code.
class Buffer
include Haml::Helpers
include Haml::Util
# The string that holds the compiled XHTML. This is aliased as
# _erbout for compatibility with ERB-specific code.
@ -152,7 +153,7 @@ module Haml
attributes = class_id
attributes_hashes.each do |old|
self.class.merge_attrs(attributes, old.inject({}) {|h, (key, val)| h[key.to_s] = val; h})
self.class.merge_attrs(attributes, to_hash(old.map {|k, v| [k.to_s, v]}))
end
self.class.merge_attrs(attributes, parse_object_ref(obj_ref)) if obj_ref

View file

@ -162,19 +162,15 @@ module Haml
def dynamic_attributes
@dynamic_attributes ||= begin
attributes.inject({}) do |dynamic, pair|
name, value = pair
unless value.empty?
full_match = nil
ruby_value = value.gsub(%r{<haml:loud>\s*(.+?)\s*</haml:loud>}) do
full_match = $`.empty? && $'.empty?
full_match ? $1: "\#{#{$1}}"
end
unless ruby_value == value
dynamic[name] = full_match ? ruby_value : %("#{ruby_value}")
end
Haml::Util.map_hash(attributes) do |name, value|
next if value.empty?
full_match = nil
ruby_value = value.gsub(%r{<haml:loud>\s*(.+?)\s*</haml:loud>}) do
full_match = $`.empty? && $'.empty?
full_match ? $1: "\#{#{$1}}"
end
dynamic
next if ruby_value == value
[name, full_match ? ruby_value : %("#{ruby_value}")]
end
end
end

View file

@ -29,7 +29,7 @@ module ActionView
@@template_args[render_symbol] ||= {}
locals_keys = @@template_args[render_symbol].keys | locals
@@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
@@template_args[render_symbol] = to_hash(locals_keys.map {|k| [k, true]})
options = Haml::Template.options.dup
options[:filename] = file_name || 'compiled-template'

21
lib/haml/util.rb Normal file
View file

@ -0,0 +1,21 @@
module Haml
module Util
class << self; include Haml::Util; end
def to_hash(arr)
arr.compact.inject({}) {|h, (k, v)| h[k] = v; h}
end
def map_keys(hash)
to_hash(hash.map {|k, v| [yield(k), v]})
end
def map_vals(hash)
to_hash(hash.map {|k, v| [k, yield(v)]})
end
def map_hash(hash, &block)
to_hash(hash.map(&block))
end
end
end

View file

@ -968,5 +968,6 @@ $LOAD_PATH.unshift dir unless $LOAD_PATH.include?(dir)
#
module Sass; end
require 'haml/util'
require 'sass/engine'
require 'sass/plugin' if defined?(Merb::Plugins)

View file

@ -2,25 +2,24 @@ require 'sass/script/literal'
module Sass::Script
class Color < Literal # :nodoc:
HTML4_COLORS = {
'black' => 0x000000,
'silver' => 0xc0c0c0,
'gray' => 0x808080,
'white' => 0xffffff,
'maroon' => 0x800000,
'red' => 0xff0000,
'purple' => 0x800080,
'fuchsia' => 0xff00ff,
'green' => 0x008000,
'lime' => 0x00ff00,
'olive' => 0x808000,
'yellow' => 0xffff00,
'navy' => 0x000080,
'blue' => 0x0000ff,
'teal' => 0x008080,
'aqua' => 0x00ffff
}
'black' => 0x000000,
'silver' => 0xc0c0c0,
'gray' => 0x808080,
'white' => 0xffffff,
'maroon' => 0x800000,
'red' => 0xff0000,
'purple' => 0x800080,
'fuchsia' => 0xff00ff,
'green' => 0x008000,
'lime' => 0x00ff00,
'olive' => 0x808000,
'yellow' => 0xffff00,
'navy' => 0x000080,
'blue' => 0x0000ff,
'teal' => 0x008080,
'aqua' => 0x00ffff
}
def initialize(rgb)
rgb = rgb.map {|c| c.to_i}