Module: Sharkey::ImporterExporter

Defined in:
lib/sharkey/importerexporter.rb

Overview

Knows how to import from and export to Netscape Bookmark HTML file.

This format is commonly used when importing/exporting bookmarks from most browsers and tools such as Delicious.

I’m using a modified version of the Ruby Gem Markio (github.com/spajus/markio)

Class Method Summary collapse

Class Method Details

.export(filename) ⇒ Object

Exports



74
75
76
# File 'lib/sharkey/importerexporter.rb', line 74

def export filename
  # Nothing for now
end

.import(filename) ⇒ Object

Imports all Links, their Tags and Categories from a file.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sharkey/importerexporter.rb', line 20

def import filename

  # TODO Before anything, I should make sure it's a HTML file
  #      and it's not corrupted or anything...

  # Opening and parsing the temporary file, all at once
  bookmarks = File.open(filename) { |file| Markio::parse(file) }

  # Now we go through all of them, creating the Categories and Links
  bookmarks.each do |b|

    # First, we make sure the categories of this Link
    # exist.
    #
    # "Folder" is to "Markio" as "Categories" is to "Sharkey"
    #
    # `b.folders` is an array of category names, like:
    #
    #     ["grandparent", "parent", "child"]
    #
    # So all we need to do is keep creating from the
    # first to the last and the whole category hierarchy
    # will derive.
    #
    last_category        = nil
    last_category_parent = nil

    b.folders.each do |category_name|
      last_category_parent = last_category

      last_category = Sharkey::Category.first_or_create(name: category_name)

      if last_category_parent
#            last_category_parent.add_child last_category
      end
    end

    Sharkey::Link.create_link(b.title,
                              b.href,
                              b.add_date,
                              b.tags,
                              if   last_category
                              then if   last_category.id
                                   then last_category.id
                                   else nil
                                   end
                              else nil
                              end,
                              "")
  end

end