Skip to content

Commit 14007ba

Browse files
Merge pull request #409 from openstax/algebra-1-recipe
Algebra 1 recipe
2 parents 7b5c07a + 8a083bf commit 14007ba

23 files changed

+70636
-64
lines changed

CHANGELOG.md

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

77
## [Unreleased]
88

9+
* Add `suppress_solution_title` to `bake_numbered_exercise`
10+
* Add recipe for `algebra-1`
11+
* Add support for offsets in `ElementBase.os_number`
912
* Support `unit-closer` pages in TOC
1013
* Fix iframes links for Polish books
1114
* Create `number_parts` method in `ElementBase`

lib/kitchen/chapter_element.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ def title
3636
first!("./*[@data-type = 'document-title']")
3737
end
3838

39+
# Returns the title's text regardless of whether the title has been baked
40+
#
41+
# @return [String]
42+
#
43+
def title_text
44+
title.children.one? ? title.text : title.first('.os-text').text
45+
end
46+
3947
# Returns the introduction page
4048
#
4149
# @return [Element, nil]

lib/kitchen/directions/bake_numbered_exercise/v1.rb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def bake(exercise:, number:, options:)
5151
exercise: exercise,
5252
number: number,
5353
solution_stays_put: options[:solution_stays_put],
54+
with_title: !options[:suppress_solution_title],
5455
in_appendix: in_appendix
5556
)
5657
end
@@ -65,17 +66,25 @@ def bake(exercise:, number:, options:)
6566
)
6667
end
6768

68-
def bake_solution(exercise:, number:, solution_stays_put:, divider: '. ', in_appendix: false)
69+
# rubocop:disable Metrics/ParameterLists
70+
def bake_solution(exercise:,
71+
number:,
72+
solution_stays_put:,
73+
with_title: true,
74+
divider: '. ',
75+
in_appendix: false)
6976
solution = exercise.solution
7077
if solution_stays_put
7178
solution.wrap_children(class: 'os-solution-container')
72-
solution.prepend(child:
73-
<<~HTML
74-
<h4 class="solution-title" data-type="title">
75-
<span class="os-text">#{I18n.t(:solution)}</span>
76-
</h4>
77-
HTML
78-
)
79+
if with_title
80+
solution.prepend(child:
81+
<<~HTML
82+
<h4 class="solution-title" data-type="title">
83+
<span class="os-text">#{I18n.t(:solution)}</span>
84+
</h4>
85+
HTML
86+
)
87+
end
7988
return
8089
end
8190

@@ -94,5 +103,6 @@ def bake_solution(exercise:, number:, solution_stays_put:, divider: '. ', in_app
94103
HTML
95104
)
96105
end
106+
# rubocop:enable Metrics/ParameterLists
97107
end
98108
end

lib/kitchen/directions/bake_toc.rb

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,53 @@
22

33
module Kitchen
44
module Directions
5+
# rubocop:disable Metrics/ModuleLength
56
module BakeToc
67
def self.v1(book:, options: { cases: false })
78
options.reverse_merge!(
89
cases: false,
9-
numbering_options: { mode: :chapter_page, separator: '.' }
10+
numbering_options: { mode: :chapter_page, separator: '.' },
11+
controller: {
12+
get_chapter_toc_title: nil,
13+
get_unit_toc_title: nil
14+
}
1015
)
16+
controller = options[:controller]
17+
@toc_title_for_unit =
18+
if controller[:get_unit_toc_title].nil?
19+
lambda do |unit|
20+
number = unit.os_number(options[:numbering_options])
21+
<<~HTML
22+
<span class="os-number"><span class="os-part-text">#{I18n.t(:unit)} </span>#{number}</span>
23+
<span class="os-divider"> </span>
24+
<span data-type="" itemprop="" class="os-text">#{unit.title_text}</span>
25+
HTML
26+
end
27+
else
28+
controller[:get_unit_toc_title]
29+
end
30+
31+
@toc_title_for_chapter =
32+
if controller[:get_chapter_toc_title].nil?
33+
lambda do |chapter|
34+
number = chapter.os_number(options[:numbering_options])
35+
<<~HTML
36+
<span class="os-number"><span class="os-part-text">#{I18n.t("chapter#{'.nominative' \
37+
if options[:cases]}")} </span>#{number}</span>
38+
<span class="os-divider"> </span>
39+
<span class="os-text" data-type="" itemprop="">#{chapter.title.first!('.os-text').text}</span>
40+
HTML
41+
end
42+
else
43+
controller[:get_chapter_toc_title]
44+
end
1145

1246
li_tags = book.body.element_children.map do |element|
1347
case element
1448
when UnitElement
15-
li_for_unit(element, options)
49+
li_for_unit(element)
1650
when ChapterElement
17-
li_for_chapter(element, options)
51+
li_for_chapter(element)
1852
when PageElement, CompositePageElement
1953
li_for_page(element)
2054
when CompositeChapterElement
@@ -31,7 +65,7 @@ def self.v1(book:, options: { cases: false })
3165
)
3266
end
3367

34-
def self.li_for_unit(unit, options)
68+
def self.li_for_unit(unit)
3569
chapters = unit.element_children.only(ChapterElement)
3670
pages = unit.element_children.only(PageElement).to_a
3771
after_chapter = pages.filter(&:is_unit_closer?)
@@ -40,13 +74,11 @@ def self.li_for_unit(unit, options)
4074
<<~HTML
4175
<li cnx-archive-uri="" cnx-archive-shortid="" class="os-toc-unit" data-toc-type="unit">
4276
<a href="#">
43-
<span class="os-number"><span class="os-part-text">#{I18n.t(:unit)} </span>#{unit.count_in(:book)}</span>
44-
<span class="os-divider"> </span>
45-
<span data-type="" itemprop="" class="os-text">#{unit.title_text}</span>
77+
#{@toc_title_for_unit.call(unit)}
4678
</a>
4779
<ol class="os-unit">
4880
#{before_chapter.map { |page| li_for_page(page) }.join("\n")}
49-
#{chapters.map { |chapter| li_for_chapter(chapter, options) }.join("\n")}
81+
#{chapters.map { |chapter| li_for_chapter(chapter) }.join("\n")}
5082
#{after_chapter.map { |page| li_for_page(page) }.join("\n")}
5183
</ol>
5284
</li>
@@ -68,22 +100,19 @@ def self.li_for_composite_chapter(composite_chapter)
68100
HTML
69101
end
70102

71-
def self.li_for_chapter(chapter, options)
103+
def self.li_for_chapter(chapter)
72104
chapter_children = chapter.element_children.map do |child|
73105
if child.instance_of?(PageElement) || child.instance_of?(CompositePageElement)
74106
li_for_page(child)
75107
elsif child.instance_of?(CompositeChapterElement)
76108
li_for_composite_chapter(child)
77109
end
78110
end.join("\n")
79-
number = chapter.os_number(options[:numbering_options])
111+
80112
<<~HTML
81113
<li class="os-toc-chapter" cnx-archive-shortid="" cnx-archive-uri="" data-toc-type="chapter">
82114
<a href="##{chapter.title.id}">
83-
<span class="os-number"><span class="os-part-text">#{I18n.t("chapter#{'.nominative' \
84-
if options[:cases]}")} </span>#{number}</span>
85-
<span class="os-divider"> </span>
86-
<span class="os-text" data-type="" itemprop="">#{chapter.title.first!('.os-text').text}</span>
115+
#{@toc_title_for_chapter.call(chapter)}
87116
</a>
88117
<ol class="os-chapter">
89118
#{chapter_children}
@@ -162,4 +191,5 @@ def self.li_for_page(page)
162191
end
163192
end
164193
end
194+
# rubocop:enable Metrics/ModuleLength
165195
end

lib/kitchen/element_base.rb

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,26 +381,30 @@ def count_in(ancestor_type)
381381
# :unit_chapter_page unit level, chapter level, and page level
382382
# @return [Array<integer>] an array of numbers ordered hierarchically
383383
#
384-
def number_parts(mode)
384+
def number_parts(mode, unit_offset:, chapter_offset:, page_offset:)
385385
case mode
386386
when :chapter_page
387387
case data_type
388388
when 'chapter'
389-
[count_in(:book)]
389+
[count_in(:book) + chapter_offset]
390+
when 'unit'
391+
[count_in(:book) + unit_offset]
390392
else
391-
[ancestor(:chapter).count_in(:book), count_in(:chapter)]
393+
[ancestor(:chapter).count_in(:book) + chapter_offset, count_in(:chapter) + page_offset]
392394
end
393395
when :unit_chapter_page
394396
case data_type
395397
when 'chapter'
396398
unit = ancestor(:unit)
397-
[unit.count_in(:book), count_in(:unit)]
399+
[unit.count_in(:book) + unit_offset, count_in(:unit) + chapter_offset]
398400
when 'unit'
399-
[count_in(:book)]
401+
[count_in(:book) + unit_offset]
400402
else
401403
unit = ancestor(:unit)
402404
chapter = ancestor(:chapter)
403-
[unit.count_in(:book), chapter.count_in(:unit), count_in(:chapter)]
405+
[unit.count_in(:book) + unit_offset,
406+
chapter.count_in(:unit) + chapter_offset,
407+
count_in(:chapter) + page_offset]
404408
end
405409
else
406410
# Further levels of nesting are not currently supported because of how
@@ -419,9 +423,18 @@ def number_parts(mode)
419423
def os_number(options={})
420424
options.reverse_merge!(
421425
mode: :chapter_page,
422-
separator: '.'
426+
separator: '.',
427+
unit_offset: 0,
428+
chapter_offset: 0,
429+
page_offset: 0
430+
)
431+
parts = number_parts(
432+
options[:mode],
433+
unit_offset: options[:unit_offset],
434+
chapter_offset: options[:chapter_offset],
435+
page_offset: options[:page_offset]
423436
)
424-
number_parts(options[:mode]).join(options[:separator])
437+
parts.join(options[:separator])
425438
end
426439

427440
# Track that a sub element found by the given query has been counted

lib/kitchen/page_element.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def is_introduction?
7575
@is_introduction ||= has_class?('introduction')
7676
end
7777

78-
# Returns true if this page is a unit closer
78+
# Returns true if this page appears after all chapters in a unit
7979
#
8080
# @return [Boolean]
8181
#

lib/recipes/algebra-1/locales/en.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
en:
2+
chapter: Lesson
3+
eoc:
4+
chapter-review: Chapter Review
5+
key-terms: Key Terms
6+
notes:
7+
chapter-objectives: Chapter Objectives
8+
general-strategies: General Strategies
9+
link-to-learning: Link to Learning
10+
mini-lesson-question: Mini Lesson Question
11+
self-check: Self Check
12+
try: Try it

0 commit comments

Comments
 (0)