lib/asciidoctor_fb2.rb in asciidoctor-fb2-0.1.0 vs lib/asciidoctor_fb2.rb in asciidoctor-fb2-0.2.0
- old
+ new
@@ -21,11 +21,11 @@
super
outfilesuffix '.fb2.zip'
end
# @param node [Asciidoctor::Document]
- def convert_document(node) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
+ def convert_document(node) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
@book = FB2rb::Book.new
document_info = @book.description.document_info
title_info = @book.description.title_info
title_info.book_title = node.doctitle
@@ -55,10 +55,16 @@
date = node.attr('revdate') || node.attr('docdate')
fb2date = FB2rb::FB2Date.new(date, Date.parse(date))
title_info.date = document_info.date = fb2date
+ unless (cover_image = node.attr('front-cover-image')).nil?
+ cover_image_path = node.image_uri(cover_image)
+ register_binary(node, cover_image_path)
+ title_info.coverpage = FB2rb::Coverpage.new([%(##{cover_image_path})])
+ end
+
document_info.id = node.attr('uuid', '')
document_info.version = node.attr('revnumber')
document_info.program_used = %(Asciidoctor FB2 #{VERSION} using Asciidoctor #{node.attr('asciidoctor-version')})
publisher = node.attr('publisher')
@@ -140,10 +146,24 @@
open, close = QUOTE_TAGS[node.type]
%(#{open}#{node.text}#{close})
end
# @param node [Asciidoctor::Inline]
+ def convert_inline_menu(node)
+ caret = ' <strong>›</strong> '
+ menu = node.attr('menu')
+ menuitem = node.attr('menuitem')
+ submenus = node.attr('submenus').join(%(</b>#{caret}<b>))
+
+ result = %(<strong>#{menu}</strong>)
+ result += %(#{caret}<strong>#{submenus}</strong>) unless submenus.empty?
+ result += %(#{caret}<strong>#{menuitem}</strong>) unless menuitem.empty?
+
+ result
+ end
+
+ # @param node [Asciidoctor::Inline]
def convert_inline_anchor(node) # rubocop:disable Metrics/MethodLength
case node.type
when :xref
%(<a l:href="#{node.target}">#{node.text}</a>)
when :link
@@ -167,17 +187,17 @@
%(<a l:href="#note-#{index}" type="note">[#{index}]</a>)
end
# @param node [Asciidoctor::Inline]
def convert_inline_image(node)
- image_attrs = resolve_image_attrs(node, node.target)
+ image_attrs = register_binary(node, node.image_uri(node.target))
%(<image #{image_attrs * ' '}/>)
end
# @param node [Asciidoctor::Block]
def convert_image(node)
- image_attrs = resolve_image_attrs(node, node.attr('target'))
+ image_attrs = register_binary(node, node.image_uri(node.attr('target')))
image_attrs << %(title="#{node.captioned_title}") if node.title?
image_attrs << %(id="#{node.id}") if node.id
%(<p><image #{image_attrs * ' '}/></p>)
end
@@ -188,12 +208,11 @@
doc
end
# @param node [Asciidoctor::AbstractNode]
# @param target [String]
- def resolve_image_attrs(node, target) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
- target = node.image_uri(target)
+ def register_binary(node, target) # rubocop:disable Metrics/MethodLength
unless Asciidoctor::Helpers.uriish?(target)
out_dir = node.attr('outdir', nil, true) || doc_option(node.document, :to_dir)
fs_path = File.join(out_dir, target)
unless File.readable?(fs_path)
base_dir = root_document(node.document).base_dir
@@ -210,34 +229,44 @@
image_attrs << %(alt="#{node.attr('alt')}") if node.attr? 'alt'
end
# @param node [Asciidoctor::Block]
def convert_admonition(node)
- %(<p><strong>#{node.title || node.caption}:</strong>
+ lines = [%(<p><strong>#{node.title || node.caption}:</strong>
#{node.content}
-</p>)
+</p>)]
+ lines << '<empty-line/>' unless node.has_role?('last')
+ lines * "\n"
end
# @param node [Asciidoctor::List]
def convert_ulist(node)
lines = []
+ @stack ||= []
+
node.items.each do |item|
- lines << %(<p>• #{item.text}</p>)
+ @stack << '•'
+ lines << %(<p>#{@stack * ' '} #{item.text}</p>)
lines << %(<p>#{item.content}</p>) if item.blocks?
+ @stack.pop
end
- lines << '<empty-line/>' unless node.has_role?('last')
+
+ lines << '<empty-line/>' unless node.has_role?('last') || !@stack.empty?
lines * "\n"
end
# @param node [Asciidoctor::List]
- def convert_olist(node)
+ def convert_olist(node) # rubocop:disable Metrics/AbcSize
lines = []
+ @stack ||= []
node.items.each_with_index do |item, index|
- lines << %(<p>#{index + 1}. #{item.text}</p>)
+ @stack << %(#{index + 1}.)
+ lines << %(<p>#{@stack * ' '} #{item.text}</p>)
lines << %(<p>#{item.content}</p>) if item.blocks?
+ @stack.pop
end
- lines << '<empty-line/>' unless node.has_role?('last')
+ lines << '<empty-line/>' unless node.has_role?('last') || !@stack.empty?
lines * "\n"
end
# @param node [Asciidoctor::List]
def convert_dlist(node) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
@@ -317,10 +346,14 @@
nil
end
# @param output [FB2rb::Book]
def write(output, target)
- output.write(target)
+ if target.respond_to?(:end_with?) && target.end_with?('.zip')
+ output.write_compressed(target)
+ else
+ output.write_uncompressed(target)
+ end
end
end
end
end