class Bookmarks::NetscapeBookmark

Public: A single bookmark in netscape format.

REVIEW Should we extract some of the class methods?

Attributes

date[RW]

Public: Set/get the Date/DateTime date.

description[RW]

Public: Set/get the String description.

tags[RW]

Public: Set/get the String tags.

title[RW]

Public: Set/get the String title.

url[RW]

Public: Set/get the String url.

Public Class Methods

extract_tags(line) click to toggle source

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.

# File lib/bookmarks/netscape_bookmark.rb, line 86
def self.extract_tags line
  md = /TAGS="(.*?)"/.match(line)
  md ? md[1] : ""
end
from_string(line) click to toggle source

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
new(url: "", title: "", date: nil, tags: "", description: "") click to toggle source

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
prettify_title(title, url) click to toggle source

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

add_tags(list) click to toggle source

Public: Add some tags to this bookmark.

list - An Array of String tags.

Returns nothing.

# File lib/bookmarks/netscape_bookmark.rb, line 30
def add_tags list
  unless @tags.empty?
    @tags += ','
  end
  @tags += list.join(',')
end
to_s() click to toggle source

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