opml-handler 0.1.0 → 1.0.0
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.
- checksums.yaml +4 -4
- data/README.md +31 -4
- data/lib/opml-handler/version.rb +1 -1
- data/lib/opml-handler.rb +41 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a893fe56d0bf2920440dcf6f52014d7bc0f754b
|
4
|
+
data.tar.gz: a16826bdedb97c7805770b003f4148b4bd453e45
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1253344d7ce2aa0f28d3bd929c96726296ca269bb6b38a54d8d7ff0ba7ac3027458bfcb6d07d84db5cb2cb27a17fb623d3f5a889cda724c040ebb13a33aebc8
|
7
|
+
data.tar.gz: bb921818be9a5cdd27d51312074d0b287215c6559c8e94052d1f49134996fc45841fc2d9f8558cd14b1ab9916dbb1297aeb3490535aa9c8423f7087b9191bb9d
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Opml::Handler
|
2
2
|
|
3
|
-
Handle opml(xml) in your ruby application
|
3
|
+
Handle opml(xml) in your ruby application, both import and export and support children outlines
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -20,10 +20,10 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
24
|
-
|
23
|
+
You can convert ruby hash to opml string
|
25
24
|
```ruby
|
26
25
|
require 'opml-handler'
|
26
|
+
|
27
27
|
data = [{text: 'First level',
|
28
28
|
children: [{text: 'Second Level',
|
29
29
|
children: [{text: 'Item 1', _note: "Some notes", children: []},
|
@@ -31,11 +31,38 @@ data = [{text: 'First level',
|
|
31
31
|
}
|
32
32
|
]
|
33
33
|
}]
|
34
|
-
|
34
|
+
|
35
|
+
opml = OpmlHandler::Opml.new(outlines_attributes: data, title: "title")
|
35
36
|
|
36
37
|
puts opml.to_xml
|
37
38
|
```
|
38
39
|
|
40
|
+
Or convert opml string to ruby hash
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
content = '<?xml version="1.0" encoding="UTF-8"?>
|
44
|
+
<opml version="1.0">
|
45
|
+
<head>
|
46
|
+
<title>Title</title>
|
47
|
+
</head>
|
48
|
+
<body>
|
49
|
+
<outline text="First level">
|
50
|
+
<outline text="Second Level">
|
51
|
+
<outline text="Item 1" _note="Some notes"/>
|
52
|
+
<outline text="Item 2"/>
|
53
|
+
</outline>
|
54
|
+
</outline>
|
55
|
+
</body>
|
56
|
+
</opml>'
|
57
|
+
|
58
|
+
opml_object = OpmlHandler::Opml.new(content: content)
|
59
|
+
|
60
|
+
opml_object.to_hash
|
61
|
+
|
62
|
+
puts opml_object.title
|
63
|
+
puts opml_object.outlines_attributes
|
64
|
+
```
|
65
|
+
|
39
66
|
## Development
|
40
67
|
|
41
68
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/opml-handler/version.rb
CHANGED
data/lib/opml-handler.rb
CHANGED
@@ -3,25 +3,55 @@ require 'nokogiri'
|
|
3
3
|
module OpmlHandler
|
4
4
|
|
5
5
|
class Opml
|
6
|
-
attr_reader :title
|
6
|
+
attr_reader :title, :content, :outlines_attributes
|
7
7
|
|
8
|
-
def initialize(
|
9
|
-
@
|
10
|
-
@
|
8
|
+
def initialize(params={})
|
9
|
+
@content = params[:content] || ""
|
10
|
+
@outlines_attributes = params[:outlines_attributes] || []
|
11
|
+
@title = params[:title] || "no title"
|
11
12
|
end
|
12
13
|
|
13
14
|
def outlines
|
14
|
-
|
15
|
+
outlines_attributes.map { |data| OpmlHandler::Outline.new(data) }
|
15
16
|
end
|
16
17
|
|
17
18
|
def to_xml
|
18
|
-
|
19
|
-
|
20
|
-
xml.
|
21
|
-
|
19
|
+
@content = begin
|
20
|
+
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
|
21
|
+
xml.opml(version: "1.0") do
|
22
|
+
xml.head { xml.title title }
|
23
|
+
xml.body { outlines.map { |outline| outline.to_xml(xml) } }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
builder.to_xml
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_hash
|
31
|
+
doc = Nokogiri.XML(content)
|
32
|
+
@title = doc.xpath("//head//title").children.first.text
|
33
|
+
@outlines_attributes = nodes_to_hash(doc.xpath("//body//outline"))
|
34
|
+
end
|
35
|
+
|
36
|
+
def nodes_to_hash(nodes)
|
37
|
+
[].tap do |outlines_attributes|
|
38
|
+
nodes.each do |node|
|
39
|
+
outline_attributes = node_to_hash(node)
|
40
|
+
if outline_attributes && node.children
|
41
|
+
outline_attributes[:children] = nodes_to_hash(node.children)
|
42
|
+
end
|
43
|
+
outlines_attributes << outline_attributes
|
44
|
+
end
|
45
|
+
end.compact
|
46
|
+
end
|
47
|
+
|
48
|
+
def node_to_hash(node)
|
49
|
+
return nil if node.attributes.empty?
|
50
|
+
{}.tap do |attributes|
|
51
|
+
node.attributes.each do |k, v|
|
52
|
+
attributes[k.to_sym] = v.value
|
22
53
|
end
|
23
54
|
end
|
24
|
-
builder.to_xml
|
25
55
|
end
|
26
56
|
end
|
27
57
|
|
@@ -39,4 +69,4 @@ module OpmlHandler
|
|
39
69
|
end
|
40
70
|
end
|
41
71
|
|
42
|
-
end
|
72
|
+
end
|