Skip to content

Add support for unit closer pages in toc #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

* Support `unit-closer` pages in TOC
* Fix iframes links for Polish books
* Create `number_parts` method in `ElementBase`
* Create `os_number` method in `ElementBase` for creating `os-number` text
Expand Down
9 changes: 6 additions & 3 deletions lib/kitchen/directions/bake_toc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def self.v1(book:, options: { cases: false })

def self.li_for_unit(unit, options)
chapters = unit.element_children.only(ChapterElement)
pages = unit.element_children.only(PageElement)
pages = unit.element_children.only(PageElement).to_a
after_chapter = pages.filter(&:is_unit_closer?)
before_chapter = pages.difference(after_chapter)

<<~HTML
<li cnx-archive-uri="" cnx-archive-shortid="" class="os-toc-unit" data-toc-type="unit">
Expand All @@ -43,8 +45,9 @@ def self.li_for_unit(unit, options)
<span data-type="" itemprop="" class="os-text">#{unit.title_text}</span>
</a>
<ol class="os-unit">
#{pages.map { |page| li_for_page(page) }.join("\n")}
#{before_chapter.map { |page| li_for_page(page) }.join("\n")}
#{chapters.map { |chapter| li_for_chapter(chapter, options) }.join("\n")}
#{after_chapter.map { |page| li_for_page(page) }.join("\n")}
</ol>
</li>
HTML
Expand Down Expand Up @@ -110,7 +113,7 @@ def self.li_for_page(page)
elsif page.has_ancestor?(:unit) && !
page.has_ancestor?(:chapter) && !
page.has_ancestor?(:composite_chapter)
['os-toc-unit-page', 'intro']
['os-toc-unit-page', page.is_unit_closer? ? 'numbered-section' : 'intro']
else
raise "could not detect which page type class to apply for page.id `#{page.id}`
during baking the TOC. The classes on the page are: `#{page.classes}`
Expand Down
8 changes: 8 additions & 0 deletions lib/kitchen/page_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ def is_introduction?
@is_introduction ||= has_class?('introduction')
end

# Returns true if this page is a unit closer
#
# @return [Boolean]
#
def is_unit_closer?
@is_unit_closer ||= has_class?('unit-closer')
end

# Returns replaces generic call to page.count_in(:chapter)
#
# @raise [StandardError] if called on an introduction page
Expand Down
7 changes: 7 additions & 0 deletions spec/kitchen_spec/directions/bake_toc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@
</h2>
</div>
</div>
<div data-type="page" id="p-unit-closer" class="unit-closer">
<h2 data-type="document-title">
<span class="os-number">3</span>
<span class="os-divider"> </span>
<span data-type="" itemprop="" class="os-text">Unit Closer</span>
</h2>
</div>
</div>
<div data-type="page" id="p8" class="appendix">
<h1 data-type="document-title">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
</ol>
</li>


</ol>
</li>

Expand Down Expand Up @@ -117,6 +118,12 @@
</ol>
</li>

<li class="os-toc-unit-page" cnx-archive-shortid="" cnx-archive-uri="p-unit-closer" data-toc-type="book-content" data-toc-target-type="numbered-section">
<a href="#p-unit-closer">
<span class="os-number">3</span><span class="os-divider"> </span><span data-type="" itemprop="" class="os-text">Unit Closer</span>
</a>
</li>

</ol>
</li>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
</ol>
</li>


</ol>
</li>

Expand Down Expand Up @@ -117,6 +118,12 @@
</ol>
</li>

<li class="os-toc-unit-page" cnx-archive-shortid="" cnx-archive-uri="p-unit-closer" data-toc-type="book-content" data-toc-target-type="numbered-section">
<a href="#p-unit-closer">
<span class="os-number">3</span><span class="os-divider"> </span><span data-type="" itemprop="" class="os-text">Unit Closer</span>
</a>
</li>

</ol>
</li>

Expand Down