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 !
commit  12a136dd7837ef527727fa7d7ea7e76bb97be81b
tree    ea55ccc308e8ad3fc66aee159aa03e92501ccdad
parent  ec43d7432a4cd6d27765bced40067602a8df814a
mephisto / app / filters / url_filters.rb
100644 170 lines (137 sloc) 5.938 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
require 'digest/md5'
module UrlFilters
  include Mephisto::Liquid::UrlMethods
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::AssetTagHelper
 
  def link_to_article(article, *args)
    options = link_args_to_options(args)
    text = options.delete(:text) || h(article['title'])
    content_tag :a, text, { :href => article['url'], :title => text }.merge(options)
  end
  
  def link_to_page(page, section = nil, *args)
    options = link_args_to_options(args)
    text = options.delete(:text) || h(page['title'])
    content_tag :a, text, page_anchor_options(page, section, { :title => text }.merge(options))
  end
 
  def link_to_comments(article, *args)
    options = link_args_to_options(args)
    text = options.delete(:text) || pluralize(article['comments_count'], 'comment')
    content_tag :a, text, { :href => article['url'] + '#comments', :title => text }.merge(options)
  end
  
  def link_to_section(section, *args)
    options = link_args_to_options(args)
    text = options.delete(:text) || h(section['name'])
    content_tag :a, text, section_anchor_options(section, { :title => text }.merge(options))
  end
 
  def img_tag(img, options = {})
    tag 'img', {:src => asset_url(img), :alt => img.split('.').first }.merge(options)
  end
  
  # Special link that checks for current section. If it exists and it's a paged section, use link_to_page instead.
  def link_to_search_result(article, *args)
    if current_page_section && current_page_section[:is_paged]
      link_to_page(article, current_page_section, *args)
    else
      link_to_article(article, *args)
    end
  end
  
  def stylesheet_url(css)
    absolute_url :stylesheets, css
  end
  
  def javascript_url(js)
    absolute_url :javascripts, js
  end
  
  def asset_url(asset)
    absolute_url :images, asset
  end
 
  def stylesheet(stylesheet, media = nil)
    stylesheet << '.css' unless stylesheet.include? '.'
    tag 'link', :rel => 'stylesheet', :type => 'text/css', :href => stylesheet_url(stylesheet), :media => media
  end
 
  def javascript(javascript)
    javascript << '.js' unless javascript.include? '.'
    content_tag 'script', '', :type => 'text/javascript', :src => javascript_url(javascript)
  end
 
  def gravatar(comment, size=80, default=nil)
    return '' unless comment['author_email']
    url = "http://www.gravatar.com/avatar.php?size=#{size}&gravatar_id=#{Digest::MD5.hexdigest(comment['author_email'])}"
    url << "&default=#{default}" if default
 
    image_tag url, :class => 'gravatar', :size => "#{size}x#{size}", :alt => comment['author']
  end
 
  def link_to_tag(tag, *args)
    options = link_args_to_options(args)
    text = options.delete(:text) || h(tag)
    content_tag :a, text, { :href => tag_url(tag), :rel => 'tag', :title => text }.merge(options)
  end
 
  def link_to_month(section, date = nil, format = 'my', *args)
    options = link_args_to_options(args)
    text = options.delete(:text) || h(format_date(date, format))
    content_tag :a, text, { :href => monthly_url(section, date), :title => text }.merge(options)
  end
 
  def monthly_url(section, date = nil)
    date = parse_date(date)
    archive_url(section, date.year.to_s, date.month.to_s)
  end
 
  def archive_url(section, *pieces)
    File.join(section.url, section['archive_path'], *pieces)
  end
 
  def tag_url(*tags)
    tags = [tags] ; tags.flatten!
    absolute_url @context['site'].source.tag_url(*tags)
  end
 
  def search_url(query, page = nil)
    absolute_url @context['site'].source.search_url(query, page)
  end
 
  def page_url(page, section = nil)
    section ||= current_page_section
    page[:is_page_home] ? section.url : section.url + (section.url == '/' ? '' : '/') + page[:permalink]
  end
 
  def atom_feed(url, title = nil)
    options = {:rel => 'alternate', :type => 'application/atom+xml', :href => absolute_url(url)}
    options[:title] = title unless title.blank?
    tag(:link, options)
  end
 
  def all_comments_feed(title = nil)
    atom_feed '/feed/all_comments.xml', title.blank? ? 'All Comments' : title
  end
 
  def comments_feed(section_or_article, title = nil)
    section_or_article.is_a?(SectionDrop) ?
      atom_feed('/feed/' + section_or_article.source.to_comments_url.join('/'), (title.blank? ? "Comments for #{section_or_article['name']}" : title)) :
      atom_feed(section_or_article.url + '/comments.xml', (title.blank? ? "Comments for #{section_or_article['title']}" : title))
  end
 
  def changes_feed(article, title = nil)
    atom_feed article.url + '/changes.xml', (title.blank? ? "Changes for #{article['title']}" : title)
  end
 
  def articles_feed(section, title = nil)
    atom_feed '/feed/' + section.source.to_feed_url.join('/'), (title.blank? ? "Articles for #{section['name']}" : title)
  end
 
  def next(article, section = nil)
    article.next(section)
  end
 
  def previous(article, section = nil)
    article.previous(section)
  end
 
  private
    # marks a page as class=selected
    def page_anchor_options(page, section = nil, options = {})
      options.update(:href => page_url(page, section))
      options[:class].nil? ? options.update(:class => 'selected') : options[:class] += ' selected' if current_page_article == page
      options
    end
    
    # marks a section as class=selected
    def section_anchor_options(section, options = {})
      options.update(:href => section['url'])
      options[:class].nil? ? options.update(:class => 'selected') : options[:class] += ' selected' if current_page_section && (current_page_section.url == section.url)
      options
    end
    
    def link_args_to_options(args)
      options = {}
      [:text, :title, :id, :class, :rel].zip(args) {|key, value| options[key] = h(value) unless value.blank?}
      options
    end
    
    def current_page_section
      @current_page_section ||= @context['section']
    end
    
    def current_page_article
      @current_page_article ||= @context['article']
    end
end