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
File.join __dir__, 'temp'
end
def temp_file *path
File.join temp_dir, *path
end
def fixtures_dir
File.join __dir__, 'fixtures'
end
def fixture_file *path
File.join fixtures_dir, path
end
def examples_dir
File.join __dir__, '..', 'data', 'samples'
end
def example_file *path
File.join examples_dir, 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 darwin_platform?
RbConfig::CONFIG['host_os'] =~ /darwin/
end
def skip_if_darwin
# TODO: https://github.com/asciidoctor/asciidoctor-epub3/issues/236
skip '#236: Kindlegen is unavailable for-bit MacOS' if darwin_platform?
end
def convert input, opts = {}
input = fixture_file input if String === input
opts[:to_dir] = temp_dir unless opts.key?(:to_dir) || opts.key?(:to_file)
opts[:backend] = 'epub3'
opts[:header_footer] = true
opts[:mkdirs] = true
opts[:safe] = Asciidoctor::SafeMode::UNSAFE unless opts.key? :safe
Asciidoctor.convert_file input, opts
end
def to_epub input, opts = {}
doc = convert input, opts
output = Pathname.new doc.attr('outfile')
book = GEPUB::Book.parse output
[book, output]
end
def to_mobi input, opts = {}
skip_if_darwin
(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
|