public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
mephisto / app / filters / core_filters.rb
100644 69 lines (56 sloc) 1.53 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
61
62
63
64
65
66
67
68
69
module CoreFilters
  def escape_html(html)
    CGI::escapeHTML(html)
  end
  
  alias h escape_html
 
  def pluralize(count, singular, plural = nil)
    "#{count} " + if count == 1
      singular
    elsif plural
      plural
    elsif Object.const_defined?(:Inflector)
      Inflector.pluralize(singular)
    else
      singular + "s"
    end
  end
  
  # See: http://starbase.trincoll.edu/~crypto/resources/LetFreq.html
  def word_count(text)
    (text.split(/[^a-zA-Z]/).join(' ').size / 4.5).round
  end
 
  def textilize(text)
    text.blank? ? '' : RedCloth.new(text).to_html
  end
 
  def parse_date(date)
    return Time.now.utc if date.blank?
    date = "#{date}-1" if date.to_s =~ /^\d{4}-\d{1,2}$/ unless [Time, Date].include?(date.class)
    date = date.to_time
  end
 
  def format_date(date, format, ordinalized = false)
    return '' if date.nil?
    if ordinalized
      date ? parse_date(date).to_ordinalized_s(format.to_sym) : nil
    else
      date ? parse_date(date).to_s(format.to_sym) : nil unless ordinalized
    end
  end
  
  def strftime(date, format)
    date ? date.strftime(format) : nil
  end
 
  def index(array, index = 0)
    array[index] if array.respond_to?(:[])
  end
 
  def assign_to(value, name)
    @context[name] = value ; nil
  end
 
  def assign_to_global(value, name)
    @context.scopes.last[name] = value ; nil
  end
  
  def contains(source, text)
    !source[text].nil?
  end
 
  protected
    def liquify(*records, &block)
      BaseDrop.liquify(@context, *records, &block)
    end
end