Module: Potion::Helpers
- Included in:
- Renderable
- Defined in:
- lib/potion.rb,
lib/potion/extensions/photo_helper.rb,
lib/potion/extensions/link_to_helper.rb,
lib/potion/extensions/category_helper.rb
Instance Method Summary collapse
- #category(name) ⇒ Object
- #link_to(arg1, arg2 = nil, &block) ⇒ Object
- #photo(identifier, attributes = {}) ⇒ Object
Instance Method Details
#category(name) ⇒ Object
2 3 4 5 6 7 8 |
# File 'lib/potion/extensions/category_helper.rb', line 2 def category(name) posts = @site.posts.select do |post| post.relative_output_path.split("/").select {|chunk| chunk != ""}[0] == name end posts.sort {|a,b| a.relative_output_path <=> b.relative_output_path} end |
#link_to(arg1, arg2 = nil, &block) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/potion/extensions/link_to_helper.rb', line 2 def link_to(arg1, arg2 = nil, &block) raise "link_to requires either an item and a block, or a content string and an item" if arg2.nil? && block.nil? if block content = block.call item = arg1 else content = arg1 item = arg2 end "<a href=\"#{item.relative_output_path}\">#{content}</a>" end |
#photo(identifier, attributes = {}) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/potion/extensions/photo_helper.rb', line 2 def photo(identifier, attributes = {}) raise "\n\nERROR: The 'photo' helper only works for posts.\n\n" unless self.is_a?(Potion::Post) extensions = [".jpg", ".jpeg", ".png", ".gif"] photos = @static_files.select {|file| extensions.include?(File.extname(file.path).downcase)} if identifier.is_a?(Integer) photo = photos.sort {|a,b| a.path <=> b.path }[identifier - 1] else candidates = photos.select{|photo| File.split(photo.path)[1].include?(identifier)} raise "\n\nERROR: '#{identifier}' could refer to more than one photo: #{candidates.map{|x|File.split(x.path)[1]}.inspect} in #{self.path}\n\n" unless candidates.length == 1 photo = candidates.first end raise "\n\nERROR: No photo matching '#{identifier}' was found in: #{@path}\n\n" if photo.nil? attributes_string = "" attributes.each_pair {|k,v| attributes_string << "#{k}=\"#{v}\" "} "<img #{attributes_string} src=\"#{photo.relative_output_path}\"/>" end |