Skip to content

Commit abe1663

Browse files
Core 711 raise toc (#407)
* Add options to include unit numbering in ToC Might be better to either allow external customization or centralize numbering generation It could work with a recursive function that walks up the tree looking for units/chapters The page only has access to ancestors that the iterator visits * Update tests for ToC * Add number parts and os number method to element base Hopefully these can replace the scattered logic for numbering * Add v2 bake_chapter_title that takes chapters as input * Add tests for v2 bake_chapter_title * Unit numbering in bake_non_introduction_pages * Update tests for bake_non_introduction_pages * Unit numbering in bake_toc * Update tests for bake_toc * Unit numbering in bake_chapter_section_exercises * Unit numbering in move_solutions_from_numbered_note * Update tests for bake_chapter_section_exercises * Add v2 bake_equations that takes chapters as input * Update tests for bake_equations * Update tests for element_base; fix data-source It should print in the format that corgi already recognizes * Update tests for bake_chapter_section_exercises * Unit numbering in bake_learning_objectives * Unit numbering in bake_chapter_introductions When unit numbering is desired, send chapters with unit ancestors and the associated numbering options * Unit numbering in bake_index * Update tests for bake_index * Unit numbering to bake_references * Update tests for bake_references * Unit numbering in bake_folio * Update tests for bake_folio * Update changelog * 👕 Lint * Ensure full test coverage
1 parent 18b23df commit abe1663

File tree

50 files changed

+1760
-127
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1760
-127
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88

99
* Fix iframes links for Polish books
10+
* Create `number_parts` method in `ElementBase`
11+
* Create `os_number` method in `ElementBase` for creating `os-number` text
12+
* Add backward-compatible support for unit numbering where possible
13+
* Create `v2` of `BakeEquations` that takes chapters as input instead of book
14+
* Create `v2` of `BakeChapterTitle` that takes chapters as input instead of book
1015
* Remove `ElementBase#rex_link` in favor of generating links elsewhere
1116
* Update `BakeIframes` to mark iframe links as needing rex linking
1217

lib/kitchen/directions/bake_chapter_introductions/bake_chapter_objectives.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
module Kitchen::Directions::BakeChapterIntroductions
44
class BakeChapterObjectives
5-
def bake(chapter:, strategy:)
5+
def bake(chapter:, strategy:, options:)
66
case strategy
77
when :default
88
bake_as_note(chapter: chapter)
99
when :add_objectives
10-
add_chapter_objectives(chapter: chapter)
10+
add_chapter_objectives(chapter: chapter, options: options)
1111
when :none
1212
''
1313
else
@@ -31,12 +31,14 @@ def bake_as_note(chapter:)
3131
chapter_objectives_note.cut.paste
3232
end
3333

34-
def add_chapter_objectives(chapter:)
34+
def add_chapter_objectives(chapter:, options: {})
35+
options.reverse_merge!(numbering_options: { mode: :chapter_page, separator: '.' })
3536
chapter.non_introduction_pages.map do |page|
37+
number = page.os_number(options[:numbering_options])
3638
<<~HTML
3739
<div class="os-chapter-objective">
3840
<a class="os-chapter-objective" href="##{page.id}">
39-
<span class="os-number">#{chapter.count_in(:book)}.#{page.count_in(:chapter)}</span>
41+
<span class="os-number">#{number}</span>
4042
<span class="os-divider"> </span>
4143
<span data-type="" itemprop="" class="os-text">#{page.title_children}</span>
4244
</a>

lib/kitchen/directions/bake_chapter_introductions/main.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,34 @@ def self.v1(book:)
1111

1212
def self.v2(
1313
book:,
14+
chapters: nil,
1415
options: {
1516
strategy: :default, bake_chapter_outline: false, introduction_order: :v1,
1617
block_target_label: false,
17-
cases: false
18+
cases: false,
19+
numbering_options: { mode: :chapter_page, separator: '.' }
1820
}
1921
)
2022
options.reverse_merge!(
2123
strategy: :default,
2224
bake_chapter_outline: false,
2325
introduction_order: :v1,
2426
block_target_label: false,
25-
cases: false
27+
cases: false,
28+
numbering_options: { mode: :chapter_page, separator: '.' }
2629
)
2730
V2.new.bake(
2831
book: book,
32+
chapters: chapters || book.chapters,
2933
options: options
3034
)
3135
end
3236

33-
def self.bake_chapter_objectives(chapter:, strategy:)
37+
def self.bake_chapter_objectives(chapter:, strategy:, options:)
3438
BakeChapterObjectives.new.bake(
3539
chapter: chapter,
36-
strategy: strategy
40+
strategy: strategy,
41+
options: options
3742
)
3843
end
3944

lib/kitchen/directions/bake_chapter_introductions/v2.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
module Kitchen::Directions::BakeChapterIntroductions
44
class V2
5-
def bake(book:, options:)
6-
book.chapters.each do |chapter|
5+
def bake(book:, chapters:, options:)
6+
chapters.each do |chapter|
77
introduction_page = chapter.introduction_page
8-
number = chapter.count_in(:book)
8+
number = chapter.os_number(options[:numbering_options])
99
title_label = chapter.title.search('.os-text').first&.text
1010
title_label = chapter.title.text if title_label.nil?
1111

@@ -16,7 +16,8 @@ def bake(book:, options:)
1616
chapter_intro_html =
1717
Kitchen::Directions::BakeChapterIntroductions.bake_chapter_objectives(
1818
chapter: chapter,
19-
strategy: options[:strategy]
19+
strategy: options[:strategy],
20+
options: options
2021
)
2122

2223
if options[:bake_chapter_outline]

lib/kitchen/directions/bake_chapter_section_exercises/main.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ module Directions
55
module BakeChapterSectionExercises
66
def self.v1(chapter:, options: {
77
trash_title: false,
8-
create_title: true
8+
create_title: true,
9+
numbering_options: { mode: :chapter_page, separator: '.' }
910
})
1011
options.reverse_merge!(
1112
trash_title: false,
12-
create_title: true
13+
create_title: true,
14+
numbering_options: { mode: :chapter_page, separator: '.' }
1315
)
1416
V1.new.bake(chapter: chapter, options: options)
1517
end

lib/kitchen/directions/bake_chapter_section_exercises/v1.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def bake(chapter:, options:)
1515

1616
section_title = I18n.t(
1717
:section_exercises,
18-
number: "#{chapter.count_in(:book)}.#{page.count_in(:chapter)}")
18+
number: page.os_number(options[:numbering_options]))
1919

2020
section.prepend(sibling:
2121
<<~HTML

lib/kitchen/directions/bake_chapter_title/main.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ module BakeChapterTitle
66
def self.v1(book:, cases: false)
77
V1.new.bake(book: book, cases: cases)
88
end
9+
10+
def self.v2(chapters:, cases: false, numbering_options: {})
11+
V2.new.bake(chapters: chapters, cases: cases, numbering_options: numbering_options)
12+
end
913
end
1014
end
1115
end

lib/kitchen/directions/bake_chapter_title/v1.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@ module Kitchen::Directions::BakeChapterTitle
44
class V1
55
def bake(book:, cases: false)
66
book.chapters.each do |chapter|
7-
fix_up_chapter_title(chapter: chapter, cases: cases)
7+
fix_up_chapter_title(
8+
chapter: chapter,
9+
cases: cases,
10+
numbering_options: { mode: :chapter_page, separator: '.' })
811
end
912
end
1013

11-
def fix_up_chapter_title(chapter:, cases:)
14+
protected
15+
16+
def fix_up_chapter_title(chapter:, cases:, numbering_options:)
1217
heading = chapter.at('h1[2]')
1318
heading[:id] = "chapTitle#{chapter.count_in(:book)}"
19+
number = chapter.os_number(numbering_options)
1420
heading.replace_children(with:
1521
<<~HTML
1622
<span class="os-part-text">#{I18n.t("chapter#{'.nominative' if cases}")} </span>
17-
<span class="os-number">#{chapter.count_in(:book)}</span>
23+
<span class="os-number">#{number}</span>
1824
<span class="os-divider"> </span>
1925
<span data-type="" itemprop="" class="os-text">#{heading.text}</span>
2026
HTML
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
module Kitchen::Directions::BakeChapterTitle
4+
class V2 < V1
5+
def bake(chapters:, cases: false, numbering_options: {})
6+
chapters.each do |chapter|
7+
fix_up_chapter_title(
8+
chapter: chapter,
9+
cases: cases,
10+
numbering_options: numbering_options)
11+
end
12+
end
13+
end
14+
end

lib/kitchen/directions/bake_equations.rb

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,24 @@ module Kitchen
44
module Directions
55
module BakeEquations
66
def self.v1(book:, number_decorator: :none, cases: false)
7-
book.chapters.search('div[data-type="equation"]:not(.unnumbered)').each do |eq|
8-
chapter = eq.ancestor(:chapter)
9-
number = "#{chapter.count_in(:book)}.#{eq.count_in(:chapter)}"
7+
_v1_bake_in_chapters(
8+
chapters: book.chapters,
9+
number_decorator: number_decorator,
10+
cases: cases,
11+
numbering_options: { mode: :chapter_page, separator: '.' })
12+
end
13+
14+
def self.v2(chapters:, number_decorator: :none, cases: false, numbering_options: {})
15+
_v1_bake_in_chapters(
16+
chapters: chapters,
17+
number_decorator: number_decorator,
18+
cases: cases,
19+
numbering_options: numbering_options)
20+
end
21+
22+
def self._v1_bake_in_chapters(chapters:, number_decorator:, cases:, numbering_options:)
23+
chapters.search('div[data-type="equation"]:not(.unnumbered)').each do |eq|
24+
number = eq.os_number(numbering_options)
1025

1126
# Store label information
1227
eq.target_label(label_text: 'equation', custom_content: number, cases: cases)
@@ -31,6 +46,10 @@ def self.v1(book:, number_decorator: :none, cases: false)
3146
)
3247
end
3348
end
49+
50+
class << self
51+
private :_v1_bake_in_chapters
52+
end
3453
end
3554
end
3655
end

lib/kitchen/directions/bake_folio.rb

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
module Kitchen
44
module Directions
55
module BakeFolio
6-
def self.v1(book:, options: { new_approach: false })
7-
options.reverse_merge!(new_approach: false)
8-
V1.new.bake(book: book, options: options)
6+
def self.v1(book:, chapters: nil, options: {})
7+
options.reverse_merge!(
8+
new_approach: false,
9+
numbering_options: { mode: :chapter_page, separator: '.' })
10+
V1.new.bake(
11+
book: book,
12+
chapters: chapters || book.chapters,
13+
options: options)
914
end
1015

1116
class V1
@@ -56,19 +61,27 @@ def create_para
5661
end
5762
end
5863

59-
def bake(book:, options:)
64+
def bake(book:, chapters:, options:)
6065
book['data-pdf-folio-preface-message'] = I18n.t(:"folio.preface")
6166
book['data-pdf-folio-access-message'] = I18n.t(:"folio.access_for_free")
6267

6368
return unless options[:new_approach]
6469

6570
# TODO: apply to all books and remove an option
66-
67-
book.chapters.each do |chapter|
68-
chapter_folio(chapter: chapter)
69-
module_folio(chapter: chapter)
70-
eoc_folio(chapter: chapter, klass: 'folio-eoc-left')
71-
eoc_folio(chapter: chapter, klass: 'folio-eoc-right')
71+
numbering_options = options[:numbering_options]
72+
chapters.each do |chapter|
73+
chapter_folio(chapter: chapter, numbering_options: numbering_options)
74+
module_folio(chapter: chapter, numbering_options: numbering_options)
75+
eoc_folio(
76+
chapter: chapter,
77+
klass: 'folio-eoc-left',
78+
numbering_options: numbering_options
79+
)
80+
eoc_folio(
81+
chapter: chapter,
82+
klass: 'folio-eoc-right',
83+
numbering_options: numbering_options
84+
)
7285
end
7386

7487
appendix_folio(book: book, klass: 'folio-appendix-left')
@@ -78,23 +91,23 @@ def bake(book:, options:)
7891
eob_folio(book: book, klass: 'folio-eob-right')
7992
end
8093

81-
def chapter_folio(chapter:)
94+
def chapter_folio(chapter:, numbering_options:)
8295
chapter_para = FolioParaWithNumber.new(
8396
klass: 'folio-chapter',
8497
title: chapter.title,
85-
title_number: chapter.count_in(:book).to_s
98+
title_number: chapter.os_number(numbering_options)
8699
)
87100

88101
chapter_para.create_para
89102
end
90103

91-
def module_folio(chapter:)
104+
def module_folio(chapter:, numbering_options:)
92105
# Introduction para
93106
chapter.pages('$.introduction').each do |page|
94107
intro_para = FolioParaWithNumber.new(
95108
klass: 'folio-module',
96109
title: page.search('h2[data-type="document-title"]').first,
97-
title_number: chapter.count_in(:book).to_s
110+
title_number: chapter.os_number(numbering_options)
98111
)
99112

100113
intro_para.create_para
@@ -105,19 +118,19 @@ def module_folio(chapter:)
105118
module_para = FolioParaWithNumber.new(
106119
klass: 'folio-module',
107120
title: page.title,
108-
title_number: "#{chapter.count_in(:book)}.#{page.count_in(:chapter)}"
121+
title_number: page.os_number(numbering_options)
109122
)
110123

111124
module_para.create_para
112125
end
113126
end
114127

115-
def eoc_folio(chapter:, klass:)
128+
def eoc_folio(chapter:, klass:, numbering_options:)
116129
chapter.search(' > .os-eoc').each do |eoc_page|
117130
eoc_para = FolioParaWithNumber.new(
118131
klass: klass,
119132
title: eoc_page.search('h2[data-type="document-title"]').first,
120-
title_number: chapter.count_in(:book).to_s
133+
title_number: chapter.os_number(numbering_options)
121134
)
122135

123136
eoc_para.create_para

lib/kitchen/directions/bake_index/main.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@
33
module Kitchen
44
module Directions
55
module BakeIndex
6-
def self.v1(book:, types: %w[main], uuid_prefix: nil)
7-
V1.new.bake(book: book, types: types, uuid_prefix: uuid_prefix)
6+
def self.v1(book:,
7+
chapters: nil,
8+
types: %w[main],
9+
uuid_prefix: nil,
10+
numbering_options: { mode: :chapter_page, separator: '.' })
11+
V1.new.bake(
12+
book: book,
13+
chapters: chapters || book.chapters,
14+
types: types,
15+
uuid_prefix: uuid_prefix,
16+
numbering_options: numbering_options)
817
end
918
end
1019
end

lib/kitchen/directions/bake_index/v1.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,14 @@ def section_named(name)
118118
end
119119
end
120120

121-
def bake(book:, types: %w[main], uuid_prefix: '')
121+
def bake(book:, chapters:, types: %w[main], uuid_prefix: '', numbering_options: {})
122122
@metadata_elements = book.metadata.children_to_keep.copy
123123
@uuid_prefix = uuid_prefix
124124
@indexes = types.each.with_object({}) do |type, hash|
125125
index_name = type == 'main' ? 'term' : type
126126
hash[index_name] = Index.new
127127
end
128+
numbering_options.reverse_merge!(mode: :chapter_page, separator: '.')
128129

129130
# Numbering of IDs doesn't depend on term type
130131

@@ -137,7 +138,7 @@ def bake(book:, types: %w[main], uuid_prefix: '')
137138
add_term_to_index(term_element, page_title)
138139
end
139140

140-
book.chapters.search_with(
141+
chapters.search_with(
141142
Kitchen::PageElementEnumerator, Kitchen::CompositePageElementEnumerator
142143
).terms.each do |term_element|
143144

@@ -149,7 +150,7 @@ def bake(book:, types: %w[main], uuid_prefix: '')
149150
page = term_element.ancestor(:composite_page)
150151
chapter = term_element.ancestor(:chapter)
151152
term_element.id ||= "auto_composite_page_term#{term_element.count_in(:book)}"
152-
chapter_number = chapter.count_in(:book)
153+
chapter_number = chapter.os_number(numbering_options)
153154
page_title = "#{chapter_number} #{page.title.text.strip}".strip
154155
end
155156

0 commit comments

Comments
 (0)