link2epub 0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6ded3727b3eb3223ef2c324fb86de08e001b8315
4
+ data.tar.gz: 766a4fadfb0050e8c9e13908a4499cf8cafec700
5
+ SHA512:
6
+ metadata.gz: d8e6d0841ca4560388ef7290a00fa149b93feea9e35a03b6dc0fac6d4a333b715b6f038c4af2eaffac096c6f84f4b1e2fb23cf45cc625dfe058aeb4c628be1a1
7
+ data.tar.gz: d157f16bc237fec354ea8d950e3fd8a1c4ed2b79cacdfa2cef9b68a9a771112d08c0fcfff89a4492caffe78e235a6375efbe29c1e0ed465a254805d892c5149a
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gem 'erubis'
4
+ gem "gepub"
5
+ gem "nokogiri"
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2013 lyuehh
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ desc "help"
2
+ task :help do
3
+ puts "commands:"
4
+ puts "rake help"
5
+ puts "rake build"
6
+ end
7
+
8
+ desc "build"
9
+ task :build do
10
+ sh 'gem build link2epub.gemspec'
11
+ end
data/bin/link2epub ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ if ARGV.include?('--help') or ARGV.include?('-h')
3
+ puts "haha"
4
+ exit 0
5
+ end
6
+
7
+ require 'link2epub'
8
+ Link2Epub.new(ARGV[0]).run
@@ -0,0 +1,7 @@
1
+ link: http://coolshell.cn/
2
+ title: 酷壳 – CoolShell.cn
3
+ subtitle: 陈浩的博客
4
+ author: 陈浩
5
+ description: 陈浩的博客
6
+ links_file: links2.txt
7
+ file_name: coolshell.epub
data/lib/link2epub.rb ADDED
@@ -0,0 +1,146 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'open-uri'
5
+ require 'erubis'
6
+ require 'gepub'
7
+ require 'nokogiri'
8
+ require 'yaml'
9
+
10
+ class Links
11
+ def initialize(file)
12
+ @file = file
13
+ end
14
+
15
+ def to_a
16
+ File.readlines(@file)
17
+ end
18
+ end
19
+
20
+ class Article
21
+ attr_accessor :title, :link, :content, :author, :date, :tpl, :file
22
+
23
+ def initialize(link)
24
+ @link = link
25
+ end
26
+
27
+ def to_file
28
+ tpl = File.read(@tpl)
29
+ eruby = Erubis::Eruby.new(tpl)
30
+
31
+ File.open(@file, 'w') do |f|
32
+ f.puts eruby.evaluate(self)
33
+ end
34
+ end
35
+
36
+ def to_s
37
+ puts "link: #{@link}"
38
+ puts "title: #{@title}"
39
+ puts "author: #{@author}"
40
+ # puts "content: #{@content}"
41
+ end
42
+ end
43
+
44
+ class Epub
45
+ attr_accessor :articles, :conf_file, :conf, :links, :titles
46
+
47
+ def initialize(conf_file)
48
+ @conf_file = conf_file
49
+ @conf = YAML::load(File.read(@conf_file))
50
+ @links = Links.new(@conf['links_file']).to_a
51
+ @articles = []
52
+ @titles = []
53
+ end
54
+
55
+ def getArticles
56
+ @links.each_with_index do |l, i|
57
+ art = Article.new(l)
58
+ art.tpl = "./tpl/article.tpl"
59
+ art.file = "./file#{i+1}.html"
60
+ art.content = Nokogiri::HTML(open(art.link)).css('.post .content').to_html
61
+ art.title = Nokogiri::HTML(open(art.link)).css('.post h2').text
62
+ art.author = Nokogiri::HTML(open(art.link)).css('.post .author').text
63
+ art.date = Nokogiri::HTML(open(art.link)).css('.post .date').text
64
+ art.to_file
65
+ @articles.push(art)
66
+ end
67
+ end
68
+
69
+ def getTitles
70
+ @articles.each do |a|
71
+ @titles.push(a.title)
72
+ end
73
+ @titles
74
+ end
75
+
76
+ def createFile(tpl, file)
77
+ eruby = Erubis::Eruby.new(File.read(tpl))
78
+
79
+ File.open(file, 'w') do |f|
80
+ f.puts eruby.evaluate(self)
81
+ end
82
+ end
83
+
84
+ def to_s
85
+ @articles.each do |a|
86
+ puts a.title
87
+ end
88
+ end
89
+
90
+ def to_epub
91
+ getArticles
92
+ getTitles
93
+ createFile('./tpl/nav.tpl', './nav.html')
94
+ createFile('./tpl/index.tpl', './index.html')
95
+ conf = @conf
96
+ articles = @articles
97
+
98
+ builder = GEPUB::Builder.new {
99
+ language 'zh-cn'
100
+ unique_identifier conf['link'], 'BookID', 'URL'
101
+ title conf['title']
102
+ subtitle conf['subtitle']
103
+ creator conf['author']
104
+ date Time.new
105
+ # contributors '123', '456'
106
+
107
+ resources(:workdir => '.') {
108
+ nav 'nav.html'
109
+
110
+ # cover_image 'img/image1.jpg' => 'image1.jpg'
111
+ ordered {
112
+ file 'index.html'
113
+ file 'nav.html'
114
+
115
+ articles.each do |a|
116
+ file a.file
117
+ heading a.title
118
+ end
119
+ }
120
+ }
121
+ }
122
+ builder.generate_epub(@conf['file_name'])
123
+ clean
124
+ end
125
+
126
+ def clean
127
+ FileUtils.rm('./nav.html')
128
+ FileUtils.rm('./index.html')
129
+ @articles.each do |a|
130
+ FileUtils.rm(a.file)
131
+ end
132
+ end
133
+ end
134
+
135
+ class Link2Epub
136
+ attr_accessor :conf
137
+ def initialize(conf)
138
+ @conf = conf
139
+ end
140
+
141
+ def run
142
+ epub = Epub.new(@conf)
143
+ epub.to_epub
144
+ end
145
+ end
146
+ #Link2Epub.new('./conf.yaml').run
data/tpl/article.tpl ADDED
@@ -0,0 +1,13 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
2
+ <html style="font-family: Georgia, serif;" xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN">
3
+ <head>
4
+ <title></title>
5
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
6
+ </head>
7
+ <body>
8
+ <h1><%= @title %></h1>
9
+ <h3>link: <%= @link %></h3>
10
+ <h3>author: <%= @author %></h3>
11
+ <h3>date: <%= @date %></h3>
12
+ <div><%= @content %></div>
13
+ </body>
data/tpl/index.tpl ADDED
@@ -0,0 +1,13 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
2
+ <html style="font-family: Georgia, serif;" xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN">
3
+ <head>
4
+ <title></title>
5
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
6
+ </head>
7
+ <body>
8
+ <h1><%= @conf['title'] %></h1>
9
+ <h2><%= @conf['link'] %></h2>
10
+ <h3><%= @conf['description'] %></h3>
11
+ <h3><%= Time.new %></h3>
12
+ <h3><%= @conf['author'] %></h3>
13
+ </body>
data/tpl/nav.tpl ADDED
@@ -0,0 +1,13 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
2
+ <html style="font-family: Georgia, serif;" xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN">
3
+ <head>
4
+ <title></title>
5
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
6
+ </head>
7
+ <body>
8
+ <ul>
9
+ <% @titles.each_with_index do |i, n| %>
10
+ <li><%= n+1 %>: <%= i %></li>
11
+ <% end %>
12
+ </ul>
13
+ </body>
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: link2epub
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - lyuehh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: erubis
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: gepub
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: nokogiri
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
+ description: link2epub is a tool to turn a list of url links to a epub book.
56
+ email: lyuehh@gmail.com
57
+ executables:
58
+ - link2epub
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README.md
63
+ - Rakefile
64
+ - Gemfile
65
+ - MIT-LICENSE.txt
66
+ - bin/link2epub
67
+ - lib/link2epub.rb
68
+ - tpl/article.tpl
69
+ - tpl/index.tpl
70
+ - tpl/nav.tpl
71
+ - examples/conf.yaml
72
+ homepage: http://github.com/lyuehh/link2epub
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project: ''
92
+ rubygems_version: 2.0.6
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: give me an links list, I make a epub for you
96
+ test_files: []