lib/bookmarks/netscape_bookmark.rb in bookmarks-0.1.1 vs lib/bookmarks/netscape_bookmark.rb in bookmarks-0.2.0
- old
+ new
@@ -1,10 +1,12 @@
# -*- encoding: utf-8 -*-
module Bookmarks
# Public: A single bookmark in netscape format.
+ #
+ # REVIEW Should we extract some of the class methods?
class NetscapeBookmark
# Public: Init a new NetscapeBookmark.
#
# url - An optional String url.
@@ -18,27 +20,22 @@
@date = date
@tags = tags
@description = description
end
- # Public: Initialize a new NetscapeBookmark object from a line from
- # a bookmarks file.
+ # Public: Add some tags to this bookmark.
#
- # line - String line from a bookmarks file.
+ # list - An Array of String tags.
#
- # Returns a new NetscapeBookmark object.
- def self.from_string line
- url = /HREF="(.*?)"/.match(line)[1]
- title = /HREF=.*>(.*)<\/A>$/.match(line)[1]
- date = /ADD_DATE="(.*?)"/.match(line)[1]
- date = Time.at(date.to_i).to_s
- tags = /TAGS="(.*?)"/.match(line)[1]
- title = prettify_title title, url
- new url: url, title: title, date: date, tags: tags
+ # Returns nothing.
+ def add_tags list
+ unless @tags.empty?
+ @tags += ','
+ end
+ @tags += list.join(',')
end
-
# Public: Set/get the String url.
attr_accessor :url
# Public: Set/get the String title.
attr_accessor :title
@@ -61,9 +58,35 @@
if @description.empty?
str
else
str + "\n<DD>#{@description}"
end
+ end
+
+ # Public: Initialize a new NetscapeBookmark object from a line from
+ # a bookmarks file.
+ #
+ # line - String line from a bookmarks file.
+ #
+ # Returns a new NetscapeBookmark object.
+ def self.from_string line
+ url = /HREF="(.*?)"/.match(line)[1]
+ title = /HREF=.*>(.*)<\/A>$/.match(line)[1]
+ date = /ADD_DATE="(.*?)"/.match(line)[1]
+ date = Time.at(date.to_i).to_s
+ tags = self.extract_tags(line)
+ title = prettify_title title, url
+ new url: url, title: title, date: date, tags: tags
+ end
+
+ # Public: Get tags from a line from a bookmarks file.
+ #
+ # line - String line from a bookmarks file.
+ #
+ # Returns a String with tags or an empty string.
+ def self.extract_tags line
+ md = /TAGS="(.*?)"/.match(line)
+ md ? md[1] : ""
end
# In some case (which?) Delicious miss the title and we have a crapy
# 'None' instead of the original title. Bad news is the original title
# is lost. And there is no good news :) In such case, we build a new