Class: Bookmarks

Inherits:
Object
  • Object
show all
Defined in:
lib/bookmarks.rb

Overview

Main Bookmarks facade class

Constant Summary collapse

PARSER_SOURCE =
{
  ChromeBookmarkParser => "chrome",
  FirefoxBookmarkParser => "firefox",
  SafariBookmarkParser => "safari"
}

Instance Method Summary collapse

Constructor Details

#initialize(search_term = "") ⇒ Bookmarks

Returns a new instance of Bookmarks.



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/bookmarks.rb', line 322

def initialize(search_term = "")
  @conf = BConfig.new
  file_paths = @conf.bookmarks
  @allurls = []
  seen = {}

  loaded_sources = file_paths.count { |p| p && File.exist?(p) }
  @multi_source = loaded_sources > 1

  file_paths.each_with_index do |file_path, source_index|
    next unless file_path && File.exist?(file_path)

    parser_class = detect_parser(file_path)
    source = PARSER_SOURCE[parser_class]
    parser = parser_class.new(file_path, search_term)
    parser.parse

    parser.results.each do |bookmark|
      key = [bookmark.title, bookmark.url]
      next if seen[key]
      seen[key] = true

      @allurls << Bookmark.new(
        bookmark.folder,
        bookmark.title,
        bookmark.url,
        "#{source_index}_#{bookmark.id}",
        source
      )
    end
  end
end

Instance Method Details

#autocompleteObject

output for zsh autocompetion, print out id, title and cleaned url



356
357
358
359
360
361
362
# File 'lib/bookmarks.rb', line 356

def autocomplete
  @allurls.each do |url|
    name = clean_name(url)
    link = clean_link(url)
    puts url.id + ":" + name + ":" + link
  end
end

#bookmark_url(id) ⇒ Object

get link (from id number)



390
391
392
393
394
# File 'lib/bookmarks.rb', line 390

def bookmark_url(id)
  @allurls.each do |url|
    return url.url if id == url.id
  end
end

clean link for completion, remove strange things from any linkurls



380
381
382
383
384
385
386
387
# File 'lib/bookmarks.rb', line 380

def clean_link(url)
  link = url.url.gsub(/[,'"&?].*/, "")
  link.gsub!(/.*:\/+/, "")
  link.delete!(" ")
  # Use half terminal width for URL
  max_width = [TERMWIDTH / 2 - CODEWIDTH, 50].min
  link[0..max_width]
end

#clean_name(url) ⇒ Object

clean title for completion, delete anything not allowed in linktitle



365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/bookmarks.rb', line 365

def clean_name(url)
  # Clean up folder display (remove leading |, show [root] for top-level)
  folder = url.folder.gsub(/^\|/, "")
  folder = (folder == "/") ? "[root]" : folder.chomp("/")

  # Format: [source] folder | title (source only when multi-source)
  prefix = (@multi_source && url.source) ? "[#{url.source}] " : ""
  name = prefix + folder + " | " + url.title.gsub(/[^a-z0-9\-\/_ ]/i, "")
  name.squeeze!("-")
  name.squeeze!(" ")
  # Use half terminal width for name to leave room for URL
  name.window([TERMWIDTH / 2, 50].min)
end