docrails_kindle 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,47 @@
1
+ # docrails_kindle
2
+
3
+ This gem lets you generate a Kindle version of the [Ruby on Rails Guides][railsguide] with
4
+ working section and article navigation.
5
+
6
+ [railsguide]:http://guides.rubyonrails.org/
7
+
8
+ ## Install
9
+
10
+ gem install docrails_kindle
11
+
12
+ ## Prerequisistes
13
+
14
+ Download [KindleGen 2][kindlegen] and put it on your PATH.
15
+
16
+ [kindlegen]:http://www.amazon.com/gp/feature.html?ie=UTF8&docId=1000234621
17
+
18
+ ## How to use
19
+
20
+ gem install railsguides_kindle
21
+ git clone git@github.com:lifo/docrails.git
22
+ cd docrails/railties/guides
23
+ KINDLE=1 rake generate_guides
24
+ cd output/kindle
25
+ docrails_kindle
26
+
27
+ The output document will be called something like
28
+ `rails-guide.2012-11-18.mobi`. Don't confuse it with the other mobi document
29
+ that gets generated by `rake genereate_guides`, which looks like
30
+ `ruby_on_rails_guides_16c5f48.mobi`.
31
+
32
+ You can transfer this mobi file to your Kindle.
33
+
34
+
35
+ ## Contribute
36
+
37
+ This program is a rough prototype. Some internal links may not work. Other
38
+ defects in the resulting ebook may exist. Please file issues.
39
+
40
+ Also, if the `docsrails` team would like to incorporate this into their
41
+ `rake` tasks code, please take and modify my code as you see fit.
42
+
43
+
44
+ ## Author
45
+
46
+ Daniel Choi http://github.com/danchoi
47
+
@@ -0,0 +1,71 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'bundler'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), 'lib')
7
+
8
+ desc "release and build and push new website"
9
+ task :push => [:release, :web]
10
+
11
+ desc "Bumps version number up one and git commits"
12
+ task :bump do
13
+ basefile = "lib/vmail/version.rb"
14
+ file = File.read(basefile)
15
+ oldver = file[/VERSION = '(\d.\d.\d)'/, 1]
16
+ newver_i = oldver.gsub(".", '').to_i + 1
17
+ newver = ("%.3d" % newver_i).split(//).join('.')
18
+ puts oldver
19
+ puts newver
20
+ puts "Bumping version: #{oldver} => #{newver}"
21
+ newfile = file.gsub("VERSION = '#{oldver}'", "VERSION = '#{newver}'")
22
+ File.open(basefile, 'w') {|f| f.write newfile}
23
+ `git commit -am 'Bump'`
24
+ end
25
+
26
+
27
+ desc "build and push website"
28
+ task :web => :build_webpage do
29
+ puts "Building and pushing website"
30
+ Dir.chdir "../project-webpages" do
31
+ `scp out/vmail.html zoe2@instantwatcher.com:~/danielchoi.com/public/software/`
32
+ `rsync -avz out/images-vmail zoe2@instantwatcher.com:~/danielchoi.com/public/software/`
33
+ `rsync -avz out/stylesheets zoe2@instantwatcher.com:~/danielchoi.com/public/software/`
34
+ `rsync -avz out/lightbox2 zoe2@instantwatcher.com:~/danielchoi.com/public/software/`
35
+ end
36
+ `open http://danielchoi.com/software/vmail.html`
37
+ end
38
+
39
+ desc "build webpage"
40
+ task :build_webpage do
41
+ `cp README.markdown ../project-webpages/src/vmail.README.markdown`
42
+ `cp coverage.markdown ../project-webpages/src/vmail.coverage.markdown`
43
+ Dir.chdir "../project-webpages" do
44
+ puts `ruby gen.rb vmail #{Vmail::VERSION}`
45
+ #`open out/vmail.html`
46
+ end
47
+ end
48
+
49
+
50
+ desc "git push and rake release bumped version"
51
+ task :bumped do
52
+ puts `git push && rake release`
53
+ Rake::Task["web"].execute
54
+ end
55
+
56
+ desc "Run tests"
57
+ task :test do
58
+ $:.unshift File.expand_path("test")
59
+ require 'test_helper'
60
+ Dir.chdir("test") do
61
+ Dir['*_test.rb'].each do |x|
62
+ puts "requiring #{x}"
63
+ require x
64
+ end
65
+ end
66
+
67
+ MiniTest::Unit.autorun
68
+ end
69
+
70
+ task :default => :test
71
+
@@ -0,0 +1,110 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # take toc.ncx
4
+
5
+ require 'nokogiri'
6
+ require 'fileutils'
7
+ require 'yaml'
8
+ require 'date'
9
+
10
+ # Get html pages in document order
11
+
12
+ html_pages = Nokogiri::XML(File.read("toc.ncx")).search("navMap//content[@src]").map {|c| c[:src]}.uniq
13
+
14
+ frontmatter = []
15
+
16
+ html_pages.delete_if {|x|
17
+ # we need to treat frontmatter differently
18
+ if x =~ /(toc|welcome|credits|copyright).html/
19
+ frontmatter << x unless x =~ /toc/
20
+ true
21
+ end
22
+
23
+ }
24
+
25
+ puts "=> Making one section for all frontmatter."
26
+ html = frontmatter.map {|x|
27
+ Nokogiri::HTML(File.open(x)).at("body").inner_html
28
+ }.join("\n")
29
+
30
+ fdoc = Nokogiri::HTML(html)
31
+
32
+ # need to turn author h3's into h4's
33
+ fdoc.search("h3").each { |h3|
34
+ if h3[:class] != 'section'
35
+ h3.name = 'h4'
36
+ else # add an id
37
+ h3['id'] = h3.inner_text.gsub(/\s/, '-')
38
+ end
39
+ }
40
+ fdoc.search("h2").each { |h2|
41
+ h2.name = 'h3'
42
+ h2['id'] = h2.inner_text.gsub(/\s/, '-')
43
+ }
44
+
45
+ head = Nokogiri::XML::Node.new "head", fdoc
46
+ title_node = Nokogiri::XML::Node.new "title", fdoc
47
+ title_node.content = "Frontmatter"
48
+ title_node.parent = head
49
+ fdoc.at("body").before head
50
+ File.open("frontmatter.html",'w'){|f| f.puts fdoc.to_html}
51
+
52
+ html_pages.unshift "frontmatter.html"
53
+
54
+ puts "=> Making one section folder per original HTML file"
55
+
56
+ html_pages.each_with_index { |page, section_idx|
57
+ FileUtils::mkdir_p("sections/%03d" % section_idx)
58
+ doc = Nokogiri::HTML(File.open(page))
59
+ title = doc.at("title").inner_text.gsub("Ruby on Rails Guides: ", '')
60
+ title = page.capitalize.gsub('.html', '') if title.strip == ''
61
+ File.open("sections/%03d/_section.txt" % section_idx, 'w') {|f| f.puts title}
62
+ puts "sections/%03d -> #{title}" % section_idx
63
+
64
+ # Fragment the page file into subsections
65
+ doc.xpath("//h3[@id]").each_with_index { |h3,item_idx|
66
+ subsection = h3.inner_text
67
+ content = h3.xpath("./following-sibling::*").take_while {|x| x.name != "h3"}.map {|x| x.to_html}
68
+ item = Nokogiri::HTML(h3.to_html + content.join("\n"))
69
+ item_path = "sections/%03d/%03d.html" % [section_idx, item_idx]
70
+
71
+ # add head node and title node
72
+ head = Nokogiri::XML::Node.new "head", item
73
+ title_node = Nokogiri::XML::Node.new "title", item
74
+ title_node.content = subsection
75
+ title_node.parent = head
76
+ item.at("body").before head
77
+
78
+ # fix all image links
79
+ item.search("img").each { |img|
80
+ img['src'] = "#{Dir.pwd}/#{img['src']}"
81
+ }
82
+
83
+
84
+ File.open(item_path, 'w'){|f| f.puts item.to_html}
85
+ puts " #{item_path} -> #{subsection}"
86
+
87
+ }
88
+ }
89
+
90
+ puts "=> Generating _document.yml"
91
+
92
+
93
+ x = Nokogiri::XML(File.open("rails_guides.opf")).remove_namespaces!
94
+
95
+ document = {
96
+ 'doc_uuid' => x.at("package")['unique-identifier'],
97
+ 'title' => x.at("title").inner_text,
98
+ 'publisher' => x.at("publisher").inner_text,
99
+ 'author' => x.at("creator").inner_text,
100
+ 'subject' => x.at("subject").inner_text,
101
+ 'date' => x.at("date").inner_text,
102
+ 'cover' => x.at("item[@id=cover]")[:href],
103
+ 'masthead' => x.at("item[@id=cover]")[:href],
104
+ 'mobi_outfile' => "rails-guide.#{Date.today.to_s}.mobi"
105
+ }
106
+ puts document.inspect
107
+ File.open("_document.yml", 'w'){|f| f.puts document.to_yaml}
108
+
109
+ exec "kindlerb ."
110
+
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "docrails_kindle"
6
+ s.version = '0.0.1'
7
+ s.platform = Gem::Platform::RUBY
8
+ s.required_ruby_version = '>= 1.9.0'
9
+
10
+ s.authors = ["Daniel Choi"]
11
+ s.email = ["dhchoi@gmail.com"]
12
+ s.homepage = "http://github.com/danchoi/docrails_kindle"
13
+ s.summary = %q{Read Rails Guides for your Kindle}
14
+ s.description = %q{Read Rails Guides for your Kindle}
15
+
16
+ s.rubyforge_project = "docrails_kindle"
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+
23
+ s.add_dependency 'nokogiri'
24
+ s.add_dependency 'kindlerb'
25
+ end
Binary file
Binary file
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: docrails_kindle
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daniel Choi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-18 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: &9708860 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *9708860
25
+ - !ruby/object:Gem::Dependency
26
+ name: kindlerb
27
+ requirement: &9708360 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *9708360
36
+ description: Read Rails Guides for your Kindle
37
+ email:
38
+ - dhchoi@gmail.com
39
+ executables:
40
+ - docrails_kindle
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - README.markdown
45
+ - Rakefile
46
+ - bin/docrails_kindle
47
+ - docrails_kindle.gemspec
48
+ - images/screen1-sm.gif
49
+ - images/screen1.gif
50
+ - mobi/rails-guide.2012-01-18.mobi
51
+ homepage: http://github.com/danchoi/docrails_kindle
52
+ licenses: []
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: 1.9.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project: docrails_kindle
71
+ rubygems_version: 1.8.10
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Read Rails Guides for your Kindle
75
+ test_files: []