sc2epub 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +4 -0
- data/lib/sc2epub/converter.rb +29 -15
- data/lib/sc2epub/template/makefile.tmpl +2 -2
- data/lib/sc2epub.rb +1 -1
- data/sc2epub.gemspec +1 -2
- data/test/sc2epubspec.rb +100 -0
- metadata +3 -2
data/CHANGES
CHANGED
data/lib/sc2epub/converter.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
class Sc2epub::Converter
|
2
|
-
require '
|
2
|
+
require 'nkf'
|
3
3
|
require 'fileutils'
|
4
4
|
def initialize env, root, output
|
5
5
|
@root = path(root)
|
@@ -73,17 +73,18 @@ class Sc2epub::Converter
|
|
73
73
|
end
|
74
74
|
end
|
75
75
|
def dofile path
|
76
|
-
|
76
|
+
ext = File::extname(path)
|
77
|
+
if ext == '.html' or ext == '.xhtml' or ext == '.jpg' or ext == '.gif' or ext == '.png'
|
77
78
|
FileUtils::cp(path, @output)
|
78
79
|
return
|
79
80
|
end
|
80
81
|
output = @output
|
81
82
|
s = open(path){|io|io.read}
|
82
|
-
enc =
|
83
|
-
if enc==
|
83
|
+
enc = NKF::guess(s)
|
84
|
+
if enc==NKF::BINARY
|
84
85
|
return nil
|
85
|
-
elsif enc!=
|
86
|
-
s = s
|
86
|
+
elsif enc!=NKF::ASCII and enc!=NKF::UTF8
|
87
|
+
s = NKF::nkf('-wxm0', s)
|
87
88
|
end
|
88
89
|
title = title(local(path))
|
89
90
|
s = @template.xhtml('title'=>local(path), 'body'=>s)
|
@@ -101,18 +102,31 @@ class Sc2epub::Converter
|
|
101
102
|
end
|
102
103
|
def dodir dir
|
103
104
|
return [] if File::basename(dir)=~/^\..*/
|
104
|
-
@dirstack.push({:name =>local(dir), :src =>nil,
|
105
|
+
@dirstack.push({:name =>local(dir), :src =>nil, :path => dir,
|
105
106
|
:type => :dir, :level => @dirstack.size})
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
107
|
+
cache = {}
|
108
|
+
until(@dirstack.empty?)
|
109
|
+
dir = @dirstack.last[:path]
|
110
|
+
flag = false
|
111
|
+
Dir::foreach(dir) do |i|
|
112
|
+
next if i=="."||i==".."
|
113
|
+
path = File::join(dir, i)
|
114
|
+
next if cache[path]
|
115
|
+
if FileTest::directory? path
|
116
|
+
@dirstack.push({:name =>local(path), :src =>nil, :path => path,
|
117
|
+
:type => :dir, :level => @dirstack.size})
|
118
|
+
flag = true
|
119
|
+
break
|
120
|
+
elsif FileTest::file? path
|
121
|
+
dofile(path)
|
122
|
+
cache[path] = true
|
123
|
+
end
|
124
|
+
end
|
125
|
+
unless flag
|
126
|
+
done = @dirstack.pop
|
127
|
+
cache[done[:path]] = true
|
113
128
|
end
|
114
129
|
end
|
115
|
-
@dirstack.pop
|
116
130
|
end
|
117
131
|
def doroot dir
|
118
132
|
dir = path(dir)
|
@@ -11,8 +11,8 @@ kindle: $(MOBI)
|
|
11
11
|
|
12
12
|
.PHONY: clean
|
13
13
|
clean:
|
14
|
-
find ./ -name "*.epub" -exec rm {}
|
15
|
-
find ./ -name "*.mobi" -exec rm {}
|
14
|
+
find ./ -name "*.epub" -exec rm {} \;
|
15
|
+
find ./ -name "*.mobi" -exec rm {} \;
|
16
16
|
|
17
17
|
$(EPUB):
|
18
18
|
zip -Xr9D $(EPUB) mimetype META-INF toc.ncx $(PROJECT).opf
|
data/lib/sc2epub.rb
CHANGED
data/sc2epub.gemspec
CHANGED
@@ -11,8 +11,7 @@ GEMSPEC = Gem::Specification::new do |s|
|
|
11
11
|
s.required_ruby_version = '>= 1.8.6'
|
12
12
|
s.executables = ['sc2epub']
|
13
13
|
s.default_executable = 'sc2epub'
|
14
|
-
|
15
|
-
s.files = Dir::glob("{lib,bin}/**/*") + ['sc2epub.gemspec', 'README', 'CHANGES']
|
14
|
+
s.files = Dir::glob("{lib,bin,test}/**/*") + ['sc2epub.gemspec', 'README', 'CHANGES']
|
16
15
|
s.has_rdoc = false
|
17
16
|
s.homepage = 'https://github.com/takada-at/sc2epub'
|
18
17
|
s.rubyforge_project = 'sc2epub'
|
data/test/sc2epubspec.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'lib/sc2epub'
|
3
|
+
|
4
|
+
describe Sc2epub::Converter, 'when on html' do
|
5
|
+
before(:all) do
|
6
|
+
@in = File::join(File::dirname(__FILE__), 'in')
|
7
|
+
@out = File::join(File::dirname(__FILE__), 'out')
|
8
|
+
`rm -r #{@in}` if FileTest::exists? @in
|
9
|
+
`rm -r #{@out}` if FileTest::exists? @out
|
10
|
+
`mkdir #{@in}`
|
11
|
+
`mkdir #{@out}`
|
12
|
+
open(File::join(@in, 'some.html'), 'w') do |io|
|
13
|
+
io.write('<html></html>')
|
14
|
+
end
|
15
|
+
open(File::join(@in, 'some.rb'), 'w') do |io|
|
16
|
+
io.write('puts "hello"')
|
17
|
+
end
|
18
|
+
@converter = Sc2epub::Converter::new({:author => 'test', :lang => 'en'}, @in, @out)
|
19
|
+
@converter.doroot(@in)
|
20
|
+
@converter.dogenerate('test')
|
21
|
+
end
|
22
|
+
it 'should copy html' do
|
23
|
+
FileTest.should be_exists(File::join(@out, 'some.html'))
|
24
|
+
end
|
25
|
+
it 'should rewrite source code' do
|
26
|
+
FileTest.should be_exists(File::join(@out, 'some_rb.html'))
|
27
|
+
end
|
28
|
+
it 'should not rewrite html' do
|
29
|
+
FileTest.should_not be_exists(File::join(@out, 'some_html.html'))
|
30
|
+
end
|
31
|
+
after(:all) do
|
32
|
+
`rm -r #{@in}` if FileTest::exists? @in
|
33
|
+
`rm -r #{@out}` if FileTest::exists? @out
|
34
|
+
end
|
35
|
+
end
|
36
|
+
describe Sc2epub::Converter, 'when on sc2epub/lib'do
|
37
|
+
before(:all) do
|
38
|
+
@lib = File::join(File::dirname(__FILE__), '../lib')
|
39
|
+
@out = File::join(File::dirname(__FILE__), 'out')
|
40
|
+
`mkdir #{@out}`
|
41
|
+
@converter = Sc2epub::Converter::new({:author => 'test', :lang => 'en'}, @lib, @out)
|
42
|
+
@converter.doroot(@lib)
|
43
|
+
@converter.dogenerate('test')
|
44
|
+
end
|
45
|
+
it 'should create META-INF and container.xml' do
|
46
|
+
FileTest.should be_exists(File::join(@out, 'META-INF'))
|
47
|
+
FileTest.should be_directory(File::join(@out, 'META-INF'))
|
48
|
+
FileTest.should be_exists(File::join(@out, 'META-INF', 'container.xml'))
|
49
|
+
end
|
50
|
+
it 'should create mimetype' do
|
51
|
+
FileTest.should be_exists(File::join(@out, 'mimetype'))
|
52
|
+
end
|
53
|
+
it 'should create Makefile' do
|
54
|
+
FileTest.should be_exists(File::join(@out, 'Makefile'))
|
55
|
+
end
|
56
|
+
it 'should create toc.ncx, test.opf' do
|
57
|
+
FileTest.should be_exists(File::join(@out, 'toc.ncx'))
|
58
|
+
FileTest.should be_exists(File::join(@out, 'test.opf'))
|
59
|
+
end
|
60
|
+
it 'should create "sc2epub_rb.html"' do
|
61
|
+
FileTest.should be_exists(File::join(@out, 'sc2epub_rb.html'))
|
62
|
+
end
|
63
|
+
it 'should create "sc2epub_converter_rb.html"' do
|
64
|
+
FileTest.should be_exists(File::join(@out, 'sc2epub_converter_rb.html'))
|
65
|
+
end
|
66
|
+
it 'should create html for any file in directory' do
|
67
|
+
checkfile = lambda do |f|
|
68
|
+
return if File::extname(f)=='.jpg'
|
69
|
+
f = @converter.path(f)
|
70
|
+
html = @converter.title(@converter.local(f))+'.html'
|
71
|
+
FileTest.should be_exists(File::join(@out, html))
|
72
|
+
end
|
73
|
+
checkdir = lambda do |d|
|
74
|
+
Dir::foreach(d) do |c|
|
75
|
+
next if c == '.' or c == '..'
|
76
|
+
path = File::join(d, c)
|
77
|
+
if FileTest::file? path
|
78
|
+
checkfile.call(path)
|
79
|
+
elsif FileTest::directory? path
|
80
|
+
checkdir.call(path)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
checkdir.call(@lib)
|
85
|
+
end
|
86
|
+
it 'should have "test" in title' do
|
87
|
+
s = open(File::join(@out, 'test.opf')){|io|io.read}
|
88
|
+
/<dc:Title>(.*)<\/dc:Title>/.match(s).should_not be(nil)
|
89
|
+
Regexp::last_match[1].should == 'test'
|
90
|
+
/<dc:Language>(.*)<\/dc:Language>/.match(s).should_not be(nil)
|
91
|
+
Regexp::last_match[1].should == 'en'
|
92
|
+
/<dc:Creator>(.*)<\/dc:Creator>/.match(s).should_not be(nil)
|
93
|
+
Regexp::last_match[1].should == 'test'
|
94
|
+
end
|
95
|
+
after(:all) do
|
96
|
+
if FileTest::exists? @out
|
97
|
+
`rm -r #{@out}`
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- takada-at
|
@@ -41,6 +41,7 @@ files:
|
|
41
41
|
- lib/sc2epub/converter.rb
|
42
42
|
- lib/sc2epub/main.rb
|
43
43
|
- bin/sc2epub
|
44
|
+
- test/sc2epubspec.rb
|
44
45
|
- sc2epub.gemspec
|
45
46
|
- README
|
46
47
|
- CHANGES
|