lib/asciidoctor-epub3/converter.rb in asciidoctor-epub3-1.5.0.alpha.10 vs lib/asciidoctor-epub3/converter.rb in asciidoctor-epub3-1.5.0.alpha.11
- old
+ new
@@ -7,10 +7,11 @@
module Epub3
# Public: The main converter for the epub3 backend that handles packaging the
# EPUB3 or KF8 publication file.
class Converter
include ::Asciidoctor::Converter
+ include ::Asciidoctor::Logging
include ::Asciidoctor::Writer
register_for 'epub3'
def initialize backend, opts
@@ -25,11 +26,16 @@
def convert node, name = nil
if (name ||= node.node_name) == 'document'
@validate = node.attr? 'ebook-validate'
@extract = node.attr? 'ebook-extract'
@compress = node.attr 'ebook-compress'
- Packager.new node, (node.references[:spine_items] || [node]), node.attributes['ebook-format'].to_sym
+ spine_items = node.references[:spine_items]
+ if spine_items.nil?
+ logger.error %(#{::File.basename node.document.attr('docfile')}: failed to find spine items, produced file will be invalid)
+ spine_items = []
+ end
+ Packager.new node, spine_items, node.attributes['ebook-format'].to_sym
# converting an element from the spine document, such as an inline node in the doctitle
elsif name.start_with? 'inline_'
(@content_converter ||= ::Asciidoctor::Converter::Factory.default.create 'epub3-xhtml5').convert node, name
else
raise ::ArgumentError, %(Encountered unexpected node in epub3 package converter: #{name})
@@ -45,10 +51,11 @@
# Public: The converter for the epub3 backend that converts the individual
# content documents in an EPUB3 publication.
class ContentConverter
include ::Asciidoctor::Converter
+ include ::Asciidoctor::Logging
register_for 'epub3-xhtml5'
LF = ?\n
NoBreakSpace = ' '
@@ -74,13 +81,10 @@
'>' => '>',
}
ToHtmlSpecialCharsRx = /[#{ToHtmlSpecialCharsMap.keys.join}]/
- OpenParagraphTagRx = /^<p>/
- CloseParagraphTagRx = /<\/p>$/
-
def initialize backend, opts
super
basebackend 'html'
outfilesuffix '.xhtml'
htmlsyntax 'xml'
@@ -90,11 +94,11 @@
def convert node, name = nil
if respond_to? name ||= node.node_name
send name, node
else
- warn %(asciidoctor: WARNING: conversion missing in epub3 backend for #{name})
+ logger.warn %(conversion missing in epub3 backend for #{name})
end
end
def document node
docid = node.id
@@ -126,24 +130,19 @@
content = node.content
# NOTE must run after content is resolved
# TODO perhaps create dynamic CSS file?
if @icon_names.empty?
- icon_css_head = icon_css_scoped = ''
+ icon_css_head = ''
else
icon_defs = @icon_names.map {|name|
%(.i-#{name}::before { content: "#{FontIconMap[name.tr('-', '_').to_sym]}"; })
} * LF
icon_css_head = %(<style>
#{icon_defs}
</style>
)
- # NOTE Namo Pubtree requires icon CSS to be repeated inside <body> (or in a linked stylesheet); wrap in div to hide from Aldiko
- icon_css_scoped = (node.attr? 'ebook-format', 'kf8') ? '' : %(<div style="display: none" aria-hidden="true"><style scoped="scoped">
-#{icon_defs}
-</style></div>
-)
end
# NOTE kindlegen seems to mangle the <header> element, so we wrap its content in a div
lines = [%(<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="#{lang = node.attr 'lang', 'en'}" lang="#{lang}">
@@ -162,11 +161,11 @@
});
]]></script>
</head>
<body>
<section class="chapter" title="#{doctitle_sanitized.gsub '"', '"'}" epub:type="chapter" id="#{docid}">
-#{icon_css_scoped}<header>
+<header>
<div class="chapter-header">
#{byline}<h1 class="chapter-title">#{title}#{subtitle ? %(<small class="subtitle">#{subtitle_formatted}</small>) : ''}</h1>
</div>
</header>
#{content})]
@@ -355,11 +354,11 @@
footer_content << %(<span class="context">#{node.title}</span>) if node.title?
footer_tag = footer_content.empty? ? '' : %(
<footer>~ #{footer_content * ' '}</footer>)
- content = ((convert_content node).strip.sub OpenParagraphTagRx, '<p><span class="open-quote">“</span>').sub CloseParagraphTagRx, '<span class="close-quote">”</span></p>'
+ content = (convert_content node).strip
%(<div#{id_attr}#{class_attr}>
<blockquote>
#{content}#{footer_tag}
</blockquote>
</div>)
@@ -685,14 +684,14 @@
if (refs = refdoc.references[:refs]) && ::Asciidoctor::AbstractNode === (ref = refs[refdoc_refid])
text ||= ::Asciidoctor::Document === ref ? ((ref.attr 'docreftext') || ref.doctitle) : ref.xreftext((@xrefstyle ||= (doc.attr 'xrefstyle')))
elsif (xreftext = refdoc.references[:ids][refdoc_refid])
text ||= xreftext
else
- warn %(asciidoctor: WARNING: #{::File.basename doc.attr('docfile')}: invalid reference to unknown anchor in #{refdoc_id} chapter: #{refdoc_refid})
+ logger.warn %(#{::File.basename doc.attr('docfile')}: invalid reference to unknown anchor in #{refdoc_id} chapter: #{refdoc_refid})
end
else
- warn %(asciidoctor: WARNING: #{::File.basename doc.attr('docfile')}: invalid reference to anchor in unknown chapter: #{refdoc_id})
+ logger.warn %(#{::File.basename doc.attr('docfile')}: invalid reference to anchor in unknown chapter: #{refdoc_id})
end
else
id_attr = (@xrefs_seen.add? refid) ? %( id="xref-#{refid}") : ''
if (refs = doc.references[:refs])
if ::Asciidoctor::AbstractNode === (ref = refs[refid])
@@ -704,11 +703,11 @@
if xreftext
text ||= xreftext
else
# FIXME: we get false negatives for reference to bibref when using Asciidoctor < 1.5.6
- warn %(asciidoctor: WARNING: #{::File.basename doc.attr('docfile')}: invalid reference to unknown local anchor (or valid bibref): #{refid})
+ logger.warn %(#{::File.basename doc.attr('docfile')}: invalid reference to unknown local anchor (or valid bibref): #{refid})
end
end
%(<a#{id_attr} href="#{target}" class="xref">#{text || "[#{refid}]"}</a>)
when :ref
%(<a id="#{target}"></a>)
@@ -902,11 +901,11 @@
else
synthetic = true
end
id = %(#{pre}document#{sep}#{doc.object_id}) if synthetic
end
- warn %(asciidoctor: ERROR: chapter uses a reserved ID: #{id}) if !synthetic && (ReservedIds.include? id)
+ logger.error %(chapter uses a reserved ID: #{id}) if !synthetic && (ReservedIds.include? id)
id
end
end
end
@@ -914,10 +913,10 @@
Extensions.register do
if (document = @document).backend == 'epub3'
document.attributes['spine'] = ''
document.set_attribute 'listing-caption', 'Listing'
- if !(defined? ::AsciidoctorJ) && (::Gem.try_activate 'pygments.rb')
+ if !(defined? ::JRuby) && (::Gem.try_activate 'pygments.rb')
if document.set_attribute 'source-highlighter', 'pygments'
document.set_attribute 'pygments-css', 'style'
document.set_attribute 'pygments-style', 'bw'
end
end