segfault / mephisto_feedreader

my mephisto feed reader plugin, allows for the display of rss feeds in your layout

This URL has Read+Write access

mephisto_feedreader / lib / feedreader.rb
100644 51 lines (43 sloc) 1.284 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
# $Id$
# Mephisto FeedReader plugin
require 'feed_tools'
require 'liquid'
 
class FeedReader < Liquid::Block
  Syntax = /((#{Liquid::TagAttributes}\s?,?\s?)*)as\s(#{Liquid::VariableSignature}+)/
 
  def initialize(tag_name, markup, tokens)
    super
    if markup =~ Syntax
      @options = parse_options( $1 )
      @as = $5
    else
      raise Liquid::SyntaxError.new("Syntax Error in tag 'feedreader'")
    end
  end
 
  def render(context)
    result = []
    FeedTools.configurations[:feed_cache] = FeedTools::DatabaseFeedCache
    ft_feed = FeedTools::Feed.open(@options[:url])
    ft_entries = ft_feed.entries
    max = @options[:max]
    max = 15 unless max
    max = max.to_i
    max = ft_entries.length if ft_entries.length < max
    view_range = 0..max
 
    ft_entries[view_range].each_with_index do |ent, index|
      context['feedreader'] = { 'index' => index + 1 }
      context[@as] = ent
      result << render_all(@nodelist, context)
    end
 
    result
  end
 
  private
  def parse_options(opt_string)
    opts = {}
    pairs = opt_string.split(',')
    pairs.each do |pair|
      opt, value = pair.split(/:[^\/]/)
      opts[opt.strip.to_sym] = value.strip.gsub(/\'/,'')
    end
 
    opts
  end
end