summaryrefslogtreecommitdiff
path: root/spec/image_spec.rb
blob: 78d3d3f00df7e85729c2145fc6af7e2f3e139c58 (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
# frozen_string_literal: true

require_relative 'spec_helper'
require 'asciidoctor-diagram'

describe 'Asciidoctor::Epub3::Converter - Image' do
  it 'supports imagesoutdir != imagesdir != "{base_dir}/images"' do
    book, out_file = to_epub fixture_file('diagram/book.adoc')
    out_dir = out_file.dirname

    expect(out_dir.join('a', 'a.png')).to exist
    expect(out_dir.join('b', 'b.png')).to exist
    expect(out_dir.join('c.png')).to exist
    expect(out_dir.join('d', 'plantuml.png')).to exist

    expect(book).to have_item_with_href('a/a.png')
    expect(book).to have_item_with_href('b/b.png')
    expect(book).to have_item_with_href('c.png')
    expect(book).to have_item_with_href('d/plantuml.png')
  end

  it 'supports inline images' do
    book, out_file = to_epub fixture_file('inline-image/book.adoc')
    out_dir = out_file.dirname

    expect(out_dir.join('imagez', 'inline-diag.png')).to exist

    expect(book).to have_item_with_href('imagez/inline-diag.png')
    expect(book).to have_item_with_href('imagez/square.png')
    expect(book).to have_item_with_href('imagez/wolpertinger.jpg')
  end

  it 'does not duplicate images in manifest' do
    book, = to_epub fixture_file('inline-image/book.adoc')
    expect(book.items.keys.select {|k| k.include? 'wolpertinger' }.size).to eq(1)
  end

  it 'converts font-based icons to CSS' do
    book, = to_epub fixture_file('icon/book.adoc')
    chapter = book.item_by_href '_chapter.xhtml'
    expect(chapter).not_to be_nil
    expect(chapter.content).to include '.i-commenting::before { content: "\f4ad"; }'
  end

  it 'adds front cover image' do
    book, = to_epub fixture_file('front-cover-image/book.adoc')
    cover_image = book.item_by_href 'jacket/cover.png'
    expect(cover_image).not_to be_nil
    cover_page = book.item_by_href 'cover.xhtml'
    expect(cover_page).not_to be_nil
    expect(cover_page.content).to include '<image width="1050" height="1600" xlink:href="jacket/cover.png"/>'
  end

  it "doesn't crash if cover image points to a directory" do
    book, = to_epub fixture_file('empty.adoc'), attributes: { 'front-cover-image' => '' }
    expect(book).not_to be_nil
  end

  it 'supports image width/height' do
    book, = to_epub fixture_file('image-dimensions/book.adoc')
    chapter = book.item_by_href '_chapter.xhtml'
    expect(chapter).not_to be_nil
    expect(chapter.content).to include '<img src="square.png" alt="100x100" width="100" />'
    expect(chapter.content).to include '<img src="square.png" alt="50x50" width="50" />'
    expect(chapter.content).to include '<img src="square.png" alt="50x?" width="50" />'
  end

  # If this test fails for you, make sure you're using gepub >= 1.0.11
  it 'adds SVG attribute to EPUB manifest if chapter contains SVG images' do
    book, = to_epub fixture_file('svg/book.adoc')
    chapter = book.item_by_href '_chapter.xhtml'
    expect(chapter).not_to be_nil
    properties = chapter['properties']
    expect(properties).to include('svg')
  end
end