doctree 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +28 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/doctree.gemspec +30 -0
- data/exe/html2doctree +38 -0
- data/lib/doctree.rb +53 -0
- data/lib/doctree/version.rb +3 -0
- metadata +155 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 48ac7895871e517a7ae8a8d9720cba53fe3f81ec
|
4
|
+
data.tar.gz: b4bbf31434897c08affe812e674a87839fec69dd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c7e0ffa36d43ce48190350d848cf845d61d6c76619ed4c656b77e730cd162f7efa058507376cdfcb8140ecd1d9cb8a75d33152a5ca47be675559f36c8c69f863
|
7
|
+
data.tar.gz: d9b18e500e8135edfda526b5a8cb3a858ab7acca96b7890ef839721968ebe6708ed28cdf48180fc90aa48d0cb433e378346a9a9cde52354116cfb1e3ba12549f
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2016 Ralf Ebert
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# doctree-ruby
|
2
|
+
|
3
|
+
doctree-ruby is a Ruby gem for generating doctrees f.e. from HTML documents.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```
|
8
|
+
$ gem install doctree
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
Use `html2doctree` from the command line to extract links in list items from HTML pages:
|
14
|
+
|
15
|
+
curl https://en.wikipedia.org/wiki/Book | html2doctree --css "#toc"
|
16
|
+
|
17
|
+
See [test/doctree_test.rb](test/doctree_test.rb) for usage examples in Ruby code.
|
18
|
+
|
19
|
+
## Development
|
20
|
+
|
21
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
22
|
+
|
23
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ralfebert/doctree-ruby.
|
28
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "doctree"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/doctree.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'doctree/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "doctree"
|
8
|
+
spec.version = DocTree::VERSION
|
9
|
+
spec.authors = ["Ralf Ebert"]
|
10
|
+
spec.email = ["info@ralfebert.de"]
|
11
|
+
|
12
|
+
spec.summary = %q{Generates table of contents documents in the .doctree format.}
|
13
|
+
spec.homepage = "https://doctree.org/"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_dependency "rubytree"
|
24
|
+
spec.add_dependency "nokogiri"
|
25
|
+
spec.add_dependency "trollop"
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
29
|
+
spec.add_development_dependency "pry"
|
30
|
+
end
|
data/exe/html2doctree
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'doctree'
|
4
|
+
require 'nokogiri'
|
5
|
+
require 'trollop'
|
6
|
+
require 'pry'
|
7
|
+
|
8
|
+
opts = Trollop::options do
|
9
|
+
version "html2doctree"
|
10
|
+
banner <<-EOS
|
11
|
+
|
12
|
+
Converts HTML from STDIN to a doctree.
|
13
|
+
|
14
|
+
Usage:
|
15
|
+
html2doctree [options]
|
16
|
+
|
17
|
+
EOS
|
18
|
+
|
19
|
+
opt :css, "A css selector to restrict content", type: String
|
20
|
+
end
|
21
|
+
|
22
|
+
html = Nokogiri::HTML(STDIN)
|
23
|
+
|
24
|
+
tree = DocTree::DocTreeNode.forHTML(html)
|
25
|
+
|
26
|
+
elements = if css = opts[:css] then elements = html.css(css) else [html] end
|
27
|
+
|
28
|
+
raise "No elements found" if elements.count == 0
|
29
|
+
|
30
|
+
elements.each do |element|
|
31
|
+
tree.map_html_tree(element) do |html|
|
32
|
+
if html.name == 'li' and link = html.css(">a").first
|
33
|
+
DocTree::DocTreeNode.forHTML(link)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
puts tree.to_s
|
data/lib/doctree.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'doctree/version'
|
2
|
+
require 'tree'
|
3
|
+
require 'json'
|
4
|
+
require 'nokogiri'
|
5
|
+
|
6
|
+
module DocTree
|
7
|
+
|
8
|
+
class DocTreeNode < Tree::TreeNode
|
9
|
+
|
10
|
+
attr_accessor :attrs
|
11
|
+
attr_accessor :name
|
12
|
+
|
13
|
+
def attrs
|
14
|
+
@attrs ||= { }
|
15
|
+
end
|
16
|
+
|
17
|
+
def href
|
18
|
+
attrs[:href]
|
19
|
+
end
|
20
|
+
|
21
|
+
def href=(new_value)
|
22
|
+
attrs[:href] = new_value
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_s
|
26
|
+
self.string_value { |node| node.name + if node.attrs and !node.attrs.empty? then " -- " + node.attrs.to_json.gsub("\n", "") else "" end }
|
27
|
+
end
|
28
|
+
|
29
|
+
def string_value(depth = 0, indent = "\t", &block)
|
30
|
+
childStr = self.children.count > 0 ? "\n" + children.map{ |node| node.string_value(depth + 1, &block) }.join("\n") : ""
|
31
|
+
return [indent * depth, block.call(self), childStr].join("")
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.forHTML(html)
|
35
|
+
return DocTreeNode.new(html.title, html) if html.is_a? Nokogiri::XML::Document
|
36
|
+
|
37
|
+
if html.name == 'a'
|
38
|
+
node = DocTreeNode.new(html.text.strip, html)
|
39
|
+
node.href = html["href"].to_s
|
40
|
+
return node
|
41
|
+
end
|
42
|
+
|
43
|
+
raise "Unsupported node \"#{node.name}\", expected a Nokigiri XML/HTML document or <a> element"
|
44
|
+
end
|
45
|
+
|
46
|
+
def map_html_tree(html, &block)
|
47
|
+
parent = if result = block.call(html) then self << result else self end
|
48
|
+
html.children.each { |c| parent.map_html_tree(c, &block) }
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: doctree
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ralf Ebert
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rubytree
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: nokogiri
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: trollop
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.13'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.13'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: minitest
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '5.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '5.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description:
|
112
|
+
email:
|
113
|
+
- info@ralfebert.de
|
114
|
+
executables:
|
115
|
+
- html2doctree
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- ".gitignore"
|
120
|
+
- ".travis.yml"
|
121
|
+
- Gemfile
|
122
|
+
- LICENSE
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- bin/console
|
126
|
+
- bin/setup
|
127
|
+
- doctree.gemspec
|
128
|
+
- exe/html2doctree
|
129
|
+
- lib/doctree.rb
|
130
|
+
- lib/doctree/version.rb
|
131
|
+
homepage: https://doctree.org/
|
132
|
+
licenses:
|
133
|
+
- MIT
|
134
|
+
metadata: {}
|
135
|
+
post_install_message:
|
136
|
+
rdoc_options: []
|
137
|
+
require_paths:
|
138
|
+
- lib
|
139
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
|
+
requirements:
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
requirements: []
|
150
|
+
rubyforge_project:
|
151
|
+
rubygems_version: 2.6.4
|
152
|
+
signing_key:
|
153
|
+
specification_version: 4
|
154
|
+
summary: Generates table of contents documents in the .doctree format.
|
155
|
+
test_files: []
|