class Bookmarks::NetscapeBookmark
Public: A single bookmark in netscape format.
REVIEW Should we extract some of the class methods?
Attributes
Public: Set/get the Date/DateTime date.
Public: Set/get the String description.
Public: Set/get the String title.
Public: Set/get the String url.
Public Class Methods
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.
# File lib/bookmarks/netscape_bookmark.rb, line 71 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: Init a new NetscapeBookmark
.
url - An optional String url. title - An optional String title. date - An optional Date/DateTime date. tags - An optional String tags. Tags are comma separated. description - An optional String description.
# File lib/bookmarks/netscape_bookmark.rb, line 17 def initialize url: "", title: "", date: nil, tags: "", description: "" @url = url @title = title @date = date @tags = tags @description = description 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 title from the url.
Examples
# Before expand_title @title # => 'None' @url # => "http://designmodo.com/flat-design-colors/" # After expand_title @title # => 'flat design colors'
Returns String prettified title.
# File lib/bookmarks/netscape_bookmark.rb, line 105 def self.prettify_title title, url return title unless title == '' || title == 'None' title = url # Remove '/' at the end if it exists. title = title.gsub(/\/$/, '') # Keep the last element of the url. title = title.match(/^.*\/(.*)/)[1] # Substitute each '-' by a space. title = title.gsub('-', ' ') end
Public Instance Methods
Public: Returns the bookmark as a String.
# File lib/bookmarks/netscape_bookmark.rb, line 53 def to_s str = "<DT><A HREF=\"#{@url}\" " + "ADD_DATE=\"#{@date.nil? ? "" : @date.to_time.to_i}\" " + "TAGS=\"#{@tags}\">" + (@title.empty? ? 'None' : @title) + "</A>" if @description.empty? str else str + "\n<DD>#{@description}" end end