public
Rubygem
Description: a humane, eval-safe templating system using Hpricot
Clone URL: git://github.com/mattly/hpreserve.git
Search Repo:
Click here to lend your support to: hpreserve and make a donation at www.pledgie.com !
mattly (author)
Mon May 12 15:18:22 -0700 2008
commit  e88d87815eff3d624235646dcbd8fb3f5e5b571c
tree    ca24be32c5c31f9582f499b95a58dc24fb7dbe63
parent  272ff8c5beade3371f627bdc8118ead3bfcf4296
hpreserve / lib / hpreserve / standard_filters.rb
100644 60 lines (47 sloc) 1.249 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module Hpreserve
  module StandardFilters
  
    def capitalize(node)
      node.inner_html = node.inner_text.capitalize
      node
    end
    
    def date(node, strftime)
      time = Time.parse(node.inner_html)
      node.inner_html = time.strftime(strftime)
      node
    end
    
    # node modification filters
    
    def remove(node)
      node.parent.children.delete(node)
    end
    
    def unwrap(node)
      node.swap node.inner_html
    end
    
    def link(node, url)
      attr(node, 'href', url)
      node
    end
    
    def add_class(node, *klasses)
      set_class(node, [node.classes, klasses].flatten)
      node
    end
    
    def set_class(node, *klasses)
      klasses = klasses.flatten.map! {|c| c.gsub(/[^\-\w]+/,'-').gsub(/^[^a-zA-Z]/,'') }
      attr(node, 'class', klasses.uniq.join(' '))
      node
    end
    
    def set_id(node, id)
      attr(node, 'id', id)
      node
    end
    
    def attr(node, attrib, value)
      node.set_attribute(attrib, value)
      node
    end
    
    def attr_on_child(node, child, attrib, value)
      child = node.at('#'+child)
      child.set_attribute(attrib, value) if child
      node
    end
    
  end
end
 
Hpreserve::Filters.register(Hpreserve::StandardFilters)