sc2epub 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/sc2epub +233 -0
- data/lib/sc2epub.rb +3 -0
- data/sc2epub.gemspec +22 -0
- metadata +68 -0
data/bin/sc2epub
ADDED
@@ -0,0 +1,233 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'kconv'
|
4
|
+
require 'optparse'
|
5
|
+
TEMPLATE = <<EOS
|
6
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
7
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
8
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja-JP" lang="ja-JP"><head><title>%(title)</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><head/><body>
|
9
|
+
<h1>%(title)</h1>
|
10
|
+
<pre>
|
11
|
+
%(body)
|
12
|
+
</pre>
|
13
|
+
</body>
|
14
|
+
</html>
|
15
|
+
EOS
|
16
|
+
TEMPLATE_INDEX = <<EOS
|
17
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
18
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
19
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja-JP" lang="ja-JP"><head><title>%(title)</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><head/><body>
|
20
|
+
<a name='#TOC' /><h1>INDEX</h1>
|
21
|
+
%(body)
|
22
|
+
</body>
|
23
|
+
</html>
|
24
|
+
EOS
|
25
|
+
TEMPLATE_LINK = <<EOS
|
26
|
+
<a href="%(url)">%(title)</a>
|
27
|
+
EOS
|
28
|
+
TEMPLATE_OPF = <<EOS
|
29
|
+
<?xml version="1.0" encoding="utf-8"?>
|
30
|
+
<package unique-identifier="uid">
|
31
|
+
<metadata>
|
32
|
+
<dc-metadata xmlns:dc="http://purl.org/metadata/dublin_core"
|
33
|
+
xmlns:oebpackage="http://openebook.org/namespaces/oeb-package/1.0/">
|
34
|
+
<dc:Title>%(title)</dc:Title>
|
35
|
+
<dc:Language>ja</dc:Language>
|
36
|
+
<dc:Creator>%(author)</dc:Creator>
|
37
|
+
<dc:Description>..</dc:Description>
|
38
|
+
<dc:Date>%(date)</dc:Date>
|
39
|
+
</dc-metadata>
|
40
|
+
<x-metadata>
|
41
|
+
<output encoding="utf-8" content-type="text/x-oeb1-document">
|
42
|
+
</output>
|
43
|
+
</x-metadata>
|
44
|
+
</metadata>
|
45
|
+
<manifest>
|
46
|
+
<item id="index" media-type-"text/x-oeb1-document" href="index.html"></item>
|
47
|
+
%(items)
|
48
|
+
<item id="toc" media-type="application/x-dtbncx+xml" href="toc.ncx"></item>
|
49
|
+
</manifest>
|
50
|
+
<spine toc="toc">
|
51
|
+
<itemref idref="index" />
|
52
|
+
</spine>
|
53
|
+
<tours></tours>
|
54
|
+
<guide>
|
55
|
+
<reference type="toc" title="Table of Contents" href="index.html"></reference>
|
56
|
+
<reference type="start" title="Table of Contents" href="index.html"></reference>
|
57
|
+
</guide>
|
58
|
+
</package>
|
59
|
+
EOS
|
60
|
+
TEMPLATE_ITEM = <<EOS
|
61
|
+
<item id="%(id)" media-type="text/x-oeb1-document" href="%(link)"></item>
|
62
|
+
EOS
|
63
|
+
TEMPLATE_NCX = <<EOS
|
64
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
65
|
+
<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN" "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">
|
66
|
+
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
|
67
|
+
<docTitle><text>%(title)</text></docTitle>
|
68
|
+
<navMap>
|
69
|
+
%(navitems)
|
70
|
+
</navMap>
|
71
|
+
</ncx>
|
72
|
+
EOS
|
73
|
+
TEMPLATE_NAVITEM = <<EOS
|
74
|
+
<navPoint id="%(id)" playOrder="%(order)">
|
75
|
+
<navLabel><text>%(title)</text></navLabel><content src="%(link)"/>
|
76
|
+
</navPoint>
|
77
|
+
EOS
|
78
|
+
|
79
|
+
$env = {}
|
80
|
+
def local path
|
81
|
+
if path.index($root)==0
|
82
|
+
path = path[$root.size, path.size]
|
83
|
+
end
|
84
|
+
if path[0,1] == "/"
|
85
|
+
path = path[1, path.size]
|
86
|
+
end
|
87
|
+
return path
|
88
|
+
end
|
89
|
+
def title path
|
90
|
+
path.gsub(/\//, "_").gsub(/\./, '_')
|
91
|
+
end
|
92
|
+
def dogenerate output, doctitle
|
93
|
+
contents = ['index.html', 'toc.ncx']
|
94
|
+
indexhtml = ''
|
95
|
+
items = ''; c=0;
|
96
|
+
nvitems = ''
|
97
|
+
$indexes.each do |data|
|
98
|
+
title = data[:name]
|
99
|
+
link = data[:src]
|
100
|
+
contents << link
|
101
|
+
if data[:type]==:dir && data[:level]+2<=4
|
102
|
+
tagname = "h" + (data[:level]+2).to_s
|
103
|
+
else
|
104
|
+
tagname = 'p'
|
105
|
+
end
|
106
|
+
indexhtml += "<#{tagname}>" + dotemplate(TEMPLATE_LINK, 'url'=>link, 'title'=>title) + "</#{tagname}>\n"
|
107
|
+
if data[:type]==:file
|
108
|
+
items += dotemplate(TEMPLATE_ITEM, 'id'=>"item#{c+=1}", 'link'=>link)
|
109
|
+
nvitems += dotemplate(TEMPLATE_NAVITEM, 'id'=>"navPoint-#{c}", 'order'=>c.to_s, 'link'=>link, 'title'=>title)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
author = $env[:author]
|
113
|
+
indexhtml = dotemplate(TEMPLATE_INDEX, 'title'=>'index', 'body'=>indexhtml)
|
114
|
+
opf = dotemplate(TEMPLATE_OPF, 'title'=>doctitle, 'date'=>Time::now.strftime("%Y/%m/%d"), 'items'=>items, 'author'=>author)
|
115
|
+
ncx = dotemplate(TEMPLATE_NCX, 'title'=>doctitle, 'navitems'=>nvitems)
|
116
|
+
open(File::join(output, 'index.html'), 'w') do |io|
|
117
|
+
io.write(indexhtml)
|
118
|
+
end
|
119
|
+
opfpath = "#{doctitle}.opf"
|
120
|
+
open(File::join(output, opfpath), 'w') do |io|
|
121
|
+
io.write(opf)
|
122
|
+
end
|
123
|
+
open(File::join(output, 'toc.ncx'), 'w') do |io|
|
124
|
+
io.write(ncx)
|
125
|
+
end
|
126
|
+
Dir::mkdir(File::join(output, 'META-INF')) unless FileTest::exists? File::join(output, 'META-INF')
|
127
|
+
open(File::join(output, 'META-INF', 'container.xml'), 'w') do |io|
|
128
|
+
io.write <<EOS
|
129
|
+
<?xml version="1.0"?>
|
130
|
+
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
|
131
|
+
<rootfiles>
|
132
|
+
<rootfile full-path="#{opfpath}" media-type="application/oebps-package+xml"/>
|
133
|
+
</rootfiles>
|
134
|
+
</container>
|
135
|
+
EOS
|
136
|
+
end
|
137
|
+
open(File::join(output, 'mimetype'), 'w') do |io|
|
138
|
+
io.puts('application/epub+zip')
|
139
|
+
end
|
140
|
+
|
141
|
+
epub = "#{doctitle}.epub"
|
142
|
+
dir = Dir::pwd
|
143
|
+
begin
|
144
|
+
Dir::chdir(output)
|
145
|
+
`zip -Xr9D #{epub} mimetype META-INF #{opfpath}`
|
146
|
+
contents.each do |c|
|
147
|
+
`zip -Xr9D #{epub} #{c} -x mimetype`
|
148
|
+
end
|
149
|
+
ensure
|
150
|
+
Dir::chdir(dir)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
def dotemplate template, params
|
154
|
+
t = template
|
155
|
+
for k,v in params
|
156
|
+
t = t.gsub(/%\(#{k}\)/, v)
|
157
|
+
end
|
158
|
+
t
|
159
|
+
end
|
160
|
+
def dofile path, output
|
161
|
+
s = open(path){|io|io.read}
|
162
|
+
return nil if Kconv::guess(s)==Kconv::BINARY
|
163
|
+
title = title(local(path))
|
164
|
+
s = dotemplate(TEMPLATE, 'title'=>local(path), 'body'=>s)
|
165
|
+
npath = title+".html"
|
166
|
+
if $dirstack.last and not $dirstack.last[:src]
|
167
|
+
$dirstack.last[:src] = npath
|
168
|
+
$indexes << $dirstack.last
|
169
|
+
end
|
170
|
+
open(File::join(output, npath), "w") do |io|
|
171
|
+
io.write(s)
|
172
|
+
end
|
173
|
+
level = $dirstack.empty? ? 1 : $dirstack.last[:level]+1
|
174
|
+
$indexes << {:src => npath, :name =>local(path), :type => :file, :level => level}
|
175
|
+
title
|
176
|
+
end
|
177
|
+
$dirstack = []
|
178
|
+
$indexes = []
|
179
|
+
def dodir dir, output
|
180
|
+
return [] if File::basename(dir)=~/^\..*/
|
181
|
+
$dirstack.push({:name =>local(dir), :src =>nil, :type => :dir, :level => $dirstack.size})
|
182
|
+
Dir::foreach(dir) do |i|
|
183
|
+
next if i=="."||i==".."
|
184
|
+
path = File::join(dir, i)
|
185
|
+
if FileTest::directory? path
|
186
|
+
dodir(path, output)
|
187
|
+
elsif FileTest::file? path
|
188
|
+
dofile(path, output)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
$dirstack.pop
|
192
|
+
end
|
193
|
+
def doroot dir, output
|
194
|
+
$root = dir
|
195
|
+
Dir::foreach(dir) do |i|
|
196
|
+
next if i=="."||i==".."
|
197
|
+
path = File::join(dir, i)
|
198
|
+
if FileTest::directory? path
|
199
|
+
dodir(path, output)
|
200
|
+
elsif FileTest::file? path
|
201
|
+
dofile(path, output)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
require 'sc2epub'
|
206
|
+
def main
|
207
|
+
input = ARGV[0]
|
208
|
+
output = ARGV[1]
|
209
|
+
user = ENV['USER']
|
210
|
+
opts = OptionParser.new("Usage: #{File::basename($0)} SOME_DIR OUTPUT_DIR PROJECT_NAME")
|
211
|
+
opts.on("-v", "--version", "show version") do
|
212
|
+
puts "%s %s" %[File.basename($0), Sc2epub::VERSION]
|
213
|
+
puts "ruby %s" % RUBY_VERSION
|
214
|
+
exit
|
215
|
+
end
|
216
|
+
opts.on("-u", "--user", "set user name") do |v|
|
217
|
+
user = v
|
218
|
+
end
|
219
|
+
opts.on_tail("-h", "--help", "show this message") do
|
220
|
+
puts opts
|
221
|
+
exit
|
222
|
+
end
|
223
|
+
if ARGV.size < 3
|
224
|
+
puts opts
|
225
|
+
exit
|
226
|
+
end
|
227
|
+
|
228
|
+
$env[:author] = user
|
229
|
+
Dir::mkdir(output) unless FileTest::exists? output
|
230
|
+
doroot(input, output)
|
231
|
+
dogenerate(output, ARGV[2])
|
232
|
+
end
|
233
|
+
main
|
data/lib/sc2epub.rb
ADDED
data/sc2epub.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
GEMSPEC = Gem::Specification::new do |s|
|
4
|
+
s.name = 'sc2epub'
|
5
|
+
s.version = '0.0.1'
|
6
|
+
s.author = 'takada-at'
|
7
|
+
s.email = 'takada-at@klab.jp'
|
8
|
+
s.date = '2010-11-21'
|
9
|
+
s.summary = 'sourcecode to epub'
|
10
|
+
s.platform = Gem::Platform::RUBY
|
11
|
+
s.required_ruby_version = '>= 1.8.6'
|
12
|
+
s.executables = ['sc2epub']
|
13
|
+
s.default_executable = 'sc2epub'
|
14
|
+
|
15
|
+
s.files = Dir::glob("{lib,bin}/**/*") + ['sc2epub.gemspec']
|
16
|
+
s.has_rdoc = false
|
17
|
+
s.homepage = 'https://github.com/takada-at/sc2epub'
|
18
|
+
s.rubyforge_project = 'sc2epub'
|
19
|
+
s.description = <<EOF
|
20
|
+
convert sourcecode to epub
|
21
|
+
EOF
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sc2epub
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- takada-at
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-11-21 00:00:00 +09:00
|
18
|
+
default_executable: sc2epub
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: |
|
22
|
+
convert sourcecode to epub
|
23
|
+
|
24
|
+
email: takada-at@klab.jp
|
25
|
+
executables:
|
26
|
+
- sc2epub
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- lib/sc2epub.rb
|
33
|
+
- bin/sc2epub
|
34
|
+
- sc2epub.gemspec
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: https://github.com/takada-at/sc2epub
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
segments:
|
49
|
+
- 1
|
50
|
+
- 8
|
51
|
+
- 6
|
52
|
+
version: 1.8.6
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project: sc2epub
|
63
|
+
rubygems_version: 1.3.6
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: sourcecode to epub
|
67
|
+
test_files: []
|
68
|
+
|