xbel 0.2.0 → 0.2.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.markdown CHANGED
@@ -1,23 +1,34 @@
1
1
  # xbel
2
2
 
3
- Introduces XBEL decorations for Nokogiri. Write support is still in
4
- development.
3
+ Introduces XBEL decorations for Nokogiri.
5
4
 
6
5
  ## Installation
7
6
 
8
7
  gem install xbel
9
8
 
10
- ## Use
9
+ ## Using
11
10
 
12
11
  require 'rubygems'
13
12
  require 'xbel'
14
13
 
15
- xbel = XBEL.parse File.read('complex.xbel')
14
+ ### Reading
16
15
 
17
- with_aliases = true # TODO...
18
- xbel.root.bookmarks(with_aliases).each { |bm| system 'open', bm.href }
16
+ XBEL.open('test/wikipedia.xbel')[:f0].bookmarks.
17
+ map do |bookmark|
18
+ link_to bookmark.title, bookmark.href
19
+ end
19
20
 
20
- ### Note on Patches/Pull Requests
21
+ ### Writing
22
+
23
+ xbel = XBEL.new
24
+ xbel.build_folder 'XBEL' do |folder|
25
+ folder.build_bookmark "boof's xbel", 'http://github.com/boof/xbel'
26
+ end
27
+ File.open('my.xbel', 'w') { |file| file << xbel }
28
+
29
+ _Note: You can use all Nokogiri::XML::Node methods._
30
+
31
+ ## Patches/Pull Requests:
21
32
 
22
33
  1. Fork the project.
23
34
  2. Make your feature addition or bug fix.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -2,6 +2,10 @@ module Nokogiri::Decorators::XBEL
2
2
  module Bookmark
3
3
  include Entry
4
4
 
5
+ def self.extended(node)
6
+ node.initialize_decorator
7
+ end
8
+
5
9
  def modified
6
10
  if value = self['modified'] then Date.parse value end
7
11
  end
@@ -2,9 +2,13 @@ module Nokogiri::Decorators::XBEL
2
2
  module Folder
3
3
  include Entry
4
4
 
5
+ def self.extended(node)
6
+ node.initialize_decorator
7
+ end
8
+
5
9
  # Returns an instance of NodeSet with all valid children for this folder.
6
10
  def entries
7
- xpath './alias', './bookmark', './folder', './separator'
11
+ xpath './alias | ./bookmark | ./folder | ./separator'
8
12
  end
9
13
  # Returns an instance of NodeSet with all aliases for this folder.
10
14
  def aliases
@@ -31,16 +35,17 @@ module Nokogiri::Decorators::XBEL
31
35
  end
32
36
 
33
37
  # Builds a bookmark with given attributes and add it.
34
- def build_bookmark(title, attributes = {}, &block)
38
+ def build_bookmark(title, href, attributes = {}, &block)
35
39
  node = Nokogiri::XML::Node.new('bookmark', document)
36
- assign_to node, attributes.merge('title' => title)
40
+ assign_to node, attributes.merge('title' => title, 'href' => href),
41
+ &block
37
42
 
38
43
  add_child node
39
44
  end
40
45
  # Builds a folder with given attributes and add it.
41
46
  def build_folder(title, attributes = {}, &block)
42
47
  node = Nokogiri::XML::Node.new('folder', document)
43
- assign_to node, attributes.merge('title' => title)
48
+ assign_to node, attributes.merge('title' => title), &block
44
49
 
45
50
  add_child node
46
51
  end
@@ -1,9 +1,9 @@
1
1
  module Nokogiri::Decorators::XBEL
2
2
 
3
- autoload :Folder, "#{ File.dirname __FILE__ }/xbel/folder.rb"
4
- autoload :Seperator, "#{ File.dirname __FILE__ }/xbel/folder.rb"
5
3
  autoload :Bookmark, "#{ File.dirname __FILE__ }/xbel/bookmark.rb"
4
+ autoload :Folder, "#{ File.dirname __FILE__ }/xbel/folder.rb"
6
5
  autoload :Alias, "#{ File.dirname __FILE__ }/xbel/alias.rb"
6
+ autoload :Seperator, "#{ File.dirname __FILE__ }/xbel/folder.rb"
7
7
 
8
8
  def self.extended(base)
9
9
  case base.name
@@ -13,16 +13,28 @@ module Nokogiri::Decorators::XBEL
13
13
  base.extend Bookmark
14
14
  when 'folder'
15
15
  base.extend Folder
16
- when 'separator'
17
- base.extend Separator
18
16
  when 'alias'
19
17
  base.extend Alias
18
+ when 'separator'
19
+ base.extend Separator
20
20
  when 'xbel'
21
21
  base.extend Folder
22
22
  end
23
23
  end
24
24
 
25
25
  module Entry
26
+
27
+ def initialize_decorator
28
+ @info = Hash.new do |info, owner|
29
+ if String === owner
30
+ info[owner] = at "./info/metadata[@owner='#{owner}']"
31
+ else
32
+ info[owner.to_s]
33
+ end
34
+ end
35
+ end
36
+ attr_reader :info
37
+
26
38
  def desc
27
39
  if node = at('./desc') then node.content end
28
40
  end
data/lib/xbel.rb CHANGED
@@ -11,6 +11,10 @@ class XBEL < Nokogiri::XML::Document
11
11
  parse %Q'<!DOCTYPE xbel PUBLIC "+//IDN python.org//DTD XML Bookmark Exchange Language 1.0//EN//XML" "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd"><xbel version="%i.%i"></xbel>' % [major, minor]
12
12
  end
13
13
 
14
+ def self.open(path)
15
+ parse File.read(path.to_s)
16
+ end
17
+
14
18
  # Use <tt>XBEL.parse(string)</tt> create an instance.
15
19
  def initialize(*args)
16
20
  super
@@ -18,6 +22,10 @@ class XBEL < Nokogiri::XML::Document
18
22
  decorate!
19
23
  end
20
24
 
25
+ def [](id)
26
+ root.at("//*[@id='#{ id }']")
27
+ end
28
+
21
29
  # Returns an array of version numbers.
22
30
  def version
23
31
  root.attribute('version').value.split('.').map { |n| n.to_i }
data/test/helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'riot'
3
+ require 'pathname'
3
4
 
4
5
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
6
  $LOAD_PATH.unshift(File.dirname(__FILE__))
data/test/test_xbel.rb CHANGED
@@ -23,65 +23,101 @@ end
23
23
  context 'XBEL' do
24
24
 
25
25
  setup do
26
- path = File.join "#{ File.dirname __FILE__ }", 'wikipedia.xbel'
27
- @xbel = XBEL.parse File.read(path)
26
+ root = Pathname.new File.dirname(__FILE__)
27
+ XBEL.open root.join('wikipedia.xbel')
28
28
  end
29
29
 
30
- asserts('version') { @xbel.version }.equals [1, 0]
30
+ asserts('version') { topic.version }.equals [1, 0]
31
31
 
32
- asserts("title") { @xbel.title }.equals %q"Lesezeichen!"
33
- should('delegate title to root') { @xbel.root.title == @xbel.title }
32
+ asserts('title') { topic.title }.equals 'Lesezeichen!'
33
+ should('delegate title accessor to root') do
34
+ topic.title = 'Bookmarks!'
35
+
36
+ topic.title == topic.root.title and
37
+ topic.title == 'Bookmarks!'
38
+ end
34
39
 
35
40
  context 'Alias' do
36
- setup { @alias = @xbel.root.aliases.first }
37
- should('be an alias') { @alias.alias? }
41
+ setup { topic.root.aliases.first }
42
+ should('be an alias') { topic.alias? }
43
+
44
+ should('have a reference') do
45
+ topic.ref and
46
+ topic.ref == topic.reference
47
+ end
38
48
  end
39
49
 
40
50
  context 'Folder' do
41
- setup { @folder = @xbel.root.folders.first }
51
+ setup { topic[:f0] }
52
+ should('be a folder') { topic.folder? }
42
53
 
43
- asserts('title') { @folder.title }.equals 'Wiki'
44
- asserts('entries') { @folder.entries }.kind_of Nokogiri::XML::NodeSet
45
- asserts('bookmarks') { @folder.bookmarks }.kind_of Nokogiri::XML::NodeSet
46
- asserts('aliases') { @folder.bookmarks }.kind_of Nokogiri::XML::NodeSet
47
- should('be a folder') { @folder.folder? }
54
+ asserts('title') { topic.title }.equals 'Wiki'
55
+ # TODO: check type with <name>?
56
+ asserts('entries') { topic.entries }.kind_of Nokogiri::XML::NodeSet
57
+ asserts('bookmarks') { topic.bookmarks }.kind_of Nokogiri::XML::NodeSet
58
+ asserts('aliases') { topic.aliases }.kind_of Nokogiri::XML::NodeSet
59
+ asserts('folders') { topic.folders }.kind_of Nokogiri::XML::NodeSet
48
60
 
49
61
  context 'Bookmark' do
50
- setup { @bookmark = @folder.bookmarks.first }
62
+ setup { topic.bookmarks.first }
63
+ should('be a bookmark') { topic.bookmark? }
51
64
 
52
- asserts('modified attribute') { @bookmark.modified }.kind_of Date
53
- asserts('visited') { @bookmark.visited }.kind_of Date
54
- asserts('visit') { @bookmark.visit; @bookmark.visited }.equals Date.today
55
- should('be a bookmark') { @bookmark.bookmark? }
65
+ asserts('modified') { topic.modified }.kind_of Date
66
+
67
+ asserts('visited') { topic.visited }.kind_of Date
68
+ asserts('visit') do
69
+ topic.visit
70
+ topic.visited
71
+ end.equals Date.today
56
72
  end
57
73
  context 'new Bookmark' do
58
74
  setup do
59
- @bookmark = @folder.build_bookmark 'Bookmark',
60
- :href => 'http://www.github.com/boof/xbel', :description => 'desc'
75
+ topic.build_bookmark "boof's xbel", 'http://www.github.com/boof/xbel',
76
+ :description => 'Ruby API for XBEL based on Nokogiri.'
61
77
  end
78
+ should('be a bookmark') { topic.bookmark? }
62
79
 
63
- asserts('added attribute') { @bookmark.added }.equals Date.today
64
- asserts('title attribute') { @bookmark.title }.equals 'Bookmark'
65
- asserts('href attribute') { @bookmark.href }.
80
+ asserts('added') { topic.added }.equals Date.today
81
+ asserts('title') { topic.title }.equals 'boof\'s xbel'
82
+ asserts('href') { topic.href }.
66
83
  equals 'http://www.github.com/boof/xbel'
67
- asserts('description attribute') { @bookmark.description }.
68
- equals 'desc'
84
+ asserts('description') { topic.description }.
85
+ equals 'Ruby API for XBEL based on Nokogiri.'
69
86
  end
70
87
 
71
88
  context 'new Folder' do
72
- setup do
73
- @subfolder = @folder.build_folder 'subfolder',
74
- :id => 'id-2', :desc => 'description'
75
- end
76
- asserts('added attribute') { @subfolder.added }.equals Date.today
77
- asserts('title attribute') { @subfolder.title }.equals 'subfolder'
78
- asserts('id attribute') { @subfolder.id }.equals 'id-2'
79
- asserts('desc attribute') { @subfolder.desc }.equals 'description'
89
+ setup { topic.build_folder 'sub', :id => 'id-2', :desc => 'desc' }
90
+ should('be a folder') { topic.folder? }
91
+
92
+ asserts('added') { topic.added }.equals Date.today
93
+ asserts('title') { topic.title }.equals 'sub'
94
+ asserts('id') { topic.id }.equals 'id-2'
95
+ asserts('desc') { topic.desc }.equals 'desc'
80
96
  end
81
97
 
82
98
  context 'new Alias' do
83
- setup { @alias = @folder.build_alias @folder }
84
- should('reference its folder') { @alias.entry == @folder }
99
+ setup { topic.build_alias topic }
100
+ should('be an alias') { topic.alias? }
101
+
102
+ should('reference its folder') { topic.entry == topic.parent }
103
+ end
104
+
105
+ end
106
+
107
+ context 'Bookmark b0' do
108
+ setup { topic[:b0] }
109
+ should('be a bookmark') { topic.bookmark? }
110
+
111
+ context 'Info' do
112
+ setup { topic.info }
113
+ should('be a hash') { Hash === topic }
114
+
115
+ context 'metadata for xbel' do
116
+ setup { topic[:xbel] if topic }
117
+ should('be a node') { Nokogiri::XML::Node === topic }
118
+
119
+ should('be have an alias') { not topic.at('./alias').nil? }
120
+ end
85
121
  end
86
122
 
87
123
  end
data/test/wikipedia.xbel CHANGED
@@ -1,20 +1,25 @@
1
1
  <?xml version="1.0" encoding="ISO-8859-1"?>
2
2
  <!DOCTYPE xbel
3
- PUBLIC "+//IDN python.org//DTD XML Bookmark Exchange Language 1.0//EN//XML"
4
- "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd">
3
+ PUBLIC "+//IDN python.org//DTD XML Bookmark Exchange Language 1.0//EN//XML"
4
+ "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd">
5
5
  <xbel version="1.0">
6
- <title>Lesezeichen!</title>
7
- <desc>Lernen von XBEL am Beispiel von zwei Lesezeichen und einer Verknüpfung</desc>
8
- <folder id="f0" added="2007-11-10">
9
- <title>Wiki</title>
10
- <desc>Webseiten von Autoren-Gemeinschaften</desc>
11
- <bookmark href="http://wikimediafoundation.org/" id="b0" added="2007-11-11" modified="2007-11-14" visited="2007-11-14">
12
- <title>Wikimedia Foundation</title>
13
- </bookmark>
14
- <bookmark href="http://de.wikipedia.org/" id="b1" added="2007-11-11" modified="2007-11-14" visited="2007-12-27">
15
- <title>Wikipedia</title>
16
- </bookmark>
17
- </folder>
18
- <separator />
19
- <alias ref="b1" />
6
+ <title>Lesezeichen!</title>
7
+ <desc>Lernen von XBEL am Beispiel von zwei Lesezeichen und einer Verknüpfung</desc>
8
+ <folder id="f0" added="2007-11-10">
9
+ <title>Wiki</title>
10
+ <desc>Webseiten von Autoren-Gemeinschaften</desc>
11
+ <bookmark href="http://wikimediafoundation.org/" id="b0" added="2007-11-11" modified="2007-11-14" visited="2007-11-14">
12
+ <info>
13
+ <metadata owner="xbel">
14
+ <alias ref="f0" />
15
+ </metadata>
16
+ </info>
17
+ <title>Wikimedia Foundation</title>
18
+ </bookmark>
19
+ <bookmark href="http://de.wikipedia.org/" id="b1" added="2007-11-11" modified="2007-11-14" visited="2007-12-27">
20
+ <title>Wikipedia</title>
21
+ </bookmark>
22
+ </folder>
23
+ <separator />
24
+ <alias ref="b1" />
20
25
  </xbel>
data/xbel.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{xbel}
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Florian A\303\237mann"]
12
- s.date = %q{2009-11-10}
12
+ s.date = %q{2009-11-11}
13
13
  s.description = %q{}
14
14
  s.email = %q{florian.assmann@email.de}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xbel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Florian A\xC3\x9Fmann"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-10 00:00:00 +01:00
12
+ date: 2009-11-11 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency