require 'vendor/delicious/delicious' require 'liquidized_delicious' module MephistoDelicious def self.authenticate(options={}) Delicious.authenticate(options) end class DelRecentLinks < Liquid::Block Syntax = /((#{Liquid::TagAttributes}\s?,?\s?)*)as\s([a-zA-Z_\.\-]+)/ def initialize(tag_name, markup, tokens) super if markup =~ Syntax @options = parse_options($1) @as = $5 Delicious.authenticate :username => @options.delete(:user), :password => @options.delete(:password) if @options[:user] && @options[:password] else raise Liquid::SyntaxError.new("Syntax Error in tag 'delrecentlinks' - Valid syntax: delrecentlinks [ opt : 'val', opt : 'val' ] as [name]") end end def render(context) result = [] posts = Delicious::Post.recent(evaluate(@options, context)) posts.each_with_index do |item, index| context.stack do context['delrecentlinks'] = { 'index' => index + 1 } context[@as] = item result << render_all(@nodelist, context) end end result end private def parse_options(opt_string) pairs, opts = opt_string.split(','), {} pairs.each do |pair| opt, value = pair.split(':') opt, value = opt.strip, value.strip opts[opt.to_sym] = value end return opts end def evaluate(options, context) evaluated = {} options.each { |opt, value| evaluated[opt] = context[value] } return evaluated end end end