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#169 images in tables don't show up
resolves asciidoctor#190 plantumlconfig is looking in the wrong dir
resolves asciidoctor#30 Inline images are not included
  • Loading branch information
slonopotamus committed Jan 31, 2020
1 parent 47e2051 commit 6e5a15f
Show file tree
Hide file tree
Showing 17 changed files with 192 additions and 32 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[
* provide a human-readable error message when we fail to find KindleGen (#268)
* try to use KindleGen/EPUBCheck binary from `$PATH` (#276)
* add `ebook-kindlegen-path`/`ebook-epubcheck-path` attributes to override KindleGen/EPUBCheck executable location (#276)
* 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.11 (2020-01-26) - @slonopotamus

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
gem 'pygments.rb', '1.2.1'
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
30 changes: 28 additions & 2 deletions lib/asciidoctor-epub3/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,33 @@ 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 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)
fs_path = ::File.join node.attr('docdir', nil, true), 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 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 @@ -652,7 +677,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 Expand Up @@ -760,6 +785,7 @@ def 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 @@ -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.
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?
|===
48 changes: 48 additions & 0 deletions spec/images_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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
expect(zip.find_entry('OEBPS/imagez/wolpertinger.jpg')).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 6e5a15f

Please sign in to comment.