Skip to content

Commit 7b5c07a

Browse files
Add support for unit closer pages in toc (#408)
The default for unit pages will remain the same: treat them as introductions This new unit-closer class supports specifying pages that should appear at the end of the unit
1 parent abe1663 commit 7b5c07a

File tree

6 files changed

+36
-3
lines changed

6 files changed

+36
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
* Support `unit-closer` pages in TOC
910
* Fix iframes links for Polish books
1011
* Create `number_parts` method in `ElementBase`
1112
* Create `os_number` method in `ElementBase` for creating `os-number` text

lib/kitchen/directions/bake_toc.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ def self.v1(book:, options: { cases: false })
3333

3434
def self.li_for_unit(unit, options)
3535
chapters = unit.element_children.only(ChapterElement)
36-
pages = unit.element_children.only(PageElement)
36+
pages = unit.element_children.only(PageElement).to_a
37+
after_chapter = pages.filter(&:is_unit_closer?)
38+
before_chapter = pages.difference(after_chapter)
3739

3840
<<~HTML
3941
<li cnx-archive-uri="" cnx-archive-shortid="" class="os-toc-unit" data-toc-type="unit">
@@ -43,8 +45,9 @@ def self.li_for_unit(unit, options)
4345
<span data-type="" itemprop="" class="os-text">#{unit.title_text}</span>
4446
</a>
4547
<ol class="os-unit">
46-
#{pages.map { |page| li_for_page(page) }.join("\n")}
48+
#{before_chapter.map { |page| li_for_page(page) }.join("\n")}
4749
#{chapters.map { |chapter| li_for_chapter(chapter, options) }.join("\n")}
50+
#{after_chapter.map { |page| li_for_page(page) }.join("\n")}
4851
</ol>
4952
</li>
5053
HTML
@@ -110,7 +113,7 @@ def self.li_for_page(page)
110113
elsif page.has_ancestor?(:unit) && !
111114
page.has_ancestor?(:chapter) && !
112115
page.has_ancestor?(:composite_chapter)
113-
['os-toc-unit-page', 'intro']
116+
['os-toc-unit-page', page.is_unit_closer? ? 'numbered-section' : 'intro']
114117
else
115118
raise "could not detect which page type class to apply for page.id `#{page.id}`
116119
during baking the TOC. The classes on the page are: `#{page.classes}`

lib/kitchen/page_element.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ def is_introduction?
7575
@is_introduction ||= has_class?('introduction')
7676
end
7777

78+
# Returns true if this page is a unit closer
79+
#
80+
# @return [Boolean]
81+
#
82+
def is_unit_closer?
83+
@is_unit_closer ||= has_class?('unit-closer')
84+
end
85+
7886
# Returns replaces generic call to page.count_in(:chapter)
7987
#
8088
# @raise [StandardError] if called on an introduction page

spec/kitchen_spec/directions/bake_toc_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@
121121
</h2>
122122
</div>
123123
</div>
124+
<div data-type="page" id="p-unit-closer" class="unit-closer">
125+
<h2 data-type="document-title">
126+
<span class="os-number">3</span>
127+
<span class="os-divider"> </span>
128+
<span data-type="" itemprop="" class="os-text">Unit Closer</span>
129+
</h2>
130+
</div>
124131
</div>
125132
<div data-type="page" id="p8" class="appendix">
126133
<h1 data-type="document-title">

spec/snapshots/Kitchen_Directions_BakeToc_supports_unit_numbering.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
</ol>
7272
</li>
7373

74+
7475
</ol>
7576
</li>
7677

@@ -117,6 +118,12 @@
117118
</ol>
118119
</li>
119120

121+
<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">
122+
<a href="#p-unit-closer">
123+
<span class="os-number">3</span><span class="os-divider"> </span><span data-type="" itemprop="" class="os-text">Unit Closer</span>
124+
</a>
125+
</li>
126+
120127
</ol>
121128
</li>
122129

spec/snapshots/Kitchen_Directions_BakeToc_works_with_unit.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
</ol>
7272
</li>
7373

74+
7475
</ol>
7576
</li>
7677

@@ -117,6 +118,12 @@
117118
</ol>
118119
</li>
119120

121+
<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">
122+
<a href="#p-unit-closer">
123+
<span class="os-number">3</span><span class="os-divider"> </span><span data-type="" itemprop="" class="os-text">Unit Closer</span>
124+
</a>
125+
</li>
126+
120127
</ol>
121128
</li>
122129

0 commit comments

Comments
 (0)