Skip to content

Commit

Permalink
resolves asciidoctor#327 add customizable splitting into chapters
Browse files Browse the repository at this point in the history
  • Loading branch information
slonopotamus committed Apr 16, 2020
1 parent 65f8f4b commit a274327
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 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[
* add basic audio and video support (#9)
* add support for `[horizontal]` definition list (#165)
* add proper handling of `:data-uri:` document attribute (#324)
* add support for customizable document splitting into chapters via `epub-chapter-level` attribute (#327)

== 1.5.0.alpha.15 (2020-03-11) - @slonopotamus

Expand Down
6 changes: 6 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,12 @@ The recommended practice is to identify the referenced resource by means of a st
|An optional override of the properties attribute for this document's item in the manifest.
_Only applies to a chapter document._

|epub-chapter-level
|Specify the section level at which to split the EPUB into separate "chapter" files.
The default is to split into chapters at level-1 sections.
This option only affects the internal composition of the EPUB, not the way chapters and sections are displayed to users.
Some readers may be slow if the chapter files are too large, so for large documents with few level-1 headings, one might want to use a chapter level of 2 or 3.

|series-name, series-volume, series-id
|Populates the series statements (`belongs-to-collection`) in the package metadata.
Volume is a number, ID probably a UUID that is constant for all volumes in the series.
Expand Down
19 changes: 11 additions & 8 deletions lib/asciidoctor-epub3/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ def get_chapter_name node
return Asciidoctor::Document === node ? node.attr('docname') || node.id : nil
end
return (node.id || 'preamble') if node.context == :preamble && node.level == 0
Asciidoctor::Section === node && node.level <= 1 ? node.id : nil
chapter_level = [node.document.attr('epub-chapter-level', 1).to_i, 1].max
Asciidoctor::Section === node && node.level <= chapter_level ? node.id : nil
end

def get_numbered_title node
Expand All @@ -141,6 +142,14 @@ def get_numbered_title node
title
end

def collect_toc_items node, into
node.sections.each do |section|
next if get_chapter_name(section).nil?
into << section
collect_toc_items section, into
end
end

def convert_document node
@format = node.attr('ebook-format').to_sym

Expand Down Expand Up @@ -223,13 +232,7 @@ def convert_document node

if node.doctype == 'book'
toc_items = []
node.sections.each do |section|
toc_items << section
section.sections.each do |subsection|
next if get_chapter_name(node).nil?
toc_items << subsection
end
end
collect_toc_items node, toc_items
node.content
else
toc_items = [node]
Expand Down
25 changes: 25 additions & 0 deletions spec/converter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,31 @@
expect(chapter_b.content).not_to include footnote
end

it 'supports custom epub-chapter-level' do
book = to_epub <<~EOS
= Book
:epub-chapter-level: 2
:doctype: book
text0
== Level 1
text1
=== Level 2
text2
==== Level 3
text3
EOS

spine = book.spine.itemref_list
expect(spine).to have_size(3)
end

it 'resolves deep includes relative to document that contains include directive' do
book, = to_epub fixture_file('deep-include/book.adoc')
chapter = book.item_by_href '_chapter.xhtml'
Expand Down

0 comments on commit a274327

Please sign in to comment.