From 456499f990b061690158fd11f3c1c2f9bcd9e2f4 Mon Sep 17 00:00:00 2001 From: Tyler Nullmeier Date: Tue, 22 Apr 2025 14:52:14 -0500 Subject: [PATCH] Add support for unit closer pages in toc 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 --- CHANGELOG.md | 1 + lib/kitchen/directions/bake_toc.rb | 9 ++++++--- lib/kitchen/page_element.rb | 8 ++++++++ spec/kitchen_spec/directions/bake_toc_spec.rb | 7 +++++++ ...tchen_Directions_BakeToc_supports_unit_numbering.snap | 7 +++++++ .../Kitchen_Directions_BakeToc_works_with_unit.snap | 7 +++++++ 6 files changed, 36 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f2cb0be6..994d4b489 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/kitchen/directions/bake_toc.rb b/lib/kitchen/directions/bake_toc.rb index 8cce20017..df95221db 100644 --- a/lib/kitchen/directions/bake_toc.rb +++ b/lib/kitchen/directions/bake_toc.rb @@ -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
  • @@ -43,8 +45,9 @@ def self.li_for_unit(unit, options) #{unit.title_text}
      - #{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")}
  • HTML @@ -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}` diff --git a/lib/kitchen/page_element.rb b/lib/kitchen/page_element.rb index 5d6a4e3a2..aad6bdd74 100644 --- a/lib/kitchen/page_element.rb +++ b/lib/kitchen/page_element.rb @@ -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 diff --git a/spec/kitchen_spec/directions/bake_toc_spec.rb b/spec/kitchen_spec/directions/bake_toc_spec.rb index d366ab332..690b9d9bd 100644 --- a/spec/kitchen_spec/directions/bake_toc_spec.rb +++ b/spec/kitchen_spec/directions/bake_toc_spec.rb @@ -121,6 +121,13 @@ +
    +

    + 3 + + Unit Closer +

    +

    diff --git a/spec/snapshots/Kitchen_Directions_BakeToc_supports_unit_numbering.snap b/spec/snapshots/Kitchen_Directions_BakeToc_supports_unit_numbering.snap index 139aa1c17..95fc0a179 100644 --- a/spec/snapshots/Kitchen_Directions_BakeToc_supports_unit_numbering.snap +++ b/spec/snapshots/Kitchen_Directions_BakeToc_supports_unit_numbering.snap @@ -71,6 +71,7 @@ + @@ -117,6 +118,12 @@ +
  • + + 3 Unit Closer + +
  • + diff --git a/spec/snapshots/Kitchen_Directions_BakeToc_works_with_unit.snap b/spec/snapshots/Kitchen_Directions_BakeToc_works_with_unit.snap index 934443b16..27036e947 100644 --- a/spec/snapshots/Kitchen_Directions_BakeToc_works_with_unit.snap +++ b/spec/snapshots/Kitchen_Directions_BakeToc_works_with_unit.snap @@ -71,6 +71,7 @@ + @@ -117,6 +118,12 @@ +
  • + + 3 Unit Closer + +
  • +