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.
- checksums.yaml +4 -4
- data/.devcontainer/devcontainer.json +37 -17
- data/.github/config.yml +14 -0
- data/.github/workflows/tests.yml +5 -15
- data/.gitignore +2 -2
- data/.inch.yml +6 -0
- data/.rubocop.yml +65 -0
- data/CHANGELOG.md +85 -1
- data/Gemfile +5 -3
- data/Gemfile.lock +65 -17
- data/README.md +65 -11
- data/Rakefile +5 -3
- data/bin/console +4 -3
- data/docker/Dockerfile +36 -0
- data/docker/Dockerfile.ci +10 -0
- data/docker/bash +5 -1
- data/docker/build +10 -0
- data/docker/ci +15 -0
- data/docker/run +9 -0
- data/docker/tag_and_push_latest +17 -0
- data/lefthook.yml +6 -0
- data/lib/kitchen/ancestor.rb +38 -1
- data/lib/kitchen/book_document.rb +20 -2
- data/lib/kitchen/book_element.rb +40 -5
- data/lib/kitchen/book_element_enumerator.rb +4 -0
- data/lib/kitchen/book_recipe.rb +15 -1
- data/lib/kitchen/chapter_element.rb +43 -6
- data/lib/kitchen/chapter_element_enumerator.rb +9 -1
- data/lib/kitchen/clipboard.rb +35 -4
- data/lib/kitchen/composite_chapter_element.rb +21 -6
- data/lib/kitchen/composite_page_element.rb +35 -7
- data/lib/kitchen/composite_page_element_enumerator.rb +9 -1
- data/lib/kitchen/config.rb +20 -6
- data/lib/kitchen/counter.rb +9 -2
- data/lib/kitchen/debug/print_recipe_error.rb +53 -35
- data/lib/kitchen/directions/.rubocop.yml +22 -0
- data/lib/kitchen/directions/bake_appendix.rb +4 -4
- data/lib/kitchen/directions/bake_chapter_glossary/main.rb +18 -0
- data/lib/kitchen/directions/bake_chapter_glossary/v1.rb +30 -0
- data/lib/kitchen/directions/bake_chapter_introductions.rb +7 -7
- data/lib/kitchen/directions/bake_chapter_key_concepts/main.rb +16 -0
- data/lib/kitchen/directions/bake_chapter_key_concepts/v1.rb +35 -0
- data/lib/kitchen/directions/bake_chapter_key_equations.rb +30 -20
- data/lib/kitchen/directions/bake_chapter_references/main.rb +16 -0
- data/lib/kitchen/directions/bake_chapter_references/v1.rb +35 -0
- data/lib/kitchen/directions/bake_chapter_section_exercises/main.rb +11 -0
- data/lib/kitchen/directions/bake_chapter_section_exercises/v1.rb +28 -0
- data/lib/kitchen/directions/bake_chapter_summary.rb +45 -36
- data/lib/kitchen/directions/bake_chapter_title/main.rb +11 -0
- data/lib/kitchen/directions/bake_chapter_title/v1.rb +24 -0
- data/lib/kitchen/directions/bake_checkpoint.rb +44 -0
- data/lib/kitchen/directions/bake_composite_chapters.rb +14 -0
- data/lib/kitchen/directions/bake_composite_pages.rb +2 -2
- data/lib/kitchen/directions/bake_equations.rb +37 -0
- data/lib/kitchen/directions/bake_example.rb +39 -11
- data/lib/kitchen/directions/bake_figure.rb +8 -5
- data/lib/kitchen/directions/bake_first_elements.rb +16 -0
- data/lib/kitchen/directions/bake_footnotes/main.rb +2 -2
- data/lib/kitchen/directions/bake_footnotes/v1.rb +6 -5
- data/lib/kitchen/directions/bake_free_response/free_response.xhtml.erb +10 -0
- data/lib/kitchen/directions/bake_free_response/main.rb +11 -0
- data/lib/kitchen/directions/bake_free_response/v1.rb +29 -0
- data/lib/kitchen/directions/bake_further_research.rb +59 -0
- data/lib/kitchen/directions/bake_index/main.rb +2 -2
- data/lib/kitchen/directions/bake_index/v1.rb +46 -18
- data/lib/kitchen/directions/bake_link_placeholders.rb +24 -0
- data/lib/kitchen/directions/bake_math_in_paragraph.rb +5 -3
- data/lib/kitchen/directions/bake_non_introduction_pages.rb +26 -0
- data/lib/kitchen/directions/bake_notes/bake_autotitled_notes.rb +29 -0
- data/lib/kitchen/directions/bake_notes/bake_note_subtitle.rb +22 -0
- data/lib/kitchen/directions/bake_notes/bake_numbered_notes.rb +51 -0
- data/lib/kitchen/directions/bake_notes/bake_unclassified_notes.rb +30 -0
- data/lib/kitchen/directions/bake_numbered_exercise/main.rb +15 -0
- data/lib/kitchen/directions/bake_numbered_exercise/v1.rb +47 -0
- data/lib/kitchen/directions/bake_numbered_table/main.rb +4 -4
- data/lib/kitchen/directions/bake_numbered_table/v1.rb +37 -18
- data/lib/kitchen/directions/bake_page_abstracts.rb +30 -0
- data/lib/kitchen/directions/bake_preface/main.rb +11 -0
- data/lib/kitchen/directions/bake_preface/v1.rb +18 -0
- data/lib/kitchen/directions/bake_references/main.rb +16 -0
- data/lib/kitchen/directions/bake_references/v1.rb +48 -0
- data/lib/kitchen/directions/bake_stepwise.rb +8 -12
- data/lib/kitchen/directions/bake_suggested_reading.rb +31 -0
- data/lib/kitchen/directions/bake_theorem/main.rb +11 -0
- data/lib/kitchen/directions/bake_theorem/v1.rb +28 -0
- data/lib/kitchen/directions/bake_toc.rb +49 -22
- data/lib/kitchen/directions/bake_unit_title/main.rb +11 -0
- data/lib/kitchen/directions/bake_unit_title/v1.rb +23 -0
- data/lib/kitchen/directions/bake_unnumbered_tables.rb +7 -5
- data/lib/kitchen/directions/book_answer_key_container/eob_solutions_container.xhtml.erb +9 -0
- data/lib/kitchen/directions/book_answer_key_container/main.rb +11 -0
- data/lib/kitchen/directions/book_answer_key_container/v1.rb +13 -0
- data/lib/kitchen/directions/chapter_review_container/chapter_review.xhtml.erb +9 -0
- data/lib/kitchen/directions/chapter_review_container/main.rb +11 -0
- data/lib/kitchen/directions/chapter_review_container/v1.rb +13 -0
- data/lib/kitchen/directions/eoc_section_title_link_snippet.rb +20 -0
- data/lib/kitchen/directions/move_exercises_to_eoc/main.rb +27 -0
- data/lib/kitchen/directions/move_exercises_to_eoc/v1.rb +36 -0
- data/lib/kitchen/directions/move_exercises_to_eoc/v2.rb +49 -0
- data/lib/kitchen/directions/move_solutions_to_answer_key/main.rb +14 -0
- data/lib/kitchen/directions/move_solutions_to_answer_key/strategies/american_government.rb +19 -0
- data/lib/kitchen/directions/move_solutions_to_answer_key/strategies/calculus.rb +41 -0
- data/lib/kitchen/directions/move_solutions_to_answer_key/strategies/uphysics.rb +63 -0
- data/lib/kitchen/directions/move_solutions_to_answer_key/v1.rb +34 -0
- data/lib/kitchen/directions/move_title_text_into_span.rb +2 -2
- data/lib/kitchen/document.rb +83 -13
- data/lib/kitchen/element.rb +20 -3
- data/lib/kitchen/element_base.rb +373 -63
- data/lib/kitchen/element_enumerator.rb +8 -0
- data/lib/kitchen/element_enumerator_base.rb +297 -28
- data/lib/kitchen/element_enumerator_factory.rb +64 -53
- data/lib/kitchen/element_factory.rb +27 -12
- data/lib/kitchen/errors.rb +5 -0
- data/lib/kitchen/example_element.rb +21 -6
- data/lib/kitchen/example_element_enumerator.rb +9 -1
- data/lib/kitchen/exercise_element.rb +42 -0
- data/lib/kitchen/exercise_element_enumerator.rb +21 -0
- data/lib/kitchen/figure_element.rb +36 -5
- data/lib/kitchen/figure_element_enumerator.rb +9 -1
- data/lib/kitchen/metadata_element.rb +34 -0
- data/lib/kitchen/metadata_element_enumerator.rb +21 -0
- data/lib/kitchen/mixins/block_error_if.rb +24 -4
- data/lib/kitchen/note_element.rb +48 -20
- data/lib/kitchen/note_element_enumerator.rb +9 -1
- data/lib/kitchen/oven.rb +66 -15
- data/lib/kitchen/page_element.rb +84 -14
- data/lib/kitchen/page_element_enumerator.rb +9 -1
- data/lib/kitchen/pantry.rb +28 -1
- data/lib/kitchen/patches/nokogiri.rb +59 -2
- data/lib/kitchen/patches/renderable.rb +9 -3
- data/lib/kitchen/patches/string.rb +8 -0
- data/lib/kitchen/recipe.rb +69 -32
- data/lib/kitchen/reference_element.rb +27 -0
- data/lib/kitchen/references_element_enumerator.rb +20 -0
- data/lib/kitchen/search_history.rb +43 -4
- data/lib/kitchen/search_query.rb +106 -0
- data/lib/kitchen/selector.rb +24 -0
- data/lib/kitchen/selectors/base.rb +65 -0
- data/lib/kitchen/selectors/standard_1.rb +21 -0
- data/lib/kitchen/table_element.rb +55 -7
- data/lib/kitchen/table_element_enumerator.rb +9 -1
- data/lib/kitchen/templates/eob_section_title_template.xhtml.erb +10 -0
- data/lib/kitchen/templates/eoc_section_title_template.xhtml.erb +10 -0
- data/lib/kitchen/term_element.rb +15 -4
- data/lib/kitchen/term_element_enumerator.rb +9 -1
- data/lib/kitchen/transliterations.rb +7 -5
- data/lib/kitchen/type_casting_element_enumerator.rb +17 -1
- data/lib/kitchen/unit_element.rb +45 -0
- data/lib/kitchen/unit_element_enumerator.rb +20 -0
- data/lib/kitchen/utils.rb +10 -13
- data/lib/kitchen/version.rb +5 -1
- data/lib/locales/en.yml +18 -7
- data/lib/locales/pl.yml +24 -0
- data/lib/openstax_kitchen.rb +44 -42
- data/openstax_kitchen.gemspec +26 -20
- data/tutorials/00/solution1.rb +9 -0
- data/tutorials/00/solution2.rb +8 -0
- data/tutorials/01/solution1.rb +18 -0
- data/tutorials/01/solution2.rb +26 -0
- data/tutorials/02/solution1.rb +31 -0
- data/tutorials/03/{solution_1.rb → solution1.rb} +6 -4
- data/tutorials/03/solution2.rb +18 -0
- data/tutorials/04/{solution_1.rb → solution1.rb} +4 -2
- data/tutorials/04/{solution_2.rb → solution2.rb} +6 -4
- data/tutorials/05/solution1.rb +11 -0
- data/tutorials/check_it +16 -15
- data/tutorials/setup_my_recipes +7 -6
- metadata +149 -24
- data/Dockerfile +0 -19
- data/docker-compose.yml +0 -12
- data/docker/entrypoint +0 -9
- data/lib/kitchen/directions/bake_chapter_glossary.rb +0 -34
- data/lib/kitchen/directions/bake_exercises.rb +0 -164
- data/lib/kitchen/directions/bake_notes.rb +0 -58
- data/tutorials/00/solution_1.rb +0 -7
- data/tutorials/00/solution_2.rb +0 -6
- data/tutorials/01/solution_1.rb +0 -16
- data/tutorials/01/solution_2.rb +0 -24
- data/tutorials/02/solution_1.rb +0 -29
- data/tutorials/03/solution_2.rb +0 -15
- data/tutorials/05/solution_1.rb +0 -9
@@ -1,9 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Kitchen
|
4
|
+
# An enumerator for note elements
|
5
|
+
#
|
2
6
|
class NoteElementEnumerator < 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:
|
14
|
+
default_css_or_xpath: Selector.named(:note),
|
7
15
|
sub_element_class: NoteElement,
|
8
16
|
enumerator_class: self
|
9
17
|
)
|
data/lib/kitchen/oven.rb
CHANGED
@@ -1,11 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Kitchen
|
4
|
+
# A class for baking documents according to the instructions in recipes
|
5
|
+
#
|
2
6
|
class Oven
|
3
7
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
8
|
+
# Bakes an input file using a recipe to produce an output file
|
9
|
+
#
|
10
|
+
# @param input_file [String] the path to the input file
|
11
|
+
# @param config_file [String] the path to the configuration file
|
12
|
+
# @param recipes [Array<Recipe>] an array of recipes with which to bake the document
|
13
|
+
# @param output_file [String] the path to the output file
|
14
|
+
#
|
15
|
+
def self.bake(input_file:, recipes:, output_file:, config_file: nil)
|
9
16
|
profile = BakeProfile.new
|
10
17
|
profile.started!
|
11
18
|
|
@@ -27,32 +34,76 @@ module Kitchen
|
|
27
34
|
end
|
28
35
|
profile.baked!
|
29
36
|
|
30
|
-
File.open(output_file,
|
31
|
-
f.write doc.to_xhtml(indent:2)
|
37
|
+
File.open(output_file, 'w') do |f|
|
38
|
+
f.write doc.to_xhtml(indent: 2)
|
32
39
|
end
|
33
40
|
profile.written!
|
34
41
|
|
35
42
|
profile
|
36
43
|
end
|
37
44
|
|
45
|
+
# Stats on baking
|
46
|
+
#
|
38
47
|
class BakeProfile
|
48
|
+
# Record that baking has started
|
39
49
|
def started!; @started_at = Time.now; end
|
50
|
+
|
51
|
+
# Record that the input file has been opened
|
40
52
|
def opened!; @opened_at = Time.now; end
|
53
|
+
|
54
|
+
# Record that the input file has been parsed
|
41
55
|
def parsed!; @parsed_at = Time.now; end
|
56
|
+
|
57
|
+
# Record that the input file has been baked
|
42
58
|
def baked!; @baked_at = Time.now; end
|
59
|
+
|
60
|
+
# Record that the output file has been written
|
43
61
|
def written!; @written_at = Time.now; end
|
44
62
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
def
|
63
|
+
# Return the number of seconds it took to open the input file or nil if this
|
64
|
+
# info isn't available.
|
65
|
+
# @return [Float, nil]
|
66
|
+
def open_seconds
|
67
|
+
@opened_at - @started_at
|
68
|
+
rescue NoMethodError
|
69
|
+
nil
|
70
|
+
end
|
71
|
+
|
72
|
+
# Return the number of seconds it took to parse the input file after opening or
|
73
|
+
# nil if this info isn't available.
|
74
|
+
# @return [Float, nil]
|
75
|
+
def parse_seconds
|
76
|
+
@parsed_at - @opened_at
|
77
|
+
rescue NoMethodError
|
78
|
+
nil
|
79
|
+
end
|
80
|
+
|
81
|
+
# Return the number of seconds it took to bake the parsed file or nil if this
|
82
|
+
# info isn't available.
|
83
|
+
# @return [Float, nil]
|
84
|
+
def bake_seconds
|
85
|
+
@baked_at - @parsed_at
|
86
|
+
rescue NoMethodError
|
87
|
+
nil
|
88
|
+
end
|
89
|
+
|
90
|
+
# Return the number of seconds it took to write the baked file or nil if this
|
91
|
+
# info isn't available.
|
92
|
+
# @return [Float, nil]
|
93
|
+
def write_seconds
|
94
|
+
@written_at - @baked_at
|
95
|
+
rescue NoMethodError
|
96
|
+
nil
|
97
|
+
end
|
49
98
|
|
99
|
+
# Return the profile stats as a string
|
100
|
+
# @return [String]
|
50
101
|
def to_s
|
51
102
|
<<~STRING
|
52
|
-
Open: #{open_seconds} s
|
53
|
-
Parse: #{parse_seconds} s
|
54
|
-
Bake: #{bake_seconds} s
|
55
|
-
Write: #{write_seconds} s
|
103
|
+
Open: #{open_seconds || '??'} s
|
104
|
+
Parse: #{parse_seconds || '??'} s
|
105
|
+
Bake: #{bake_seconds || '??'} s
|
106
|
+
Write: #{write_seconds || '??'} s
|
56
107
|
STRING
|
57
108
|
end
|
58
109
|
end
|
data/lib/kitchen/page_element.rb
CHANGED
@@ -1,50 +1,120 @@
|
|
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,
|
7
|
-
enumerator_class: PageElementEnumerator
|
8
|
-
|
16
|
+
enumerator_class: PageElementEnumerator)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Returns the short type
|
20
|
+
# @return [Symbol]
|
21
|
+
#
|
22
|
+
def self.short_type
|
23
|
+
:page
|
9
24
|
end
|
10
25
|
|
26
|
+
# Returns the title element. This method is aware that the title of the
|
27
|
+
# introduction page moves during the baking process.
|
28
|
+
#
|
29
|
+
# @raise [ElementNotFoundError] if no matching element is found
|
30
|
+
# @return [Element]
|
31
|
+
#
|
11
32
|
def title
|
12
33
|
# The selector for intro titles changes during the baking process
|
13
|
-
first!(is_introduction? ?
|
14
|
-
|
15
|
-
|
34
|
+
first!(is_introduction? ? selectors.title_in_introduction_page : selectors.title_in_page)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns the title's text regardless of whether the title has been baked
|
38
|
+
#
|
39
|
+
# @return [String]
|
40
|
+
#
|
41
|
+
def title_text
|
42
|
+
title.children.one? ? title.text : title.first('.os-text').text
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns an enumerator for titles.
|
46
|
+
#
|
47
|
+
# @return [ElementEnumerator]
|
48
|
+
#
|
49
|
+
def titles
|
50
|
+
search("div[data-type='document-title']")
|
16
51
|
end
|
17
52
|
|
53
|
+
# Returns true if this page is an introduction
|
54
|
+
#
|
55
|
+
# @return [Boolean]
|
56
|
+
#
|
18
57
|
def is_introduction?
|
19
|
-
has_class?(
|
58
|
+
has_class?('introduction')
|
20
59
|
end
|
21
60
|
|
61
|
+
# Returns true if this page is a preface
|
62
|
+
#
|
63
|
+
# @return [Boolean]
|
64
|
+
#
|
22
65
|
def is_preface?
|
23
|
-
has_class?(
|
66
|
+
has_class?('preface')
|
24
67
|
end
|
25
68
|
|
69
|
+
# Returns true if this page is an appendix
|
70
|
+
#
|
71
|
+
# @return [Boolean]
|
72
|
+
#
|
26
73
|
def is_appendix?
|
27
|
-
has_class?(
|
74
|
+
has_class?('appendix')
|
28
75
|
end
|
29
76
|
|
77
|
+
# Returns the metadata element.
|
78
|
+
#
|
79
|
+
# @raise [ElementNotFoundError] if no matching element is found
|
80
|
+
# @return [Element]
|
81
|
+
#
|
30
82
|
def metadata
|
31
83
|
first!("div[data-type='metadata']")
|
32
84
|
end
|
33
85
|
|
86
|
+
# Returns the summary element.
|
87
|
+
#
|
88
|
+
# @raise [ElementNotFoundError] if no matching element is found
|
89
|
+
# @return [Element]
|
90
|
+
#
|
34
91
|
def summary
|
35
|
-
first!(
|
92
|
+
first!(selectors.page_summary)
|
36
93
|
end
|
37
94
|
|
95
|
+
# Returns the exercises element.
|
96
|
+
#
|
97
|
+
# @raise [ElementNotFoundError] if no matching element is found
|
98
|
+
# @return [Element]
|
99
|
+
#
|
38
100
|
def exercises
|
39
|
-
first!(
|
101
|
+
first!('section.exercises')
|
40
102
|
end
|
41
103
|
|
42
|
-
|
43
|
-
|
104
|
+
# Returns the key concepts
|
105
|
+
#
|
106
|
+
# @return [Element]
|
107
|
+
#
|
108
|
+
def key_concepts
|
109
|
+
search('section.key-concepts')
|
44
110
|
end
|
45
111
|
|
46
|
-
|
47
|
-
|
112
|
+
# Returns the free response questions
|
113
|
+
#
|
114
|
+
# @return [Element]
|
115
|
+
#
|
116
|
+
def free_response
|
117
|
+
search('section.free-response')
|
48
118
|
end
|
49
119
|
|
50
120
|
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:
|
14
|
+
default_css_or_xpath: Selector.named(:page),
|
7
15
|
sub_element_class: PageElement,
|
8
16
|
enumerator_class: self
|
9
17
|
)
|
data/lib/kitchen/pantry.rb
CHANGED
@@ -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
|
-
|
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,
|
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,10 +33,56 @@ 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
|
46
|
+
|
47
|
+
def quick_matches?(selector)
|
48
|
+
self.class.selector_to_css_nodes(selector).any? { |css_node| matches_css_node?(css_node) }
|
49
|
+
end
|
50
|
+
|
51
|
+
def classes
|
52
|
+
self[:class]&.split || []
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.selector_to_css_nodes(selector)
|
56
|
+
# No need to parse the same selector more than once.
|
57
|
+
@parsed_selectors ||= {}
|
58
|
+
@parsed_selectors[selector] ||= Nokogiri::CSS::Parser.new.parse(selector)
|
59
|
+
end
|
60
|
+
|
61
|
+
protected
|
62
|
+
|
63
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
64
|
+
def matches_css_node?(css_node)
|
65
|
+
case css_node.type
|
66
|
+
when :CONDITIONAL_SELECTOR
|
67
|
+
css_node.value.all? { |inner_css_node| matches_css_node?(inner_css_node) }
|
68
|
+
when :ELEMENT_NAME
|
69
|
+
css_node.value == ['*'] || css_node.value.include?(name)
|
70
|
+
when :CLASS_CONDITION
|
71
|
+
(css_node.value & classes).any?
|
72
|
+
when :ATTRIBUTE_CONDITION
|
73
|
+
attribute, operator, value = css_node.value
|
74
|
+
|
75
|
+
raise "Unknown attribute condition operator in #{css_node}" if operator != :equal
|
76
|
+
|
77
|
+
attribute_name = attribute.value
|
78
|
+
raise "More attribute names than expected, #{attribute_name}" if attribute_name.many?
|
79
|
+
|
80
|
+
self[attribute_name.first] == value.gsub('"', '').gsub("'", '')
|
81
|
+
else
|
82
|
+
raise "Unknown Nokogiri::CSS:Node type in #{css_node}"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
29
86
|
end
|
30
87
|
end
|
31
88
|
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
|