summaryrefslogtreecommitdiff
path: root/spec/converter_spec.rb
blob: 7674f358a5e45d0915d86fdf3d342fc7a4b31567 (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
# 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 'empty.adoc'
    end

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

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

    it 'converts multi-part book' do
      book, = to_epub '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 '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 '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 '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 '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 'resolves deep includes relative to document that contains include directive' do
      book, = to_epub '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 '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 '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 '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 '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 'author/book-no-author.adoc'
      expect(book.publisher).not_to be_nil
      expect(book.publisher.content).to eq('MyProducer')
    end
  end
end