Skip to content

Commit

Permalink
Import for Diigo sidebar plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Frédéric de Villamil committed Apr 27, 2008
0 parents commit 38edc3c
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Rakefile
@@ -0,0 +1,22 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the diigo_sidebar plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the diigo_sidebar plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'DeliciousSidebar'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
5 changes: 5 additions & 0 deletions init.rb
@@ -0,0 +1,5 @@
require 'sidebar'
require 'diigo_sidebar'

DiigoSidebar.view_root = File.dirname(__FILE__) + '/views'

57 changes: 57 additions & 0 deletions lib/diigo.rb
@@ -0,0 +1,57 @@
require 'open-uri'
require 'time'
require 'rexml/document'

class Diigo
include REXML

attr_accessor :url, :items, :link, :title, :days

# This object holds given information of an item
class DiigoItem < Struct.new(:link, :title, :description, :description_link, :date)
def to_s; title end
end

# Pass the url to the RSS feed you would like to keep tabs on
# by default this will request the rss from the server right away and
# fill the items array
def initialize(url, refresh = true)
self.items = []
self.url = url
self.days = {}
self.refresh if refresh
end

# This method lets you refresh the items in the items array
# useful if you keep the object cached in memory and
def refresh
open(@url) do |http|
parse(http.read)
end
end

private

def parse(body)

xml = Document.new(body)

self.items = []
self.link = XPath.match(xml, "//channel/link/text()").first.value rescue ""
self.title = XPath.match(xml, "//channel/title/text()").first.value rescue ""

XPath.each(xml, "//item/") do |elem|
item = DiigoItem.new
item.title = XPath.match(elem, "title/text()").first.value rescue ""
item.link = XPath.match(elem, "link/text()").first.value rescue ""
item.description = XPath.match(elem, "description/text()").first.value rescue ""
item.date = Time.mktime(*ParseDate.parsedate(XPath.match(elem, "dc:date/text()").first.value)) rescue Time.now

item.description_link = item.description
item.description.gsub!(/<\/?a\b.*?>/, "") # remove all <a> tags
items << item
end

self.items = items.sort_by { |item| item.date }.reverse
end
end
35 changes: 35 additions & 0 deletions lib/diigo_sidebar.rb
@@ -0,0 +1,35 @@
class DiigoSidebar < Sidebar
display_name "Diigo"
description 'Bookmarks from <a href="http://diigo.com">Diigo</a> social bookmarking service'

setting :feed, nil, :label => 'Diigo Username'
setting :count, 10, :label => 'Items Limit'
setting :groupdate, false, :input_type => :checkbox, :label => 'Group links by day'
setting :description, false, :input_type => :checkbox, :label => 'Show description'
setting :desclink, false, :input_type => :checkbox, :label => 'Allow links in description'

lifetime 1.hour

def diigo
@diigo ||= Diigo.new("http://www.diigo.com/rss/user/" + feed) rescue nil
end

def parse_request(contents, params)
return unless diigo

if groupdate
@diigo.days = {}
@diigo.items.each_with_index do |d,i|
break if i >= count.to_i
index = d.date.strftime("%Y-%m-%d").to_sym
(@diigo.days[index] ||= []) << d
end
@diigo.days =
@diigo.days.sort_by { |d| d.to_s }.reverse.collect do |d|
{:container => d.last, :date => d.first}
end
else
@diigo.items = @diigo.items.slice(0, count.to_i)
end
end
end
8 changes: 8 additions & 0 deletions test/delicious_sidebar_test.rb
@@ -0,0 +1,8 @@
require 'test/unit'
require File.dirname(__FILE__) + '/../../../../test/test_helper'

class DeliciousSidebarTest < Test::Unit::TestCase
def test_sidebar_is_available
assert Sidebar.available_sidebars.include?(DeliciousSidebar)
end
end
20 changes: 20 additions & 0 deletions views/content.rhtml
@@ -0,0 +1,20 @@
<% if sidebar.diigo -%>
<div id="diigo">
<h3><a href="<%=h sidebar.diigo.link %>"><%=h sidebar.diigo.title %></a></h3>
<% (groupdate ? sidebar.diigo.days : [{ :container => sidebar.diigo.items }]).each do |group| -%>
<% if groupdate -%>
<span class="date"><%=h group[:date].to_s.to_date.strftime("%b %d") %></span>
<% end -%>
<ul>
<% for item in group[:container] %>
<li>
<a href="<%= item.link %>" title="<%= item.description%>"><%=h item.title %></a>
<% if item.description -%>
<br /><span class="desc"><%= desclink ? item.description_link : item.description %></span>
<% end -%>
</li>
<% end -%>
</ul>
<% end -%>
</div>
<% end -%>

0 comments on commit 38edc3c

Please sign in to comment.