Skip to content

Commit

Permalink
resolves asciidoctor#396 add support for :back-cover-image: document …
Browse files Browse the repository at this point in the history
…attribute
  • Loading branch information
slonopotamus committed Apr 18, 2021
1 parent 0b1021b commit fc6c16b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[
* update Font Awesome Solid to 5.15.1
* use CSS for image size scaling instead of `width` attribute. #382
* use pygments.rb 2.0.0 in CI
* add support for `:back-cover-image:` document attribute (#396)

== 1.5.0.alpha.19 (2020-10-21) - @slonopotamus

Expand Down
1 change: 0 additions & 1 deletion WORKLOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
.Packager
* HIGH: add NOTICE.adoc to e-book archive
* HIGH: minimize CSS (and any other assets worth minimizing)
* HIGH: add back cover if specified
* MEDIUM: add JavaScript to nav.xhtml to add class for epubReadingSystem to body
* MEDIUM: option to add nav.xhtml to navigation flow?
* support subtitle as separate from main title in package metadata
Expand Down
21 changes: 13 additions & 8 deletions lib/asciidoctor-epub3/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ def convert_document node
# https://idpf.github.io/epub-vocabs/structure/
landmarks = []

cover_page = add_cover_page node
landmarks << { type: 'cover', href: cover_page.href, title: 'Cover' } unless cover_page.nil?
front_cover = add_cover_page node, 'front-cover'
landmarks << { type: 'cover', href: front_cover.href, title: 'Front Cover' } unless front_cover.nil?

front_matter_page = add_front_matter_page node
landmarks << { type: 'frontmatter', href: front_matter_page.href, title: 'Front Matter' } unless front_matter_page.nil?
Expand All @@ -253,6 +253,9 @@ def convert_document node
add_chapter node
end

_back_cover = add_cover_page node, 'back-cover'
# TODO: add landmark for back cover? But what epub:type?

landmarks << { type: 'bodymatter', href: %(#{get_chapter_name toc_items[0]}.xhtml), title: 'Start of Content' } unless toc_items.empty?

toc_items.each do |item|
Expand Down Expand Up @@ -1347,28 +1350,30 @@ def add_theme_assets doc
nil
end

def add_cover_page doc
return nil if (image_path = doc.attr 'front-cover-image').nil?
def add_cover_page doc, name
image_attr_name = %(#{name}-image)

return nil if (image_path = doc.attr image_attr_name).nil?

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

image_attrs = {}
if (image_path.include? ':') && image_path =~ ImageMacroRx
logger.warn %(deprecated block macro syntax detected in front-cover-image attribute) if image_path.start_with? 'image::'
logger.warn %(deprecated block macro syntax detected in :#{attr_name}: attribute) if image_path.start_with? 'image::'
image_path = %(#{imagesdir}#{$1})
(::Asciidoctor::AttributeList.new $2).parse_into image_attrs, %w(alt width height) unless $2.empty?
end

image_href = %(#{imagesdir}jacket/cover#{::File.extname image_path})
image_href = %(#{imagesdir}jacket/#{name}#{::File.extname image_path})

workdir = doc.attr 'docdir'
workdir = '.' if workdir.nil_or_empty?

begin
@book.add_item(image_href, content: File.join(workdir, image_path)).cover_image
rescue => e
logger.error %(#{::File.basename doc.attr('docfile')}: error adding front cover image. Make sure that :front-cover-image: attribute points to a valid image file. #{e})
logger.error %(#{::File.basename doc.attr('docfile')}: error adding cover image. Make sure that :#{image_attr_name}: attribute points to a valid image file. #{e})
return nil
end

Expand Down Expand Up @@ -1409,7 +1414,7 @@ def add_cover_page doc
</svg></body>
</html>).to_ios

@book.add_ordered_item 'cover.xhtml', content: content, id: 'cover'
@book.add_ordered_item %(#{name}.xhtml), content: content, id: name
end

def get_frontmatter_files doc, workdir
Expand Down
5 changes: 5 additions & 0 deletions spec/fixtures/back-cover-image/book.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
= Book with front cover image
:doctype: book
:back-cover-image: default-cover.png

Text
Binary file added spec/fixtures/back-cover-image/default-cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 12 additions & 3 deletions spec/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,20 @@

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'
cover_image = book.item_by_href 'jacket/front-cover.png'
expect(cover_image).not_to be_nil
cover_page = book.item_by_href 'cover.xhtml'
cover_page = book.item_by_href 'front-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"/>'
expect(cover_page.content).to include '<image width="1050" height="1600" xlink:href="jacket/front-cover.png"/>'
end

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

it "doesn't crash if cover image points to a directory" do
Expand Down

0 comments on commit fc6c16b

Please sign in to comment.