repub 0.3.2 → 0.3.3
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.
- data/History.txt +25 -9
- data/README.rdoc +46 -40
- data/Rakefile +1 -0
- data/bin/repub +1 -1
- data/lib/repub.rb +1 -1
- data/lib/repub/app.rb +3 -3
- data/lib/repub/app/builder.rb +84 -36
- data/lib/repub/app/fetcher.rb +13 -11
- data/lib/repub/app/options.rb +36 -5
- data/lib/repub/app/parser.rb +1 -1
- data/lib/repub/app/profile.rb +16 -15
- data/lib/repub/epub/container.rb +28 -28
- data/lib/repub/epub/content.rb +59 -34
- data/lib/repub/epub/toc.rb +139 -139
- data/repub.gemspec +3 -3
- data/test/data/custom.css +3 -0
- data/test/data/invisiblellama.png +0 -0
- data/test/data/test.css +5 -0
- data/test/data/test.html +60 -0
- data/test/epub/test_container.rb +4 -4
- data/test/epub/test_content.rb +42 -38
- data/test/epub/test_toc.rb +19 -7
- data/test/test_builder.rb +145 -1
- data/test/test_fetcher.rb +79 -20
- data/test/test_parser.rb +45 -32
- metadata +6 -2
data/lib/repub/app/parser.rb
CHANGED
@@ -121,7 +121,7 @@ module Repub
|
|
121
121
|
# Get item's anchor and href
|
122
122
|
a = item.name == 'a' ? item : item.at('a')
|
123
123
|
next if !a
|
124
|
-
href = a[
|
124
|
+
href = a['href']
|
125
125
|
next if !href
|
126
126
|
# Is this a leaf item or node ?
|
127
127
|
subsection = item.xpath(@selectors[:toc_section]).first
|
data/lib/repub/app/profile.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'delegate'
|
2
|
+
require 'yaml'
|
2
3
|
|
3
4
|
module Repub
|
4
5
|
class App
|
@@ -29,26 +30,26 @@ module Repub
|
|
29
30
|
profile = Profile.new
|
30
31
|
if p = profile[name]
|
31
32
|
keys = p.keys.map{|k| k.to_s }.sort.map{|k| k.to_sym }
|
32
|
-
keys.each do |
|
33
|
-
|
34
|
-
next if
|
35
|
-
case
|
33
|
+
keys.each do |key|
|
34
|
+
val = p[key]
|
35
|
+
next if val.nil? || (val.respond_to?(:empty?) && val.empty?)
|
36
|
+
case key
|
36
37
|
when :selectors
|
37
|
-
printf("%4s%-6s\n", '',
|
38
|
-
selector_keys =
|
39
|
-
selector_keys.each { |sk| printf("%8s%-12s %s\n", '', sk,
|
38
|
+
printf("%4s%-6s\n", '', key)
|
39
|
+
selector_keys = val.keys.map{|k| k.to_s }.sort.map{|k| k.to_sym }
|
40
|
+
selector_keys.each { |sk| printf("%8s%-12s %s\n", '', sk, val[sk]) }
|
40
41
|
when :remove
|
41
|
-
printf("%4s%-6s\n", '',
|
42
|
-
|
42
|
+
printf("%4s%-6s\n", '', key)
|
43
|
+
val.each { |rk| printf("%20s %s\n", '', rk) }
|
43
44
|
when :rx
|
44
|
-
printf("%4s%-6s\n", '',
|
45
|
-
|
45
|
+
printf("%4s%-6s\n", '', key)
|
46
|
+
val.each { |rk| printf("%20s %s\n", '', rk) }
|
46
47
|
when :metadata
|
47
|
-
printf("%4s%-6s\n", '',
|
48
|
-
metadata_keys =
|
49
|
-
metadata_keys.each { |mk| printf("%8s%-12s %s\n", '', mk,
|
48
|
+
printf("%4s%-6s\n", '', key)
|
49
|
+
metadata_keys = val.keys.map{|k| k.to_s }.sort.map{|k| k.to_sym }
|
50
|
+
metadata_keys.each { |mk| printf("%8s%-12s %s\n", '', mk, val[mk]) }
|
50
51
|
else
|
51
|
-
printf("%4s%-16s %s\n", '',
|
52
|
+
printf("%4s%-16s %s\n", '', key, val)
|
52
53
|
end
|
53
54
|
end
|
54
55
|
end
|
data/lib/repub/epub/container.rb
CHANGED
@@ -1,28 +1,28 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'builder'
|
3
|
-
|
4
|
-
module Repub
|
5
|
-
module Epub
|
6
|
-
|
7
|
-
class Container
|
8
|
-
def to_xml
|
9
|
-
out = ''
|
10
|
-
builder = Builder::XmlMarkup.new(:target => out
|
11
|
-
builder.instruct!
|
12
|
-
builder.container :xmlns => "urn:oasis:names:tc:opendocument:xmlns:container", :version => "1.0" do
|
13
|
-
builder.rootfiles do
|
14
|
-
builder.rootfile 'full-path' => "content.opf", 'media-type' => "application/oebps-package+xml"
|
15
|
-
end
|
16
|
-
end
|
17
|
-
out
|
18
|
-
end
|
19
|
-
|
20
|
-
def save(path = 'container.xml')
|
21
|
-
File.open(path, 'w') do |f|
|
22
|
-
f << to_xml
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
|
-
end
|
1
|
+
require 'rubygems'
|
2
|
+
require 'builder'
|
3
|
+
|
4
|
+
module Repub
|
5
|
+
module Epub
|
6
|
+
|
7
|
+
class Container
|
8
|
+
def to_xml
|
9
|
+
out = ''
|
10
|
+
builder = Builder::XmlMarkup.new(:target => out)
|
11
|
+
builder.instruct!
|
12
|
+
builder.container :xmlns => "urn:oasis:names:tc:opendocument:xmlns:container", :version => "1.0" do
|
13
|
+
builder.rootfiles do
|
14
|
+
builder.rootfile 'full-path' => "content.opf", 'media-type' => "application/oebps-package+xml"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
out
|
18
|
+
end
|
19
|
+
|
20
|
+
def save(path = 'container.xml')
|
21
|
+
File.open(path, 'w') do |f|
|
22
|
+
f << to_xml
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
data/lib/repub/epub/content.rb
CHANGED
@@ -8,12 +8,9 @@ module Repub
|
|
8
8
|
|
9
9
|
def initialize(uid)
|
10
10
|
@metadata = Metadata.new('Untitled', 'en', uid, Date.today.to_s)
|
11
|
-
@css_counter = 0
|
12
|
-
@img_counter = 0
|
13
|
-
@html_counter = 0
|
14
11
|
@manifest_items = []
|
15
12
|
@spine_items = []
|
16
|
-
@manifest_items << ContentItem.new('ncx', 'toc.ncx'
|
13
|
+
@manifest_items << ContentItem.new('ncx', 'toc.ncx')
|
17
14
|
end
|
18
15
|
|
19
16
|
class Metadata < Struct.new(
|
@@ -31,40 +28,42 @@ module Repub
|
|
31
28
|
end
|
32
29
|
|
33
30
|
attr_reader :metadata
|
34
|
-
|
35
|
-
def
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
def add_stylesheet(href, id = nil)
|
40
|
-
@manifest_items << ContentItem.new(id || "css_#{@css_counter += 1}", href, 'text/css')
|
41
|
-
end
|
42
|
-
|
43
|
-
def add_image(href, id = nil)
|
44
|
-
image_type = case(href.strip.downcase)
|
45
|
-
when /.*\.(jpeg|jpg)$/
|
46
|
-
'image/jpeg'
|
47
|
-
when /.*\.png$/
|
48
|
-
'image/png'
|
49
|
-
when /.*\.gif$/
|
50
|
-
'image/gif'
|
51
|
-
when /.*\.svg$/
|
52
|
-
'image/svg+xml'
|
53
|
-
else
|
54
|
-
raise 'Unsupported image type'
|
55
|
-
end
|
56
|
-
@manifest_items << ContentItem.new(id || "img_#{@img_counter += 1}", href, image_type)
|
31
|
+
|
32
|
+
def add_item(href, id = nil)
|
33
|
+
item = ContentItem.new(id || "item_#{@manifest_items.size}", href)
|
34
|
+
@manifest_items << item
|
35
|
+
@spine_items << item if item.document?
|
57
36
|
end
|
58
37
|
|
59
|
-
def
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
38
|
+
#def add_stylesheet(href, id = nil)
|
39
|
+
# @manifest_items << ContentItem.new(id || "css_#{@css_counter += 1}", href, 'text/css')
|
40
|
+
#end
|
41
|
+
#
|
42
|
+
#def add_image(href, id = nil)
|
43
|
+
# image_type = case(href.strip.downcase)
|
44
|
+
# when /.*\.(jpeg|jpg)$/
|
45
|
+
# 'image/jpeg'
|
46
|
+
# when /.*\.png$/
|
47
|
+
# 'image/png'
|
48
|
+
# when /.*\.gif$/
|
49
|
+
# 'image/gif'
|
50
|
+
# when /.*\.svg$/
|
51
|
+
# 'image/svg+xml'
|
52
|
+
# else
|
53
|
+
# raise 'Unsupported image type'
|
54
|
+
# end
|
55
|
+
# @manifest_items << ContentItem.new(id || "img_#{@img_counter += 1}", href, image_type)
|
56
|
+
#end
|
57
|
+
#
|
58
|
+
#def add_document(href, id = nil)
|
59
|
+
# manifest_item = ContentItem.new(id || "item_#{@html_counter += 1}", href, 'application/xhtml+xml')
|
60
|
+
# @manifest_items << manifest_item
|
61
|
+
# @spine_items << manifest_item
|
62
|
+
#end
|
64
63
|
|
65
64
|
def to_xml
|
66
65
|
out = ''
|
67
|
-
builder = Builder::XmlMarkup.new(:target => out
|
66
|
+
builder = Builder::XmlMarkup.new(:target => out)
|
68
67
|
builder.instruct!
|
69
68
|
builder.package :xmlns => "http://www.idpf.org/2007/opf",
|
70
69
|
'unique-identifier' => "dcidid",
|
@@ -83,7 +82,7 @@ module Repub
|
|
83
82
|
end
|
84
83
|
|
85
84
|
private
|
86
|
-
|
85
|
+
|
87
86
|
def metadata_to_xml(builder)
|
88
87
|
builder.metadata 'xmlns:dc' => "http://purl.org/dc/elements/1.1/",
|
89
88
|
'xmlns:dcterms' => "http://purl.org/dc/terms/",
|
@@ -129,6 +128,32 @@ module Repub
|
|
129
128
|
:href,
|
130
129
|
:media_type
|
131
130
|
)
|
131
|
+
|
132
|
+
def initialize(id, href)
|
133
|
+
super(id, href)
|
134
|
+
self.media_type = case href.strip.downcase
|
135
|
+
when /.*\.html?$/
|
136
|
+
'application/xhtml+xml'
|
137
|
+
when /.*\.css$/
|
138
|
+
'text/css'
|
139
|
+
when /.*\.(jpeg|jpg)$/
|
140
|
+
'image/jpeg'
|
141
|
+
when /.*\.png$/
|
142
|
+
'image/png'
|
143
|
+
when /.*\.gif$/
|
144
|
+
'image/gif'
|
145
|
+
when /.*\.svg$/
|
146
|
+
'image/svg+xml'
|
147
|
+
when /.*\.ncx$/
|
148
|
+
'application/x-dtbncx+xml'
|
149
|
+
else
|
150
|
+
raise 'Unknown media type'
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def document?
|
155
|
+
self.media_type == 'application/xhtml+xml'
|
156
|
+
end
|
132
157
|
end
|
133
158
|
|
134
159
|
def manifest_to_xml(manifest_items, builder)
|
data/lib/repub/epub/toc.rb
CHANGED
@@ -1,139 +1,139 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'builder'
|
3
|
-
|
4
|
-
module Repub
|
5
|
-
module Epub
|
6
|
-
|
7
|
-
class Toc
|
8
|
-
|
9
|
-
def initialize(uid)
|
10
|
-
@head = Head.new(uid)
|
11
|
-
@doc_title = DocTitle.new('Untitled')
|
12
|
-
@nav_map = NavMap.new
|
13
|
-
end
|
14
|
-
|
15
|
-
def title
|
16
|
-
@doc_title.text
|
17
|
-
end
|
18
|
-
|
19
|
-
def title=(text)
|
20
|
-
@doc_title = DocTitle.new(text)
|
21
|
-
end
|
22
|
-
|
23
|
-
attr_reader :nav_map
|
24
|
-
|
25
|
-
def to_xml
|
26
|
-
out = ''
|
27
|
-
builder = Builder::XmlMarkup.new(:target => out
|
28
|
-
builder.instruct!
|
29
|
-
builder.declare! :DOCTYPE, :ncx, :PUBLIC, "-//NISO//DTD ncx 2005-1//EN", "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd"
|
30
|
-
builder.ncx :xmlns => "http://www.daisy.org/z3986/2005/ncx/", :version => "2005-1" do
|
31
|
-
@nav_map.calc_depth_and_play_order
|
32
|
-
@head.depth = @nav_map.depth
|
33
|
-
@head.to_xml(builder)
|
34
|
-
@doc_title.to_xml(builder)
|
35
|
-
@nav_map.to_xml(builder)
|
36
|
-
end
|
37
|
-
out
|
38
|
-
end
|
39
|
-
|
40
|
-
def save(path = 'toc.ncx')
|
41
|
-
File.open(path, 'w') do |f|
|
42
|
-
f << to_xml
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
class Head < Struct.new(
|
47
|
-
:uid
|
48
|
-
)
|
49
|
-
|
50
|
-
attr_accessor :depth
|
51
|
-
|
52
|
-
def to_xml(builder)
|
53
|
-
builder.head do
|
54
|
-
builder.meta :name => "dtb:uid", :content => self.uid
|
55
|
-
builder.meta :name => "dtb:depth", :content => @depth
|
56
|
-
builder.meta :name => "dtb:totalPageCount", :content => 0
|
57
|
-
builder.meta :name => "dtb:
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
class DocTitle < Struct.new(
|
63
|
-
:text
|
64
|
-
)
|
65
|
-
|
66
|
-
def to_xml(builder)
|
67
|
-
builder.docTitle do
|
68
|
-
builder.text self.text
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
class NavMap
|
74
|
-
class NavPoint < Struct.new(
|
75
|
-
:title,
|
76
|
-
:src
|
77
|
-
)
|
78
|
-
|
79
|
-
def initialize(title, src)
|
80
|
-
super
|
81
|
-
@@last_play_order = 0
|
82
|
-
@play_order = 0
|
83
|
-
@child_points = []
|
84
|
-
end
|
85
|
-
|
86
|
-
def add_nav_point(title, src)
|
87
|
-
nav_point = NavPoint.new(title, src)
|
88
|
-
@child_points << nav_point
|
89
|
-
nav_point
|
90
|
-
end
|
91
|
-
|
92
|
-
def to_xml(builder)
|
93
|
-
builder.navPoint :id => "navPoint-#{@play_order}", :playOrder => @play_order do
|
94
|
-
builder.navLabel do
|
95
|
-
builder.text self.title
|
96
|
-
end
|
97
|
-
builder.content :src => self.src
|
98
|
-
@child_points.each { |child_point| child_point.to_xml(builder) }
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
def calc_depth_and_play_order(nav_map, depth)
|
103
|
-
nav_map.depth = depth
|
104
|
-
@play_order = @@last_play_order += 1
|
105
|
-
@child_points.each { |child_point| child_point.calc_depth_and_play_order(nav_map, depth + 1) }
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
def initialize
|
110
|
-
@nav_points = []
|
111
|
-
@depth = 1
|
112
|
-
end
|
113
|
-
|
114
|
-
def add_nav_point(title, src)
|
115
|
-
nav_point = NavPoint.new(title, src)
|
116
|
-
@nav_points << nav_point
|
117
|
-
nav_point
|
118
|
-
end
|
119
|
-
|
120
|
-
attr_reader :depth
|
121
|
-
|
122
|
-
def depth=(value)
|
123
|
-
@depth = value if value > @depth
|
124
|
-
end
|
125
|
-
|
126
|
-
def calc_depth_and_play_order
|
127
|
-
@nav_points.each { |nav_point| nav_point.calc_depth_and_play_order(self, 1) }
|
128
|
-
end
|
129
|
-
|
130
|
-
def to_xml(builder)
|
131
|
-
builder.navMap do
|
132
|
-
@nav_points.each { |nav_point| nav_point.to_xml(builder) }
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
end
|
139
|
-
end
|
1
|
+
require 'rubygems'
|
2
|
+
require 'builder'
|
3
|
+
|
4
|
+
module Repub
|
5
|
+
module Epub
|
6
|
+
|
7
|
+
class Toc
|
8
|
+
|
9
|
+
def initialize(uid)
|
10
|
+
@head = Head.new(uid)
|
11
|
+
@doc_title = DocTitle.new('Untitled')
|
12
|
+
@nav_map = NavMap.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def title
|
16
|
+
@doc_title.text
|
17
|
+
end
|
18
|
+
|
19
|
+
def title=(text)
|
20
|
+
@doc_title = DocTitle.new(text)
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_reader :nav_map
|
24
|
+
|
25
|
+
def to_xml
|
26
|
+
out = ''
|
27
|
+
builder = Builder::XmlMarkup.new(:target => out)
|
28
|
+
builder.instruct!
|
29
|
+
builder.declare! :DOCTYPE, :ncx, :PUBLIC, "-//NISO//DTD ncx 2005-1//EN", "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd"
|
30
|
+
builder.ncx :xmlns => "http://www.daisy.org/z3986/2005/ncx/", :version => "2005-1" do
|
31
|
+
@nav_map.calc_depth_and_play_order
|
32
|
+
@head.depth = @nav_map.depth
|
33
|
+
@head.to_xml(builder)
|
34
|
+
@doc_title.to_xml(builder)
|
35
|
+
@nav_map.to_xml(builder)
|
36
|
+
end
|
37
|
+
out
|
38
|
+
end
|
39
|
+
|
40
|
+
def save(path = 'toc.ncx')
|
41
|
+
File.open(path, 'w') do |f|
|
42
|
+
f << to_xml
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class Head < Struct.new(
|
47
|
+
:uid
|
48
|
+
)
|
49
|
+
|
50
|
+
attr_accessor :depth
|
51
|
+
|
52
|
+
def to_xml(builder)
|
53
|
+
builder.head do
|
54
|
+
builder.meta :name => "dtb:uid", :content => self.uid
|
55
|
+
builder.meta :name => "dtb:depth", :content => @depth
|
56
|
+
builder.meta :name => "dtb:totalPageCount", :content => 0
|
57
|
+
builder.meta :name => "dtb:maxPageNumber", :content => 0
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class DocTitle < Struct.new(
|
63
|
+
:text
|
64
|
+
)
|
65
|
+
|
66
|
+
def to_xml(builder)
|
67
|
+
builder.docTitle do
|
68
|
+
builder.text self.text
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class NavMap
|
74
|
+
class NavPoint < Struct.new(
|
75
|
+
:title,
|
76
|
+
:src
|
77
|
+
)
|
78
|
+
|
79
|
+
def initialize(title, src)
|
80
|
+
super
|
81
|
+
@@last_play_order = 0
|
82
|
+
@play_order = 0
|
83
|
+
@child_points = []
|
84
|
+
end
|
85
|
+
|
86
|
+
def add_nav_point(title, src)
|
87
|
+
nav_point = NavPoint.new(title, src)
|
88
|
+
@child_points << nav_point
|
89
|
+
nav_point
|
90
|
+
end
|
91
|
+
|
92
|
+
def to_xml(builder)
|
93
|
+
builder.navPoint :id => "navPoint-#{@play_order}", :playOrder => @play_order do
|
94
|
+
builder.navLabel do
|
95
|
+
builder.text self.title
|
96
|
+
end
|
97
|
+
builder.content :src => self.src
|
98
|
+
@child_points.each { |child_point| child_point.to_xml(builder) }
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def calc_depth_and_play_order(nav_map, depth)
|
103
|
+
nav_map.depth = depth
|
104
|
+
@play_order = @@last_play_order += 1
|
105
|
+
@child_points.each { |child_point| child_point.calc_depth_and_play_order(nav_map, depth + 1) }
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def initialize
|
110
|
+
@nav_points = []
|
111
|
+
@depth = 1
|
112
|
+
end
|
113
|
+
|
114
|
+
def add_nav_point(title, src)
|
115
|
+
nav_point = NavPoint.new(title, src)
|
116
|
+
@nav_points << nav_point
|
117
|
+
nav_point
|
118
|
+
end
|
119
|
+
|
120
|
+
attr_reader :depth
|
121
|
+
|
122
|
+
def depth=(value)
|
123
|
+
@depth = value if value > @depth
|
124
|
+
end
|
125
|
+
|
126
|
+
def calc_depth_and_play_order
|
127
|
+
@nav_points.each { |nav_point| nav_point.calc_depth_and_play_order(self, 1) }
|
128
|
+
end
|
129
|
+
|
130
|
+
def to_xml(builder)
|
131
|
+
builder.navMap do
|
132
|
+
@nav_points.each { |nav_point| nav_point.to_xml(builder) }
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
end
|