diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index c145d30a..d2e8d85c 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -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 diff --git a/WORKLOG.adoc b/WORKLOG.adoc index ed061149..237cf68e 100644 --- a/WORKLOG.adoc +++ b/WORKLOG.adoc @@ -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 diff --git a/lib/asciidoctor-epub3/converter.rb b/lib/asciidoctor-epub3/converter.rb index 58114297..5ffdaf5b 100644 --- a/lib/asciidoctor-epub3/converter.rb +++ b/lib/asciidoctor-epub3/converter.rb @@ -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? @@ -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| @@ -1347,20 +1350,22 @@ 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? @@ -1368,7 +1373,7 @@ def add_cover_page doc 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 @@ -1409,7 +1414,7 @@ def add_cover_page doc ).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 diff --git a/spec/fixtures/back-cover-image/book.adoc b/spec/fixtures/back-cover-image/book.adoc new file mode 100644 index 00000000..4f7095b7 --- /dev/null +++ b/spec/fixtures/back-cover-image/book.adoc @@ -0,0 +1,5 @@ += Book with front cover image +:doctype: book +:back-cover-image: default-cover.png + +Text diff --git a/spec/fixtures/back-cover-image/default-cover.png b/spec/fixtures/back-cover-image/default-cover.png new file mode 100644 index 00000000..2d1d2764 Binary files /dev/null and b/spec/fixtures/back-cover-image/default-cover.png differ diff --git a/spec/image_spec.rb b/spec/image_spec.rb index 3323fded..437c9500 100644 --- a/spec/image_spec.rb +++ b/spec/image_spec.rb @@ -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 '' + expect(cover_page.content).to include '' + 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 '' end it "doesn't crash if cover image points to a directory" do