Skip to content

Commit

Permalink
resolves asciidoctor#282 use imagedir from an image's context during …
Browse files Browse the repository at this point in the history
…packaging

resolves asciidoctor#30 inline images are not included
resolves asciidoctor#169 images in tables don't show up
resolves asciidoctor#190 plantumlconfig is looking in the wrong dir
  • Loading branch information
slonopotamus committed Jan 30, 2020
1 parent 9707cfc commit 831ddab
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 29 deletions.
6 changes: 5 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ source 'https://rubygems.org'
# Look in asciidoctor-epub3.gemspec for runtime and development dependencies.
gemspec

gem 'asciidoctor', ENV['ASCIIDOCTOR_VERSION'], require: false if ENV.key? 'ASCIIDOCTOR_VERSION'
if ENV.key? 'ASCIIDOCTOR_VERSION'
gem 'asciidoctor', ENV['ASCIIDOCTOR_VERSION'], require: false
# Newer asciidoctor-diagram 1.5.x require asciidoctor >=1.5.7
gem 'asciidoctor-diagram', '1.5.16', require: false if Gem::Version.new(ENV['ASCIIDOCTOR_VERSION']) < Gem::Version.new('2.0.0')
end

group :optional do
gem 'pygments.rb', '1.2.1'
Expand Down
1 change: 1 addition & 0 deletions asciidoctor-epub3.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ An extension for Asciidoctor that converts AsciiDoc documents to EPUB3 and KF8/M

s.require_paths = ['lib']

s.add_development_dependency 'asciidoctor-diagram', '>= 1.5.0', '< 3.0.0'
s.add_development_dependency 'rake', '~> 13.0.0'
s.add_development_dependency 'rspec', '~> 3.9.0'
s.add_development_dependency 'rubocop', '~> 0.79.0'
Expand Down
25 changes: 23 additions & 2 deletions lib/asciidoctor-epub3/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,29 @@ def ulist node
lines * LF
end

def doc_option document, key
loop do
value = document.options[key]
return value unless value.nil?
document = document.parent_document
break if document.nil?
end
nil
end

def root_document document
document = document.parent_document until document.parent_document.nil?
document
end

def image node
target = node.attr 'target'
out_dir = node.attr('outdir', nil, true) || doc_option(node.document, :to_dir)
target = node.image_uri node.attr 'target'
unless ::File.exist? fs_path = (::File.join out_dir, target)
fs_path = ::File.join node.attr('docdir', nil, true), target
end
# TODO: with Asciidoctor >= 1.5.7, we can use references[:images] since they now have .imagesdir field.
(root_document(node.document).references[:epub_images] ||= []) << { name: target, path: fs_path } if node.document.options[:catalog_assets]
type = (::File.extname target)[1..-1]
id_attr = node.id ? %( id="#{node.id}") : ''
img_attrs = [%(alt="#{node.attr 'alt'}")]
Expand Down Expand Up @@ -652,7 +673,7 @@ def image node
=end
%(<figure#{id_attr} class="image#{prepend_space node.role}">
<div class="content">
<img src="#{node.image_uri node.attr('target')}" #{img_attrs * ' '}/>
<img src="#{target}" #{img_attrs * ' '}/>
</div>#{node.title? ? %(
<figcaption>#{node.captioned_title}</figcaption>) : ''}
</figure>)
Expand Down
23 changes: 11 additions & 12 deletions lib/asciidoctor-epub3/packager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def add_theme_assets doc

def add_cover_image doc
imagesdir = (doc.attr 'imagesdir', '.').chomp '/'
imagesdir = (imagesdir == '.' ? nil : %(#{imagesdir}/))
imagesdir = (imagesdir == '.' ? '' : %(#{imagesdir}/))

if (image_path = doc.attr 'front-cover-image')
image_attrs = {}
Expand Down Expand Up @@ -225,13 +225,13 @@ def add_content_images doc, images
self_logger = logger
workdir = (workdir = doc.attr 'docdir').nil_or_empty? ? '.' : workdir
resources workdir: workdir do
images.each do |image|
if (image_path = image[:path]).start_with? %(#{docimagesdir}jacket/cover.)
self_logger.warn %(image path is reserved for cover artwork: #{image_path}; skipping image found in content)
elsif ::File.readable? image_path
file image_path
images.each do |image_name, image_data|
if image_name.start_with? %(#{docimagesdir}jacket/cover.)
self_logger.warn %(image path is reserved for cover artwork: #{image_name}; skipping image found in content)
elsif ::File.readable? image_data[:path]
file [image_name, image_data[:path]]
else
self_logger.error %(#{::File.basename image[:docfile]}: image not found or not readable: #{::File.expand_path image_path, workdir})
self_logger.error %(#{::File.basename image_data[:docfile]}: image not found or not readable: #{image_data[:path]})
end
end
end
Expand Down Expand Up @@ -284,20 +284,19 @@ def add_content doc
builder.add_front_matter_page doc, self
spine.each_with_index do |item, _i|
docfile = item.attr 'docfile'
imagesdir = (item.attr 'imagesdir', '.').chomp '/'
imagesdir = (imagesdir == '.' ? '' : %(#{imagesdir}/))
file %(#{item.id || (item.attr 'docname')}.xhtml) => (builder.postprocess_xhtml item.convert, format)
add_property 'svg' if ((item.attr 'epub-properties') || []).include? 'svg'
# QUESTION should we pass the document itself?
item.references[:images].each do |target|
images[image_path = %(#{imagesdir}#{target})] ||= { docfile: docfile, path: image_path }
next if (image_refs = item.references[:epub_images]).nil?
image_refs.each do |image|
images[image[:name]] ||= { docfile: docfile, path: image[:path] }
end
# QUESTION reenable?
#linear 'yes' if i == 0
end
end
end
add_content_images doc, images.values
add_content_images doc, images
nil
end

Expand Down
4 changes: 2 additions & 2 deletions lib/asciidoctor-epub3/spine_item_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ def process doc, reader, target, _attributes

# NOTE can't assign spine document as parent since there's too many assumptions in the Asciidoctor processor
spine_item_doc = ::Asciidoctor.load_file include_file,
# setting base_dir breaks if outdir is not a subdirectory of spine_doc.base_dir
#base_dir: spine_doc.base_dir,
base_dir: spine_doc.base_dir,
# NOTE won't write to correct directory if safe mode is :secure
safe: spine_doc.safe,
backend: 'epub3-xhtml5',
doctype: :article,
header_footer: true,
catalog_assets: true,
to_dir: spine_doc.options[:to_dir],
attributes: inherited_attrs

# restore attributes to those defined in the document header
Expand Down
11 changes: 3 additions & 8 deletions spec/converter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,9 @@
backend: 'epub3',
header_footer: true,
mkdirs: true
prev_zip_encoding = Zip.force_entry_names_encoding
begin
Zip.force_entry_names_encoding = 'UTF-8'
Zip::File.open outfile do |zip|
expect(zip.find_entry('OEBPS/test-é.xhtml')).not_to be_nil
end
ensure
Zip.force_entry_names_encoding = prev_zip_encoding

Zip::File.open outfile do |zip|
expect(zip.find_entry('OEBPS/test-é.xhtml')).not_to be_nil
end
end

Expand Down
5 changes: 5 additions & 0 deletions spec/fixtures/diagram/book.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
= Diagrams book
:doctype: book
:plantumlconfig: ./plantuml.cfg

include::subdir/chapter.adoc[]
3 changes: 3 additions & 0 deletions spec/fixtures/diagram/plantuml.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
' This file only exists to check that plantuml finds it in correct location without warnings'

skinparam handwritten true
48 changes: 48 additions & 0 deletions spec/fixtures/diagram/subdir/chapter.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
== Diagrams
:imagesdir: a

[ditaa, "a"]
....
+--+
|A |
+--+
....

:imagesdir: b

[ditaa, "b"]
....
+--+
|B |
+--+
....

:!imagesdir:

[ditaa, "c"]
....
+--+
|C |
+--+
....

:imagesdir: d

[plantuml]
---------------------------------------------------------------------
@startuml
title Accepting Payments: Blocking the GUI and Synchronous
boundary "Web GUI" as GUI
control "API"
boundary "Payment Gateway" as PG
GUI -> API: POST /payments
API -> PG: POST /submitPaymentAttempt
PG -> PG: May take 30-60s to confirm
API <-- PG: HTTP 200 OK
GUI <-- API: HTTP 200 OK
@enduml
---------------------------------------------------------------------
5 changes: 5 additions & 0 deletions spec/fixtures/inline-image/book.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
= Inline images
:doctype: book
:imagesdir: imagez

include::subdir/chapter.adoc[]
Binary file added spec/fixtures/inline-image/imagez/square.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions spec/fixtures/inline-image/subdir/chapter.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
== Chapter

[separator=¦]
|===
a¦[ditaa, "inline-diag"]
....
+--------------+
|Inline diagram|
+--------------+
....
|===

[cols="1,1"]
|===
a|image::square.png[]
a|Do do see a red square on the left?
|===
47 changes: 47 additions & 0 deletions spec/images_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require_relative 'spec_helper'
require 'asciidoctor-diagram'

describe 'Asciidoctor::Epub3::Converter - Images' do
# safe-mode + (outdir != sourcedir) == jail violation in asciidoctor-diagram
# See https://github.com/asciidoctor/asciidoctor-diagram/issues/262
diagram_safe_mode = Asciidoctor::SafeMode::UNSAFE

it 'handles imagesoutdir != imagesdir != "{base_dir}/images"' do
in_file = fixture_file 'diagram', 'book.adoc'
out_dir = temp_file 'diagram'
out_file = File.join out_dir, 'book.epub'
Asciidoctor.convert_file in_file,
to_dir: out_dir,
backend: 'epub3',
header_footer: true,
mkdirs: true,
safe: diagram_safe_mode
expect(File).to exist(File.join(out_dir, 'a', 'a.png'))
expect(File).to exist(File.join(out_dir, 'b', 'b.png'))
expect(File).to exist(File.join(out_dir, 'c.png'))
Zip::File.open out_file do |zip|
expect(zip.find_entry('OEBPS/a/a.png')).not_to be_nil
expect(zip.find_entry('OEBPS/b/b.png')).not_to be_nil
expect(zip.find_entry('OEBPS/c.png')).not_to be_nil
end
end

it 'handles inline images' do
in_file = fixture_file 'inline-image', 'book.adoc'
out_dir = temp_file 'inline-image'
out_file = File.join out_dir, 'book.epub'
Asciidoctor.convert_file in_file,
to_dir: out_dir,
backend: 'epub3',
header_footer: true,
mkdirs: true,
safe: diagram_safe_mode
expect(File).to exist(File.join(out_dir, 'imagez', 'inline-diag.png'))
Zip::File.open out_file do |zip|
expect(zip.find_entry('OEBPS/imagez/inline-diag.png')).not_to be_nil
expect(zip.find_entry('OEBPS/imagez/square.png')).not_to be_nil
end
end
end
10 changes: 6 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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
Expand Down Expand Up @@ -39,23 +41,23 @@ def temp_dir
File.join __dir__, 'temp'
end

def temp_file path
File.join temp_dir, path
def temp_file *path
File.join temp_dir, *path
end

def fixtures_dir
File.join __dir__, 'fixtures'
end

def fixture_file path
def fixture_file *path
File.join fixtures_dir, path
end

def examples_dir
File.join __dir__, '..', 'data', 'samples'
end

def example_file path
def example_file *path
File.join examples_dir, path
end

Expand Down

0 comments on commit 831ddab

Please sign in to comment.