openstax_kitchen 2.0.0 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) 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 -1
  9. data/Gemfile +5 -3
  10. data/Gemfile.lock +65 -17
  11. data/README.md +65 -11
  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_references/main.rb +16 -0
  45. data/lib/kitchen/directions/bake_chapter_references/v1.rb +35 -0
  46. data/lib/kitchen/directions/bake_chapter_section_exercises/main.rb +11 -0
  47. data/lib/kitchen/directions/bake_chapter_section_exercises/v1.rb +28 -0
  48. data/lib/kitchen/directions/bake_chapter_summary.rb +45 -36
  49. data/lib/kitchen/directions/bake_chapter_title/main.rb +11 -0
  50. data/lib/kitchen/directions/bake_chapter_title/v1.rb +24 -0
  51. data/lib/kitchen/directions/bake_checkpoint.rb +44 -0
  52. data/lib/kitchen/directions/bake_composite_chapters.rb +14 -0
  53. data/lib/kitchen/directions/bake_composite_pages.rb +2 -2
  54. data/lib/kitchen/directions/bake_equations.rb +37 -0
  55. data/lib/kitchen/directions/bake_example.rb +39 -11
  56. data/lib/kitchen/directions/bake_figure.rb +8 -5
  57. data/lib/kitchen/directions/bake_first_elements.rb +16 -0
  58. data/lib/kitchen/directions/bake_footnotes/main.rb +2 -2
  59. data/lib/kitchen/directions/bake_footnotes/v1.rb +6 -5
  60. data/lib/kitchen/directions/bake_free_response/free_response.xhtml.erb +10 -0
  61. data/lib/kitchen/directions/bake_free_response/main.rb +11 -0
  62. data/lib/kitchen/directions/bake_free_response/v1.rb +29 -0
  63. data/lib/kitchen/directions/bake_further_research.rb +59 -0
  64. data/lib/kitchen/directions/bake_index/main.rb +2 -2
  65. data/lib/kitchen/directions/bake_index/v1.rb +46 -18
  66. data/lib/kitchen/directions/bake_link_placeholders.rb +24 -0
  67. data/lib/kitchen/directions/bake_math_in_paragraph.rb +5 -3
  68. data/lib/kitchen/directions/bake_non_introduction_pages.rb +26 -0
  69. data/lib/kitchen/directions/bake_notes/bake_autotitled_notes.rb +29 -0
  70. data/lib/kitchen/directions/bake_notes/bake_note_subtitle.rb +22 -0
  71. data/lib/kitchen/directions/bake_notes/bake_numbered_notes.rb +51 -0
  72. data/lib/kitchen/directions/bake_notes/bake_unclassified_notes.rb +30 -0
  73. data/lib/kitchen/directions/bake_numbered_exercise/main.rb +15 -0
  74. data/lib/kitchen/directions/bake_numbered_exercise/v1.rb +47 -0
  75. data/lib/kitchen/directions/bake_numbered_table/main.rb +4 -4
  76. data/lib/kitchen/directions/bake_numbered_table/v1.rb +37 -18
  77. data/lib/kitchen/directions/bake_page_abstracts.rb +30 -0
  78. data/lib/kitchen/directions/bake_preface/main.rb +11 -0
  79. data/lib/kitchen/directions/bake_preface/v1.rb +18 -0
  80. data/lib/kitchen/directions/bake_references/main.rb +16 -0
  81. data/lib/kitchen/directions/bake_references/v1.rb +48 -0
  82. data/lib/kitchen/directions/bake_stepwise.rb +8 -12
  83. data/lib/kitchen/directions/bake_suggested_reading.rb +31 -0
  84. data/lib/kitchen/directions/bake_theorem/main.rb +11 -0
  85. data/lib/kitchen/directions/bake_theorem/v1.rb +28 -0
  86. data/lib/kitchen/directions/bake_toc.rb +49 -22
  87. data/lib/kitchen/directions/bake_unit_title/main.rb +11 -0
  88. data/lib/kitchen/directions/bake_unit_title/v1.rb +23 -0
  89. data/lib/kitchen/directions/bake_unnumbered_tables.rb +7 -5
  90. data/lib/kitchen/directions/book_answer_key_container/eob_solutions_container.xhtml.erb +9 -0
  91. data/lib/kitchen/directions/book_answer_key_container/main.rb +11 -0
  92. data/lib/kitchen/directions/book_answer_key_container/v1.rb +13 -0
  93. data/lib/kitchen/directions/chapter_review_container/chapter_review.xhtml.erb +9 -0
  94. data/lib/kitchen/directions/chapter_review_container/main.rb +11 -0
  95. data/lib/kitchen/directions/chapter_review_container/v1.rb +13 -0
  96. data/lib/kitchen/directions/eoc_section_title_link_snippet.rb +20 -0
  97. data/lib/kitchen/directions/move_exercises_to_eoc/main.rb +27 -0
  98. data/lib/kitchen/directions/move_exercises_to_eoc/v1.rb +36 -0
  99. data/lib/kitchen/directions/move_exercises_to_eoc/v2.rb +49 -0
  100. data/lib/kitchen/directions/move_solutions_to_answer_key/main.rb +14 -0
  101. data/lib/kitchen/directions/move_solutions_to_answer_key/strategies/american_government.rb +19 -0
  102. data/lib/kitchen/directions/move_solutions_to_answer_key/strategies/calculus.rb +41 -0
  103. data/lib/kitchen/directions/move_solutions_to_answer_key/strategies/uphysics.rb +63 -0
  104. data/lib/kitchen/directions/move_solutions_to_answer_key/v1.rb +34 -0
  105. data/lib/kitchen/directions/move_title_text_into_span.rb +2 -2
  106. data/lib/kitchen/document.rb +83 -13
  107. data/lib/kitchen/element.rb +20 -3
  108. data/lib/kitchen/element_base.rb +373 -63
  109. data/lib/kitchen/element_enumerator.rb +8 -0
  110. data/lib/kitchen/element_enumerator_base.rb +297 -28
  111. data/lib/kitchen/element_enumerator_factory.rb +64 -53
  112. data/lib/kitchen/element_factory.rb +27 -12
  113. data/lib/kitchen/errors.rb +5 -0
  114. data/lib/kitchen/example_element.rb +21 -6
  115. data/lib/kitchen/example_element_enumerator.rb +9 -1
  116. data/lib/kitchen/exercise_element.rb +42 -0
  117. data/lib/kitchen/exercise_element_enumerator.rb +21 -0
  118. data/lib/kitchen/figure_element.rb +36 -5
  119. data/lib/kitchen/figure_element_enumerator.rb +9 -1
  120. data/lib/kitchen/metadata_element.rb +34 -0
  121. data/lib/kitchen/metadata_element_enumerator.rb +21 -0
  122. data/lib/kitchen/mixins/block_error_if.rb +24 -4
  123. data/lib/kitchen/note_element.rb +48 -20
  124. data/lib/kitchen/note_element_enumerator.rb +9 -1
  125. data/lib/kitchen/oven.rb +66 -15
  126. data/lib/kitchen/page_element.rb +84 -14
  127. data/lib/kitchen/page_element_enumerator.rb +9 -1
  128. data/lib/kitchen/pantry.rb +28 -1
  129. data/lib/kitchen/patches/nokogiri.rb +59 -2
  130. data/lib/kitchen/patches/renderable.rb +9 -3
  131. data/lib/kitchen/patches/string.rb +8 -0
  132. data/lib/kitchen/recipe.rb +69 -32
  133. data/lib/kitchen/reference_element.rb +27 -0
  134. data/lib/kitchen/references_element_enumerator.rb +20 -0
  135. data/lib/kitchen/search_history.rb +43 -4
  136. data/lib/kitchen/search_query.rb +106 -0
  137. data/lib/kitchen/selector.rb +24 -0
  138. data/lib/kitchen/selectors/base.rb +65 -0
  139. data/lib/kitchen/selectors/standard_1.rb +21 -0
  140. data/lib/kitchen/table_element.rb +55 -7
  141. data/lib/kitchen/table_element_enumerator.rb +9 -1
  142. data/lib/kitchen/templates/eob_section_title_template.xhtml.erb +10 -0
  143. data/lib/kitchen/templates/eoc_section_title_template.xhtml.erb +10 -0
  144. data/lib/kitchen/term_element.rb +15 -4
  145. data/lib/kitchen/term_element_enumerator.rb +9 -1
  146. data/lib/kitchen/transliterations.rb +7 -5
  147. data/lib/kitchen/type_casting_element_enumerator.rb +17 -1
  148. data/lib/kitchen/unit_element.rb +45 -0
  149. data/lib/kitchen/unit_element_enumerator.rb +20 -0
  150. data/lib/kitchen/utils.rb +10 -13
  151. data/lib/kitchen/version.rb +5 -1
  152. data/lib/locales/en.yml +18 -7
  153. data/lib/locales/pl.yml +24 -0
  154. data/lib/openstax_kitchen.rb +44 -42
  155. data/openstax_kitchen.gemspec +26 -20
  156. data/tutorials/00/solution1.rb +9 -0
  157. data/tutorials/00/solution2.rb +8 -0
  158. data/tutorials/01/solution1.rb +18 -0
  159. data/tutorials/01/solution2.rb +26 -0
  160. data/tutorials/02/solution1.rb +31 -0
  161. data/tutorials/03/{solution_1.rb → solution1.rb} +6 -4
  162. data/tutorials/03/solution2.rb +18 -0
  163. data/tutorials/04/{solution_1.rb → solution1.rb} +4 -2
  164. data/tutorials/04/{solution_2.rb → solution2.rb} +6 -4
  165. data/tutorials/05/solution1.rb +11 -0
  166. data/tutorials/check_it +16 -15
  167. data/tutorials/setup_my_recipes +7 -6
  168. metadata +149 -24
  169. data/Dockerfile +0 -19
  170. data/docker-compose.yml +0 -12
  171. data/docker/entrypoint +0 -9
  172. data/lib/kitchen/directions/bake_chapter_glossary.rb +0 -34
  173. data/lib/kitchen/directions/bake_exercises.rb +0 -164
  174. data/lib/kitchen/directions/bake_notes.rb +0 -58
  175. data/tutorials/00/solution_1.rb +0 -7
  176. data/tutorials/00/solution_2.rb +0 -6
  177. data/tutorials/01/solution_1.rb +0 -16
  178. data/tutorials/01/solution_2.rb +0 -24
  179. data/tutorials/02/solution_1.rb +0 -29
  180. data/tutorials/03/solution_2.rb +0 -15
  181. data/tutorials/05/solution_1.rb +0 -9
@@ -1,4 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Monkey patches for +String+
4
+ #
1
5
  class String
6
+ # Downcases the first letter of self, returning a new string
7
+ #
8
+ # @return [String]
9
+ #
2
10
  def uncapitalize
3
11
  sub(/^[A-Z]/, &:downcase)
4
12
  end
@@ -1,9 +1,28 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Kitchen
4
+ # An object that yields a +Document+ for modification (those modifications are
5
+ # the "recipe")
6
+ #
2
7
  class Recipe
3
8
 
9
+ # The document the recipe makes available for modification
10
+ # @return [Document]
4
11
  attr_reader :document
12
+
13
+ # The file location of the recipe
14
+ # @return [String]
5
15
  attr_reader :source_location
6
16
 
17
+ # An I18n backend specific to this recipe, may be nil
18
+ # @return [I18n::Backend::Simple, nil]
19
+ attr_reader :my_i18n_backend
20
+
21
+ # Sets the document so the recipe can yield it for modification
22
+ #
23
+ # @param document [Document] the document to modify
24
+ # @raise [StandardError] if not passed supported document type
25
+ #
7
26
  def document=(document)
8
27
  @document =
9
28
  case document
@@ -16,57 +35,75 @@ module Kitchen
16
35
 
17
36
  # Make a new Recipe
18
37
  #
38
+ # @param locales_dir [String, nil] the absolute path to a folder containing recipe-specific
39
+ # I18n translations. If not provided, Kitchen will look for a `locales` directory in the
40
+ # same directory as the recipe source. Recipe-specific translations override those in
41
+ # Kitchen. If no recipe-specific locales directory exists, Kitchen will just use its default
42
+ # translations.
43
+ # @yield A block for defining the steps of the recipe
19
44
  # @yieldparam doc [Document] an object representing an XML document
20
45
  #
21
- def initialize(&block)
46
+ def initialize(locales_dir: nil, &block)
47
+ raise(RecipeError, 'Recipes must be initialized with a block') unless block_given?
48
+
22
49
  @source_location = block.source_location[0]
23
50
  @block = block
24
- end
25
51
 
26
- def node!
27
- node || raise("The recipe's node has not been set")
52
+ load_my_i18n_backend(locales_dir)
28
53
  end
29
54
 
55
+ # Executes the block given to +Recipe.new+ on the document. Aka, does the baking.
56
+ #
30
57
  def bake
31
- begin
58
+ with_my_locales do
32
59
  @block.to_proc.call(document)
33
- rescue RecipeError => ee
34
- print_recipe_error_and_exit(ee)
35
- rescue ArgumentError => ee
36
- if if_any_stack_file_matches_source_location?(ee)
37
- print_recipe_error_and_exit(ee)
38
- else
39
- raise
40
- end
41
- rescue NoMethodError => ee
42
- if if_any_stack_file_matches_source_location?(ee)
43
- print_recipe_error_and_exit(ee)
44
- else
45
- raise
46
- end
47
- rescue NameError => ee
48
- if if_stack_starts_with_source_location?(ee)
49
- print_recipe_error_and_exit(ee)
50
- else
51
- raise
52
- end
53
- rescue ElementNotFoundError => ee
54
- print_recipe_error_and_exit(ee)
55
- rescue Nokogiri::CSS::SyntaxError => ee
56
- print_recipe_error_and_exit(ee)
57
60
  end
61
+ rescue RecipeError, ElementNotFoundError, Nokogiri::CSS::SyntaxError => e
62
+ print_recipe_error_and_exit(e)
63
+ rescue ArgumentError, NoMethodError => e
64
+ raise unless any_stack_file_matches_source_location?(e)
65
+
66
+ print_recipe_error_and_exit(e)
67
+ rescue NameError => e
68
+ raise unless stack_starts_with_source_location?(e)
69
+
70
+ print_recipe_error_and_exit(e)
58
71
  end
59
72
 
60
73
  protected
61
74
 
62
- def if_stack_starts_with_source_location?(error)
75
+ def stack_starts_with_source_location?(error)
63
76
  error.backtrace.first.start_with?(source_location)
64
77
  end
65
78
 
66
- def if_any_stack_file_matches_source_location?(error)
67
- error.backtrace.any? {|entry| entry.start_with?(@source_location)}
79
+ def any_stack_file_matches_source_location?(error)
80
+ error.backtrace.any? { |entry| entry.start_with?(@source_location) }
81
+ end
82
+
83
+ def load_my_i18n_backend(locales_dir)
84
+ locales_dir ||= begin
85
+ guessed_locales_dir = "#{File.dirname(@source_location)}/locales"
86
+ File.directory?(guessed_locales_dir) ? guessed_locales_dir : nil
87
+ end
88
+
89
+ return unless locales_dir
90
+
91
+ @my_i18n_backend = I18n::Backend::Simple.new
92
+ @my_i18n_backend.load_translations(Dir[File.expand_path("#{locales_dir}/*.yml")])
93
+ end
94
+
95
+ def with_my_locales
96
+ original_i18n_backend = I18n.backend
97
+ I18n.backend = I18n::Backend::Chain.new(my_i18n_backend, original_i18n_backend)
98
+ yield
99
+ ensure
100
+ I18n.backend = original_i18n_backend
68
101
  end
69
102
 
103
+ # Print the given recipe error and do a process exit
104
+ #
105
+ # @param error [RecipeError] the error
106
+ #
70
107
  def print_recipe_error_and_exit(error)
71
108
  Kitchen::Debug.print_recipe_error(error: error,
72
109
  source_location: source_location,
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen
4
+ # An element for an example
5
+ #
6
+ class ReferenceElement < ElementBase
7
+
8
+ # Creates a new +ReferenceElement+
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: ReferenceElementEnumerator)
17
+ end
18
+
19
+ # Returns the short type
20
+ # @return [Symbol]
21
+ #
22
+ def self.short_type
23
+ :reference
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen
4
+ # An enumerator for table elements
5
+ #
6
+ class ReferenceElementEnumerator < ElementEnumeratorBase
7
+ # Returns a factory for this enumerator
8
+ #
9
+ # @return [ElementEnumeratorFactory]
10
+ #
11
+ def self.factory
12
+ ElementEnumeratorFactory.new(
13
+ default_css_or_xpath: Selector.named(:reference),
14
+ sub_element_class: ReferenceElement,
15
+ enumerator_class: self
16
+ )
17
+ end
18
+
19
+ end
20
+ end
@@ -1,31 +1,70 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Kitchen
4
+ # Records the search history that was used to find a certain element
5
+ #
2
6
  class SearchHistory
3
7
  attr_reader :latest
4
8
  attr_reader :upstream
5
9
 
10
+ # Returns an empty search history
11
+ #
12
+ # @return [SearchHistory]
13
+ #
6
14
  def self.empty
7
15
  new
8
16
  end
9
17
 
10
- def add(css_or_xpath)
11
- self.class.new(self, css_or_xpath.nil? ? nil : [css_or_xpath].join(", "))
18
+ # Returns a new +SearchHistory+ that contains the current history plus the
19
+ # provided query
20
+ #
21
+ # @param search_query [SearchQuery] the search query to add to the history
22
+ # @return [SearchHistory]
23
+ #
24
+ def add(search_query)
25
+ search_query = SearchQuery.new(css_or_xpath: search_query) if search_query.is_a?(String)
26
+ self.class.new(self, search_query)
12
27
  end
13
28
 
14
- def to_s(missing_string="?")
15
- to_a.map{|item| "[#{item || missing_string}]"}.join(" ")
29
+ # Returns the history as a string
30
+ #
31
+ # @param missing_string [String] if there's a missing part of the history, this string
32
+ # is used in its place
33
+ # @return [String]
34
+ #
35
+ def to_s(missing_string='?')
36
+ array = to_a
37
+ array.shift while array.any? && array[0].nil?
38
+ array.map { |item| "[#{item || missing_string}]" }.join(' ')
16
39
  end
17
40
 
41
+ # Returns this instance as an array of selectors
42
+ #
43
+ # @return [Array<String>]
44
+ #
18
45
  def to_a
19
46
  empty? ? [] : [upstream&.to_a || [], latest].flatten
20
47
  end
21
48
 
49
+ # Returns true if the search history is empty
50
+ #
51
+ # @return [Boolean]
52
+ #
22
53
  def empty?
23
54
  upstream.nil? && latest.nil?
24
55
  end
25
56
 
26
57
  protected
27
58
 
59
+ # Create a new instance
60
+ #
61
+ # @param upstream [SearchHistory] prior search history
62
+ # @param latest [SearchQuery] the new history
63
+ #
28
64
  def initialize(upstream=nil, latest=nil)
65
+ raise 'Upstream must be a SearchHistory' unless upstream.nil? || upstream.is_a?(SearchHistory)
66
+ raise 'Latest must be a SearchQuery' unless latest.nil? || latest.is_a?(SearchQuery)
67
+
29
68
  @upstream = upstream
30
69
  @latest = latest
31
70
  end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen
4
+ # Records the search history that was used to find a certain element
5
+ #
6
+ class SearchQuery
7
+ attr_reader :css_or_xpath
8
+ attr_reader :only
9
+ attr_reader :except
10
+
11
+ # Create a new SearchQuery
12
+ #
13
+ # @param css_or_xpath [String, Array<String>] selectors to use to limit iteration results
14
+ # a "$" in this argument can be replaced with a default selector via
15
+ # #apply_default_css_or_xpath_and_normalize
16
+ # @param only [Symbol, Callable] the name of a method to call on an element or a
17
+ # lambda or proc that accepts an element; elements will only be included in the
18
+ # search results if the method or callable returns true
19
+ # @param except [Symbol, Callable] the name of a method to call on an element or a
20
+ # lambda or proc that accepts an element; elements will not be included in the
21
+ # search results if the method or callable returns false
22
+ #
23
+ def initialize(css_or_xpath: nil, only: nil, except: nil)
24
+ @css_or_xpath = css_or_xpath
25
+ @only = only.is_a?(String) ? only.to_sym : only
26
+ @except = except.is_a?(String) ? except.to_sym : except
27
+ @default_already_applied = false
28
+ end
29
+
30
+ # Returns true iff the element passes the `only` and `except` conditions
31
+ #
32
+ # @return [Boolean]
33
+ #
34
+ def conditions_match?(element)
35
+ condition_passes?(except, element, false) && condition_passes?(only, element, true)
36
+ end
37
+
38
+ # Replaces '$' in the `css_or_xpath` with the provided value; also normalizes
39
+ # `css_or_xpath` to an array.
40
+ #
41
+ # @param default_css_or_xpath [String, Proc, Symbol] The selectors to substitute for the "$" character
42
+ # when this factory is used to build an enumerator. A string argument is used literally. A proc
43
+ # is eventually called given the document's Config object (for accessing selectors). A symbol
44
+ # is interpreted as the name of a selector and is called on the document's Config object's
45
+ # selectors object.
46
+ #
47
+ def apply_default_css_or_xpath_and_normalize(default_css_or_xpath=nil, config: nil)
48
+ return if @default_already_applied
49
+
50
+ default_css_or_xpath = [default_css_or_xpath].flatten.map do |item|
51
+ case item
52
+ when Proc
53
+ item.call(config)
54
+ when Symbol
55
+ config.selectors.send(item)
56
+ else
57
+ item
58
+ end
59
+ end
60
+
61
+ @as_type = nil
62
+ @css_or_xpath = [css_or_xpath || '$'].flatten.map do |item|
63
+ item.gsub(/\$/, default_css_or_xpath.join(', '))
64
+ end
65
+
66
+ @default_already_applied = true
67
+ end
68
+
69
+ # Returns the search query as a spaceless string suitable for use as an element type
70
+ #
71
+ def as_type
72
+ @as_type ||= [
73
+ [css_or_xpath].flatten.join(','),
74
+ stringify_condition(only, 'only'),
75
+ stringify_condition(except, 'except')
76
+ ].compact.join(';')
77
+ end
78
+
79
+ # Returns a string representation of the search query
80
+ #
81
+ def to_s
82
+ as_type
83
+ end
84
+
85
+ protected
86
+
87
+ def condition_passes?(method_or_callable, element, success_outcome)
88
+ return true if method_or_callable.nil?
89
+
90
+ result =
91
+ if method_or_callable.is_a?(Symbol)
92
+ element.send(method_or_callable)
93
+ else
94
+ method_or_callable.call(element)
95
+ end
96
+
97
+ !!result == success_outcome
98
+ end
99
+
100
+ def stringify_condition(condition, name)
101
+ return nil unless condition
102
+
103
+ "#{name}:#{condition.is_a?(Symbol) ? condition : condition.source_location.join(':')}"
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kitchen
4
+ # A wrapper for a selector. Can be used as the default_css_or_xpath.
5
+ #
6
+ class Selector < Proc
7
+ attr_reader :name
8
+
9
+ def self.named(name)
10
+ new(name) { |config| config.selectors.send(name) }
11
+ end
12
+
13
+ def initialize(name, &block)
14
+ @name = name
15
+ super(&block)
16
+ end
17
+
18
+ def matches?(node, config:)
19
+ # This may not be incredibly efficient as it does a search of this node's
20
+ # ancestors to see if the node is in the results. Watch the performance.
21
+ node.quick_matches?(call(config))
22
+ end
23
+ end
24
+ end
@@ -1,8 +1,73 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Kitchen
4
+ # A centralized place to put common CSS selectors so they aren't sprinkled
5
+ # throughout the baseline (and so that they can be changed easily if needed)
6
+ #
2
7
  module Selectors
8
+ # Base class for different selector configurations
9
+ #
3
10
  class Base
11
+ # Selector for the title in a page
12
+ # @return [String]
4
13
  attr_accessor :title_in_page
14
+ # Selector for the title in an introduction page
15
+ # @return [String]
5
16
  attr_accessor :title_in_introduction_page
17
+ # Selector for the summary in a page
18
+ # @return [String]
19
+ attr_accessor :page_summary
20
+ # Selector for a reference
21
+ # @return [String]
22
+ attr_accessor :reference
23
+ # Selector for a chapter
24
+ # @return [String]
25
+ attr_accessor :chapter
26
+ # Selector for a page
27
+ # @return [String]
28
+ attr_accessor :page
29
+ # Selector for a note
30
+ # @return [String]
31
+ attr_accessor :note
32
+ # Selector for a term
33
+ # @return [String]
34
+ attr_accessor :term
35
+ # Selector for a table
36
+ # @return [String]
37
+ attr_accessor :table
38
+ # Selector for a figure
39
+ # @return [String]
40
+ attr_accessor :figure
41
+ # Selector for a metadata
42
+ # @return [String]
43
+ attr_accessor :metadata
44
+ # Selector for a composite page
45
+ # @return [String]
46
+ attr_accessor :composite_page
47
+ # Selector for a composite chapter
48
+ # @return [String]
49
+ attr_accessor :composite_chapter
50
+ # Selector for an example
51
+ # @return [String]
52
+ attr_accessor :example
53
+ # Selector for an exercise
54
+ # @return [String]
55
+ attr_accessor :exercise
56
+ # Selector for an unit
57
+ # @return [String]
58
+ attr_accessor :unit
59
+
60
+ # Override specific selectors
61
+ #
62
+ # @param hash [Hash] a hash of selectors to selector values, e.g. {title_in_page: '.title'}
63
+ # @return [Base] this object
64
+ #
65
+ def override(hash={})
66
+ hash.each do |selector, value|
67
+ send("#{selector}=", value)
68
+ end
69
+ self
70
+ end
6
71
  end
7
72
  end
8
73
  end