diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 0d140d6a0..0e6902669 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -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) diff --git a/lib/asciidoctor/pdf/converter.rb b/lib/asciidoctor/pdf/converter.rb index 5dc3b2086..0c8d25154 100644 --- a/lib/asciidoctor/pdf/converter.rb +++ b/lib/asciidoctor/pdf/converter.rb @@ -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 @@ -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 diff --git a/spec/toc_spec.rb b/spec/toc_spec.rb index f661cc899..cf50c02a3 100644 --- a/spec/toc_spec.rb +++ b/spec/toc_spec.rb @@ -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 @@ -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