mobicoder 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/README.mkd +44 -0
  2. data/Rakefile +13 -0
  3. data/VERSION +1 -0
  4. data/bin/mobicoder +11 -0
  5. data/lib/mobicoder.rb +151 -0
  6. metadata +68 -0
@@ -0,0 +1,44 @@
1
+ # mobicoder - publish mobi file for kindle from source codes
2
+
3
+ ## Description
4
+
5
+ Make HTML and convert to mobi from source codes
6
+
7
+ ## Requirements
8
+
9
+ * ruby (1.9.2 recommended)
10
+ * vim 7.3+
11
+ * tohtml.vim (bundled with vim7.3 or latest)
12
+ * [kindlegen](http://www.amazon.com/gp/feature.html?ie=UTF8&docId=1000234621)
13
+
14
+ ## Usage
15
+
16
+ $ mobicoder
17
+ usage: mobicoder basedir output files
18
+
19
+ base output file files
20
+ $ mobicoder ./ /Volumes/Kindle/documents/project.mobi ./**/*
21
+
22
+ ## License
23
+
24
+ (c) Shota Fukumori 2010-
25
+
26
+ ## MIT Licence
27
+
28
+ Permission is hereby granted, free of charge, to any person obtaining a copy
29
+ of this software and associated documentation files (the "Software"), to deal
30
+ in the Software without restriction, including without limitation the rights
31
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
32
+ copies of the Software, and to permit persons to whom the Software is
33
+ furnished to do so, subject to the following conditions:
34
+
35
+ The above copyright notice and this permission notice shall be included in
36
+ all copies or substantial portions of the Software.
37
+
38
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
41
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
42
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
43
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
44
+ THE SOFTWARE.
@@ -0,0 +1,13 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "mobicoder"
5
+ gemspec.summary = "convert source codes to mobi file for reading codes on kindle"
6
+ gemspec.email = "sorah@tubusu.net"
7
+ gemspec.homepage = "http://github.com/sorah/mobicoder"
8
+ gemspec.description = "convert codes to mobi file for reading codes on kindle"
9
+ gemspec.authors = ["Shota Fukumori (sora_h)"]
10
+ end
11
+ rescue LoadError
12
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
13
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # vim: filetype=ruby
3
+
4
+ require_relative '../lib/mobicoder.rb'
5
+
6
+ abort "usage: mobicoder basedir output files" unless ARGV.size >= 3
7
+
8
+ a = ARGV.dup
9
+
10
+ Mobicoder.run(a.shift,a.shift,*a)
11
+
@@ -0,0 +1,151 @@
1
+ require 'tmpdir'
2
+ require 'fileutils'
3
+ require 'cgi'
4
+
5
+ module Mobicoder
6
+ VIM = ENV["PATHTOVIM"] || "vim"
7
+ NUL = /mswin|mingw|cygwin/ =~ RUBY_PLATFORM ? "NUL" : "/dev/null"
8
+ class << self
9
+ def run(base,to,from,*addfiles)
10
+ Dir.mktmpdir("mobicoder") do |dir|
11
+ files = (Dir[from]+addfiles).map do |x|
12
+ a = [File.expand_path(x)]
13
+ a << CGI.escapeHTML(a[0].sub(File.expand_path(base),""))
14
+ a << CGI.escape(a[0].sub(File.expand_path(base),""))
15
+ a << File.join(dir,a[2])
16
+ end.reject{|x| File.directory?(x[0]) }
17
+ #f[0]: real path
18
+ #f[1]: display name
19
+ #f[2]: for hash
20
+ #f[3]: to...
21
+
22
+ files.each do |f|
23
+ puts "converting: #{f[1]}"
24
+ Mobicoder.convert(f, f[3], base)
25
+ end
26
+ Mobicoder.merge(dir, files, base)
27
+
28
+ Dir.chdir(dir)
29
+ puts "generating .mobi ..."
30
+ system "kindlegen", "-o", File.basename(to), File.join(dir,"index.opf")
31
+ FileUtils.mv(File.join(dir, File.basename(to)),to)
32
+ end
33
+ end
34
+
35
+ def convert(from, to, base)
36
+ system VIM, "-c", "set noswapfile",
37
+ "-c", "e #{from[0]}",
38
+ "-c", "colorscheme default",
39
+ "-c", "set nonu",
40
+ "-c", "TOhtml",
41
+ "-c", "w! #{to.gsub("%","\\%")}| qa!"#,
42
+ # :out => NUL, :err => NUL
43
+ a = File.read(to)
44
+
45
+ open(to,"w") do |io|
46
+ io.puts "<a name=\"#{from[2]}\" /><h2>#{from[1]}</h2>"
47
+ io.puts a.gsub(/.+<body>\n<pre>/m,"<pre>") \
48
+ .gsub(/<\/pre>\n<\/body>.+/m,"</pre>")
49
+ io.puts "<mbp:pagebreak />"
50
+ end; nil
51
+ end
52
+
53
+ def merge(dir, files, base)
54
+ title = File.basename(File.expand_path(base))
55
+ open(File.join(dir,"index.html"), "w") do |io|
56
+ io.puts <<-EOH
57
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
58
+ <html>
59
+ <head>
60
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8">
61
+ <title>#{title}</title>
62
+ <style type="text/css">
63
+ <!--
64
+ pre { font-family: monospace; color: #cccccc; background-color: #000000; }
65
+ body { font-family: monospace; color: #cccccc; background-color: #000000; }
66
+ .lnr { color: #666666; }
67
+ .Comment { color: #87ceeb; }
68
+ .Statement { color: #6699ff; }
69
+ .Identifier { color: #99ff00; }
70
+ .Type { color: #ffcc66; }
71
+ .Constant { color: #ffcc66; }
72
+ .Special { color: #ffdead; }
73
+ .PreProc { color: #ff6666; }
74
+ -->
75
+ </style>
76
+ </head>
77
+ <body>
78
+ <h1>#{title}</h1>
79
+ EOH
80
+ io.puts '<a name="TOC" /><h2>Files</h2>'
81
+ files.each do |f|
82
+ io.puts "<p><a href=\"##{f[2]}\">#{f[1]}</a></p>"
83
+ end
84
+ io.puts "<mbp:pagebreak />"
85
+ files.each do |f|
86
+ io.puts File.read(f[3])
87
+ end
88
+ io.puts "</body></html>"
89
+ end
90
+ open(File.join(dir,"toc.ncx"), "w") do |io|
91
+ io.puts <<-EOX
92
+ <?xml version="1.0" encoding="UTF-8"?>
93
+ <!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN" "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">
94
+
95
+ <ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
96
+
97
+ <docTitle><text>#{title}</text></docTitle>
98
+
99
+ <navMap>
100
+ EOX
101
+ files.each_with_index do |f,i|
102
+ io.puts <<-EOX
103
+ <navPoint id="navPoint-#{i+1}" playOrder="#{i+1}">
104
+ <navLabel><text></text></navLabel><content src="index.html#{"#"+f[2]}"/>
105
+ </navPoint>
106
+ EOX
107
+ end
108
+ io.puts <<-EOX
109
+ </navMap>
110
+ </ncx>
111
+ EOX
112
+
113
+ end
114
+ open(File.join(dir,"index.opf"),"w") do |io|
115
+ io.puts <<-EOX
116
+ <?xml version="1.0" encoding="utf-8"?>
117
+ <package unique-identifier="uid">
118
+ <metadata>
119
+ <dc-metadata xmlns:dc="http://purl.org/metadata/dublin_core"
120
+ xmlns:oebpackage="http://openebook.org/namespaces/oeb-package/1.0/">
121
+
122
+ <dc:Title>#{title}</dc:Title>
123
+ <dc:Language>en-us</dc:Language>
124
+ <dc:Date>#{Time.now.strftime("%m/%d/%Y")}</dc:Date>
125
+ </dc-metadata>
126
+ <x-metadata>
127
+ <output encoding="utf-8" content-type="text/x-oeb1-document">
128
+ </output>
129
+ </x-metadata>
130
+ </metadata>
131
+ <manifest>
132
+ <item id="item1" media-type="text/x-oeb1-document" href="index.html"></item>
133
+ <item id="toc" media-type="application/x-dtbncx+xml" href="toc.ncx"></item>
134
+ </manifest>
135
+ <spine toc="toc">
136
+ <itemref idref="item1" />
137
+ </spine>
138
+ <tours></tours>
139
+ <guide>
140
+ <reference type="toc" title="Table of Contents" href="index.html%23TOC"></reference>
141
+ <reference type="start" title="Startup Page" href="index.html%23TOC"></reference>
142
+ </guide>
143
+ </package>
144
+ EOX
145
+ end
146
+ puts File.join(dir,"index.opf")
147
+ puts File.join(dir,"toc.ncx")
148
+ STDIN.gets
149
+ end
150
+ end
151
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mobicoder
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Shota Fukumori (sora_h)
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-01 00:00:00 +09:00
18
+ default_executable: mobicoder
19
+ dependencies: []
20
+
21
+ description: convert codes to mobi file for reading codes on kindle
22
+ email: sorah@tubusu.net
23
+ executables:
24
+ - mobicoder
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.mkd
29
+ files:
30
+ - README.mkd
31
+ - Rakefile
32
+ - VERSION
33
+ - bin/mobicoder
34
+ - lib/mobicoder.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/sorah/mobicoder
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.7
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: convert source codes to mobi file for reading codes on kindle
67
+ test_files: []
68
+