openstax_kitchen 1.0.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (182) hide show
  1. checksums.yaml +4 -4
  2. data/.devcontainer/devcontainer.json +37 -17
  3. data/.github/config.yml +14 -0
  4. data/.github/workflows/tests.yml +5 -15
  5. data/.gitignore +2 -2
  6. data/.inch.yml +6 -0
  7. data/.rubocop.yml +65 -0
  8. data/CHANGELOG.md +85 -2
  9. data/Gemfile +5 -3
  10. data/Gemfile.lock +66 -18
  11. data/README.md +69 -15
  12. data/Rakefile +5 -3
  13. data/bin/console +4 -3
  14. data/docker/Dockerfile +36 -0
  15. data/docker/Dockerfile.ci +10 -0
  16. data/docker/bash +5 -1
  17. data/docker/build +10 -0
  18. data/docker/ci +15 -0
  19. data/docker/run +9 -0
  20. data/docker/tag_and_push_latest +17 -0
  21. data/lefthook.yml +6 -0
  22. data/lib/kitchen/ancestor.rb +38 -1
  23. data/lib/kitchen/book_document.rb +20 -2
  24. data/lib/kitchen/book_element.rb +40 -5
  25. data/lib/kitchen/book_element_enumerator.rb +4 -0
  26. data/lib/kitchen/book_recipe.rb +15 -1
  27. data/lib/kitchen/chapter_element.rb +43 -6
  28. data/lib/kitchen/chapter_element_enumerator.rb +9 -1
  29. data/lib/kitchen/clipboard.rb +35 -4
  30. data/lib/kitchen/composite_chapter_element.rb +21 -6
  31. data/lib/kitchen/composite_page_element.rb +35 -7
  32. data/lib/kitchen/composite_page_element_enumerator.rb +9 -1
  33. data/lib/kitchen/config.rb +20 -6
  34. data/lib/kitchen/counter.rb +9 -2
  35. data/lib/kitchen/debug/print_recipe_error.rb +53 -35
  36. data/lib/kitchen/directions/.rubocop.yml +22 -0
  37. data/lib/kitchen/directions/bake_appendix.rb +4 -4
  38. data/lib/kitchen/directions/bake_chapter_glossary/main.rb +18 -0
  39. data/lib/kitchen/directions/bake_chapter_glossary/v1.rb +30 -0
  40. data/lib/kitchen/directions/bake_chapter_introductions.rb +7 -7
  41. data/lib/kitchen/directions/bake_chapter_key_concepts/main.rb +16 -0
  42. data/lib/kitchen/directions/bake_chapter_key_concepts/v1.rb +35 -0
  43. data/lib/kitchen/directions/bake_chapter_key_equations.rb +30 -20
  44. data/lib/kitchen/directions/bake_chapter_section_exercises/main.rb +11 -0
  45. data/lib/kitchen/directions/bake_chapter_section_exercises/v1.rb +28 -0
  46. data/lib/kitchen/directions/bake_chapter_summary.rb +45 -36
  47. data/lib/kitchen/directions/bake_chapter_title/main.rb +11 -0
  48. data/lib/kitchen/directions/bake_chapter_title/v1.rb +24 -0
  49. data/lib/kitchen/directions/bake_checkpoint.rb +44 -0
  50. data/lib/kitchen/directions/bake_composite_chapters.rb +14 -0
  51. data/lib/kitchen/directions/bake_composite_pages.rb +2 -2
  52. data/lib/kitchen/directions/bake_equations.rb +37 -0
  53. data/lib/kitchen/directions/bake_example.rb +39 -11
  54. data/lib/kitchen/directions/bake_figure.rb +8 -5
  55. data/lib/kitchen/directions/bake_first_elements.rb +16 -0
  56. data/lib/kitchen/directions/bake_footnotes/main.rb +2 -2
  57. data/lib/kitchen/directions/bake_footnotes/v1.rb +6 -5
  58. data/lib/kitchen/directions/bake_free_response/free_response.xhtml.erb +10 -0
  59. data/lib/kitchen/directions/bake_free_response/main.rb +11 -0
  60. data/lib/kitchen/directions/bake_free_response/v1.rb +29 -0
  61. data/lib/kitchen/directions/bake_further_research.rb +59 -0
  62. data/lib/kitchen/directions/bake_index/main.rb +2 -2
  63. data/lib/kitchen/directions/bake_index/v1.rb +46 -18
  64. data/lib/kitchen/directions/bake_link_placeholders.rb +24 -0
  65. data/lib/kitchen/directions/bake_math_in_paragraph.rb +5 -3
  66. data/lib/kitchen/directions/bake_non_introduction_pages.rb +26 -0
  67. data/lib/kitchen/directions/bake_notes/bake_autotitled_notes.rb +29 -0
  68. data/lib/kitchen/directions/bake_notes/bake_note_subtitle.rb +22 -0
  69. data/lib/kitchen/directions/bake_notes/bake_numbered_notes.rb +51 -0
  70. data/lib/kitchen/directions/bake_notes/bake_unclassified_notes.rb +30 -0
  71. data/lib/kitchen/directions/bake_numbered_exercise/main.rb +15 -0
  72. data/lib/kitchen/directions/bake_numbered_exercise/v1.rb +47 -0
  73. data/lib/kitchen/directions/bake_numbered_table/main.rb +4 -4
  74. data/lib/kitchen/directions/bake_numbered_table/v1.rb +37 -18
  75. data/lib/kitchen/directions/bake_page_abstracts.rb +30 -0
  76. data/lib/kitchen/directions/bake_preface/main.rb +11 -0
  77. data/lib/kitchen/directions/bake_preface/v1.rb +18 -0
  78. data/lib/kitchen/directions/bake_references/main.rb +16 -0
  79. data/lib/kitchen/directions/bake_references/v1.rb +48 -0
  80. data/lib/kitchen/directions/bake_stepwise.rb +8 -12
  81. data/lib/kitchen/directions/bake_suggested_reading.rb +31 -0
  82. data/lib/kitchen/directions/bake_theorem/main.rb +11 -0
  83. data/lib/kitchen/directions/bake_theorem/v1.rb +28 -0
  84. data/lib/kitchen/directions/bake_toc.rb +49 -22
  85. data/lib/kitchen/directions/bake_unit_title/main.rb +11 -0
  86. data/lib/kitchen/directions/bake_unit_title/v1.rb +23 -0
  87. data/lib/kitchen/directions/bake_unnumbered_tables.rb +7 -5
  88. data/lib/kitchen/directions/book_answer_key_container/eob_solutions_container.xhtml.erb +9 -0
  89. data/lib/kitchen/directions/book_answer_key_container/main.rb +11 -0
  90. data/lib/kitchen/directions/book_answer_key_container/v1.rb +13 -0
  91. data/lib/kitchen/directions/chapter_review_container/chapter_review.xhtml.erb +9 -0
  92. data/lib/kitchen/directions/chapter_review_container/main.rb +11 -0
  93. data/lib/kitchen/directions/chapter_review_container/v1.rb +13 -0
  94. data/lib/kitchen/directions/eoc_section_title_link_snippet.rb +20 -0
  95. data/lib/kitchen/directions/move_exercises_to_eoc/main.rb +27 -0
  96. data/lib/kitchen/directions/move_exercises_to_eoc/v1.rb +36 -0
  97. data/lib/kitchen/directions/move_exercises_to_eoc/v2.rb +49 -0
  98. data/lib/kitchen/directions/move_solutions_to_answer_key/main.rb +14 -0
  99. data/lib/kitchen/directions/move_solutions_to_answer_key/strategies/american_government.rb +19 -0
  100. data/lib/kitchen/directions/move_solutions_to_answer_key/strategies/calculus.rb +41 -0
  101. data/lib/kitchen/directions/move_solutions_to_answer_key/strategies/uphysics.rb +63 -0
  102. data/lib/kitchen/directions/move_solutions_to_answer_key/v1.rb +34 -0
  103. data/lib/kitchen/directions/move_title_text_into_span.rb +2 -2
  104. data/lib/kitchen/document.rb +83 -13
  105. data/lib/kitchen/element.rb +20 -3
  106. data/lib/kitchen/element_base.rb +373 -63
  107. data/lib/kitchen/element_enumerator.rb +8 -0
  108. data/lib/kitchen/element_enumerator_base.rb +297 -28
  109. data/lib/kitchen/element_enumerator_factory.rb +64 -53
  110. data/lib/kitchen/element_factory.rb +27 -12
  111. data/lib/kitchen/errors.rb +5 -0
  112. data/lib/kitchen/example_element.rb +21 -6
  113. data/lib/kitchen/example_element_enumerator.rb +9 -1
  114. data/lib/kitchen/exercise_element.rb +42 -0
  115. data/lib/kitchen/exercise_element_enumerator.rb +21 -0
  116. data/lib/kitchen/figure_element.rb +36 -5
  117. data/lib/kitchen/figure_element_enumerator.rb +9 -1
  118. data/lib/kitchen/metadata_element.rb +34 -0
  119. data/lib/kitchen/metadata_element_enumerator.rb +21 -0
  120. data/lib/kitchen/mixins/block_error_if.rb +24 -4
  121. data/lib/kitchen/note_element.rb +48 -20
  122. data/lib/kitchen/note_element_enumerator.rb +9 -1
  123. data/lib/kitchen/oven.rb +66 -15
  124. data/lib/kitchen/page_element.rb +84 -14
  125. data/lib/kitchen/page_element_enumerator.rb +9 -1
  126. data/lib/kitchen/pantry.rb +28 -1
  127. data/lib/kitchen/patches/nokogiri.rb +19 -2
  128. data/lib/kitchen/patches/renderable.rb +9 -3
  129. data/lib/kitchen/patches/string.rb +8 -0
  130. data/lib/kitchen/recipe.rb +69 -32
  131. data/lib/kitchen/reference_element.rb +27 -0
  132. data/lib/kitchen/references_element_enumerator.rb +20 -0
  133. data/lib/kitchen/search_history.rb +43 -4
  134. data/lib/kitchen/search_query.rb +106 -0
  135. data/lib/kitchen/selector.rb +24 -0
  136. data/lib/kitchen/selectors/base.rb +65 -0
  137. data/lib/kitchen/selectors/standard_1.rb +21 -0
  138. data/lib/kitchen/table_element.rb +55 -7
  139. data/lib/kitchen/table_element_enumerator.rb +9 -1
  140. data/lib/kitchen/templates/eob_section_title_template.xhtml.erb +10 -0
  141. data/lib/kitchen/templates/eoc_section_title_template.xhtml.erb +10 -0
  142. data/lib/kitchen/term_element.rb +15 -4
  143. data/lib/kitchen/term_element_enumerator.rb +9 -1
  144. data/lib/kitchen/transliterations.rb +7 -5
  145. data/lib/kitchen/type_casting_element_enumerator.rb +17 -1
  146. data/lib/kitchen/unit_element.rb +45 -0
  147. data/lib/kitchen/unit_element_enumerator.rb +20 -0
  148. data/lib/kitchen/utils.rb +10 -13
  149. data/lib/kitchen/version.rb +5 -1
  150. data/lib/locales/en.yml +18 -7
  151. data/lib/locales/pl.yml +24 -0
  152. data/lib/openstax_kitchen.rb +59 -0
  153. data/openstax_kitchen.gemspec +26 -20
  154. data/tutorials/00/solution1.rb +9 -0
  155. data/tutorials/00/solution2.rb +8 -0
  156. data/tutorials/01/solution1.rb +18 -0
  157. data/tutorials/01/solution2.rb +26 -0
  158. data/tutorials/02/solution1.rb +31 -0
  159. data/tutorials/03/{solution_1.rb → solution1.rb} +6 -4
  160. data/tutorials/03/solution2.rb +18 -0
  161. data/tutorials/04/{solution_1.rb → solution1.rb} +4 -2
  162. data/tutorials/04/{solution_2.rb → solution2.rb} +6 -4
  163. data/tutorials/05/solution1.rb +11 -0
  164. data/tutorials/check_it +16 -15
  165. data/tutorials/setup_my_recipes +7 -6
  166. metadata +148 -27
  167. data/Dockerfile +0 -19
  168. data/bin/normalize +0 -79
  169. data/books/chemistry2e/bake.rb +0 -133
  170. data/docker-compose.yml +0 -12
  171. data/docker/entrypoint +0 -9
  172. data/lib/kitchen.rb +0 -57
  173. data/lib/kitchen/directions/bake_chapter_glossary.rb +0 -34
  174. data/lib/kitchen/directions/bake_exercises.rb +0 -164
  175. data/lib/kitchen/directions/bake_notes.rb +0 -58
  176. data/tutorials/00/solution_1.rb +0 -7
  177. data/tutorials/00/solution_2.rb +0 -6
  178. data/tutorials/01/solution_1.rb +0 -16
  179. data/tutorials/01/solution_2.rb +0 -24
  180. data/tutorials/02/solution_1.rb +0 -29
  181. data/tutorials/03/solution_2.rb +0 -15
  182. data/tutorials/05/solution_1.rb +0 -9
@@ -1,32 +1,47 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Kitchen
4
+ # Builds Elements from Nokogiri Nodes
5
+ #
2
6
  class ElementFactory
3
7
 
4
8
  ELEMENT_CLASSES = ElementBase.descendants
5
9
 
6
- def self.specific_element_class_for_node(node)
7
- ELEMENT_CLASSES.find do |klass|
8
- klass.is_the_element_class_for?(node)
9
- end || Element
10
- end
11
-
10
+ # Builds a new concrete subclass of ElementBase for the provided node.
11
+ #
12
+ # @param node [Nokogiri::XML::Node] the node to wrap in an element
13
+ # @param document [Document] the document
14
+ # @param element_class [ElementBase] actually a subclass of +ElementBase+ to
15
+ # use when wrapping the node.
16
+ # @param default_short_type [Symbol, String] if we are making an instance
17
+ # of an element class where we know the short_type we'll use that; otherwise
18
+ # we'll make an Element instance and use this argument as the short_type.
19
+ # @param detect_element_class [Boolean] if true and +element_class+ is not given,
20
+ # attempts to detect the element class from the node
21
+ # @return [ElementBase] actually a subclass of +ElementBase+
22
+ #
12
23
  def self.build_from_node(node:,
13
24
  document:,
14
25
  element_class: nil,
15
26
  default_short_type: nil,
16
27
  detect_element_class: false)
17
- element_class ||= detect_element_class ?
18
- specific_element_class_for_node(node) :
19
- Element
28
+ element_class ||= detect_element_class ? find_element_class(node, document.config) : Element
20
29
 
21
30
  if element_class == Element
22
31
  element_class.new(node: node,
23
- document: document,
24
- short_type: default_short_type)
32
+ document: document,
33
+ short_type: default_short_type)
25
34
  else
26
35
  element_class.new(node: node,
27
- document: document)
36
+ document: document)
28
37
  end
29
38
  end
30
39
 
40
+ def self.find_element_class(node, config)
41
+ ELEMENT_CLASSES.find do |klass|
42
+ klass.is_the_element_class_for?(node, config: config)
43
+ end || Element
44
+ end
45
+
31
46
  end
32
47
  end
@@ -1,4 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Kitchen
4
+ # Raised likely due to a problem in the recipe, not in Kitchen
2
5
  class RecipeError < StandardError; end
6
+
7
+ # Raised when an element not found
3
8
  class ElementNotFoundError < StandardError; end
4
9
  end
@@ -1,19 +1,34 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Kitchen
4
+ # An element for an example
5
+ #
2
6
  class ExampleElement < ElementBase
3
7
 
8
+ # Creates a new +ExampleElement+
9
+ #
10
+ # @param node [Nokogiri::XML::Node] the node this element wraps
11
+ # @param document [Document] this element's document
12
+ #
4
13
  def initialize(node:, document: nil)
5
14
  super(node: node,
6
15
  document: document,
7
- enumerator_class: ExampleElementEnumerator,
8
- short_type: :example)
16
+ enumerator_class: ExampleElementEnumerator)
9
17
  end
10
18
 
11
- def titles
12
- search("span[data-type='title']")
19
+ # Returns the short type
20
+ # @return [Symbol]
21
+ #
22
+ def self.short_type
23
+ :example
13
24
  end
14
25
 
15
- def self.is_the_element_class_for?(node)
16
- node['data-type'] == "example"
26
+ # Returns the an enumerator for titles.
27
+ #
28
+ # @return [ElementEnumerator]
29
+ #
30
+ def titles
31
+ search("span[data-type='title']")
17
32
  end
18
33
 
19
34
  end
@@ -1,9 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Kitchen
4
+ # An enumerator for example elements
5
+ #
2
6
  class ExampleElementEnumerator < ElementEnumeratorBase
3
7
 
8
+ # Returns a factory for this enumerator
9
+ #
10
+ # @return [ElementEnumeratorFactory]
11
+ #
4
12
  def self.factory
5
13
  ElementEnumeratorFactory.new(
6
- default_css_or_xpath: "div[data-type='example']", # TODO element.document.selectors.example
14
+ default_css_or_xpath: Selector.named(:example),
7
15
  sub_element_class: ExampleElement,
8
16
  enumerator_class: self
9
17
  )
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen
4
+ # An element for an example
5
+ #
6
+ class ExerciseElement < ElementBase
7
+
8
+ # Creates a new +ExerciseElement+
9
+ #
10
+ # @param node [Nokogiri::XML::Node] the node this element wraps
11
+ # @param document [Document] this element's document
12
+ #
13
+ def initialize(node:, document: nil)
14
+ super(node: node,
15
+ document: document,
16
+ enumerator_class: ExerciseElementEnumerator)
17
+ end
18
+
19
+ # Returns the short type
20
+ # @return [Symbol]
21
+ #
22
+ def self.short_type
23
+ :exercise
24
+ end
25
+
26
+ # Returns the enumerator for problem.
27
+ #
28
+ # @return ElementEnumerator
29
+ #
30
+ def problem
31
+ first("[data-type='problem']")
32
+ end
33
+
34
+ # Returns the enumerator for solution.
35
+ #
36
+ # @return ElementEnumerator
37
+ #
38
+ def solution
39
+ first("[data-type='solution']")
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen
4
+ # An enumerator for example elements
5
+ #
6
+ class ExerciseElementEnumerator < ElementEnumeratorBase
7
+
8
+ # Returns a factory for this enumerator
9
+ #
10
+ # @return [ElementEnumeratorFactory]
11
+ #
12
+ def self.factory
13
+ ElementEnumeratorFactory.new(
14
+ default_css_or_xpath: Selector.named(:exercise),
15
+ sub_element_class: ExerciseElement,
16
+ enumerator_class: self
17
+ )
18
+ end
19
+
20
+ end
21
+ end
@@ -1,19 +1,50 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Kitchen
4
+ # An element for a figure
5
+ #
2
6
  class FigureElement < ElementBase
3
7
 
8
+ # Creates a new +FigureElement+
9
+ #
10
+ # @param node [Nokogiri::XML::Node] the node this element wraps
11
+ # @param document [Document] this element's document
12
+ #
4
13
  def initialize(node:, document: nil)
5
14
  super(node: node,
6
15
  document: document,
7
- enumerator_class: FigureElementEnumerator,
8
- short_type: :figure)
16
+ enumerator_class: FigureElementEnumerator)
17
+ end
18
+
19
+ # Returns the short type
20
+ # @return [Symbol]
21
+ #
22
+ def self.short_type
23
+ :figure
9
24
  end
10
25
 
26
+ # Returns the caption element
27
+ #
28
+ # @return [Element, nil]
29
+ #
11
30
  def caption
12
- first("figcaption")
31
+ first('figcaption')
32
+ end
33
+
34
+ # Returns the Figure Title
35
+ #
36
+ # @return [Element, nil]
37
+ #
38
+ def title
39
+ first("div[data-type='title']")
13
40
  end
14
41
 
15
- def self.is_the_element_class_for?(node)
16
- node.name == "figure"
42
+ # Returns true if the figure is a child of another figure
43
+ #
44
+ # @return [Boolean]
45
+ #
46
+ def subfigure?
47
+ parent.name == 'figure'
17
48
  end
18
49
 
19
50
  end
@@ -1,9 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Kitchen
4
+ # An enumerator for figure elements
5
+ #
2
6
  class FigureElementEnumerator < ElementEnumeratorBase
3
7
 
8
+ # Returns a factory for this enumerator
9
+ #
10
+ # @return [ElementEnumeratorFactory]
11
+ #
4
12
  def self.factory
5
13
  ElementEnumeratorFactory.new(
6
- default_css_or_xpath: "figure", # TODO get from config?
14
+ default_css_or_xpath: Selector.named(:figure),
7
15
  sub_element_class: FigureElement,
8
16
  enumerator_class: self
9
17
  )
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen
4
+ # An element for metadata
5
+ #
6
+ class MetadataElement < ElementBase
7
+ # Creates a new +MetadataElement+
8
+ #
9
+ # @param node [Nokogiri::XML::Node] the node this element wraps
10
+ # @param document [Document] this element's document
11
+ #
12
+ def initialize(node:, document: nil)
13
+ super(node: node,
14
+ document: document,
15
+ enumerator_class: MetadataElementEnumerator)
16
+ end
17
+
18
+ # Returns the short type
19
+ # @return [Symbol]
20
+ #
21
+ def self.short_type
22
+ :metadata
23
+ end
24
+
25
+ # Returns set of selected data elements
26
+ #
27
+ # @return [ElementEnumerator]
28
+ #
29
+ def children_to_keep
30
+ search(%w([data-type='revised'] .authors .publishers .print-style .permissions
31
+ [data-type='subject']))
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen
4
+ # An enumerator for metadata elements
5
+ #
6
+ class MetadataElementEnumerator < ElementEnumeratorBase
7
+
8
+ # Returns a factory for this enumerator
9
+ #
10
+ # @return [ElementEnumeratorFactory]
11
+ #
12
+ def self.factory
13
+ ElementEnumeratorFactory.new(
14
+ default_css_or_xpath: Selector.named(:metadata),
15
+ sub_element_class: MetadataElement,
16
+ enumerator_class: self
17
+ )
18
+ end
19
+
20
+ end
21
+ end
@@ -1,19 +1,39 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Kitchen
2
4
  module Mixins
5
+ # A mixin for including the block_error_if method
6
+ #
7
+ # @example
8
+ # class SomeClass
9
+ # include Mixins::BlockErrorIf
10
+ #
11
+ # def foo
12
+ # block_error_if(block_given?)
13
+ # end
14
+ # end
3
15
  module BlockErrorIf
4
-
16
+ # All Ruby methods can take blocks, but not all of them use the block. If a block is given but
17
+ # not expected, we want to raise an error to help the developer figure out why their block isn't
18
+ # doing what they expect. The method does some work to figure out where the block was errantly
19
+ # given to help the developer find the errant line of code.
20
+ #
21
+ # @param block_given [Boolean] true if block was given
22
+ # @raise [RecipeError] if a block was given
23
+ #
5
24
  def block_error_if(block_given)
25
+ return unless block_given
26
+
6
27
  calling_method = begin
7
28
  this_method_location_index = caller_locations.find_index do |location|
8
- location.label == "block_error_if"
29
+ location.label == 'block_error_if'
9
30
  end
10
31
 
11
32
  caller_locations[(this_method_location_index || -1) + 1].label
12
33
  end
13
34
 
14
- raise(RecipeError, "The `#{calling_method}` method does not take a block argument") if block_given
35
+ raise(RecipeError, "The `#{calling_method}` method does not take a block")
15
36
  end
16
-
17
37
  end
18
38
  end
19
39
  end
@@ -1,43 +1,71 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Kitchen
4
+ # An element for a note
5
+ #
2
6
  class NoteElement < ElementBase
3
7
 
4
- TITLE_TRANSLATION_KEYS = %w(
5
- link-to-learning
6
- everyday-life
7
- sciences-interconnect
8
- chemist-portrait
9
- )
10
-
8
+ # Creates a new +NoteElement+
9
+ #
10
+ # @param node [Nokogiri::XML::Node] the node this element wraps
11
+ # @param document [Document] this element's document
12
+ #
11
13
  def initialize(node:, document: nil)
12
14
  super(node: node,
13
15
  document: document,
14
- enumerator_class: NoteElementEnumerator,
15
- short_type: :note)
16
+ enumerator_class: NoteElementEnumerator)
16
17
  end
17
18
 
19
+ # Returns the short type
20
+ # @return [Symbol]
21
+ #
22
+ def self.short_type
23
+ :note
24
+ end
25
+
26
+ # Returns the note's title element
27
+ #
28
+ # @return [Element, nil]
29
+ #
18
30
  def title
19
31
  block_error_if(block_given?)
20
32
  first("[data-type='title']")
21
33
  end
22
34
 
35
+ # Returns true if the note's title is autogenerated
36
+ #
37
+ # @return [Boolean]
38
+ #
23
39
  def indicates_autogenerated_title?
24
- translation_key_in(TITLE_TRANSLATION_KEYS).present?
40
+ detected_note_title_key != 0 && detected_note_title_key.present?
25
41
  end
26
42
 
43
+ # Get the autogenerated title for this note
44
+ #
45
+ # @return [String]
46
+ #
27
47
  def autogenerated_title
28
- translation_key = translation_key_in(TITLE_TRANSLATION_KEYS)
29
- I18n.t(:"notes.#{document.short_name}.#{translation_key}", default: :"notes.#{translation_key}")
48
+ if indicates_autogenerated_title?
49
+ I18n.t(:"notes.#{detected_note_title_key}")
50
+ else
51
+ "unknown title for note with classes #{classes}"
52
+ end
30
53
  end
31
54
 
32
- def translation_key_in(possible_translation_keys)
33
- keys = possible_translation_keys & classes
34
- raise("too many translation keys: #{keys.join(', ')}") if keys.many?
35
- keys.first
36
- end
55
+ protected
37
56
 
38
- def self.is_the_element_class_for?(node)
39
- node['data-type'] == "note"
40
- end
57
+ def detected_note_title_key
58
+ @detected_note_title_key ||= begin
59
+ return 0 if classes.empty? || !I18n.t('.').key?(:notes)
60
+
61
+ possible_keys = I18n.t(:notes).keys.map(&:to_s)
62
+ keys = possible_keys & classes
41
63
 
64
+ raise("too many translation keys: #{keys.join(', ')}") if keys.many?
65
+ return 0 if keys.empty?
66
+
67
+ keys.first
68
+ end
69
+ end
42
70
  end
43
71
  end