markio 0.0.3 → 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +40 -14
- data/lib/markio/bookmark.rb +1 -1
- data/lib/markio/builder.rb +38 -9
- data/lib/markio/parser.rb +14 -1
- data/lib/markio/version.rb +1 -1
- data/markio.gemspec +2 -2
- data/spec/builder_spec.rb +54 -6
- metadata +4 -4
data/README.md
CHANGED
@@ -1,33 +1,59 @@
|
|
1
1
|
# Markio
|
2
2
|
|
3
|
-
A
|
4
|
-
http://msdn.microsoft.com/en-us/library/aa753582(v=vs.85).aspx
|
3
|
+
A Ruby Gem for parsing [Netscape Bookmark File Format](http://msdn.microsoft.com/en-us/library/aa753582\(v=vs.85\).aspx)
|
5
4
|
|
6
5
|
## Installation
|
7
6
|
|
8
7
|
Add this line to your application's Gemfile:
|
9
8
|
|
10
|
-
|
9
|
+
```ruby
|
10
|
+
gem 'markio'
|
11
|
+
```
|
11
12
|
|
12
13
|
And then execute:
|
13
14
|
|
14
|
-
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
15
18
|
|
16
19
|
Or install it yourself as:
|
17
20
|
|
18
|
-
|
21
|
+
```bash
|
22
|
+
$ gem install markio
|
23
|
+
```
|
19
24
|
|
20
25
|
## Usage
|
21
26
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
### Parsing bookmarks file
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
bookmarks = Markio::parse(File.open('/path/to/bookmarks.html'))
|
31
|
+
bookmarks.each do |b|
|
32
|
+
b.title # String
|
33
|
+
b.href # String with bookmark URL
|
34
|
+
b.folders # Array of strings - folders (tags)
|
35
|
+
b.add_date # DateTime
|
36
|
+
b.last_visit # DateTime
|
37
|
+
b.last_modified # DateTime
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
### Building bookmarks file
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
builder = Markio::Builder.new
|
45
|
+
builder.bookmarks << Bookmark.create({
|
46
|
+
:title => "Google",
|
47
|
+
:href => "http://google.com"
|
48
|
+
})
|
49
|
+
file_contents = builder.build_string
|
50
|
+
File.open('/path/to/bookmarks.html', 'w') { |f| f.write file_contents }
|
51
|
+
```
|
52
|
+
|
53
|
+
## TODO
|
54
|
+
|
55
|
+
- Builder output to file
|
56
|
+
- Builder should not ignore bookmark folders
|
31
57
|
|
32
58
|
## Contributing
|
33
59
|
|
data/lib/markio/bookmark.rb
CHANGED
data/lib/markio/builder.rb
CHANGED
@@ -23,6 +23,19 @@ class Markio::Builder
|
|
23
23
|
|
24
24
|
def build
|
25
25
|
|
26
|
+
bookmarks = { nil => [] }
|
27
|
+
@bookmarks.each do |b|
|
28
|
+
|
29
|
+
if b.folders.any?
|
30
|
+
b.folders.each do |f|
|
31
|
+
bookmarks[f] ||= []
|
32
|
+
bookmarks[f] << b
|
33
|
+
end
|
34
|
+
else
|
35
|
+
bookmarks[nil] << b
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
26
39
|
docparts = Nokogiri::HTML::DocumentFragment.parse ''
|
27
40
|
Nokogiri::HTML::Builder.with(docparts) do |html|
|
28
41
|
html.comment <<END
|
@@ -34,18 +47,34 @@ END
|
|
34
47
|
html.title @title
|
35
48
|
html.dl {
|
36
49
|
html.p
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
50
|
+
bookmarks.keys.each do |f|
|
51
|
+
unless f.nil?
|
52
|
+
html.dt {
|
53
|
+
html.h3 f
|
54
|
+
html.dl {
|
55
|
+
html.p
|
56
|
+
bookmarks[f].each { |b| build_bookmark html, b }
|
57
|
+
}
|
58
|
+
}
|
59
|
+
else
|
60
|
+
bookmarks[nil].each { |b| build_bookmark html, b }
|
61
|
+
end
|
45
62
|
end
|
46
63
|
}
|
47
64
|
end
|
48
65
|
|
49
|
-
|
66
|
+
"<!DOCTYPE NETSCAPE-Bookmark-file-1>\n" << docparts.to_html
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def build_bookmark(html, b)
|
72
|
+
html.dt {
|
73
|
+
params = { :href => b.href }
|
74
|
+
params[:add_date] = b.add_date.to_time.to_i if b.add_date
|
75
|
+
params[:last_visit] = b.last_visit.to_time.to_i if b.last_visit
|
76
|
+
params[:last_modified] = b.last_modified.to_time.to_i if b.last_modified
|
77
|
+
html.a(b.title, params)
|
78
|
+
}
|
50
79
|
end
|
51
80
|
end
|
data/lib/markio/parser.rb
CHANGED
@@ -12,7 +12,7 @@ module Markio
|
|
12
12
|
traverse(@document, []) do |bookmark|
|
13
13
|
bookmarks << bookmark
|
14
14
|
end
|
15
|
-
bookmarks
|
15
|
+
consolidate bookmarks
|
16
16
|
end
|
17
17
|
|
18
18
|
private
|
@@ -55,5 +55,18 @@ module Markio
|
|
55
55
|
Time.at(timestamp.to_i).to_datetime if timestamp
|
56
56
|
end
|
57
57
|
|
58
|
+
def consolidate(bookmarks)
|
59
|
+
consolidated = []
|
60
|
+
bookmarks.each do |b|
|
61
|
+
index = consolidated.index b
|
62
|
+
unless index.nil?
|
63
|
+
consolidated[index].folders += b.folders
|
64
|
+
else
|
65
|
+
consolidated << b
|
66
|
+
end
|
67
|
+
end
|
68
|
+
consolidated
|
69
|
+
end
|
70
|
+
|
58
71
|
end
|
59
72
|
end
|
data/lib/markio/version.rb
CHANGED
data/markio.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |gem|
|
|
9
9
|
gem.authors = ["Tomas Varaneckas"]
|
10
10
|
gem.email = ["tomas.varaneckas@gmail.com"]
|
11
11
|
gem.description = %q{Import/export utility for Netscape Bookmark Format}
|
12
|
-
gem.summary = %q{
|
13
|
-
gem.homepage = ""
|
12
|
+
gem.summary = %q{Handles parsing and building Netscabe Bookmark Format in Ruby way.}
|
13
|
+
gem.homepage = "https://github.com/spajus/markio"
|
14
14
|
|
15
15
|
gem.files = `git ls-files`.split($/)
|
16
16
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
data/spec/builder_spec.rb
CHANGED
@@ -32,12 +32,12 @@ module Markio
|
|
32
32
|
it 'should be able to parse what it builds' do
|
33
33
|
builder = Builder.new
|
34
34
|
builder.bookmarks << Bookmark.create({
|
35
|
-
:title => "Test bookmark",
|
35
|
+
:title => "Test bookmark 1",
|
36
36
|
:href => "http://test.com"
|
37
37
|
})
|
38
38
|
builder.bookmarks << Bookmark.create({
|
39
|
-
:title => "Test bookmark",
|
40
|
-
:href => "http://
|
39
|
+
:title => "Test bookmark 2",
|
40
|
+
:href => "http://test2.com",
|
41
41
|
:add_date => Date.parse('2012-01-12 11:22:33').to_datetime,
|
42
42
|
:last_visit => Date.parse('2012-01-13 10:20:30').to_datetime,
|
43
43
|
:last_modified => Date.parse('2012-01-14 10:21:31').to_datetime
|
@@ -54,11 +54,11 @@ module Markio
|
|
54
54
|
builder = Builder.new
|
55
55
|
builder.bookmarks << Bookmark.create({
|
56
56
|
:title => "Test bookmark",
|
57
|
-
:href => "http://
|
57
|
+
:href => "http://test1.com"
|
58
58
|
})
|
59
59
|
builder.bookmarks << Bookmark.create({
|
60
|
-
:title => "Test bookmark",
|
61
|
-
:href => "http://
|
60
|
+
:title => "Test bookmark 2",
|
61
|
+
:href => "http://test2.com",
|
62
62
|
:add_date => Date.parse('2012-01-12 11:22:33').to_datetime,
|
63
63
|
:last_visit => Date.parse('2012-01-13 10:20:30').to_datetime,
|
64
64
|
:last_modified => Date.parse('2012-01-14 10:21:31').to_datetime
|
@@ -69,5 +69,53 @@ module Markio
|
|
69
69
|
builder.bookmarks = bookmarks
|
70
70
|
builder.build_string.should eq file_contents
|
71
71
|
end
|
72
|
+
|
73
|
+
it 'should group bookmarks into folders' do
|
74
|
+
builder = Builder.new
|
75
|
+
builder.bookmarks << Bookmark.create({
|
76
|
+
:title => "Test bookmark 1",
|
77
|
+
:href => "http://test1.com",
|
78
|
+
:folders => ["One"]
|
79
|
+
})
|
80
|
+
builder.bookmarks << Bookmark.create({
|
81
|
+
:title => "Test bookmark 2",
|
82
|
+
:href => "http://test2.com",
|
83
|
+
:folders => ["Two"]
|
84
|
+
})
|
85
|
+
file_contents = builder.build_string
|
86
|
+
file_contents.should match "One"
|
87
|
+
file_contents.should match "Two"
|
88
|
+
bookmarks = Markio.parse file_contents
|
89
|
+
bookmarks.length.should eq builder.bookmarks.length
|
90
|
+
bookmarks.each do |b|
|
91
|
+
builder.bookmarks.should include b
|
92
|
+
b.folders.length.should eq 1
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'should work with multi-folder bookmarks' do
|
97
|
+
builder = Builder.new
|
98
|
+
builder.bookmarks << Bookmark.create({
|
99
|
+
:title => "Test bookmark 1",
|
100
|
+
:href => "http://test1.com",
|
101
|
+
:folders => ["One", "Shared"]
|
102
|
+
})
|
103
|
+
builder.bookmarks << Bookmark.create({
|
104
|
+
:title => "Test bookmark 2",
|
105
|
+
:href => "http://test2.com",
|
106
|
+
:folders => ["Two", "Shared"]
|
107
|
+
})
|
108
|
+
file_contents = builder.build_string
|
109
|
+
file_contents.should match "One"
|
110
|
+
file_contents.should match "Two"
|
111
|
+
file_contents.should match "Shared"
|
112
|
+
bookmarks = Markio.parse file_contents
|
113
|
+
bookmarks.length.should eq builder.bookmarks.length
|
114
|
+
bookmarks.each do |b|
|
115
|
+
builder.bookmarks.should include b
|
116
|
+
b.folders.length.should eq 2
|
117
|
+
b.folders.should include "Shared"
|
118
|
+
end
|
119
|
+
end
|
72
120
|
end
|
73
121
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: markio
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.1'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|
@@ -101,7 +101,7 @@ files:
|
|
101
101
|
- spec/builder_spec.rb
|
102
102
|
- spec/parser_spec.rb
|
103
103
|
- spec/spec_helper.rb
|
104
|
-
homepage:
|
104
|
+
homepage: https://github.com/spajus/markio
|
105
105
|
licenses: []
|
106
106
|
post_install_message:
|
107
107
|
rdoc_options: []
|
@@ -124,7 +124,7 @@ rubyforge_project:
|
|
124
124
|
rubygems_version: 1.8.24
|
125
125
|
signing_key:
|
126
126
|
specification_version: 3
|
127
|
-
summary:
|
127
|
+
summary: Handles parsing and building Netscabe Bookmark Format in Ruby way.
|
128
128
|
test_files:
|
129
129
|
- spec/assets/corrupted.html
|
130
130
|
- spec/assets/many_bookmarks.html
|