openstax_kitchen 3.1.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/CHANGELOG.md +64 -0
  4. data/Gemfile.lock +20 -14
  5. data/README.md +16 -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 +7 -2
  21. data/lib/kitchen/directions/bake_chapter_key_concepts/v1.rb +12 -7
  22. data/lib/kitchen/directions/bake_chapter_key_equations.rb +26 -21
  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_summary.rb +48 -42
  26. data/lib/kitchen/directions/bake_composite_chapters.rb +1 -1
  27. data/lib/kitchen/directions/bake_composite_pages.rb +1 -1
  28. data/lib/kitchen/directions/bake_equations.rb +14 -4
  29. data/lib/kitchen/directions/bake_example.rb +5 -1
  30. data/lib/kitchen/directions/bake_figure.rb +1 -1
  31. data/lib/kitchen/directions/bake_first_elements.rb +22 -0
  32. data/lib/kitchen/directions/bake_footnotes/v1.rb +2 -1
  33. data/lib/kitchen/directions/bake_free_response/free_response.xhtml.erb +10 -0
  34. data/lib/kitchen/directions/bake_free_response/main.rb +11 -0
  35. data/lib/kitchen/directions/bake_free_response/v1.rb +29 -0
  36. data/lib/kitchen/directions/bake_further_research.rb +59 -0
  37. data/lib/kitchen/directions/bake_index/v1.rb +36 -26
  38. data/lib/kitchen/directions/bake_link_placeholders.rb +1 -1
  39. data/lib/kitchen/directions/bake_notes/bake_note_subtitle.rb +4 -0
  40. data/lib/kitchen/directions/bake_notes/bake_numbered_notes.rb +9 -21
  41. data/lib/kitchen/directions/bake_numbered_exercise/main.rb +6 -2
  42. data/lib/kitchen/directions/bake_numbered_exercise/v1.rb +25 -12
  43. data/lib/kitchen/directions/bake_numbered_table/v1.rb +1 -8
  44. data/lib/kitchen/directions/bake_page_abstracts.rb +1 -1
  45. data/lib/kitchen/directions/bake_references/main.rb +16 -0
  46. data/lib/kitchen/directions/bake_references/v1.rb +48 -0
  47. data/lib/kitchen/directions/bake_suggested_reading.rb +5 -0
  48. data/lib/kitchen/directions/bake_toc.rb +4 -2
  49. data/lib/kitchen/directions/{bake_book_answer_key → book_answer_key_container}/eob_solutions_container.xhtml.erb +0 -0
  50. data/lib/kitchen/directions/{bake_book_answer_key → book_answer_key_container}/main.rb +1 -1
  51. data/lib/kitchen/directions/{bake_book_answer_key → book_answer_key_container}/v1.rb +2 -2
  52. data/lib/kitchen/directions/{bake_chapter_review → chapter_review_container}/chapter_review.xhtml.erb +0 -0
  53. data/lib/kitchen/directions/{bake_chapter_review → chapter_review_container}/main.rb +1 -1
  54. data/lib/kitchen/directions/{bake_chapter_review → chapter_review_container}/v1.rb +2 -2
  55. data/lib/kitchen/directions/eoc_section_title_link_snippet.rb +1 -1
  56. data/lib/kitchen/directions/move_exercises_to_eoc/main.rb +27 -0
  57. data/lib/kitchen/directions/{bake_chapter_review_exercises → move_exercises_to_eoc}/v1.rb +8 -10
  58. data/lib/kitchen/directions/{bake_chapter_review_exercises → move_exercises_to_eoc}/v2.rb +8 -9
  59. data/lib/kitchen/directions/{bake_chapter_answer_key → move_solutions_to_answer_key}/main.rb +1 -1
  60. data/lib/kitchen/directions/move_solutions_to_answer_key/strategies/american_government.rb +19 -0
  61. data/lib/kitchen/directions/{bake_chapter_answer_key → move_solutions_to_answer_key}/strategies/calculus.rb +1 -1
  62. data/lib/kitchen/directions/{bake_chapter_answer_key → move_solutions_to_answer_key}/strategies/uphysics.rb +7 -5
  63. data/lib/kitchen/directions/{bake_chapter_answer_key → move_solutions_to_answer_key}/v1.rb +3 -1
  64. data/lib/kitchen/document.rb +20 -42
  65. data/lib/kitchen/element.rb +9 -3
  66. data/lib/kitchen/element_base.rb +93 -21
  67. data/lib/kitchen/element_enumerator_base.rb +33 -2
  68. data/lib/kitchen/element_enumerator_factory.rb +28 -12
  69. data/lib/kitchen/element_factory.rb +3 -3
  70. data/lib/kitchen/example_element.rb +8 -11
  71. data/lib/kitchen/example_element_enumerator.rb +1 -1
  72. data/lib/kitchen/exercise_element.rb +7 -10
  73. data/lib/kitchen/exercise_element_enumerator.rb +1 -1
  74. data/lib/kitchen/figure_element.rb +8 -11
  75. data/lib/kitchen/figure_element_enumerator.rb +1 -1
  76. data/lib/kitchen/id_tracker.rb +68 -0
  77. data/lib/kitchen/metadata_element.rb +8 -2
  78. data/lib/kitchen/metadata_element_enumerator.rb +1 -1
  79. data/lib/kitchen/note_element.rb +8 -11
  80. data/lib/kitchen/note_element_enumerator.rb +1 -1
  81. data/lib/kitchen/oven.rb +5 -1
  82. data/lib/kitchen/page_element.rb +25 -9
  83. data/lib/kitchen/page_element_enumerator.rb +1 -1
  84. data/lib/kitchen/patches/i18n.rb +34 -0
  85. data/lib/kitchen/patches/nokogiri.rb +55 -0
  86. data/lib/kitchen/patches/nokogiri_profiling.rb +60 -0
  87. data/lib/kitchen/reference_element.rb +27 -0
  88. data/lib/kitchen/references_element_enumerator.rb +20 -0
  89. data/lib/kitchen/search_query.rb +31 -3
  90. data/lib/kitchen/selector.rb +25 -0
  91. data/lib/kitchen/selectors/base.rb +39 -0
  92. data/lib/kitchen/selectors/standard_1.rb +13 -0
  93. data/lib/kitchen/table_element.rb +8 -11
  94. data/lib/kitchen/table_element_enumerator.rb +1 -1
  95. data/lib/kitchen/templates/eob_section_title_template.xhtml.erb +10 -0
  96. data/lib/kitchen/templates/eoc_section_title_template.xhtml.erb +10 -0
  97. data/lib/kitchen/term_element.rb +5 -8
  98. data/lib/kitchen/term_element_enumerator.rb +1 -1
  99. data/lib/kitchen/unit_element.rb +13 -7
  100. data/lib/kitchen/unit_element_enumerator.rb +1 -1
  101. data/lib/kitchen/version.rb +1 -1
  102. data/lib/locales/en.yml +3 -0
  103. data/lib/locales/es.yml +32 -0
  104. data/lib/openstax_kitchen.rb +2 -5
  105. data/openstax_kitchen.gemspec +1 -0
  106. metadata +51 -23
  107. data/lib/kitchen/directions/bake_chapter_glossary.rb +0 -39
  108. data/lib/kitchen/directions/bake_chapter_key_concepts/key_concepts.xhtml.erb +0 -16
  109. data/lib/kitchen/directions/bake_chapter_review_exercises/main.rb +0 -15
  110. data/lib/kitchen/directions/bake_chapter_review_exercises/review_exercises.xhtml.erb +0 -10
  111. data/lib/kitchen/directions/bake_exercises/main.rb +0 -12
  112. data/lib/kitchen/directions/bake_exercises/v1.rb +0 -169
  113. data/lib/kitchen/directions/bake_notes/bake_notes.rb +0 -48
  114. data/lib/kitchen/directions/bake_problem_first_elements.rb +0 -16
  115. data/lib/kitchen/transliterations.rb +0 -21
@@ -6,6 +6,7 @@ module Kitchen
6
6
  #
7
7
  module BakeSuggestedReading
8
8
  def self.v1(book:)
9
+ metadata_elements = book.metadata.children_to_keep.copy
9
10
  book.chapters.each do |chapter|
10
11
  suggested_reading = chapter.search('section.suggested-reading').cut
11
12
 
@@ -15,6 +16,10 @@ module Kitchen
15
16
  <h2 data-type="document-title">
16
17
  <span class="os-text">#{I18n.t(:eoc_suggested_reading)}</span>
17
18
  </h2>
19
+ <div data-type="metadata" style="display: none;">
20
+ <h1 data-type="document-title" itemprop="name">#{I18n.t(:eoc_suggested_reading)}</h1>
21
+ #{metadata_elements.paste}
22
+ </div>
18
23
  #{suggested_reading.paste}
19
24
  </div>
20
25
  HTML
@@ -31,9 +31,9 @@ module Kitchen
31
31
  <<~HTML
32
32
  <li cnx-archive-uri="" cnx-archive-shortid="" class="os-toc-unit">
33
33
  <a href="#">
34
- <span class="os-number"><span class="os-part-text">#{I18n.t(:unit)} </span> #{unit.count_in(:book)}</span>
34
+ <span class="os-number"><span class="os-part-text">#{I18n.t(:unit)} </span>#{unit.count_in(:book)}</span>
35
35
  <span class="os-divider"> </span>
36
- <span data-type itemprop class="os-text"> #{unit.title.children} </span>
36
+ <span data-type itemprop class="os-text">#{unit.title_text}</span>
37
37
  </a>
38
38
  <ol class="os-unit">
39
39
  #{chapters.map { |chapter| li_for_chapter(chapter) }.join("\n")}
@@ -96,6 +96,8 @@ module Kitchen
96
96
  when CompositePageElement
97
97
  if page.is_index?
98
98
  'os-toc-index'
99
+ elsif page.is_reference?
100
+ 'os-toc-reference'
99
101
  elsif page.has_ancestor?(:composite_chapter) || page.has_ancestor?(:chapter)
100
102
  'os-toc-chapter-composite-page'
101
103
  else
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kitchen
4
4
  module Directions
5
- module BakeBookAnswerKey
5
+ module BookAnswerKeyContainer
6
6
  def self.v1(book:)
7
7
  V1.new.bake(book: book)
8
8
  end
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Kitchen::Directions::BakeBookAnswerKey
3
+ module Kitchen::Directions::BookAnswerKeyContainer
4
4
  class V1
5
5
  renderable
6
6
 
7
7
  def bake(book:)
8
8
  @metadata = book.metadata.children_to_keep.copy
9
9
  book.body.append(child: render(file: 'eob_solutions_container.xhtml.erb'))
10
- book.body.first('.os-eob.os-solutions-container')
10
+ book.body.first('div.os-eob.os-solutions-container')
11
11
  end
12
12
  end
13
13
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kitchen
4
4
  module Directions
5
- module BakeChapterReview
5
+ module ChapterReviewContainer
6
6
  def self.v1(chapter:, metadata_source:)
7
7
  V1.new.bake(chapter: chapter, metadata_source: metadata_source)
8
8
  end
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Kitchen::Directions::BakeChapterReview
3
+ module Kitchen::Directions::ChapterReviewContainer
4
4
  class V1
5
5
  renderable
6
6
 
7
7
  def bake(chapter:, metadata_source:)
8
8
  @metadata = metadata_source.children_to_keep.copy
9
9
  chapter.append(child: render(file: 'chapter_review.xhtml.erb'))
10
- chapter.first('.os-eoc.os-chapter-review-container')
10
+ chapter.first('div.os-eoc.os-chapter-review-container')
11
11
  end
12
12
  end
13
13
  end
@@ -10,7 +10,7 @@ module Kitchen
10
10
  <h3 data-type="document-title" id="#{page.title.copied_id}">
11
11
  <span class="os-number">#{chapter.count_in(:book)}.#{page.count_in(:chapter)}</span>
12
12
  <span class="os-divider"> </span>
13
- <span class="os-text" data-type="" itemprop="">#{page.title.text}</span>
13
+ <span class="os-text" data-type="" itemprop="">#{page.title_text}</span>
14
14
  </h3>
15
15
  </a>
16
16
  HTML
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen
4
+ module Directions
5
+ module MoveExercisesToEOC
6
+ def self.v1(chapter:, metadata_source:, klass:, append_to: nil, uuid_prefix: '.')
7
+ V1.new.bake(
8
+ chapter: chapter,
9
+ metadata_source: metadata_source,
10
+ append_to: append_to,
11
+ klass: klass,
12
+ uuid_prefix: uuid_prefix
13
+ )
14
+ end
15
+
16
+ def self.v2(chapter:, metadata_source:, klass:, append_to: nil, uuid_prefix: '.')
17
+ V2.new.bake(
18
+ chapter: chapter,
19
+ metadata_source: metadata_source,
20
+ append_to: append_to,
21
+ klass: klass,
22
+ uuid_prefix: uuid_prefix
23
+ )
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,13 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Kitchen::Directions::BakeChapterReviewExercises
3
+ module Kitchen::Directions::MoveExercisesToEOC
4
4
  class V1
5
5
  renderable
6
6
 
7
- def bake(chapter:, metadata_source:, append_to:, klass:)
7
+ def bake(chapter:, metadata_source:, klass:, append_to: nil, uuid_prefix: '.')
8
8
  @klass = klass
9
9
  @metadata = metadata_source.children_to_keep.copy
10
10
  @title = I18n.t(:"eoc.#{klass}")
11
+ @uuid_prefix = uuid_prefix
11
12
 
12
13
  exercise_clipboard = Kitchen::Clipboard.new
13
14
 
@@ -17,13 +18,6 @@ module Kitchen::Directions::BakeChapterReviewExercises
17
18
  sections.each do |exercise_section|
18
19
  exercise_section.first("[data-type='title']")&.trash
19
20
 
20
- exercise_section.exercises.each do |exercise|
21
- exercise.document.pantry(name: :link_text).store(
22
- "#{I18n.t(:exercise_label)} #{chapter.count_in(:book)}.#{exercise.count_in(:chapter)}",
23
- label: exercise.id
24
- )
25
- end
26
-
27
21
  exercise_section.cut(to: exercise_clipboard)
28
22
  end
29
23
  end
@@ -32,7 +26,11 @@ module Kitchen::Directions::BakeChapterReviewExercises
32
26
 
33
27
  @content = exercise_clipboard.paste
34
28
 
35
- append_to.append(child: render(file: 'review_exercises.xhtml.erb'))
29
+ append_to_element = append_to || chapter
30
+ @in_composite_chapter = append_to_element.is?(:composite_chapter)
31
+
32
+ append_to_element.append(child: render(file:
33
+ '../../templates/eoc_section_title_template.xhtml.erb'))
36
34
  end
37
35
  end
38
36
  end
@@ -1,15 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Kitchen::Directions::BakeChapterReviewExercises
3
+ module Kitchen::Directions::MoveExercisesToEOC
4
4
  # Main difference from v1 is the presence of a section title
5
5
  # and some additional wrappers
6
6
  class V2
7
7
  renderable
8
8
 
9
- def bake(chapter:, metadata_source:, append_to:, klass:)
9
+ def bake(chapter:, metadata_source:, klass:, append_to: nil, uuid_prefix: '.')
10
10
  @klass = klass
11
11
  @metadata = metadata_source.children_to_keep.copy
12
12
  @title = I18n.t(:"eoc.#{klass}")
13
+ @uuid_prefix = uuid_prefix
13
14
 
14
15
  exercise_clipboard = Kitchen::Clipboard.new
15
16
 
@@ -21,12 +22,6 @@ module Kitchen::Directions::BakeChapterReviewExercises
21
22
 
22
23
  # Get parent page title
23
24
  section_title = Kitchen::Directions::EocSectionTitleLinkSnippet.v1(page: page)
24
- exercise_section.exercises.each do |exercise|
25
- exercise.document.pantry(name: :link_text).store(
26
- "#{I18n.t(:exercise_label)} #{chapter.count_in(:book)}.#{exercise.count_in(:chapter)}",
27
- label: exercise.id
28
- )
29
- end
30
25
 
31
26
  # Configure section title & wrappers
32
27
  exercise_section.prepend(child: section_title)
@@ -44,7 +39,11 @@ module Kitchen::Directions::BakeChapterReviewExercises
44
39
  </div>
45
40
  HTML
46
41
 
47
- append_to.append(child: render(file: 'review_exercises.xhtml.erb'))
42
+ append_to_element = append_to || chapter
43
+ @in_composite_chapter = append_to_element[:'data-type'] == 'composite-chapter'
44
+
45
+ append_to_element.append(child: render(file:
46
+ '../../templates/eoc_section_title_template.xhtml.erb'))
48
47
  end
49
48
  end
50
49
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Kitchen
4
4
  module Directions
5
- module BakeChapterAnswerKey
5
+ module MoveSolutionsToAnswerKey
6
6
  def self.v1(chapter:, metadata_source:, strategy:, append_to:)
7
7
  V1.new.bake(
8
8
  chapter: chapter,
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen::Directions::MoveSolutionsToAnswerKey
4
+ module Strategies
5
+ class AmericanGovernment
6
+ def bake(chapter:, append_to:)
7
+ bake_section(chapter: chapter, append_to: append_to, klass: 'review-questions')
8
+ end
9
+
10
+ protected
11
+
12
+ def bake_section(chapter:, append_to:, klass:)
13
+ chapter.search(".#{klass} [data-type='solution']").each do |solution|
14
+ append_to.add_child(solution.cut.to_s)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Kitchen::Directions::BakeChapterAnswerKey
3
+ module Kitchen::Directions::MoveSolutionsToAnswerKey
4
4
  module Strategies
5
5
  class Calculus
6
6
  def bake(chapter:, append_to:)
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Kitchen::Directions::BakeChapterAnswerKey
3
+ module Kitchen::Directions::MoveSolutionsToAnswerKey
4
4
  module Strategies
5
5
  class UPhysics
6
6
  def bake(chapter:, append_to:)
@@ -18,7 +18,7 @@ module Kitchen::Directions::BakeChapterAnswerKey
18
18
  def bake_section(chapter:, append_to:, klass:)
19
19
  section_solutions_set = []
20
20
  chapter.search(".#{klass}").each do |section|
21
- section.search('[data-type="solution"]').each do |solution|
21
+ section.search('div[data-type="solution"]').each do |solution|
22
22
  section_solutions_set.push(solution.cut)
23
23
  end
24
24
  end
@@ -31,9 +31,11 @@ module Kitchen::Directions::BakeChapterAnswerKey
31
31
 
32
32
  def bake_from_notes(chapter:, append_to:, klass:)
33
33
  solutions = []
34
- chapter.notes(".#{klass}").each do |note|
35
- solution = note.exercises.first.solution
36
- solutions.push(solution.cut) if solution
34
+ chapter.notes("$.#{klass}").each do |note|
35
+ note.exercises.each do |exercise|
36
+ solution = exercise.solution
37
+ solutions.push(solution.cut) if solution
38
+ end
37
39
  end
38
40
  return if solutions.empty?
39
41
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Kitchen::Directions::BakeChapterAnswerKey
3
+ module Kitchen::Directions::MoveSolutionsToAnswerKey
4
4
  class V1
5
5
  def bake(chapter:, metadata_source:, strategy:, append_to:)
6
6
  strategy =
@@ -9,6 +9,8 @@ module Kitchen::Directions::BakeChapterAnswerKey
9
9
  Strategies::Calculus
10
10
  when :uphysics
11
11
  Strategies::UPhysics
12
+ when :american_government
13
+ Strategies::AmericanGovernment
12
14
  else
13
15
  raise 'No such strategy'
14
16
  end
@@ -13,6 +13,8 @@ module Kitchen
13
13
  attr_accessor :location
14
14
  # @return [Config] the configuration used in this document
15
15
  attr_reader :config
16
+ # @return [IdTracker] the counter for duplicate IDs
17
+ attr_reader :id_tracker
16
18
 
17
19
  # @!method selectors
18
20
  # The document's selectors
@@ -34,7 +36,10 @@ module Kitchen
34
36
  # @!method to_html
35
37
  # @see https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Node#to_html-instance_method Nokogiri::XML::Node#to_html
36
38
  # @return [String] the document as an HTML string
37
- def_delegators :@nokogiri_document, :to_xhtml, :to_s, :to_xml, :to_html
39
+ # @!method encoding
40
+ # @see https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Document#encoding-instance_method Nokogiri::XML::Document#encoding
41
+ # @return [String] the document as an HTML string
42
+ def_delegators :@nokogiri_document, :to_xhtml, :to_s, :to_xml, :to_html, :encoding
38
43
 
39
44
  # Return a new instance of Document
40
45
  #
@@ -44,8 +49,10 @@ module Kitchen
44
49
  @nokogiri_document = nokogiri_document
45
50
  @location = nil
46
51
  @config = config || Config.new
47
- @next_paste_count_for_id = {}
48
- @id_copy_suffix = '_copy_'
52
+ @id_tracker = IdTracker.new
53
+
54
+ # Nokogiri by default only recognizes the namespaces on the root node. Add all others.
55
+ raw&.add_all_namespaces! if @config.enable_all_namespaces
49
56
  end
50
57
 
51
58
  # Returns an enumerator that iterates over all children of this document
@@ -145,38 +152,6 @@ module Kitchen
145
152
  end
146
153
  end
147
154
 
148
- # Keeps track that an element with the given ID has been copied. When such
149
- # elements are pasted, this information is used to give those elements unique
150
- # IDs that don't duplicate the original element.
151
- #
152
- # @param id [String] the ID
153
- #
154
- def record_id_copied(id)
155
- return if id.blank?
156
-
157
- @next_paste_count_for_id[id] ||= 1
158
- end
159
-
160
- # Returns a unique ID given the ID of an element that was copied and is about
161
- # to be pasted
162
- #
163
- # @param original_id [String]
164
- #
165
- def modified_id_to_paste(original_id)
166
- return nil if original_id.nil?
167
- return '' if original_id.blank?
168
-
169
- count = next_count_for_pasted_id(original_id)
170
-
171
- # A count of 0 means the element was cut and this is the first paste, do not
172
- # modify the ID; otherwise, use the uniquified ID.
173
- if count.zero?
174
- original_id
175
- else
176
- "#{original_id}#{@id_copy_suffix}#{count}"
177
- end
178
- end
179
-
180
155
  # Returns the underlying Nokogiri Document object
181
156
  #
182
157
  # @return [Nokogiri::XML::Document]
@@ -185,16 +160,19 @@ module Kitchen
185
160
  @nokogiri_document
186
161
  end
187
162
 
188
- protected
189
-
190
- def next_count_for_pasted_id(id)
191
- return if id.blank?
192
-
193
- (@next_paste_count_for_id[id] ||= 0).tap do
194
- @next_paste_count_for_id[id] += 1
163
+ # Returns the locale for this document, default to `:en` if no locale detected
164
+ #
165
+ # @return [Symbol]
166
+ #
167
+ def locale
168
+ raw.root['lang']&.to_sym || begin
169
+ warn 'No `lang` attribute on this document so cannot detect its locale; defaulting to `:en`'
170
+ :en
195
171
  end
196
172
  end
197
173
 
174
+ protected
175
+
198
176
  attr_reader :nokogiri_document
199
177
 
200
178
  end
@@ -19,8 +19,14 @@ module Kitchen
19
19
  short_type: short_type)
20
20
  end
21
21
 
22
- # # @!method pages
23
- # # Returns a pages enumerator
24
- # def_delegators :as_enumerator, :pages, :chapters, :terms, :figures, :notes, :tables, :examples
22
+ # Returns true if this class represents the element for the given node; always false
23
+ # for this generic class
24
+ #
25
+ # @param node [Nokogiri::XML::Node] the underlying node
26
+ # @return [Boolean]
27
+ #
28
+ def self.is_the_element_class_for?(_node, **)
29
+ false
30
+ end
25
31
  end
26
32
  end
@@ -3,6 +3,7 @@
3
3
  require 'forwardable'
4
4
  require 'securerandom'
5
5
 
6
+ # rubocop:disable Metrics/ClassLength
6
7
  module Kitchen
7
8
  # Abstract base class for all elements. If you are looking for a simple concrete
8
9
  # element class, use `Element`.
@@ -92,6 +93,16 @@ module Kitchen
92
93
  # @return [Selectors::Base]
93
94
  def_delegators :config, :selectors
94
95
 
96
+ # @!method pantry
97
+ # Access the pantry for this element's document
98
+ # @return [Pantry]
99
+ # @!method :clipboard
100
+ # Access the clipboard for this element's document
101
+ # @return [Clipboard]
102
+ def_delegators :document, :pantry, :clipboard
103
+
104
+ def_delegators :document, :id_tracker
105
+
95
106
  # Creates a new instance
96
107
  #
97
108
  # @param node [Nokogiri::XML::Node] the wrapped element
@@ -108,7 +119,9 @@ module Kitchen
108
119
 
109
120
  @enumerator_class = enumerator_class
110
121
 
111
- @short_type = short_type || "unknown_type_#{SecureRandom.hex(4)}"
122
+ @short_type = short_type ||
123
+ self.class.try(:short_type) ||
124
+ "unknown_type_#{SecureRandom.hex(4)}"
112
125
 
113
126
  @document =
114
127
  case document
@@ -121,16 +134,53 @@ module Kitchen
121
134
  @ancestors = HashWithIndifferentAccess.new
122
135
  @search_query_matches_that_have_been_counted = {}
123
136
  @is_a_clone = false
137
+ @search_cache = {}
138
+ end
139
+
140
+ # Returns ElementBase descendent type or nil if none found
141
+ #
142
+ # @param type [Symbol] the descendant type, e.g. `:page`
143
+ # @return [Class] the child class for the given type
144
+ #
145
+ def self.descendant(type)
146
+ @types_to_descendants ||=
147
+ descendants.each_with_object({}) do |descendant, hash|
148
+ next unless descendant.try(:short_type)
149
+
150
+ hash[descendant.short_type] = descendant
151
+ end
152
+
153
+ @types_to_descendants[type]
154
+ end
155
+
156
+ # Returns ElementBase descendent type or Error if none found
157
+ #
158
+ # @param type [Symbol] the descendant type, e.g. `:page`
159
+ # @raise if the type is unknown
160
+ # @return [Class] the child class for the given type
161
+ #
162
+ def self.descendant!(type)
163
+ descendant(type) || raise("Unknown ElementBase descendant type '#{type}'")
164
+ end
165
+
166
+ # Returns true if this element is the given type
167
+ #
168
+ # @param type [Symbol] the descendant type, e.g. `:page`
169
+ # @raise if the type is unknown
170
+ # @return [Boolean]
171
+ #
172
+ def is?(type)
173
+ ElementBase.descendant!(type).is_the_element_class_for?(raw, config: config)
124
174
  end
125
175
 
126
176
  # Returns true if this class represents the element for the given node
127
177
  #
128
178
  # @param node [Nokogiri::XML::Node] the underlying node
179
+ # @param config [Kitchen::Config]
129
180
  # @return [Boolean]
130
181
  #
131
- def self.is_the_element_class_for?(_node)
132
- # override this in subclasses
133
- false
182
+ def self.is_the_element_class_for?(node, config:)
183
+ Selector.named(short_type).matches?(node, config: config)
134
184
  end
135
185
 
136
186
  # Returns true if this element has the given class
@@ -298,7 +348,7 @@ module Kitchen
298
348
  # search results if the method or callable returns false
299
349
  # @return [ElementEnumerator]
300
350
  #
301
- def search(*selector_or_xpath_args, only: nil, except: nil)
351
+ def search(*selector_or_xpath_args, only: nil, except: nil, reload: false)
302
352
  block_error_if(block_given?)
303
353
 
304
354
  ElementEnumerator.factory.build_within(
@@ -307,19 +357,29 @@ module Kitchen
307
357
  css_or_xpath: selector_or_xpath_args,
308
358
  only: only,
309
359
  except: except
310
- )
360
+ ),
361
+ reload: reload
311
362
  )
312
363
  end
313
364
 
365
+ def raw_search(*selector_or_xpath_args, reload: false)
366
+ key = selector_or_xpath_args
367
+ @search_cache[key] = nil if reload || !config.enable_search_cache
368
+ # cache nil search results with a fake -1 value
369
+ @search_cache[key] ||= raw.search(*selector_or_xpath_args) || -1
370
+ @search_cache[key] == -1 ? nil : @search_cache[key]
371
+ end
372
+
314
373
  # Yields and returns the first child element that matches the provided
315
374
  # selector or XPath arguments.
316
375
  #
317
376
  # @param selector_or_xpath_args [Array<String>] CSS selectors or XPath arguments
377
+ # @param reload [Boolean] ignores cache if true
318
378
  # @yieldparam [Element] the matched XML element
319
379
  # @return [Element, nil] the matched XML element or nil if no match found
320
380
  #
321
- def first(*selector_or_xpath_args)
322
- search(*selector_or_xpath_args).first.tap do |element|
381
+ def first(*selector_or_xpath_args, reload: false)
382
+ search(*selector_or_xpath_args, reload: reload).first.tap do |element|
323
383
  yield(element) if block_given?
324
384
  end
325
385
  end
@@ -328,12 +388,13 @@ module Kitchen
328
388
  # selector or XPath arguments.
329
389
  #
330
390
  # @param selector_or_xpath_args [Array<String>] CSS selectors or XPath arguments
391
+ # @param reload [Boolean] ignores cache if true
331
392
  # @yieldparam [Element] the matched XML element
332
393
  # @raise [ElementNotFoundError] if no matching element is found
333
394
  # @return [Element] the matched XML element
334
395
  #
335
- def first!(*selector_or_xpath_args)
336
- search(*selector_or_xpath_args).first!.tap do |element|
396
+ def first!(*selector_or_xpath_args, reload: false)
397
+ search(*selector_or_xpath_args, reload: reload).first!.tap do |element|
337
398
  yield(element) if block_given?
338
399
  end
339
400
  end
@@ -382,8 +443,13 @@ module Kitchen
382
443
  def cut(to: nil)
383
444
  block_error_if(block_given?)
384
445
 
446
+ raw.traverse do |node|
447
+ next if node.text? || node.document?
448
+
449
+ id_tracker.record_id_cut(node[:id])
450
+ end
385
451
  node.remove
386
- clipboard(to).add(self) if to.present?
452
+ get_clipboard(to).add(self) if to.present?
387
453
  self
388
454
  end
389
455
 
@@ -402,9 +468,9 @@ module Kitchen
402
468
  the_copy.raw.traverse do |node|
403
469
  next if node.text? || node.document?
404
470
 
405
- document.record_id_copied(node[:id])
471
+ id_tracker.record_id_copied(node[:id])
406
472
  end
407
- clipboard(to).add(the_copy) if to.present?
473
+ get_clipboard(to).add(the_copy) if to.present?
408
474
  the_copy
409
475
  end
410
476
 
@@ -413,20 +479,22 @@ module Kitchen
413
479
  def paste
414
480
  # See `clone` method for a note about namespaces
415
481
  block_error_if(block_given?)
416
-
417
482
  temp_copy = clone
418
483
  temp_copy.raw.traverse do |node|
419
484
  next if node.text? || node.document?
420
485
 
421
- node[:id] = document.modified_id_to_paste(node[:id]) unless node[:id].blank?
486
+ if node[:id].present?
487
+ id_tracker.record_id_pasted(node[:id])
488
+ node[:id] = id_tracker.modified_id_to_paste(node[:id])
489
+ end
422
490
  end
423
491
  temp_copy.to_s
424
492
  end
425
493
 
426
494
  # Copy the element's id
427
495
  def copied_id
428
- document.record_id_copied(id)
429
- document.modified_id_to_paste(id)
496
+ id_tracker.record_id_copied(id)
497
+ id_tracker.modified_id_to_paste(id)
430
498
  end
431
499
 
432
500
  # Delete the element
@@ -646,7 +714,8 @@ module Kitchen
646
714
  # @!method pages
647
715
  # Returns a pages enumerator
648
716
  def_delegators :as_enumerator, :pages, :chapters, :terms, :figures, :notes, :tables, :examples,
649
- :metadatas, :non_introduction_pages, :units, :titles, :exercises, :composite_pages
717
+ :metadatas, :non_introduction_pages, :units, :titles, :exercises, :references,
718
+ :composite_pages, :composite_chapters
650
719
 
651
720
  # Returns this element as an enumerator (over only one element, itself)
652
721
  #
@@ -671,10 +740,10 @@ module Kitchen
671
740
  # @param name_or_object [String, Clipboard] the name of the clipboard or the clipboard itself
672
741
  # @return [Clipboard]
673
742
  #
674
- def clipboard(name_or_object)
743
+ def get_clipboard(name_or_object)
675
744
  case name_or_object
676
745
  when Symbol
677
- document.clipboard(name: name_or_object)
746
+ clipboard(name: name_or_object)
678
747
  when Clipboard
679
748
  name_or_object
680
749
  else
@@ -688,7 +757,9 @@ module Kitchen
688
757
  # @param string [String] the string to clean
689
758
  def remove_default_namespaces_if_clone(string)
690
759
  if is_a_clone
691
- string.gsub('xmlns:default="http://www.w3.org/1999/xhtml"', '').gsub('default:', '')
760
+ string.gsub('xmlns:default="http://www.w3.org/1999/xhtml"', '')
761
+ .gsub('xmlns="http://www.w3.org/1999/xhtml"', '')
762
+ .gsub('default:', '')
692
763
  else
693
764
  string
694
765
  end
@@ -701,3 +772,4 @@ module Kitchen
701
772
 
702
773
  end
703
774
  end
775
+ # rubocop:enable Metrics/ClassLength