Skip to content

Commit

Permalink
resolves #282 use imagedir from an image's context during packaging
Browse files Browse the repository at this point in the history
resolves #169 images in tables don't show up
resolves #190 plantumlconfig is looking in the wrong dir
resolves #30 Inline images are not included
  • Loading branch information
slonopotamus committed Feb 3, 2020
1 parent dfd72ee commit 905957c
Show file tree
Hide file tree
Showing 16 changed files with 188 additions and 23 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
This document provides a high-level view of the changes to the {project-name} by release.
For a detailed view of what has changed, refer to the {uri-repo}/commits/master[commit history] on GitHub.

== Unreleased

* use imagedir from an image's context during packaging (#282)
* fix images in tables not included in epub archive (#169)
* fix `base_dir` set to wrong value in chapter documents (#190)
* fix inline images not being included in epub archive (#30)

== 1.5.0.alpha.13 (2020-02-04) - @slonopotamus

* remove kindlegen and epubcheck-ruby from runtime dependencies (#288)
Expand Down
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
# epubcheck-ruby might be safe to be converted into runtime dependency, but could have issues when packaged into asciidoctorj-epub3
Expand Down
3 changes: 0 additions & 3 deletions WORKLOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

== TODO

* use ImageReference if available to locate image
* remove conum patch for listing block (fixed in Asciidoctor 1.5.6)
* change has_role? to role?
* use empty string in place of nil in interpolated strings (few more still to check)
Expand Down Expand Up @@ -53,7 +52,6 @@
* HIGH: if running on a single article, then the document id has a conflicting purpose
* HIGH: require spine option on include directive in order to create a chapter file (requires change in Asciidoctor)
* HIGH: promote regexps to constants
* HIGH: use more thorough image path resolution
* HIGH: put shy marks after dashes to allow wrapping to occur in text justification
* MEDIUM: add landmarks nav list to nav.xhtml; see https://github.com/IDPF/epub3-samples/blob/master/31/moby-dick-mo-xhtml/EPUB/toc.xhtml
* MEDIUM: Kindle links to cover page when clicking on first chapter instead of first chapter page
Expand All @@ -76,7 +74,6 @@
* MEDIUM: add JavaScript to nav.xhtml to add class for epubReadingSystem to body
* MEDIUM: rename OEBPS folder to EPUB
* MEDIUM: set modified date explicitly
* MEDIUM: use function to build and manipulate image paths
* MEDIUM: option to add nav.xhtml to navigation flow?
* MEDIUM: add Pygments stylesheet to EPUB archive if pygments-css=class
* support subtitle as separate from main title in package metadata
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
37 changes: 35 additions & 2 deletions lib/asciidoctor-epub3/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,40 @@ def convert_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 register_image node, target
out_dir = node.attr('outdir', nil, true) || doc_option(node.document, :to_dir)
unless ::File.exist? fs_path = (::File.join out_dir, target)
# This is actually a hack. It would be more correct to set base_dir of chapter document to base_dir of spine document.
# That's how things would normally work if there was no separation between these documents, and instead chapters were normally included into spine document.
# However, setting chapter base_dir to spine base_dir breaks parser.rb because it resolves includes in chapter document relative to base_dir instead of actual location of chapter file.
# Choosing between two evils - a hack here or writing a full-blown include processor for chapter files, I chose the former.
# In the future, this all should be thrown away when we stop parsing chapters as a standalone documents.
# https://github.com/asciidoctor/asciidoctor-epub3/issues/47 is used to track that.
base_dir = root_document(node.document).references[:spine].base_dir
fs_path = ::File.join base_dir, target
end
# We need *both* virtual and physical image paths. Unfortunately, references[:images] only has one of them.
(root_document(node.document).references[:epub_images] ||= []) << { name: target, path: fs_path } if doc_option node.document, :catalog_assets
end

def convert_image node
target = node.attr 'target'
target = node.image_uri node.attr 'target'
register_image node, target
type = (::File.extname target)[1..-1]
id_attr = node.id ? %( id="#{node.id}") : ''
img_attrs = [%(alt="#{node.attr 'alt'}")]
Expand Down Expand Up @@ -653,7 +685,7 @@ def convert_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 Expand Up @@ -761,6 +793,7 @@ def convert_inline_image node
%(<i class="#{i_classes * ' '}"></i>)
else
target = node.image_uri node.target
register_image node, target
img_attrs = [%(alt="#{node.attr 'alt'}"), %(class="inline#{node.role? ? " #{node.role}" : ''}")]
if target.end_with? '.svg'
img_attrs << %(style="width: #{node.attr 'scaledwidth', '100%'}")
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 @@ -121,7 +121,7 @@ def add_cover_image doc
return if (image_path = doc.attr 'front-cover-image').nil?

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

image_attrs = {}
if (image_path.include? ':') && image_path =~ ImageMacroRx
Expand Down Expand Up @@ -224,13 +224,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 @@ -283,20 +283,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
1 change: 1 addition & 0 deletions lib/asciidoctor-epub3/spine_item_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def process doc, reader, target, _attributes
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
7 changes: 7 additions & 0 deletions spec/fixtures/diagram/book.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
= Diagrams book
:doctype: book

// TODO: https://github.com/asciidoctor/asciidoctor-epub3/issues/190
// :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, "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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions spec/fixtures/inline-image/subdir/chapter.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
== Chapter

A

image:wolpertinger.jpg[] B

Z

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

[cols="1,1"]
|===
a|image::square.png[]
a|Do do see a red square on the left?
|===
32 changes: 32 additions & 0 deletions spec/image_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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 '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 '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
end
15 changes: 10 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,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 All @@ -84,7 +84,7 @@ def convert input, opts = {}
opts[:backend] = 'epub3'
opts[:header_footer] = true
opts[:mkdirs] = true
opts[:safe] = Asciidoctor::SafeMode::UNSAFE
opts[:safe] = Asciidoctor::SafeMode::UNSAFE unless opts.key? :safe
Asciidoctor.convert_file input, opts
end

Expand All @@ -109,3 +109,8 @@ def to_mobi input, opts = {}
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

0 comments on commit 905957c

Please sign in to comment.