html2record 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +67 -0
- data/Rakefile +38 -0
- data/lib/generators/parser_generator.rb +7 -0
- data/lib/generators/templates/parser.rb +9 -0
- data/lib/html2record.rb +17 -0
- data/lib/html2record/orm/activerecord.rb +27 -0
- data/lib/html2record/parser.rb +19 -0
- data/lib/html2record/version.rb +3 -0
- data/lib/tasks/html2record_tasks.rake +4 -0
- metadata +119 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2012 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Html2record
|
2
|
+
|
3
|
+
This gem provide a simple way that bind html specific element to a record column
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
In Gemfile add follow line:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'html2record'
|
11
|
+
```
|
12
|
+
|
13
|
+
## Get Start
|
14
|
+
|
15
|
+
init parser file
|
16
|
+
```
|
17
|
+
rails g parser Taobao
|
18
|
+
```
|
19
|
+
|
20
|
+
this command will generate a parser into folder app/parsers/
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
class TaobaoParser < Html2record::Parser::Base
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
you can add column binding in class
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class TaobaoParser < Html2record::Parser::Base
|
31
|
+
add :title, '#main .title'
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
In model file include the class
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
include Html2record::ActiveRecord
|
39
|
+
```
|
40
|
+
|
41
|
+
Use the model:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
html =<<-page
|
45
|
+
<html>
|
46
|
+
<body>
|
47
|
+
<div id="main">
|
48
|
+
<div><span class="title">hello</span></div>
|
49
|
+
<div><span class="content">world</span></div>
|
50
|
+
</div>
|
51
|
+
<body>
|
52
|
+
</html>
|
53
|
+
page
|
54
|
+
|
55
|
+
good = Good.new
|
56
|
+
good.apply_html(Taobao,html) # good.title should be 'hello'
|
57
|
+
```
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
This project rocks and uses MIT-LICENSE.
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'Html2record'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
|
30
|
+
Rake::TestTask.new(:test) do |t|
|
31
|
+
t.libs << 'lib'
|
32
|
+
t.libs << 'test'
|
33
|
+
t.pattern = 'test/**/*_test.rb'
|
34
|
+
t.verbose = false
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
task :default => :test
|
data/lib/html2record.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
module Html2record
|
2
|
+
autoload :ActiveRecord, 'html2record/orm/activerecord'
|
3
|
+
autoload :Parser,'html2record/parser'
|
4
|
+
end
|
5
|
+
|
6
|
+
if defined?(Rails)
|
7
|
+
module Html2record
|
8
|
+
class Railtie < Rails::Railtie
|
9
|
+
initializer "html2record.record" do
|
10
|
+
ActiveSupport.on_load :action_controller do
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'active_record'
|
3
|
+
require 'nokogiri'
|
4
|
+
|
5
|
+
module Html2record
|
6
|
+
module ActiveRecord
|
7
|
+
def apply_html(paser_class,html,&block)
|
8
|
+
doc = Nokogiri::HTML.parse(html)
|
9
|
+
paser_class.parsers.each do |x|
|
10
|
+
column = x[:column]
|
11
|
+
selector = x[:selector]
|
12
|
+
xblock = x[:block]
|
13
|
+
content= ''
|
14
|
+
|
15
|
+
element = doc.css(selector)
|
16
|
+
if xblock.nil?
|
17
|
+
element = element.first if element.kind_of?(Array)
|
18
|
+
content = element.text
|
19
|
+
else
|
20
|
+
content = xblock.call(element)
|
21
|
+
end
|
22
|
+
write_attribute(column,content)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
module Html2record
|
4
|
+
module Parser
|
5
|
+
class Base
|
6
|
+
attr_accessor :uri
|
7
|
+
|
8
|
+
class << self
|
9
|
+
attr_accessor :parsers
|
10
|
+
def add(column,selector,&block)
|
11
|
+
@parsers ||= Array.new
|
12
|
+
@parsers << {:column=>column,:selector=>selector, :block=>block}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: html2record
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jonny Zheng
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.2.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: nokogiri
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: mysql2
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '2.0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '2.0'
|
78
|
+
description: ! 'convert html into activerecord '
|
79
|
+
email:
|
80
|
+
- mars131@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- lib/generators/parser_generator.rb
|
86
|
+
- lib/generators/templates/parser.rb
|
87
|
+
- lib/html2record/orm/activerecord.rb
|
88
|
+
- lib/html2record/parser.rb
|
89
|
+
- lib/html2record/version.rb
|
90
|
+
- lib/html2record.rb
|
91
|
+
- lib/tasks/html2record_tasks.rake
|
92
|
+
- MIT-LICENSE
|
93
|
+
- Rakefile
|
94
|
+
- README.rdoc
|
95
|
+
homepage: https://github.com/jonnyzheng/html2record
|
96
|
+
licenses: []
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ! '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 1.8.24
|
116
|
+
signing_key:
|
117
|
+
specification_version: 3
|
118
|
+
summary: convert html into activerecord
|
119
|
+
test_files: []
|