1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# frozen_string_literal: true
require 'asciidoctor-epub3'
Zip.force_entry_names_encoding = 'UTF-8'
RSpec.configure do |config|
config.before do
FileUtils.rm_r temp_dir, force: true, secure: true
end
config.after do
FileUtils.rm_r temp_dir, force: true, secure: true
end
def bin_script(name, opts = {})
path = Gem.bin_path (opts.fetch :gem, 'asciidoctor-epub3'), name
[Gem.ruby, path]
end
def asciidoctor_bin
bin_script 'asciidoctor', gem: 'asciidoctor'
end
def asciidoctor_epub3_bin
bin_script 'asciidoctor-epub3'
end
def run_command cmd, *args
Dir.chdir __dir__ do
if cmd.is_a?(Array)
args.unshift(*cmd)
cmd = args.shift
end
env_override = { 'RUBYOPT' => nil }
Open3.capture3 env_override, cmd, *args
end
end
def temp_dir
Pathname.new(__dir__).join 'temp'
end
def temp_file *path
temp_dir.join(*path)
end
def fixtures_dir
Pathname.new(__dir__).join 'fixtures'
end
def fixture_file *path
fixtures_dir.join(*path)
end
def examples_dir
Pathname.new(__dir__).join '..', 'data', 'samples'
end
def example_file *path
examples_dir.join(*path)
end
def skip_unless_has_kindlegen
require 'kindlegen'
rescue LoadError
skip 'kindlegen gem is unavailable'
end
def convert(input, opts = {})
opts[:backend] = 'epub3'
opts[:header_footer] = true
opts[:mkdirs] = true
opts[:safe] = Asciidoctor::SafeMode::UNSAFE unless opts.key? :safe
if input.is_a?(Pathname)
opts[:to_dir] = temp_dir.to_s unless opts.key?(:to_dir) || opts.key?(:to_file)
Asciidoctor.convert_file input.to_s, opts
else
Asciidoctor.convert input, opts
end
end
def to_epub(input, opts = {})
result = convert input, opts
return result if result.is_a?(GEPUB::Book)
output = Pathname.new result.attr('outfile')
book = GEPUB::Book.parse output
[book, output]
end
def to_mobi(input, opts = {})
skip_unless_has_kindlegen
(opts[:attributes] ||= {})['ebook-format'] = 'mobi'
doc = convert input, opts
output = Pathname.new(doc.attr('outfile')).sub_ext '.mobi'
expect(output).to exist
output
end
end
RSpec::Matchers.define :have_size do |expected|
match { |actual| actual.size == expected }
failure_message { |actual| %(expected #{actual} to have size #{expected}, but was #{actual.size}) }
end
RSpec::Matchers.define :have_item_with_href do |expected|
match { |actual| actual.item_by_href expected }
failure_message { |actual| %(expected '#{actual.title}' to have item with href #{expected}) }
end
|