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
112
113
114
115
116
|
# 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 Array === cmd
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 has_logger?
defined? Asciidoctor::LoggerManager
end
def skip_unless_has_logger
skip 'Logger is unavailable on Asciidoctor < 1.5.7' unless has_logger?
end
def skip_unless_has_kindlegen
skip 'KindleGen is gone: https://github.com/asciidoctor/asciidoctor-epub3/issues/363'
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 Pathname === input
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 GEPUB::Book === result
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
|