Renamed helpers to correspond to renamed Controller classes.
[merb_radiant.git] / lib / radiant / .svn / text-base / taggable.rb.svn-base
blobbb3cd0638819801aa4955529b57130ca6ac7129a
1 module Radiant::Taggable
2   mattr_accessor :last_description, :tag_descriptions
3   @@tag_descriptions = {}
4     
5   def self.included(base)
6     base.extend(ClassMethods)
7     base.module_eval do
8       def self.included(new_base)
9         super
10         new_base.tag_descriptions.merge! self.tag_descriptions
11       end
12     end
13   end
14   
15   def render_tag(name, tag_binding)
16     send "tag:#{name}", tag_binding
17   end
18   
19   def tags
20     Util.tags_in_array(methods)
21   end
22   
23   def tag_descriptions(hash=nil)
24     self.class.tag_descriptions hash
25   end
26   
27   module ClassMethods
28     def inherited(subclass)
29       subclass.tag_descriptions.reverse_merge! self.tag_descriptions
30       super
31     end
32     
33     def tag_descriptions(hash = nil)
34       Radiant::Taggable.tag_descriptions[self.name] ||= (hash ||{})
35     end
36   
37     def desc(text)
38       Radiant::Taggable.last_description = RedCloth.new(Util.strip_leading_whitespace(text)).to_html
39     end
40     
41     def tag(name, &block)
42       self.tag_descriptions[name] = Radiant::Taggable.last_description if Radiant::Taggable.last_description
43       Radiant::Taggable.last_description = nil
44       define_method("tag:#{name}", &block)
45     end
46     
47     def tags
48       Util.tags_in_array(self.instance_methods)
49     end
50     
51   end
52    
53   module Util
54     def self.tags_in_array(array)
55       array.grep(/^tag:/).map { |name| name[4..-1] }.sort
56     end
57     
58     def self.strip_leading_whitespace(text)
59       text = text.dup
60       text.gsub!("\t", "  ")
61       lines = text.split("\n")
62       leading = lines.map do |line|
63         unless line =~ /^\s*$/
64            line.match(/^(\s*)/)[0].length
65         else
66           nil
67         end
68       end.compact.min
69       lines.inject([]) {|ary, line| ary << line.sub(/^[ ]{#{leading}}/, "")}.join("\n")
70     end      
71     
72   end
73   
74 end