openstax_kitchen 3.0.0 → 4.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/CHANGELOG.md +77 -3
  4. data/Gemfile.lock +13 -14
  5. data/README.md +23 -0
  6. data/codecov.yaml +1 -0
  7. data/docker/ci +0 -1
  8. data/lib/kitchen/book_document.rb +1 -1
  9. data/lib/kitchen/book_element.rb +16 -2
  10. data/lib/kitchen/chapter_element.rb +10 -13
  11. data/lib/kitchen/chapter_element_enumerator.rb +1 -1
  12. data/lib/kitchen/composite_chapter_element.rb +7 -11
  13. data/lib/kitchen/composite_chapter_element_enumerator.rb +21 -0
  14. data/lib/kitchen/composite_page_element.rb +15 -10
  15. data/lib/kitchen/composite_page_element_enumerator.rb +1 -1
  16. data/lib/kitchen/config.rb +14 -0
  17. data/lib/kitchen/directions/bake_chapter_glossary/main.rb +18 -0
  18. data/lib/kitchen/directions/bake_chapter_glossary/v1.rb +30 -0
  19. data/lib/kitchen/directions/bake_chapter_introductions.rb +1 -1
  20. data/lib/kitchen/directions/bake_chapter_key_concepts/main.rb +16 -0
  21. data/lib/kitchen/directions/bake_chapter_key_concepts/v1.rb +35 -0
  22. data/lib/kitchen/directions/bake_chapter_key_equations.rb +27 -20
  23. data/lib/kitchen/directions/bake_chapter_references/main.rb +16 -0
  24. data/lib/kitchen/directions/bake_chapter_references/v1.rb +35 -0
  25. data/lib/kitchen/directions/bake_chapter_section_exercises/main.rb +11 -0
  26. data/lib/kitchen/directions/bake_chapter_section_exercises/v1.rb +28 -0
  27. data/lib/kitchen/directions/bake_chapter_summary.rb +48 -42
  28. data/lib/kitchen/directions/bake_checkpoint.rb +44 -0
  29. data/lib/kitchen/directions/bake_composite_chapters.rb +14 -0
  30. data/lib/kitchen/directions/bake_composite_pages.rb +1 -1
  31. data/lib/kitchen/directions/bake_equations.rb +37 -0
  32. data/lib/kitchen/directions/bake_example.rb +34 -8
  33. data/lib/kitchen/directions/bake_figure.rb +1 -1
  34. data/lib/kitchen/directions/bake_first_elements.rb +16 -0
  35. data/lib/kitchen/directions/bake_footnotes/v1.rb +2 -1
  36. data/lib/kitchen/directions/bake_free_response/free_response.xhtml.erb +10 -0
  37. data/lib/kitchen/directions/bake_free_response/main.rb +11 -0
  38. data/lib/kitchen/directions/bake_free_response/v1.rb +29 -0
  39. data/lib/kitchen/directions/bake_further_research.rb +59 -0
  40. data/lib/kitchen/directions/bake_index/v1.rb +35 -14
  41. data/lib/kitchen/directions/bake_link_placeholders.rb +1 -1
  42. data/lib/kitchen/directions/bake_non_introduction_pages.rb +26 -0
  43. data/lib/kitchen/directions/bake_notes/bake_autotitled_notes.rb +29 -0
  44. data/lib/kitchen/directions/bake_notes/bake_note_subtitle.rb +22 -0
  45. data/lib/kitchen/directions/bake_notes/bake_numbered_notes.rb +51 -0
  46. data/lib/kitchen/directions/bake_notes/bake_unclassified_notes.rb +30 -0
  47. data/lib/kitchen/directions/bake_numbered_exercise/main.rb +15 -0
  48. data/lib/kitchen/directions/bake_numbered_exercise/v1.rb +47 -0
  49. data/lib/kitchen/directions/bake_numbered_table/main.rb +2 -2
  50. data/lib/kitchen/directions/bake_numbered_table/v1.rb +18 -4
  51. data/lib/kitchen/directions/bake_page_abstracts.rb +16 -0
  52. data/lib/kitchen/directions/bake_references/main.rb +16 -0
  53. data/lib/kitchen/directions/bake_references/v1.rb +48 -0
  54. data/lib/kitchen/directions/bake_stepwise.rb +1 -5
  55. data/lib/kitchen/directions/bake_suggested_reading.rb +5 -0
  56. data/lib/kitchen/directions/bake_theorem/main.rb +11 -0
  57. data/lib/kitchen/directions/bake_theorem/v1.rb +28 -0
  58. data/lib/kitchen/directions/bake_toc.rb +10 -2
  59. data/lib/kitchen/directions/book_answer_key_container/eob_solutions_container.xhtml.erb +9 -0
  60. data/lib/kitchen/directions/{bake_exercises → book_answer_key_container}/main.rb +1 -1
  61. data/lib/kitchen/directions/book_answer_key_container/v1.rb +13 -0
  62. data/lib/kitchen/directions/chapter_review_container/chapter_review.xhtml.erb +9 -0
  63. data/lib/kitchen/directions/chapter_review_container/main.rb +11 -0
  64. data/lib/kitchen/directions/chapter_review_container/v1.rb +13 -0
  65. data/lib/kitchen/directions/eoc_section_title_link_snippet.rb +20 -0
  66. data/lib/kitchen/directions/move_exercises_to_eoc/main.rb +27 -0
  67. data/lib/kitchen/directions/move_exercises_to_eoc/v1.rb +36 -0
  68. data/lib/kitchen/directions/move_exercises_to_eoc/v2.rb +49 -0
  69. data/lib/kitchen/directions/move_solutions_to_answer_key/main.rb +14 -0
  70. data/lib/kitchen/directions/move_solutions_to_answer_key/strategies/american_government.rb +19 -0
  71. data/lib/kitchen/directions/move_solutions_to_answer_key/strategies/calculus.rb +41 -0
  72. data/lib/kitchen/directions/move_solutions_to_answer_key/strategies/uphysics.rb +63 -0
  73. data/lib/kitchen/directions/move_solutions_to_answer_key/v1.rb +34 -0
  74. data/lib/kitchen/document.rb +3 -0
  75. data/lib/kitchen/element.rb +9 -3
  76. data/lib/kitchen/element_base.rb +118 -16
  77. data/lib/kitchen/element_enumerator_base.rb +118 -8
  78. data/lib/kitchen/element_enumerator_factory.rb +28 -12
  79. data/lib/kitchen/element_factory.rb +3 -3
  80. data/lib/kitchen/example_element.rb +8 -11
  81. data/lib/kitchen/example_element_enumerator.rb +1 -1
  82. data/lib/kitchen/exercise_element.rb +42 -0
  83. data/lib/kitchen/exercise_element_enumerator.rb +21 -0
  84. data/lib/kitchen/figure_element.rb +8 -11
  85. data/lib/kitchen/figure_element_enumerator.rb +1 -1
  86. data/lib/kitchen/metadata_element.rb +8 -2
  87. data/lib/kitchen/metadata_element_enumerator.rb +1 -1
  88. data/lib/kitchen/note_element.rb +25 -27
  89. data/lib/kitchen/note_element_enumerator.rb +1 -1
  90. data/lib/kitchen/oven.rb +2 -0
  91. data/lib/kitchen/page_element.rb +33 -9
  92. data/lib/kitchen/page_element_enumerator.rb +1 -1
  93. data/lib/kitchen/patches/nokogiri.rb +55 -0
  94. data/lib/kitchen/patches/nokogiri_profiling.rb +60 -0
  95. data/lib/kitchen/recipe.rb +35 -2
  96. data/lib/kitchen/reference_element.rb +27 -0
  97. data/lib/kitchen/references_element_enumerator.rb +20 -0
  98. data/lib/kitchen/search_query.rb +31 -3
  99. data/lib/kitchen/selector.rb +25 -0
  100. data/lib/kitchen/selectors/base.rb +39 -0
  101. data/lib/kitchen/selectors/standard_1.rb +13 -0
  102. data/lib/kitchen/table_element.rb +8 -11
  103. data/lib/kitchen/table_element_enumerator.rb +1 -1
  104. data/lib/kitchen/templates/eob_section_title_template.xhtml.erb +10 -0
  105. data/lib/kitchen/templates/eoc_section_title_template.xhtml.erb +10 -0
  106. data/lib/kitchen/term_element.rb +5 -8
  107. data/lib/kitchen/term_element_enumerator.rb +1 -1
  108. data/lib/kitchen/unit_element.rb +13 -7
  109. data/lib/kitchen/unit_element_enumerator.rb +1 -1
  110. data/lib/kitchen/version.rb +1 -1
  111. data/lib/locales/en.yml +12 -7
  112. data/lib/locales/pl.yml +24 -0
  113. data/lib/openstax_kitchen.rb +2 -1
  114. metadata +54 -6
  115. data/lib/kitchen/directions/bake_chapter_glossary.rb +0 -37
  116. data/lib/kitchen/directions/bake_exercises/v1.rb +0 -166
  117. data/lib/kitchen/directions/bake_notes.rb +0 -58
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen
4
+ module Directions
5
+ module BakeCompositeChapters
6
+ def self.v1(book:)
7
+ book.composite_chapters.each do |chapter|
8
+ chapter.first("[data-type='document-title']").id =
9
+ "composite-chapter-#{chapter.count_in(:book)}"
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -4,7 +4,7 @@ module Kitchen
4
4
  module Directions
5
5
  module BakeCompositePages
6
6
  def self.v1(book:)
7
- book.search("[data-type='composite-page']").each do |page|
7
+ book.composite_pages.each do |page|
8
8
  page.id = "composite-page-#{page.count_in(:book)}"
9
9
  end
10
10
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen
4
+ module Directions
5
+ module BakeEquations
6
+ def self.v1(book:, number_decorator: :none)
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)}"
10
+
11
+ # Store label information
12
+ equation_label = "#{I18n.t(:equation)} #{number}"
13
+ book.pantry(name: :link_text).store equation_label, label: eq.id
14
+
15
+ decorated_number =
16
+ case number_decorator
17
+ when :none
18
+ number
19
+ when :parentheses
20
+ "(#{number})"
21
+ else
22
+ raise "Unsupported number_decorator '#{number_decorator}'"
23
+ end
24
+
25
+ # Bake the equation
26
+ eq.append(child:
27
+ <<~HTML
28
+ <div class="os-equation-number">
29
+ <span class="os-number">#{decorated_number}</span>
30
+ </div>
31
+ HTML
32
+ )
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -4,13 +4,7 @@ module Kitchen
4
4
  module Directions
5
5
  module BakeExample
6
6
  def self.v1(example:, number:, title_tag:)
7
- example.replace_children(with:
8
- <<~HTML
9
- <div class="body">
10
- #{example.children}
11
- </div>
12
- HTML
13
- )
7
+ example.wrap_children(class: 'body')
14
8
 
15
9
  example.prepend(child:
16
10
  <<~HTML
@@ -26,7 +20,39 @@ module Kitchen
26
20
  .pantry(name: :link_text)
27
21
  .store("#{I18n.t(:example_label)} #{number}", label: example.id)
28
22
 
29
- example.titles.each { |title| title.name = 'h4' }
23
+ example.titles.each do |title|
24
+ next if title.parent.has_class?('os-caption-container')
25
+
26
+ title.name = 'h4'
27
+ end
28
+
29
+ example.exercises.each do |exercise|
30
+ if (problem = exercise.problem)
31
+ problem.titles.each { |title| title.name = 'h4' }
32
+ problem.wrap_children(class: 'os-problem-container')
33
+ end
34
+
35
+ if (solution = exercise.solution)
36
+ solution.replace_children(with:
37
+ <<~HTML
38
+ <h4 data-type="solution-title">
39
+ <span class="os-title-label">#{I18n.t(:solution)} </span>
40
+ </h4>
41
+ <div class="os-solution-container">#{solution.children}</div>
42
+ HTML
43
+ )
44
+ end
45
+
46
+ exercise.add_class('unnumbered')
47
+
48
+ commentary = exercise.first('[data-type="commentary"]')
49
+ next unless commentary.present?
50
+
51
+ commentary_title = commentary.titles.first
52
+ commentary_title.name = 'h4'
53
+ commentary_title['data-type'] = 'commentary-title'
54
+ commentary_title.wrap_children('span', class: 'os-title-label')
55
+ end
30
56
  end
31
57
  end
32
58
  end
@@ -6,7 +6,7 @@ module Kitchen
6
6
  def self.v1(figure:, number:)
7
7
  figure.wrap(%(<div class="os-figure#{' has-splash' if figure.has_class?('splash')}">))
8
8
 
9
- figure.document.pantry(name: :link_text).store "#{I18n.t(:figure)} #{number}", label: figure.id
9
+ figure.pantry(name: :link_text).store "#{I18n.t(:figure)} #{number}", label: figure.id
10
10
  title = figure.title&.cut
11
11
 
12
12
  caption = figure.caption&.cut
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen
4
+ module Directions
5
+ module BakeFirstElements
6
+ def self.v1(within:, selectors:)
7
+ selectors.each do |selector|
8
+ within.search("#{selector}:first-child").each do |problem|
9
+ problem.add_class('first-element')
10
+ problem.parent.add_class('has-first-element')
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -8,7 +8,8 @@ module Kitchen::Directions::BakeFootnotes
8
8
  # appendices, etc) or within chapters. Tackle each case separately
9
9
 
10
10
  book.body.element_children.only(Kitchen::PageElement,
11
- Kitchen::CompositePageElement).each do |page|
11
+ Kitchen::CompositePageElement,
12
+ Kitchen::CompositeChapterElement).each do |page|
12
13
  bake_footnotes_within(page)
13
14
  end
14
15
 
@@ -0,0 +1,10 @@
1
+ <div class="os-eoc os-free-response-container" data-type="composite-page" data-uuid-key=".free-response">
2
+ <<%= @title_tag %> data-type="document-title">
3
+ <span class="os-text"><%= I18n.t(:eoc_free_response) %></span>
4
+ </<%= @title_tag %>>
5
+ <div data-type="metadata" style="display: none;">
6
+ <h1 data-type="document-title" itemprop="name"><%= I18n.t(:eoc_free_response) %></h1>
7
+ <%= @metadata_elements.paste %>
8
+ </div>
9
+ <%= @free_response_clipboard.paste %>
10
+ </div>
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen
4
+ module Directions
5
+ module BakeFreeResponse
6
+ def self.v1(chapter:, metadata_source:, append_to: nil)
7
+ V1.new.bake(chapter: chapter, metadata_source: metadata_source, append_to: append_to)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen::Directions::BakeFreeResponse
4
+ class V1
5
+ renderable
6
+
7
+ def bake(chapter:, metadata_source:, append_to:)
8
+ @metadata_elements = metadata_source.children_to_keep.copy
9
+
10
+ @free_response_clipboard = Kitchen::Clipboard.new
11
+ chapter.non_introduction_pages.each do |page|
12
+ free_response_questions = page.free_response
13
+ next if free_response_questions.none?
14
+
15
+ free_response_questions.search('h3').trash
16
+ title = Kitchen::Directions::EocSectionTitleLinkSnippet.v1(page: page)
17
+ free_response_questions.each do |free_response_question|
18
+ free_response_question.prepend(child: title)
19
+ free_response_question.cut(to: @free_response_clipboard)
20
+ end
21
+ end
22
+
23
+ append_to_element = append_to || chapter
24
+ @title_tag = append_to ? 'h3' : 'h2'
25
+
26
+ append_to_element.append(child: render(file: 'free_response.xhtml.erb'))
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen
4
+ module Directions
5
+ # Bake directions for further research
6
+ #
7
+ module BakeFurtherResearch
8
+ def self.v1(chapter:, metadata_source:, uuid_prefix: '.')
9
+ V1.new.bake(
10
+ chapter: chapter,
11
+ metadata_source: metadata_source,
12
+ uuid_prefix: uuid_prefix)
13
+ end
14
+
15
+ class V1
16
+ renderable
17
+ def bake(chapter:, metadata_source:, uuid_prefix: '.')
18
+ @metadata = metadata_source.children_to_keep.copy
19
+ @klass = 'further-research'
20
+ @title = I18n.t(:eoc_further_research_title)
21
+ @uuid_prefix = uuid_prefix
22
+
23
+ further_researches = Clipboard.new
24
+
25
+ chapter.non_introduction_pages.each do |page|
26
+ further_research = page.first('.further-research')
27
+ further_research.first("[data-type='title']")&.trash # get rid of old title if exists
28
+ further_research_title = page.title.copy
29
+ further_research_title.name = 'h3'
30
+ further_research_title.replace_children(with: <<~HTML
31
+ <span class="os-number">#{chapter.count_in(:book)}.#{page.count_in(:chapter)}</span>
32
+ <span class="os-divider"> </span>
33
+ <span class="os-text" data-type="" itemprop="">#{further_research_title.children}</span>
34
+ HTML
35
+ )
36
+
37
+ further_research.prepend(child:
38
+ <<~HTML
39
+ <a href="##{page.title.id}">
40
+ #{further_research_title.paste}
41
+ </a>
42
+ HTML
43
+ )
44
+ further_research.cut(to: further_researches)
45
+ end
46
+
47
+ return if further_researches.none?
48
+
49
+ @content = further_researches.paste
50
+
51
+ @in_composite_chapter = false
52
+
53
+ chapter.append(child: render(file:
54
+ '../templates/eoc_section_title_template.xhtml.erb'))
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -44,6 +44,10 @@ module Kitchen::Directions::BakeIndex
44
44
  @term_text = @term_text.uncapitalize
45
45
  end
46
46
 
47
+ def capitalize_term_text!
48
+ @term_text = @term_text.capitalize
49
+ end
50
+
47
51
  def <=>(other)
48
52
  sortable <=> other.sortable
49
53
  end
@@ -84,6 +88,11 @@ module Kitchen::Directions::BakeIndex
84
88
  different_caps_item = @items_by_term_text[term.text.uncapitalize]
85
89
  different_caps_item&.uncapitalize_term_text!
86
90
 
91
+ unless different_caps_item
92
+ different_caps_item = @items_by_term_text[term.text.capitalize]
93
+ different_caps_item&.capitalize_term_text!
94
+ end
95
+
87
96
  (different_caps_item || IndexItem.new(term_text: term.text)).tap do |item|
88
97
  @items.add(item)
89
98
  end
@@ -119,27 +128,39 @@ module Kitchen::Directions::BakeIndex
119
128
  @index = Index.new
120
129
 
121
130
  book.pages.terms.each do |term_element|
122
- # Markup the term
123
131
  page = term_element.ancestor(:page)
124
132
  term_element.id = "auto_#{page.id}_term#{term_element.count_in(:book)}"
133
+ page_title = page.title.text
134
+ add_term_to_index(term_element, page_title)
135
+ end
125
136
 
126
- group_by = term_element.text.strip[0]
127
- group_by = I18n.t(:eob_index_symbols_group) unless group_by.match?(/\w/)
128
- term_element['group-by'] = group_by
129
-
130
- # Add it to our index object
131
- @index.add_term(
132
- Term.new(
133
- text: term_element.text,
134
- id: term_element.id,
135
- group_by: group_by,
136
- page_title: page.title.text.gsub(/\n/, '')
137
- )
138
- )
137
+ book.chapters.composite_pages.terms.each do |term_element|
138
+ page = term_element.ancestor(:composite_page)
139
+ chapter = term_element.ancestor(:chapter)
140
+ term_element.id = "auto_composite_page_term#{term_element.count_in(:book)}"
141
+ chapter_number = chapter.count_in(:book)
142
+ page_title = "#{chapter_number} #{page.title.text.strip}".strip
143
+ add_term_to_index(term_element, page_title)
139
144
  end
140
145
 
141
146
  book.first('body').append(child: render(file: 'v1.xhtml.erb'))
142
147
  end
143
148
 
149
+ def add_term_to_index(term_element, page_title)
150
+ group_by = term_element.text.strip[0]
151
+ group_by = I18n.t(:eob_index_symbols_group) unless group_by.match?(/\w/)
152
+ term_element['group-by'] = group_by
153
+
154
+ # Add it to our index object
155
+ @index.add_term(
156
+ Term.new(
157
+ text: term_element.text,
158
+ id: term_element.id,
159
+ group_by: group_by,
160
+ page_title: page_title.gsub(/\n/, '')
161
+ )
162
+ )
163
+ end
164
+
144
165
  end
145
166
  end
@@ -10,7 +10,7 @@ module Kitchen
10
10
  next unless anchor.text == '[link]'
11
11
 
12
12
  id = anchor[:href][1..-1]
13
- replacement = book.document.pantry(name: :link_text).get(id)
13
+ replacement = book.pantry(name: :link_text).get(id)
14
14
  if replacement.present?
15
15
  anchor.replace_children(with: replacement)
16
16
  else
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen
4
+ module Directions
5
+ module BakeNonIntroductionPages
6
+ def self.v1(chapter:)
7
+ chapter.non_introduction_pages.each do |page|
8
+ number = "#{chapter.count_in(:book)}.#{page.count_in(:chapter)}"
9
+
10
+ page.search("div[data-type='description']").each(&:trash)
11
+ page.add_class('chapter-content-module')
12
+
13
+ title = page.title
14
+ title.name = 'h2'
15
+ title.replace_children(with:
16
+ <<~HTML
17
+ <span class="os-number">#{number}</span>
18
+ <span class="os-divider"> </span>
19
+ <span data-type="" itemprop="" class="os-text">#{title.text}</span>
20
+ HTML
21
+ )
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen
4
+ module Directions
5
+ module BakeAutotitledNotes
6
+ def self.v1(book:, classes:)
7
+ book.notes.each do |note|
8
+ next unless (note.classes & classes).any?
9
+
10
+ bake_note(note: note)
11
+ end
12
+ end
13
+
14
+ def self.bake_note(note:)
15
+ note.wrap_children(class: 'os-note-body')
16
+
17
+ BakeNoteSubtitle.v1(note: note)
18
+
19
+ note.prepend(child:
20
+ <<~HTML
21
+ <h3 class="os-title" data-type="title">
22
+ <span class="os-title-label">#{note.autogenerated_title}</span>
23
+ </h3>
24
+ HTML
25
+ )
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen
4
+ module Directions
5
+ module BakeNoteSubtitle
6
+ def self.v1(note:)
7
+ title = note.title&.cut
8
+
9
+ return unless title
10
+
11
+ # Store label information
12
+ note_label = title.children
13
+ note.pantry(name: :link_text).store note_label, label: note.id
14
+
15
+ title.name = 'h4'
16
+ title.add_class('os-subtitle')
17
+ title.wrap_children('span', class: 'os-subtitle-label')
18
+ note.first!('.os-note-body').prepend(child: title.to_s)
19
+ end
20
+ end
21
+ end
22
+ end