openstax_kitchen 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +7 -0
  2. data/.devcontainer/devcontainer.json +19 -0
  3. data/.github/workflows/tests.yml +36 -0
  4. data/.gitignore +20 -0
  5. data/.rspec +3 -0
  6. data/.solargraph.yml +15 -0
  7. data/CHANGELOG.md +11 -0
  8. data/CODE_OF_CONDUCT.md +74 -0
  9. data/Dockerfile +19 -0
  10. data/Gemfile +9 -0
  11. data/Gemfile.lock +74 -0
  12. data/LICENSE.txt +21 -0
  13. data/README.md +674 -0
  14. data/Rakefile +6 -0
  15. data/bin/console +14 -0
  16. data/bin/normalize +79 -0
  17. data/bin/setup +8 -0
  18. data/books/chemistry2e/bake.rb +133 -0
  19. data/codecov.yaml +27 -0
  20. data/docker-compose.yml +12 -0
  21. data/docker/bash +1 -0
  22. data/docker/entrypoint +9 -0
  23. data/lib/kitchen.rb +57 -0
  24. data/lib/kitchen/ancestor.rb +30 -0
  25. data/lib/kitchen/book_document.rb +18 -0
  26. data/lib/kitchen/book_element.rb +24 -0
  27. data/lib/kitchen/book_element_enumerator.rb +5 -0
  28. data/lib/kitchen/book_recipe.rb +25 -0
  29. data/lib/kitchen/chapter_element.rb +35 -0
  30. data/lib/kitchen/chapter_element_enumerator.rb +13 -0
  31. data/lib/kitchen/clipboard.rb +37 -0
  32. data/lib/kitchen/composite_chapter_element.rb +23 -0
  33. data/lib/kitchen/composite_page_element.rb +27 -0
  34. data/lib/kitchen/composite_page_element_enumerator.rb +13 -0
  35. data/lib/kitchen/config.rb +20 -0
  36. data/lib/kitchen/counter.rb +34 -0
  37. data/lib/kitchen/debug/print_recipe_error.rb +80 -0
  38. data/lib/kitchen/directions/bake_appendix.rb +26 -0
  39. data/lib/kitchen/directions/bake_chapter_glossary.rb +34 -0
  40. data/lib/kitchen/directions/bake_chapter_introductions.rb +58 -0
  41. data/lib/kitchen/directions/bake_chapter_key_equations.rb +30 -0
  42. data/lib/kitchen/directions/bake_chapter_summary.rb +52 -0
  43. data/lib/kitchen/directions/bake_composite_pages.rb +13 -0
  44. data/lib/kitchen/directions/bake_example.rb +31 -0
  45. data/lib/kitchen/directions/bake_exercises.rb +164 -0
  46. data/lib/kitchen/directions/bake_figure.rb +25 -0
  47. data/lib/kitchen/directions/bake_footnotes/main.rb +11 -0
  48. data/lib/kitchen/directions/bake_footnotes/v1.rb +38 -0
  49. data/lib/kitchen/directions/bake_index/main.rb +11 -0
  50. data/lib/kitchen/directions/bake_index/v1.rb +138 -0
  51. data/lib/kitchen/directions/bake_index/v1.xhtml.erb +28 -0
  52. data/lib/kitchen/directions/bake_math_in_paragraph.rb +13 -0
  53. data/lib/kitchen/directions/bake_notes.rb +58 -0
  54. data/lib/kitchen/directions/bake_numbered_table/main.rb +11 -0
  55. data/lib/kitchen/directions/bake_numbered_table/v1.rb +47 -0
  56. data/lib/kitchen/directions/bake_stepwise.rb +27 -0
  57. data/lib/kitchen/directions/bake_toc.rb +103 -0
  58. data/lib/kitchen/directions/bake_unnumbered_tables.rb +14 -0
  59. data/lib/kitchen/directions/move_title_text_into_span.rb +15 -0
  60. data/lib/kitchen/document.rb +142 -0
  61. data/lib/kitchen/element.rb +15 -0
  62. data/lib/kitchen/element_base.rb +444 -0
  63. data/lib/kitchen/element_enumerator.rb +12 -0
  64. data/lib/kitchen/element_enumerator_base.rb +101 -0
  65. data/lib/kitchen/element_enumerator_factory.rb +111 -0
  66. data/lib/kitchen/element_factory.rb +32 -0
  67. data/lib/kitchen/errors.rb +4 -0
  68. data/lib/kitchen/example_element.rb +20 -0
  69. data/lib/kitchen/example_element_enumerator.rb +13 -0
  70. data/lib/kitchen/figure_element.rb +20 -0
  71. data/lib/kitchen/figure_element_enumerator.rb +13 -0
  72. data/lib/kitchen/mixins/block_error_if.rb +19 -0
  73. data/lib/kitchen/note_element.rb +43 -0
  74. data/lib/kitchen/note_element_enumerator.rb +13 -0
  75. data/lib/kitchen/oven.rb +61 -0
  76. data/lib/kitchen/page_element.rb +51 -0
  77. data/lib/kitchen/page_element_enumerator.rb +13 -0
  78. data/lib/kitchen/pantry.rb +35 -0
  79. data/lib/kitchen/patches/nokogiri.rb +31 -0
  80. data/lib/kitchen/patches/renderable.rb +31 -0
  81. data/lib/kitchen/patches/string.rb +5 -0
  82. data/lib/kitchen/recipe.rb +78 -0
  83. data/lib/kitchen/search_history.rb +33 -0
  84. data/lib/kitchen/selectors/base.rb +8 -0
  85. data/lib/kitchen/selectors/standard_1.rb +12 -0
  86. data/lib/kitchen/table_element.rb +36 -0
  87. data/lib/kitchen/table_element_enumerator.rb +13 -0
  88. data/lib/kitchen/term_element.rb +16 -0
  89. data/lib/kitchen/term_element_enumerator.rb +13 -0
  90. data/lib/kitchen/transliterations.rb +19 -0
  91. data/lib/kitchen/type_casting_element_enumerator.rb +23 -0
  92. data/lib/kitchen/utils.rb +19 -0
  93. data/lib/kitchen/version.rb +3 -0
  94. data/lib/locales/en.yml +21 -0
  95. data/lib/notes.md +9 -0
  96. data/openstax_kitchen.gemspec +39 -0
  97. data/tutorials/00/expected_baked.html +3 -0
  98. data/tutorials/00/raw.html +3 -0
  99. data/tutorials/00/solution_1.rb +7 -0
  100. data/tutorials/00/solution_2.rb +6 -0
  101. data/tutorials/01/expected_baked.html +66 -0
  102. data/tutorials/01/raw.html +62 -0
  103. data/tutorials/01/solution_1.rb +16 -0
  104. data/tutorials/01/solution_2.rb +24 -0
  105. data/tutorials/02/expected_baked.html +207 -0
  106. data/tutorials/02/raw.html +201 -0
  107. data/tutorials/02/solution_1.rb +29 -0
  108. data/tutorials/03/expected_baked.html +33 -0
  109. data/tutorials/03/raw.html +31 -0
  110. data/tutorials/03/solution_1.rb +16 -0
  111. data/tutorials/03/solution_2.rb +15 -0
  112. data/tutorials/04/expected_baked.html +36 -0
  113. data/tutorials/04/raw.html +36 -0
  114. data/tutorials/04/solution_1.rb +20 -0
  115. data/tutorials/04/solution_2.rb +25 -0
  116. data/tutorials/05/expected_baked.html +11 -0
  117. data/tutorials/05/raw.html +11 -0
  118. data/tutorials/05/solution_1.rb +9 -0
  119. data/tutorials/check_it +64 -0
  120. data/tutorials/setup_my_recipes +30 -0
  121. metadata +278 -0
@@ -0,0 +1,201 @@
1
+ <!--
2
+ 1. For each chapter, add a new container as its last child with class "eoc"
3
+ 2. Give it a title of "End of Chapter Stuff". Use the proper header
4
+ container based on nesting, and give it a data-type of 'document-title'.
5
+ 3. Inside each "eoc" container, make two new containers, one with class
6
+ "critical-thinking-container" and title "Critical Thinking Questions" and
7
+ another with class "review-questions-container" and title "Review Questions".
8
+ 4. To the "critical-thinking-container" move all of the containers from each
9
+ chapter that have the class "critical-thinking".
10
+ 5. To the "review-questions-container" move all of the containers from each chapter
11
+ that have the class "review-questions".
12
+ 6. Remove the h3 tags from the containers that are moving.
13
+ -->
14
+
15
+ <!DOCTYPE html>
16
+ <html>
17
+ <head>
18
+ <title>Move-to, creating containers</title>
19
+ </head>
20
+ <body>
21
+ <div class="book">
22
+ <div data-type="chapter">
23
+ <h1 data-type="document-title">Chapter Title (1)</h1>
24
+ <div data-type="page">
25
+ <h2 data-type="document-title">Page title (1.1)</h2>
26
+ <div class="will-not-move">
27
+ <h3>I didn't go anywhere</h3>
28
+ <p>
29
+ Content will not move <br /> Lorem ipsum dolor ...
30
+ </p>
31
+ <div class="review-questions" id="01">
32
+ <h3>Review Questions</h3>
33
+ <p>
34
+ Content will move <br /> Lorem ipsum dolor ...
35
+ </p>
36
+ </div>
37
+ </div>
38
+ <div class="review-questions" id="02">
39
+ <h3>Review Questions</h3>
40
+ <p>
41
+ Content will move <br /> Lorem ipsum dolor ...
42
+ </p>
43
+ </div>
44
+ <div class="will-not-move">
45
+ <h3> I'm here to stay.</h3>
46
+ <p>
47
+ Content will not move <br /> Lorem ipsum dolor ...
48
+ </p>
49
+ </div>
50
+ <div class="critical-thinking" id="03">
51
+ <h3>Critical Thinking Questions</h3>
52
+ <p>
53
+ Content will move <br /> Lorem ipsum dolor ...
54
+ </p>
55
+ </div>
56
+ <div class="will-not-move">
57
+ <h3>Don't make me move!</h3>
58
+ <p>
59
+ Content will not move <br /> Lorem ipsum dolor ...
60
+ </p>
61
+ </div>
62
+ </div>
63
+ <div data-type="page">
64
+ <h2 data-type="document-title">Page title (1.2)</h2>
65
+ <div class="will-not-move">
66
+ <h3>I didn't go anywhere</h3>
67
+ <p>
68
+ Content will not move <br /> Lorem ipsum dolor ...
69
+ </p>
70
+ </div>
71
+ <div class="review-questions" id="04">
72
+ <h3>Review Questions</h3>
73
+ <p>
74
+ Content will move <br /> Lorem ipsum dolor ...
75
+ </p>
76
+ </div>
77
+ <div class="will-not-move">
78
+ <h3> I'm here to stay.</h3>
79
+ <p>
80
+ Content will not move <br /> Lorem ipsum dolor ...
81
+ </p>
82
+ </div>
83
+ <div class="critical-thinking" id="05">
84
+ <h3>Critical Thinking Questions</h3>
85
+ <p>
86
+ Content will move <br /> Lorem ipsum dolor ...
87
+ </p>
88
+ </div>
89
+ <div class="will-not-move">
90
+ <h3>Don't make me move!</h3>
91
+ <p>
92
+ Content will not move <br /> Lorem ipsum dolor ...
93
+ </p>
94
+ </div>
95
+ </div>
96
+ <div data-type="page">
97
+ <h2 data-type="document-title">Page title (1.3)</h2>
98
+ <div class="will-not-move">
99
+ <h3>I didn't go anywhere</h3>
100
+ <p>
101
+ Content will not move <br /> Lorem ipsum dolor ...
102
+ </p>
103
+ </div>
104
+ <div class="review-questions" id="06">
105
+ <h3>Review Questions</h3>
106
+ <p>
107
+ Content will move <br /> Lorem ipsum dolor ...
108
+ </p>
109
+ </div>
110
+ <div class="will-not-move">
111
+ <h3> I'm here to stay.</h3>
112
+ <p>
113
+ Content will not move <br /> Lorem ipsum dolor ...
114
+ </p>
115
+ </div>
116
+ <div class="critical-thinking" id="07">
117
+ <h3>Critical Thinking Questions</h3>
118
+ <p>
119
+ Content will move <br /> Lorem ipsum dolor ...
120
+ </p>
121
+ </div>
122
+ <div class="will-not-move">
123
+ <h3>Don't make me move!</h3>
124
+ <p>
125
+ Content will not move <br /> Lorem ipsum dolor ...
126
+ </p>
127
+ </div>
128
+ </div>
129
+ </div>
130
+ <div data-type="chapter">
131
+ <h1 data-type="document-title">Chapter Title (2)</h1>
132
+ <div data-type="page">
133
+ <h2 data-type="document-title">Page title (2.1)</h2>
134
+ <div class="will-not-move">
135
+ <h3>I didn't go anywhere</h3>
136
+ <p>
137
+ Content will not move <br /> Lorem ipsum dolor ...
138
+ </p>
139
+ </div>
140
+ <div class="review-questions" id="08">
141
+ <h3>Review Questions</h3>
142
+ <p>
143
+ Content will move <br /> Lorem ipsum dolor ...
144
+ </p>
145
+ </div>
146
+ <div class="will-not-move">
147
+ <h3> I'm here to stay.</h3>
148
+ <p>
149
+ Content will not move <br /> Lorem ipsum dolor ...
150
+ </p>
151
+ </div>
152
+ <div class="critical-thinking" id="09">
153
+ <h3>Critical Thinking Questions</h3>
154
+ <p>
155
+ Content will move <br /> Lorem ipsum dolor ...
156
+ </p>
157
+ </div>
158
+ <div class="will-not-move">
159
+ <h3>Don't make me move!</h3>
160
+ <p>
161
+ Content will not move <br /> Lorem ipsum dolor ...
162
+ </p>
163
+ </div>
164
+ </div>
165
+ <div data-type="page">
166
+ <h2 data-type="document-title">Page title (2.2)</h2>
167
+ <div class="will-not-move">
168
+ <h3>I didn't go anywhere</h3>
169
+ <p>
170
+ Content will not move <br /> Lorem ipsum dolor ...
171
+ </p>
172
+ </div>
173
+ <div class="review-questions" id="10">
174
+ <h3>Review Questions</h3>
175
+ <p>
176
+ Content will move <br /> Lorem ipsum dolor ...
177
+ </p>
178
+ </div>
179
+ <div class="will-not-move">
180
+ <h3> I'm here to stay.</h3>
181
+ <p>
182
+ Content will not move <br /> Lorem ipsum dolor ...
183
+ </p>
184
+ </div>
185
+ <div class="critical-thinking" id="11">
186
+ <h3>Critical Thinking Questions</h3>
187
+ <p>
188
+ Content will move <br /> Lorem ipsum dolor ...
189
+ </p>
190
+ </div>
191
+ <div class="will-not-move">
192
+ <h3>Don't make me move!</h3>
193
+ <p>
194
+ Content will not move <br /> Lorem ipsum dolor ...
195
+ </p>
196
+ </div>
197
+ </div>
198
+ </div>
199
+ </div>
200
+ </body>
201
+ </html>
@@ -0,0 +1,29 @@
1
+ @solution_1 = Kitchen::Recipe.new do |doc|
2
+
3
+ doc.search("[data-type='chapter']").each do |chapter|
4
+ review_questions = chapter.search(".review-questions").cut
5
+ critical_thinking = chapter.search(".critical-thinking").cut
6
+
7
+ [review_questions, critical_thinking].each do |clipboard|
8
+ clipboard.each do |element|
9
+ element.first("h3").trash
10
+ end
11
+ end
12
+
13
+ chapter.append(child: <<~HTML
14
+ <div class="eoc">
15
+ <h2 data-type="document-title">End of Chapter Stuff</h2>
16
+ <div class="critical-thinking-container">
17
+ <div class="os-title">Critical Thinking Questions</div>
18
+ #{critical_thinking.paste}
19
+ </div>
20
+ <div class="review-questions-container">
21
+ <div class="os-title">Review Questions</div>
22
+ #{review_questions.paste}
23
+ </div>
24
+ </div>
25
+ HTML
26
+ )
27
+ end
28
+
29
+ end
@@ -0,0 +1,33 @@
1
+ <!--
2
+ 1. Add a title to each chapter with the text "Chapter X" where X is the chapter number.
3
+ The title should be an h1 with a data-type of 'document-title'.
4
+ 2. Change each page title to be an h2 and insert the page number "X.Y " (where X is the
5
+ chapter number and Y is the page number within the chapter) before the existing text
6
+ of the page title.
7
+ -->
8
+
9
+ <html>
10
+ <body>
11
+ <div data-type="chapter">
12
+ <h1 data-type="document-title">Chapter 1</h1>
13
+ <div data-type="page">
14
+ <h2 data-type="document-title">1.1 Apples</h2>
15
+ </div>
16
+ <div data-type="page">
17
+ <h2 data-type="document-title">1.2 Bananas</h2>
18
+ </div>
19
+ </div>
20
+ <div data-type="chapter">
21
+ <h1 data-type="document-title">Chapter 2</h1>
22
+ <div data-type="page">
23
+ <h2 data-type="document-title">2.1 Carrots</h2>
24
+ </div>
25
+ <div data-type="page">
26
+ <h2 data-type="document-title">2.2 Doughnuts</h2>
27
+ </div>
28
+ <div data-type="page">
29
+ <h2 data-type="document-title">2.3 Eggplants</h2>
30
+ </div>
31
+ </div>
32
+ </body>
33
+ </html>
@@ -0,0 +1,31 @@
1
+ <!--
2
+ 1. Add a title to each chapter with the text "Chapter X" where X is the chapter number.
3
+ The title should be an h1 with a data-type of 'document-title'.
4
+ 2. Change each page title to be an h2 and insert the page number "X.Y " (where X is the
5
+ chapter number and Y is the page number within the chapter) before the existing text
6
+ of the page title.
7
+ -->
8
+
9
+ <html>
10
+ <body>
11
+ <div data-type="chapter">
12
+ <div data-type="page">
13
+ <div data-type="document-title">Apples</div>
14
+ </div>
15
+ <div data-type="page">
16
+ <div data-type="document-title">Bananas</div>
17
+ </div>
18
+ </div>
19
+ <div data-type="chapter">
20
+ <div data-type="page">
21
+ <div data-type="document-title">Carrots</div>
22
+ </div>
23
+ <div data-type="page">
24
+ <div data-type="document-title">Doughnuts</div>
25
+ </div>
26
+ <div data-type="page">
27
+ <div data-type="document-title">Eggplants</div>
28
+ </div>
29
+ </div>
30
+ </body>
31
+ </html>
@@ -0,0 +1,16 @@
1
+ @solution_1 = Kitchen::BookRecipe.new do |doc|
2
+ book = doc.book
3
+
4
+ book.chapters.each do |chapter|
5
+ chapter.prepend(child: <<~HTML
6
+ <h1 data-type="document-title">Chapter #{chapter.count_in(:book)}</h1>
7
+ HTML
8
+ )
9
+
10
+ chapter.pages.each do |page|
11
+ page_title = page.title
12
+ page_title.name = "h2"
13
+ page_title.prepend(child: "#{chapter.count_in(:book)}.#{page.count_in(:chapter)} ")
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ @solution_2 = Kitchen::BookRecipe.new do |doc|
2
+ book = doc.book
3
+
4
+ book.chapters.each do |chapter|
5
+ chapter.prepend(child: <<~HTML
6
+ <h1 data-type="document-title">Chapter #{chapter.count_in(:book)}</h1>
7
+ HTML
8
+ )
9
+
10
+ chapter.pages.each do |page|
11
+ page.title.set(:name, "h2")
12
+ .prepend(child: "#{chapter.count_in(:book)}.#{page.count_in(:chapter)} ")
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ <!--
2
+ 1. Number each table and figure (e.g. Table 1, Figure 1) in a first child div with class
3
+ 'caption'.
4
+ 2. Replace all "[LINK]" text in "a" tags with class "needs-label" with the "Table X" or
5
+ "Figure X" text from the referred-to table or figure.
6
+ -->
7
+
8
+ <html>
9
+ <body>
10
+ <div class="book">
11
+ <div class="chapter">
12
+ <p>Lorem ipsum.</p>
13
+ <table id="g72s2414"><div class="caption">Table 1</div><th>I am a table. Use your imagination...</th></table>
14
+ <p>Lorem ipsum.</p>
15
+ <p>Lorem ipsum.</p>
16
+ <figure id="m2m4m3ag"><div class="caption">Figure 1</div>I am a figure. Use your imagination...</figure>
17
+ <p>Lorem ipsum.</p>
18
+ <p>See this table for info: <a class="needs-label" href="#g72s2414">Table 1</a></p>
19
+ <p>See this figure for a nice picture: <a class="needs-label" href="#m2m4m3ag">Figure 1</a></p>
20
+ <p>Lorem ipsum.</p>
21
+ <table id="45m5nh21"><div class="caption">Table 2</div><th>I am a table. Use your imagination...</th></table>
22
+ <p>Lorem ipsum.</p>
23
+ <p>See this table for info: <a class="needs-label" href="#45m5nh21">Table 2</a></p>
24
+ <p>Lorem ipsum.</p>
25
+ <figure id="ghtkc34x"><div class="caption">Figure 2</div>I am a figure. Use your imagination...</figure>
26
+ <p>Lorem ipsum.</p>
27
+ <figure id="58dcdthw"><div class="caption">Figure 3</div>I am a figure. Use your imagination...</figure>
28
+ <p>Lorem ipsum.</p>
29
+ <p>See this figure for a nice picture: <a class="needs-label" href="#58dcdthw">Figure 3</a></p>
30
+ <p>Lorem ipsum.</p>
31
+ <p>See this figure for a nice picture: <a class="needs-label" href="#ghtkc34x">Figure 2</a></p>
32
+ <p>Don't do anything to this link: <a href="#ghtkc34x">Don't change me</a></p>
33
+ </div>
34
+ </div>
35
+ </body>
36
+ </html>
@@ -0,0 +1,36 @@
1
+ <!--
2
+ 1. Number each table and figure (e.g. Table 1, Figure 1) in a first child div with class
3
+ 'caption'.
4
+ 2. Replace all "[LINK]" text in "a" tags with class "needs-label" with the "Table X" or
5
+ "Figure X" text from the referred-to table or figure.
6
+ -->
7
+
8
+ <html>
9
+ <body>
10
+ <div class="book">
11
+ <div class="chapter">
12
+ <p>Lorem ipsum.</p>
13
+ <table id="g72s2414"><th>I am a table. Use your imagination...</th></table>
14
+ <p>Lorem ipsum.</p>
15
+ <p>Lorem ipsum.</p>
16
+ <figure id="m2m4m3ag">I am a figure. Use your imagination...</figure>
17
+ <p>Lorem ipsum.</p>
18
+ <p>See this table for info: <a class="needs-label" href="#g72s2414">[LINK]</a></p>
19
+ <p>See this figure for a nice picture: <a class="needs-label" href="#m2m4m3ag">[LINK]</a></p>
20
+ <p>Lorem ipsum.</p>
21
+ <table id="45m5nh21"><th>I am a table. Use your imagination...</th></table>
22
+ <p>Lorem ipsum.</p>
23
+ <p>See this table for info: <a class="needs-label" href="#45m5nh21">[LINK]</a></p>
24
+ <p>Lorem ipsum.</p>
25
+ <figure id="ghtkc34x">I am a figure. Use your imagination...</figure>
26
+ <p>Lorem ipsum.</p>
27
+ <figure id="58dcdthw">I am a figure. Use your imagination...</figure>
28
+ <p>Lorem ipsum.</p>
29
+ <p>See this figure for a nice picture: <a class="needs-label" href="#58dcdthw">[LINK]</a></p>
30
+ <p>Lorem ipsum.</p>
31
+ <p>See this figure for a nice picture: <a class="needs-label" href="#ghtkc34x">[LINK]</a></p>
32
+ <p>Don't do anything to this link: <a href="#ghtkc34x">Don't change me</a></p>
33
+ </div>
34
+ </div>
35
+ </body>
36
+ </html>
@@ -0,0 +1,20 @@
1
+ @solution_1 = Kitchen::BookRecipe.new do |doc|
2
+ book = doc.book
3
+
4
+ book.tables.each do |table|
5
+ table_name = "Table #{table.count_in(:book)}"
6
+ table.prepend(child: "<div class='caption'>#{table_name}</div>")
7
+ doc.pantry(name: :link_text).store table_name, label: table.id
8
+ end
9
+
10
+ book.figures.each do |figure|
11
+ figure_name = "Figure #{figure.count_in(:book)}"
12
+ figure.prepend(child: "<div class='caption'>#{figure_name}</div>")
13
+ doc.pantry(name: :link_text).store figure_name, label: figure.id
14
+ end
15
+
16
+ book.search("a.needs-label").each do |anchor|
17
+ id = anchor[:href][1..-1]
18
+ anchor.replace_children(with: doc.pantry(name: :link_text).get(id))
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ @solution_2 = Kitchen::BookRecipe.new do |doc|
2
+ book = doc.book
3
+
4
+ # Solution 1 treats figures and tables almost identically and ends up duplicating
5
+ # a lot of code. To DRY up that code, pull out the parts that change (the search
6
+ # enumerator and the label):
7
+
8
+ enumerators_to_labels_map = {
9
+ book.tables => "Table",
10
+ book.figures => "Figure"
11
+ }
12
+
13
+ enumerators_to_labels_map.each do |enumerator, label|
14
+ enumerator.each do |element|
15
+ name = "#{label} #{element.count_in(:book)}"
16
+ element.prepend(child: "<div class='caption'>#{name}</div>")
17
+ doc.pantry(name: :link_text).store name, label: element.id
18
+ end
19
+ end
20
+
21
+ book.search("a.needs-label").each do |anchor|
22
+ id = anchor[:href][1..-1]
23
+ anchor.replace_children(with: doc.pantry(name: :link_text).get(id))
24
+ end
25
+ end