public
Description: Uber lightweight Merb blogging engine. Make sure you check out the feather-plugins repo as well!
Homepage:
Clone URL: git://github.com/mleung/feather.git
Click here to lend your support to: feather and make a donation at www.pledgie.com !
feather / app / helpers / global_helpers.rb
100644 171 lines (148 sloc) 5.515 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
module Merb
  module GlobalHelpers
    def render_title
      @settings.title
    end
 
    def render_tag_line
      @settings.tag_line
    end
 
    ##
    # This formats the specified text using the specified formatter, returning the result
    def render_text(formatter, text)
      Hooks::Formatters.format_text(formatter, text)
    end
 
    ##
    # This formats the specified article, using it's configured formatter, returning the result
    def render_article(article)
      Hooks::Formatters.format_article(article)
    end
 
    ##
    # This builds the menu list from the menu items, returning the markup
    def render_menu
      menu = ""
      menu_items.each_with_index do |item, index|
       menu += "<li>#{link_to item[:text], item[:url]} #{render_link_dot(index, menu_items.size - 1)}</li>"
      end
      menu
    end
 
    ##
    # This returns the date as a relative date (today, yesterday etc)
    def render_relative_date(date)
      date = Date.parse(date, true) unless /Date.*/ =~ date.class.to_s
      days = (date - Date.today).to_i
 
      return 'today' if days >= 0 and days < 1
      return 'tomorrow' if days >= 1 and days < 2
      return 'yesterday' if days >= -1 and days < 0
 
      return "in #{days} days" if days.abs < 60 and days > 0
      return "#{days.abs} days ago" if days.abs < 60 and days < 0
 
      return date.strftime('%A, %B %e') if days.abs < 182
      return date.strftime('%A, %B %e, %Y')
    end
 
    ##
    # This returns the contents of the notifications in the session, clearing it at the same time
    def notifications
      notifications = session[:notifications]
      session[:notifications] = nil
      notifications
    end
 
    ##
    # This returns the names of all possible timezones
    def get_timezones
      TZInfo::Timezone.all.collect { |tz| tz.name }
    end
 
    ##
    # This returns the published at date for an article as a relative date (taking into account timezones)
    def render_relative_published_at(article)
      article.published_at.nil? ? "Not yet" : render_relative_date(TZInfo::Timezone.get(logged_in? ? self.current_user.time_zone : article.user.time_zone).utc_to_local(article.published_at))
    end
 
    ##
    # This returns the markup for the about text in the sidebar
    def render_about_text
      unless @settings.nil? || @settings.about.blank?
        markup = <<-MARKUP
<div class="sidebar-node">
<h3>About</h3>
<p>#{render_text(@settings.about_formatter, @settings.about)}</p>
</div>
MARKUP
      end
      markup
    end
 
    ##
    # This returns the url for the year article index
    def year_url(year)
      url(:year, {:year => year})
    end
 
    ##
    # This returns the url for the month article index
    def month_url(year, month)
      url(:month, {:year => year, :month => Padding::pad_single_digit(month)})
    end
 
    ##
    # This returns the url for the day article index
    def day_url(year, month, day)
      url(:day, {:year => year, :month => Padding::pad_single_digit(month), :day => Padding::pad_single_digit(day)})
    end
 
    ##
    # This returns all menu items, including those provided by plugins
    def menu_items
      items = []
      items << {:text => "Dashboard", :url => url(:admin_dashboard)}
      items << {:text => "Articles", :url => url(:admin_articles)}
      items << {:text => "Plugins", :url => url(:admin_plugins)}
      items << {:text => "Settings", :url => url(:admin_configurations)}
      items << {:text => "Users", :url => url(:users)}
      if self.current_user == :false
        items << {:text => "Login", :url => url(:login)}
      else
        items << {:text => "Logout", :url => url(:logout)}
      end
      Hooks::Menu.menu_items.each { |item| items << item }
      items
    end
 
    ##
    # This either adds the breaking • character unless we're at the end of the collection (based on index and size)
    def render_link_dot(index, collection_size)
      "&nbsp;•" unless index == collection_size
    end
 
    ##
    # This renders all plugin views for the specified hook
    def render_plugin_views(name, options = {})
      output = ""
      Hooks::View.plugin_views.each do |view|
        if view[:name] == name
          if view[:partial]
            # Set the template root, create the template method and call the partial
            _template_root = File.join(view[:plugin].path, "views")
            template_location = _template_root / _template_location("#{view[:partial]}", content_type, view[:name])
            output << partial(template_location, { :with => options[:with], :as => options[:with].class.to_s.downcase.singular })
          else
            # Render the specified text using ERB and the options
            output << Proc.new { |args| ERB.new(view[:content]).result(binding) }.call(options[:with])
          end
        end
      end
      # Return the view markup generated by plugins
      output
    end
 
    ##
    # This returns the full url for an article
    def get_full_url(article)
      "http://#{request.host}#{article.permalink}"
    end
 
    ##
    # This escapes the specified url
    def escape_url(url)
      CGI.escape(url)
    end
 
    ##
    # This returns true if the specified plugin is active, false if it isn't or is unavailable
    def is_plugin_active(name)
      plugin = Plugin.first(:name => name)
      plugin && plugin.active
    end
    
    def link_to_author(author)
      link_to author["name"], author["homepage"]
    end
  end
end