Skip to content

Commit

Permalink
resolves asciidoctor#1030 allow toc to be positioned using toc macro
Browse files Browse the repository at this point in the history
  • Loading branch information
mojavelinux committed Oct 10, 2019
1 parent 0a83efc commit 2062e34
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[

* reorganize source files under asciidoctor/pdf folder (instead of asciidoctor-pdf)
* reorganize monkeypatch files under asciidoctor/pdf/ext
* allow toc to be positioned using toc macro (#1030)
* extend dots leading up to page number from wrapped line in toc (#1152)
* set fit=contain by default on cover and page background images (#1275)
* implement fit=fill for cover, page background, and running content raster (non-SVG) images (#1276)
Expand Down
12 changes: 9 additions & 3 deletions lib/asciidoctor/pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def convert_document doc
end

toc_num_levels = (doc.attr 'toclevels', 2).to_i
if (insert_toc = (doc.attr? 'toc') && doc.sections?)
if (insert_toc = (doc.attr? 'toc') && !(doc.attr? 'toc-placement', 'macro') && doc.sections?)
start_new_page if @ppbook && verso_page?
allocate_toc doc, toc_num_levels, @y, use_title_page
else
Expand Down Expand Up @@ -2245,8 +2245,14 @@ def convert_thematic_break node
alias convert_horizontal_rule convert_thematic_break

def convert_toc node
#doc = node.document
#allocate_toc doc, (doc.attr 'toclevels', 2).to_i, @y, (doc.doctype == 'book' || (doc.attr? 'title-page'))
doc = node.document
if (doc.attr? 'toc-placement', 'macro') && doc.sections?
if (is_book = doc.doctype == 'book')
start_new_page unless page.empty?
start_new_page if @ppbook && verso_page?
end
allocate_toc doc, (doc.attr 'toclevels', 2).to_i, @y, (is_book || (doc.attr? 'title-page'))
end
nil
end

Expand Down
66 changes: 66 additions & 0 deletions spec/toc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,35 @@
(expect pdf.pages[3][:strings]).to include 'Chapter 1'
end

it 'should insert toc at location of toc macro if toc attribute is macro' do
lorem = ['lorem ipsum'] * 10 * %(\n\n)
input = <<~EOS
= Document Title
:doctype: book
:toc: macro
Preamble
== Introduction
#{lorem}
toc::[]
== Main
#{lorem}
== Conclusion
#{lorem}
EOS
pdf = to_pdf input, analyze: true
(expect pdf.pages).to have_size 6
toc_title_text = (pdf.find_text 'Table of Contents')[0]
(expect toc_title_text[:page_number]).to eql 4
end

it 'should not add toc title to page or outline if toc-title is unset' do
pdf = to_pdf <<~'EOS'
= Document Title
Expand Down Expand Up @@ -359,6 +388,43 @@
(expect pdf.pages[1][:strings]).not_to include '2'
(expect pdf.pages[2][:strings]).to include 'Introduction'
end

it 'should insert toc at location of toc macro if toc attribute is macro' do
lorem = ['lorem ipsum'] * 10 * %(\n\n)
input = <<~EOS
= Document Title
:toc: macro
Preamble
== Introduction
#{lorem}
toc::[]
== Main
#{lorem}
== Conclusion
#{lorem}
EOS
pdf = to_pdf input, analyze: true
(expect pdf.pages).to have_size 2
(expect pdf.find_text string: 'Table of Contents', page_number: 1).to have_size 1
(expect pdf.find_text string: 'Introduction', page_number: 1).to have_size 2
doctitle_text = (pdf.find_text 'Document Title')[0]
toc_title_text = (pdf.find_text 'Table of Contents')[0]
toc_bottom_text = (pdf.find_text '2')[0]
content_top_text = (pdf.find_text 'Preamble')[0]
intro_title_text = (pdf.find_text 'Introduction')[0]
(expect doctitle_text[:y]).to be > toc_title_text[:y]
(expect toc_title_text[:y]).to be < content_top_text[:y]
(expect toc_bottom_text[:y]).to be < content_top_text[:y]
(expect toc_title_text[:y]).to be < intro_title_text[:y]
end
end

it 'should apply consistent font color to running content when base font color is unset', integration: true do
Expand Down

0 comments on commit 2062e34

Please sign in to comment.