| 1 |
# The methods added to this helper will be available to all templates in the application. |
|---|
| 2 |
require 'digest/sha1' |
|---|
| 3 |
|
|---|
| 4 |
module ApplicationHelper |
|---|
| 5 |
# Basic english pluralizer. |
|---|
| 6 |
# Axe? |
|---|
| 7 |
|
|---|
| 8 |
def pluralize(size, word) |
|---|
| 9 |
case size |
|---|
| 10 |
when 0 then _("no ") + word.pluralize |
|---|
| 11 |
when 1 then "1 #{word}" |
|---|
| 12 |
else "#{size} #{word.pluralize}" |
|---|
| 13 |
end |
|---|
| 14 |
end |
|---|
| 15 |
|
|---|
| 16 |
# Produce a link to the permalink_url of 'item'. |
|---|
| 17 |
def link_to_permalink(item, title, anchor=nil, style=nil) |
|---|
| 18 |
anchor = "##{anchor}" if anchor |
|---|
| 19 |
case item |
|---|
| 20 |
when Article |
|---|
| 21 |
"<a href=\"#{article_path(item)}#{anchor}\" class=\"#{style}\">#{title}</a>" |
|---|
| 22 |
else |
|---|
| 23 |
"<a href=\"#{item.permalink_url}#{anchor}\" class=\"#{style}\">#{title}</a>" |
|---|
| 24 |
end |
|---|
| 25 |
end |
|---|
| 26 |
|
|---|
| 27 |
# The '5 comments' link from the bottom of articles |
|---|
| 28 |
def comments_link(article) |
|---|
| 29 |
link_to_permalink(article,pluralize(article.published_comments.size, _('comment')),'comments') |
|---|
| 30 |
end |
|---|
| 31 |
|
|---|
| 32 |
def trackbacks_link(article) |
|---|
| 33 |
link_to_permalink(article,pluralize(article.published_trackbacks.size, 'trackback'),'trackbacks') |
|---|
| 34 |
end |
|---|
| 35 |
|
|---|
| 36 |
def check_cache(aggregator, *args) |
|---|
| 37 |
hash = "#{aggregator.to_s}_#{args.collect { |arg| Digest::SHA1.hexdigest(arg) }.join('_') }".to_sym |
|---|
| 38 |
controller.cache[hash] ||= aggregator.new(*args) |
|---|
| 39 |
end |
|---|
| 40 |
|
|---|
| 41 |
def js_distance_of_time_in_words_to_now(date) |
|---|
| 42 |
time = _(date.utc.strftime(_("%%a, %%d %%b %%Y %%H:%%M:%%S GMT", date.utc))) |
|---|
| 43 |
timestamp = date.utc.to_i ; |
|---|
| 44 |
"<span class=\"typo_date date gmttimestamp-#{timestamp}\" title=\"#{time}\" >#{time}</span>" |
|---|
| 45 |
end |
|---|
| 46 |
|
|---|
| 47 |
def meta_tag(name, value) |
|---|
| 48 |
tag :meta, :name => name, :content => value unless value.blank? |
|---|
| 49 |
end |
|---|
| 50 |
|
|---|
| 51 |
def date(date) |
|---|
| 52 |
"<span class=\"typo_date\">" + date.utc.strftime(_("%%d. %%b", date.utc)) + "</span>" |
|---|
| 53 |
end |
|---|
| 54 |
|
|---|
| 55 |
def render_theme(options) |
|---|
| 56 |
options[:controller]=Themes::ThemeController.active_theme_name |
|---|
| 57 |
render_component(options) |
|---|
| 58 |
end |
|---|
| 59 |
|
|---|
| 60 |
def toggle_effect(domid, true_effect, true_opts, false_effect, false_opts) |
|---|
| 61 |
"$('#{domid}').style.display == 'none' ? new #{false_effect}('#{domid}', {#{false_opts}}) : new #{true_effect}('#{domid}', {#{true_opts}}); return false;" |
|---|
| 62 |
end |
|---|
| 63 |
|
|---|
| 64 |
def markup_help_popup(markup, text) |
|---|
| 65 |
if markup and markup.commenthelp.size > 1 |
|---|
| 66 |
"<a href=\"#{url_for :controller => '/articles', :action => 'markup_help', :id => markup.id}\" onclick=\"return popup(this, 'Typo Markup Help')\">#{text}</a>" |
|---|
| 67 |
else |
|---|
| 68 |
'' |
|---|
| 69 |
end |
|---|
| 70 |
end |
|---|
| 71 |
|
|---|
| 72 |
# Deprecated helpers |
|---|
| 73 |
typo_deprecate :server_url_for => :url_for |
|---|
| 74 |
|
|---|
| 75 |
def config_value(name) |
|---|
| 76 |
typo_deprecated "Use this_blog.#{name} instead." |
|---|
| 77 |
this_blog.send(name) |
|---|
| 78 |
end |
|---|
| 79 |
|
|---|
| 80 |
def config |
|---|
| 81 |
typo_deprecated "Use this_blog.configname instead of config[:configname]" |
|---|
| 82 |
raise "Unimplemented" |
|---|
| 83 |
end |
|---|
| 84 |
|
|---|
| 85 |
def item_link(title, item, anchor=nil) |
|---|
| 86 |
typo_deprecated "Use link_to_permalink instead of item_link" |
|---|
| 87 |
link_to_permalink(item, title, anchor) |
|---|
| 88 |
end |
|---|
| 89 |
|
|---|
| 90 |
alias_method :article_link, :item_link |
|---|
| 91 |
alias_method :page_link, :item_link |
|---|
| 92 |
alias_method :comment_url_link, :item_link |
|---|
| 93 |
|
|---|
| 94 |
def url_of(item, only_path=true, anchor=nil) |
|---|
| 95 |
typo_deprecated "Use item.permalink_url instead" |
|---|
| 96 |
item.permalink_url |
|---|
| 97 |
end |
|---|
| 98 |
|
|---|
| 99 |
alias_method :trackback_url, :url_of |
|---|
| 100 |
alias_method :comment_url, :url_of |
|---|
| 101 |
alias_method :article_url, :url_of |
|---|
| 102 |
alias_method :page_url, :url_of |
|---|
| 103 |
|
|---|
| 104 |
def html(content, what = :all, deprecated = false) |
|---|
| 105 |
if deprecated |
|---|
| 106 |
msg = "use html(#{content.class.to_s.underscore}" + ((what == :all) ? "" : ", #{what.inspect}") + ")" |
|---|
| 107 |
typo_deprecated(msg) |
|---|
| 108 |
end |
|---|
| 109 |
|
|---|
| 110 |
content.html(what) |
|---|
| 111 |
end |
|---|
| 112 |
|
|---|
| 113 |
def article_html(article, what = :all) |
|---|
| 114 |
html(article, what, true) |
|---|
| 115 |
end |
|---|
| 116 |
|
|---|
| 117 |
def comment_html(comment) |
|---|
| 118 |
html(comment, :body, true) |
|---|
| 119 |
end |
|---|
| 120 |
|
|---|
| 121 |
def page_html(page) |
|---|
| 122 |
html(page, :body, true) |
|---|
| 123 |
end |
|---|
| 124 |
|
|---|
| 125 |
def strip_html(text) |
|---|
| 126 |
typo_deprecated "use text.strip_html" |
|---|
| 127 |
text.strip_html |
|---|
| 128 |
end |
|---|
| 129 |
|
|---|
| 130 |
def admin_tools_for(model) |
|---|
| 131 |
type = model.class.to_s.downcase |
|---|
| 132 |
tag = [] |
|---|
| 133 |
tag << content_tag("div", |
|---|
| 134 |
link_to_remote('nuke', { |
|---|
| 135 |
:url => feedback_path(model.id), |
|---|
| 136 |
:method => :delete, |
|---|
| 137 |
:confirm => "Are you sure you want to delete this #{type}?" |
|---|
| 138 |
}, :class => "admintools") << |
|---|
| 139 |
link_to('edit', { |
|---|
| 140 |
:controller => "admin/#{type.pluralize}", |
|---|
| 141 |
:article_id => model.article.id, |
|---|
| 142 |
:action => "edit", :id => model |
|---|
| 143 |
}, :class => "admintools"), |
|---|
| 144 |
:id => "admin_#{type}_#{model.id}", :style => "display: none") |
|---|
| 145 |
tag.join(" | ") |
|---|
| 146 |
end |
|---|
| 147 |
|
|---|
| 148 |
def onhover_show_admin_tools(type, id = nil) |
|---|
| 149 |
tag = [] |
|---|
| 150 |
tag << %{ onmouseover="if (getCookie('is_admin') == 'yes') { Element.show('admin_#{[type, id].compact.join('_')}'); }" } |
|---|
| 151 |
tag << %{ onmouseout="Element.hide('admin_#{[type, id].compact.join('_')}');" } |
|---|
| 152 |
tag |
|---|
| 153 |
end |
|---|
| 154 |
|
|---|
| 155 |
# Generate the image tag for a commenters gravatar based on their email address |
|---|
| 156 |
# Valid options are described at http://www.gravatar.com/implement.php |
|---|
| 157 |
def gravatar_tag(email, options={}) |
|---|
| 158 |
options.update(:gravatar_id => Digest::MD5.hexdigest(email.strip)) |
|---|
| 159 |
options[:default] = CGI::escape(options[:default]) if options.include?(:default) |
|---|
| 160 |
options[:size] ||= 60 |
|---|
| 161 |
|
|---|
| 162 |
image_tag("http://www.gravatar.com/avatar.php?" << |
|---|
| 163 |
options.map { |key,value| "#{key}=#{value}" }.sort.join("&"), :class => "gravatar") |
|---|
| 164 |
end |
|---|
| 165 |
|
|---|
| 166 |
def feed_title |
|---|
| 167 |
return @feed_title if @feed_title |
|---|
| 168 |
return @page_title \ |
|---|
| 169 |
? "#{this_blog.blog_name} : #{@page_title}" \ |
|---|
| 170 |
: this_blog.blog_name |
|---|
| 171 |
end |
|---|
| 172 |
|
|---|
| 173 |
def author_link(article) |
|---|
| 174 |
if this_blog.link_to_author and article.user and article.user.email.to_s.size>0 |
|---|
| 175 |
"<a href=\"mailto:#{h article.user.email}\">#{h article.user.name}</a>" |
|---|
| 176 |
elsif article.user and article.user.name.to_s.size>0 |
|---|
| 177 |
h article.user.name |
|---|
| 178 |
else |
|---|
| 179 |
h article.author |
|---|
| 180 |
end |
|---|
| 181 |
end |
|---|
| 182 |
|
|---|
| 183 |
def page_header |
|---|
| 184 |
page_header_includes = contents.collect { |c| c.whiteboard }.collect do |w| |
|---|
| 185 |
w.select {|k,v| k =~ /^page_header_/}.collect do |(k,v)| |
|---|
| 186 |
v = v.chomp |
|---|
| 187 |
# trim the same number of spaces from the beginning of each line |
|---|
| 188 |
# this way plugins can indent nicely without making ugly source output |
|---|
| 189 |
spaces = /\A[ \t]*/.match(v)[0].gsub(/\t/, " ") |
|---|
| 190 |
v.gsub!(/^#{spaces}/, ' ') # add 2 spaces to line up with the assumed position of the surrounding tags |
|---|
| 191 |
end |
|---|
| 192 |
end.flatten.uniq |
|---|
| 193 |
( |
|---|
| 194 |
<<-HTML |
|---|
| 195 |
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> |
|---|
| 196 |
#{ meta_tag 'ICBM', this_blog.geourl_location unless this_blog.geourl_location.empty? } |
|---|
| 197 |
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="#{ url_for :controller => '/xml', :action => 'rsd' }" /> |
|---|
| 198 |
<link rel="alternate" type="application/atom+xml" title="Atom" href="#{ @auto_discovery_url_atom }" /> |
|---|
| 199 |
<link rel="alternate" type="application/rss+xml" title="RSS" href="#{ @auto_discovery_url_rss }" /> |
|---|
| 200 |
#{ stylesheet_link_tag 'typo_code.css', :media => 'all' } |
|---|
| 201 |
#{ stylesheet_link_tag 'user-styles.css', :media => 'all' } |
|---|
| 202 |
#{ javascript_include_tag "lang/" + Localization.lang.to_s } |
|---|
| 203 |
#{ javascript_include_tag "cookies" } |
|---|
| 204 |
#{ javascript_include_tag "prototype" } |
|---|
| 205 |
#{ javascript_include_tag "effects" } |
|---|
| 206 |
#{ javascript_include_tag "typo" } |
|---|
| 207 |
#{ page_header_includes.join("\n") } |
|---|
| 208 |
<script type="text/javascript">#{ @content_for_script }</script> |
|---|
| 209 |
HTML |
|---|
| 210 |
).chomp |
|---|
| 211 |
end |
|---|
| 212 |
end |
|---|