openstax_kitchen 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) 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 +1 -1
  6. data/.inch.yml +6 -0
  7. data/.rubocop.yml +65 -0
  8. data/CHANGELOG.md +16 -0
  9. data/Gemfile +5 -3
  10. data/Gemfile.lock +54 -5
  11. data/README.md +58 -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 +16 -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 +24 -3
  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 -3
  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 +20 -1
  31. data/lib/kitchen/composite_page_element.rb +25 -2
  32. data/lib/kitchen/composite_page_element_enumerator.rb +8 -0
  33. data/lib/kitchen/config.rb +14 -7
  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.rb +10 -7
  39. data/lib/kitchen/directions/bake_chapter_introductions.rb +6 -6
  40. data/lib/kitchen/directions/bake_chapter_key_equations.rb +9 -6
  41. data/lib/kitchen/directions/bake_chapter_summary.rb +16 -13
  42. data/lib/kitchen/directions/bake_chapter_title/main.rb +11 -0
  43. data/lib/kitchen/directions/bake_chapter_title/v1.rb +24 -0
  44. data/lib/kitchen/directions/bake_composite_pages.rb +2 -2
  45. data/lib/kitchen/directions/bake_example.rb +6 -4
  46. data/lib/kitchen/directions/bake_exercises/main.rb +11 -0
  47. data/lib/kitchen/directions/bake_exercises/v1.rb +166 -0
  48. data/lib/kitchen/directions/bake_figure.rb +8 -5
  49. data/lib/kitchen/directions/bake_footnotes/main.rb +2 -2
  50. data/lib/kitchen/directions/bake_footnotes/v1.rb +4 -4
  51. data/lib/kitchen/directions/bake_index/main.rb +2 -2
  52. data/lib/kitchen/directions/bake_index/v1.rb +22 -15
  53. data/lib/kitchen/directions/bake_link_placeholders.rb +24 -0
  54. data/lib/kitchen/directions/bake_math_in_paragraph.rb +5 -3
  55. data/lib/kitchen/directions/bake_notes.rb +8 -8
  56. data/lib/kitchen/directions/bake_numbered_table/main.rb +2 -2
  57. data/lib/kitchen/directions/bake_numbered_table/v1.rb +21 -16
  58. data/lib/kitchen/directions/bake_page_abstracts.rb +14 -0
  59. data/lib/kitchen/directions/bake_preface/main.rb +11 -0
  60. data/lib/kitchen/directions/bake_preface/v1.rb +18 -0
  61. data/lib/kitchen/directions/bake_stepwise.rb +7 -7
  62. data/lib/kitchen/directions/bake_suggested_reading.rb +26 -0
  63. data/lib/kitchen/directions/bake_toc.rb +41 -22
  64. data/lib/kitchen/directions/bake_unit_title/main.rb +11 -0
  65. data/lib/kitchen/directions/bake_unit_title/v1.rb +23 -0
  66. data/lib/kitchen/directions/bake_unnumbered_tables.rb +7 -5
  67. data/lib/kitchen/directions/move_title_text_into_span.rb +2 -2
  68. data/lib/kitchen/document.rb +72 -13
  69. data/lib/kitchen/element.rb +11 -0
  70. data/lib/kitchen/element_base.rb +276 -56
  71. data/lib/kitchen/element_enumerator.rb +8 -0
  72. data/lib/kitchen/element_enumerator_base.rb +210 -28
  73. data/lib/kitchen/element_enumerator_factory.rb +59 -52
  74. data/lib/kitchen/element_factory.rb +27 -12
  75. data/lib/kitchen/errors.rb +5 -0
  76. data/lib/kitchen/example_element.rb +19 -1
  77. data/lib/kitchen/example_element_enumerator.rb +9 -1
  78. data/lib/kitchen/figure_element.rb +36 -2
  79. data/lib/kitchen/figure_element_enumerator.rb +9 -1
  80. data/lib/kitchen/metadata_element.rb +28 -0
  81. data/lib/kitchen/metadata_element_enumerator.rb +21 -0
  82. data/lib/kitchen/mixins/block_error_if.rb +24 -4
  83. data/lib/kitchen/note_element.rb +37 -7
  84. data/lib/kitchen/note_element_enumerator.rb +9 -1
  85. data/lib/kitchen/oven.rb +66 -15
  86. data/lib/kitchen/page_element.rb +62 -13
  87. data/lib/kitchen/page_element_enumerator.rb +9 -1
  88. data/lib/kitchen/pantry.rb +28 -1
  89. data/lib/kitchen/patches/nokogiri.rb +19 -2
  90. data/lib/kitchen/patches/renderable.rb +9 -3
  91. data/lib/kitchen/patches/string.rb +8 -0
  92. data/lib/kitchen/recipe.rb +38 -34
  93. data/lib/kitchen/search_history.rb +43 -4
  94. data/lib/kitchen/search_query.rb +84 -0
  95. data/lib/kitchen/selectors/base.rb +26 -0
  96. data/lib/kitchen/selectors/standard_1.rb +8 -0
  97. data/lib/kitchen/table_element.rb +54 -3
  98. data/lib/kitchen/table_element_enumerator.rb +9 -1
  99. data/lib/kitchen/term_element.rb +15 -1
  100. data/lib/kitchen/term_element_enumerator.rb +9 -1
  101. data/lib/kitchen/transliterations.rb +7 -5
  102. data/lib/kitchen/type_casting_element_enumerator.rb +17 -1
  103. data/lib/kitchen/unit_element.rb +39 -0
  104. data/lib/kitchen/unit_element_enumerator.rb +20 -0
  105. data/lib/kitchen/utils.rb +10 -13
  106. data/lib/kitchen/version.rb +5 -1
  107. data/lib/locales/en.yml +6 -0
  108. data/lib/openstax_kitchen.rb +43 -42
  109. data/openstax_kitchen.gemspec +26 -20
  110. data/tutorials/00/solution1.rb +9 -0
  111. data/tutorials/00/solution2.rb +8 -0
  112. data/tutorials/01/solution1.rb +18 -0
  113. data/tutorials/01/solution2.rb +26 -0
  114. data/tutorials/02/solution1.rb +31 -0
  115. data/tutorials/03/{solution_1.rb → solution1.rb} +6 -4
  116. data/tutorials/03/solution2.rb +18 -0
  117. data/tutorials/04/{solution_1.rb → solution1.rb} +4 -2
  118. data/tutorials/04/{solution_2.rb → solution2.rb} +6 -4
  119. data/tutorials/05/solution1.rb +11 -0
  120. data/tutorials/check_it +16 -15
  121. data/tutorials/setup_my_recipes +7 -6
  122. metadata +101 -22
  123. data/Dockerfile +0 -19
  124. data/docker-compose.yml +0 -12
  125. data/docker/entrypoint +0 -9
  126. data/lib/kitchen/directions/bake_exercises.rb +0 -164
  127. data/tutorials/00/solution_1.rb +0 -7
  128. data/tutorials/00/solution_2.rb +0 -6
  129. data/tutorials/01/solution_1.rb +0 -16
  130. data/tutorials/01/solution_2.rb +0 -24
  131. data/tutorials/02/solution_1.rb +0 -29
  132. data/tutorials/03/solution_2.rb +0 -15
  133. data/tutorials/05/solution_1.rb +0 -9
@@ -1,6 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Kitchen
4
+ # An element for a page
5
+ #
2
6
  class PageElement < ElementBase
3
7
 
8
+ # Creates a new +PageElement+
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,
@@ -8,43 +17,83 @@ module Kitchen
8
17
  short_type: :page)
9
18
  end
10
19
 
20
+ # Returns the title element. This method is aware that the title of the
21
+ # introduction page moves during the baking process.
22
+ #
23
+ # @raise [ElementNotFoundError] if no matching element is found
24
+ # @return [Element]
25
+ #
11
26
  def title
12
27
  # The selector for intro titles changes during the baking process
13
- first!(is_introduction? ?
14
- selectors.title_in_introduction_page :
15
- selectors.title_in_page)
28
+ first!(is_introduction? ? selectors.title_in_introduction_page : selectors.title_in_page)
29
+ end
30
+
31
+ # Returns an enumerator for titles.
32
+ #
33
+ # @return [ElementEnumerator]
34
+ #
35
+ def titles
36
+ search("div[data-type='document-title']")
16
37
  end
17
38
 
39
+ # Returns true if this page is an introduction
40
+ #
41
+ # @return [Boolean]
42
+ #
18
43
  def is_introduction?
19
- has_class?("introduction")
44
+ has_class?('introduction')
20
45
  end
21
46
 
47
+ # Returns true if this page is a preface
48
+ #
49
+ # @return [Boolean]
50
+ #
22
51
  def is_preface?
23
- has_class?("preface")
52
+ has_class?('preface')
24
53
  end
25
54
 
55
+ # Returns true if this page is an appendix
56
+ #
57
+ # @return [Boolean]
58
+ #
26
59
  def is_appendix?
27
- has_class?("appendix")
60
+ has_class?('appendix')
28
61
  end
29
62
 
63
+ # Returns the metadata element.
64
+ #
65
+ # @raise [ElementNotFoundError] if no matching element is found
66
+ # @return [Element]
67
+ #
30
68
  def metadata
31
69
  first!("div[data-type='metadata']")
32
70
  end
33
71
 
72
+ # Returns the summary element.
73
+ #
74
+ # @raise [ElementNotFoundError] if no matching element is found
75
+ # @return [Element]
76
+ #
34
77
  def summary
35
- first!("section.summary")
78
+ first!(selectors.page_summary)
36
79
  end
37
80
 
81
+ # Returns the exercises element.
82
+ #
83
+ # @raise [ElementNotFoundError] if no matching element is found
84
+ # @return [Element]
85
+ #
38
86
  def exercises
39
- first!("section.exercises")
40
- end
41
-
42
- def exercises_section
43
- search("")
87
+ first!('section.exercises')
44
88
  end
45
89
 
90
+ # Returns true if this class represents the element for the given node
91
+ #
92
+ # @param node [Nokogiri::XML::Node] the underlying node
93
+ # @return [Boolean]
94
+ #
46
95
  def self.is_the_element_class_for?(node)
47
- node['data-type'] == "page"
96
+ node['data-type'] == 'page'
48
97
  end
49
98
 
50
99
  end
@@ -1,9 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Kitchen
4
+ # An enumerator for page elements
5
+ #
2
6
  class PageElementEnumerator < 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='page']", # TODO get from config?
14
+ default_css_or_xpath: "div[data-type='page']", # TODO: get from config?
7
15
  sub_element_class: PageElement,
8
16
  enumerator_class: self
9
17
  )
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Kitchen
2
4
  # A place to store labeled items during recipe work. Essentially, a slightly
3
5
  # improved hash.
@@ -5,22 +7,47 @@ module Kitchen
5
7
  class Pantry
6
8
  include Enumerable
7
9
 
10
+ # Adds an item to the pantry with the provided label
11
+ #
12
+ # @param item [Object] something to store
13
+ # @param label [String, Symbol] a label with which to retrieve this item later.
8
14
  def store(item, label:)
9
15
  @hash[label.to_sym] = item
10
16
  end
11
17
 
18
+ # Get an item from the pantry
19
+ #
20
+ # @param label [String, Symbol] the item's label
21
+ # @return [Object]
22
+ #
12
23
  def get(label)
13
24
  @hash[label.to_sym]
14
25
  end
15
26
 
27
+ # Get an item from the pantry, raising if not present
28
+ #
29
+ # @param label [String, Symbol] the item's label
30
+ # @raise [RecipeError] if there's no item for the label
31
+ # @return [Object]
32
+ #
16
33
  def get!(label)
17
34
  get(label) || raise(RecipeError, "There is no pantry item labeled '#{label}'")
18
35
  end
19
36
 
37
+ # Iterate over the pantry items
38
+ #
39
+ # @yield Gives each label and item pair to the block
40
+ # @yieldparam label [Symbol] the item's label
41
+ # @yieldparam item [Object] the item
42
+ #
20
43
  def each(&block)
21
- @hash.each{|k,v| block.call(k,v)}
44
+ @hash.each { |k, v| block.call(k, v) }
22
45
  end
23
46
 
47
+ # Returns the number of items in the pantry
48
+ #
49
+ # @return [Integer]
50
+ #
24
51
  def size
25
52
  @hash.keys.size
26
53
  end
@@ -1,16 +1,27 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Make debug output more useful (dumping entire document out is not useful)
2
4
  module Nokogiri
3
5
  module XML
6
+ # Monkey patches for Nokogiri::XML::Document
7
+ # @see https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Document Nokogiri::XML::Document
4
8
  class Document
9
+ # Hides the guts of the document when printed out so you don't get 5MB dumped into your
10
+ # terminal
11
+ #
5
12
  def inspect
6
- "Nokogiri::XML::Document <hidden for brevity>"
13
+ 'Nokogiri::XML::Document <hidden for brevity>'
7
14
  end
8
15
 
16
+ # Alphabetizes all attributes within the document, useful for comparing one
17
+ # document to another (since attribute order isn't meaningful)
18
+ #
9
19
  def alphabetize_attributes!
10
20
  traverse do |child|
11
21
  next if child.text? || child.document?
22
+
12
23
  child_attributes = child.attributes
13
- child_attributes.each do |key, value|
24
+ child_attributes.each do |key, _value|
14
25
  child.remove_attribute(key)
15
26
  end
16
27
  sorted_keys = child_attributes.keys.sort
@@ -22,7 +33,13 @@ module Nokogiri
22
33
  end
23
34
  end
24
35
 
36
+ # Monkey patches for Nokogiri::XML::Node
37
+ # @see https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Node Nokogiri::XML::Node
25
38
  class Node
39
+ # Calls to_s on the node
40
+ #
41
+ # @return [String]
42
+ #
26
43
  def inspect
27
44
  to_s
28
45
  end
@@ -1,6 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Monkey patches for +Object+
4
+ #
1
5
  class Object
2
6
 
3
7
  # Adds a `render` method to a class for rendering an ERB template to a string.
8
+ #
9
+ # @param dir [String] a directory in which to find the template to be rendered,
10
+ # populated with a guess from the call stack if not provided.
4
11
  def self.renderable(dir: nil)
5
12
  dir ||= begin
6
13
  this_patch_file = __FILE__
@@ -8,11 +15,11 @@ class Object
8
15
  location.absolute_path == this_patch_file
9
16
  end
10
17
 
11
- location_that_called_renderable = caller_locations[(this_patch_file_caller_index || -1)+1]
18
+ location_that_called_renderable = caller_locations[(this_patch_file_caller_index || -1) + 1]
12
19
  File.dirname(location_that_called_renderable.path)
13
20
  end
14
21
 
15
- class_eval <<~METHOD
22
+ class_eval <<~METHOD, __FILE__, __LINE__ + 1
16
23
  def renderable_base_dir
17
24
  "#{dir}"
18
25
  end
@@ -25,7 +32,6 @@ class Object
25
32
  ERB.new(template).result(binding)
26
33
  end
27
34
  end
28
-
29
35
  end
30
36
 
31
37
  end
@@ -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,24 @@
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
+ # Sets the document so the recipe can yield it for modification
18
+ #
19
+ # @param document [Document] the document to modify
20
+ # @raise [StandardError] if not passed supported document type
21
+ #
7
22
  def document=(document)
8
23
  @document =
9
24
  case document
@@ -16,57 +31,46 @@ module Kitchen
16
31
 
17
32
  # Make a new Recipe
18
33
  #
34
+ # @yield A block for defining the steps of the recipe
19
35
  # @yieldparam doc [Document] an object representing an XML document
20
36
  #
21
37
  def initialize(&block)
38
+ raise(RecipeError, 'Recipes must be initialized with a block') unless block_given?
39
+
22
40
  @source_location = block.source_location[0]
23
41
  @block = block
24
42
  end
25
43
 
26
- def node!
27
- node || raise("The recipe's node has not been set")
28
- end
29
-
44
+ # Executes the block given to +Recipe.new+ on the document. Aka, does the baking.
45
+ #
30
46
  def bake
31
- begin
32
- @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
- end
47
+ @block.to_proc.call(document)
48
+ rescue RecipeError, ElementNotFoundError, Nokogiri::CSS::SyntaxError => e
49
+ print_recipe_error_and_exit(e)
50
+ rescue ArgumentError, NoMethodError => e
51
+ raise unless any_stack_file_matches_source_location?(e)
52
+
53
+ print_recipe_error_and_exit(e)
54
+ rescue NameError => e
55
+ raise unless stack_starts_with_source_location?(e)
56
+
57
+ print_recipe_error_and_exit(e)
58
58
  end
59
59
 
60
60
  protected
61
61
 
62
- def if_stack_starts_with_source_location?(error)
62
+ def stack_starts_with_source_location?(error)
63
63
  error.backtrace.first.start_with?(source_location)
64
64
  end
65
65
 
66
- def if_any_stack_file_matches_source_location?(error)
67
- error.backtrace.any? {|entry| entry.start_with?(@source_location)}
66
+ def any_stack_file_matches_source_location?(error)
67
+ error.backtrace.any? { |entry| entry.start_with?(@source_location) }
68
68
  end
69
69
 
70
+ # Print the given recipe error and do a process exit
71
+ #
72
+ # @param error [RecipeError] the error
73
+ #
70
74
  def print_recipe_error_and_exit(error)
71
75
  Kitchen::Debug.print_recipe_error(error: error,
72
76
  source_location: source_location,
@@ -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