summaryrefslogtreecommitdiff
path: root/spec/converter_spec.rb
blob: 3d1e24ee5bb54bfcb342da87a9e1f2bb43eb4bce (plain) (blame)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# frozen_string_literal: true

require_relative 'spec_helper'

describe Asciidoctor::Epub3::Converter do
  describe '#convert' do
    it 'converts empty file to epub without exceptions' do
      to_epub fixture_file('empty.adoc')
    end

    it 'converts empty file to mobi without exceptions' do
      to_mobi fixture_file('empty.adoc')
    end

    it 'converts empty heredoc document to epub without exceptions' do
      to_epub <<~EOS
      EOS
    end

    it 'converts minimal heredoc document to epub without exceptions' do
      book = to_epub <<~EOS
      = Title
      EOS
      expect(book).to be_a(GEPUB::Book)
    end

    it 'converts chapter with unicode title to unicode filename' do
      _, out_file = to_epub fixture_file('unicode/book.adoc')
      Zip::File.open out_file do |zip|
        expect(zip.find_entry('EPUB/test-é.xhtml')).not_to be_nil
      end
    end

    it 'extracts book when given ebook-extract attribute' do
      _, out_file = to_epub fixture_file('minimal/book.adoc'), attributes: { 'ebook-extract' => '' }
      out_dir = out_file.dirname
      expect(out_dir.join('book', 'EPUB', 'package.opf')).to exist
    end

    it 'uses current date as fallback when date attributes cannot be parsed' do
      # TODO: assert that error log contains 'failed to parse revdate' error when we add test infrastructure for logs
      book, = to_epub fixture_file('minimal/book.adoc'), attributes: { 'revdate' => 'garbage' }
      expect(book.metadata.date.content).not_to be_nil
    end

    it 'adds listing captions by default' do
      book, = to_epub fixture_file('listing/book.adoc')
      chapter = book.item_by_href '_chapter.xhtml'
      expect(chapter).not_to be_nil
      expect(chapter.content).to include '<figcaption>Listing 1. .gitattributes</figcaption>'
    end

    it 'increments listing numbering across chapters' do
      book, = to_epub fixture_file('listing-chapter/book.adoc')
      chapter_b = book.item_by_href 'chapter-b.xhtml'
      expect(chapter_b).not_to be_nil
      expect(chapter_b.content).to include '<figcaption>Listing 2. .gitattributes</figcaption>'
    end

    it 'adds preamble chapter' do
      book, = to_epub fixture_file('preamble/book.adoc')
      spine = book.spine.itemref_list
      expect(spine).to have_size(2)

      preamble = book.items[spine[0].idref]
      expect(preamble).not_to be_nil
      expect(preamble.href).to eq('preamble.xhtml')
      expect(preamble.content).to include %(I am a preamble)
    end

    it 'converts appendix to a separate book chapter' do
      book, = to_epub fixture_file('appendix.adoc')
      spine = book.spine.itemref_list
      expect(spine).to have_size(2)

      appendix = book.items[spine[1].idref]
      expect(appendix).not_to be_nil
      expect(appendix.href).to eq('appendix.xhtml')
      expect(appendix.content).to include('<b>Appendix</b> <b>A:</b> <b>Appendix</b>')
    end

    it 'supports section numbers' do
      book, = to_epub <<~EOS
= Title
:sectnums:
:doctype: book

== Chapter
      EOS
      chapter = book.item_by_href '_chapter.xhtml'
      expect(chapter).not_to be_nil
      expect(chapter.content).to include('<b>1.</b> <b>Chapter</b>')
    end

    it 'converts multi-part book' do
      book, = to_epub fixture_file('multi-part.adoc')
      spine = book.spine.itemref_list
      expect(spine).to have_size(4)

      part2 = book.items[spine[2].idref]
      expect(part2.href).to eq('part-2.xhtml')
      expect(part2.content).to include %(Three)
      chapter21 = book.items[spine[3].idref]
      expect(chapter21.href).to eq('chapter-2-1.xhtml')
      expect(chapter21.content).to include %(Four)
    end

    it 'populates ebook subject from keywords' do
      book, = to_epub fixture_file('keywords/book.adoc')
      keywords = book.subject_list.map(&:content)
      expect(keywords).to eq(%w(a b c))
    end

    it 'adds front matter page with images' do
      book, = to_epub fixture_file('front-matter/book.adoc')
      spine = book.spine.itemref_list
      expect(spine).to have_size(2)

      front_matter = book.items[spine[0].idref]
      expect(front_matter).not_to be_nil
      expect(front_matter.href).to eq('front-matter.xhtml')
      expect(front_matter.content).to include 'Matter. Front Matter.'
      expect(book).to have_item_with_href 'square.png'
    end

    it 'adds multiple front matter page with images' do
      book, = to_epub fixture_file('front-matter-multi/book.adoc')
      spine = book.spine.itemref_list
      expect(spine).to have_size(3)

      front_matter1 = book.items[spine[0].idref]
      expect(front_matter1).not_to be_nil
      expect(front_matter1.href).to eq('front-matter.1.xhtml')
      expect(front_matter1.content).to include 'Matter. Front Matter.'
      expect(book).to have_item_with_href 'square.png'

      front_matter2 = book.items[spine[1].idref]
      expect(front_matter2).not_to be_nil
      expect(front_matter2.href).to eq('front-matter.2.xhtml')
      expect(front_matter2.content).to include 'Matter. Front Matter. 2'
      expect(book).to have_item_with_href 'square_blue.png'
    end

    it 'places footnotes in the same chapter' do
      book, = to_epub fixture_file('footnote/book.adoc')
      chapter_a = book.item_by_href 'chapter-a.xhtml'
      chapter_b = book.item_by_href 'chapter-b.xhtml'
      expect(chapter_a).not_to be_nil
      expect(chapter_b).not_to be_nil

      expect(chapter_a.content).to include 'A statement.<sup class="noteref">[<a id="noteref-1" href="#note-1" epub:type="noteref">1</a>]</sup>'
      footnote = '<aside id="note-1" epub:type="footnote">
<p><sup class="noteref"><a href="#noteref-1">1</a></sup> Clarification about this statement.</p>
</aside>'
      expect(chapter_a.content).to include footnote
      expect(chapter_b.content).not_to include footnote
    end

    it 'supports custom epub-chapter-level' do
      book = to_epub <<~EOS
= Book
:epub-chapter-level: 2
:doctype: book

text0

== Level 1

text1

=== Level 2

text2

==== Level 3

text3
      EOS

      spine = book.spine.itemref_list
      expect(spine).to have_size(3)
    end

    it 'resolves deep includes relative to document that contains include directive' do
      book, = to_epub fixture_file('deep-include/book.adoc')
      chapter = book.item_by_href '_chapter.xhtml'
      expect(chapter).not_to be_nil
      expect(chapter.content).to include '<p>Hello</p>'
    end

    it 'adds no book authors if there are none' do
      book, = to_epub fixture_file('author/book-no-author.adoc')
      expect(book.creator).to be_nil
      expect(book.creator_list.size).to eq(0)
    end

    it 'adds a single book author' do
      book, = to_epub fixture_file('author/book-one-author.adoc')
      expect(book.creator).not_to be_nil
      expect(book.creator.content).to eq('Author One')
      expect(book.creator.role.content).to eq('aut')
      expect(book.creator_list.size).to eq(1)
    end

    it 'adds multiple book authors' do
      book, = to_epub fixture_file('author/book-multiple-authors.adoc')
      expect(book.metadata.creator).not_to be_nil
      expect(book.metadata.creator.content).to eq('Author One')
      expect(book.metadata.creator.role.content).to eq('aut')
      expect(book.creator_list.size).to eq(2)
      expect(book.metadata.creator_list[0].content).to eq('Author One')
      expect(book.metadata.creator_list[1].content).to eq('Author Two')
    end

    it 'adds the publisher if both publisher and producer are defined' do
      book, = to_epub fixture_file('author/book-one-author.adoc')
      expect(book.publisher).not_to be_nil
      expect(book.publisher.content).to eq('MyPublisher')
    end

    it 'adds the producer as publisher if no publisher is defined' do
      book, = to_epub fixture_file('author/book-no-author.adoc')
      expect(book.publisher).not_to be_nil
      expect(book.publisher.content).to eq('MyProducer')
    end

    it 'adds book series metadata' do
      book = to_epub <<~EOS
= Article
:series-name: My Series
:series-volume: 42
:series-id: bla
      EOS
      meta = book.metadata.meta_list[1]
      expect(meta).not_to be_nil
      expect(meta['property']).to eq('belongs-to-collection')
      expect(meta.content).to eq('My Series')
      expect(meta.refiner('group-position').content).to eq('42')
      expect(meta.refiner('dcterms:identifier').content).to eq('bla')
    end

    it 'supports video' do
      book, = to_epub fixture_file('video/book.adoc')
      chapter = book.item_by_href '_chapter.xhtml'
      expect(chapter).not_to be_nil
      expect(chapter.content).to include '<video src="small.webm" width="400" controls="controls">'
      video = book.item_by_href 'small.webm'
      expect(video).not_to be_nil
      expect(video.media_type).to eq('video/webm')
    end

    it 'supports audio' do
      book, = to_epub fixture_file('audio/book.adoc')
      chapter = book.item_by_href '_chapter.xhtml'
      expect(chapter).not_to be_nil
      expect(chapter.content).to include '<audio src="small.mp3" controls="controls">'
      audio = book.item_by_href 'small.mp3'
      expect(audio).not_to be_nil
      expect(audio.media_type).to eq('audio/mpeg')
    end

    it 'supports horizontal dlist' do
      book = to_epub <<~EOS
= Article

[horizontal]
CPU:: The brain of the computer.
Hard drive:: Permanent storage for operating system and/or user files.
RAM:: Temporarily stores information the CPU uses during operation.
      EOS

      chapter = book.item_by_href '_article.xhtml'
      expect(chapter).not_to be_nil
      expect(chapter.content).to include <<~EOS
<tr>
<td class="hdlist1">
<p>
CPU
</p>
</td>
<td class="hdlist2">
<p>The brain of the computer.</p>
</td>
</tr>
      EOS
    end
  end
end