Skip to content

Commit 4866c4a

Browse files
Add v2 bake folio that uses existing titles and numbers
1 parent 63a9b51 commit 4866c4a

10 files changed

+839
-19
lines changed

lib/kitchen/directions/bake_folio/main.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Kitchen
24
module Directions
35
module BakeFolio
@@ -10,6 +12,10 @@ def self.v1(book:, chapters: nil, options: {})
1012
chapters: chapters || book.chapters,
1113
options: options)
1214
end
15+
16+
def self.v2(book:, chapters: nil)
17+
V2.new.bake(book: book, chapters: chapters || book.chapters)
18+
end
1319
end
1420
end
15-
end
21+
end
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# frozen_string_literal: true
2+
3+
module Kitchen
4+
module Directions
5+
module BakeFolio
6+
class V2
7+
renderable
8+
9+
class FolioParaWithNumber
10+
attr_reader :klass
11+
attr_reader :title
12+
attr_reader :title_number
13+
attr_reader :append_to
14+
15+
def initialize(klass:, title:, title_number:, append_to:)
16+
@klass = klass
17+
@title = title
18+
@title_number = title_number
19+
@append_to = append_to
20+
end
21+
22+
def create_para
23+
title_divider = ' • '
24+
para_text = [
25+
@title_number&.strip,
26+
@title.strip
27+
].compact.join(title_divider)
28+
29+
@append_to.append(sibling:
30+
<<~HTML.chomp
31+
<p class="#{@klass}">
32+
#{para_text}
33+
</p>
34+
HTML
35+
)
36+
end
37+
end
38+
39+
class FolioPara
40+
attr_reader :klass
41+
attr_reader :title
42+
43+
def initialize(klass:, title:)
44+
@klass = klass
45+
@title = title
46+
end
47+
48+
def create_para
49+
title_label = @title.search('.os-text').first.children
50+
51+
@title.append(sibling:
52+
<<~HTML.chomp
53+
<p class="#{@klass}">#{title_label}</p>
54+
HTML
55+
)
56+
end
57+
end
58+
59+
def bake(book:, chapters:)
60+
book['data-pdf-folio-preface-message'] = I18n.t(:"folio.preface")
61+
book['data-pdf-folio-access-message'] = I18n.t(:"folio.access_for_free")
62+
63+
# TODO: apply to all books and remove an option
64+
chapters.each do |chapter|
65+
chapter_folio(chapter: chapter)
66+
module_folio(chapter: chapter)
67+
eoc_folio(chapter: chapter, klass: 'folio-eoc-left')
68+
eoc_folio(chapter: chapter, klass: 'folio-eoc-right')
69+
end
70+
71+
appendix_folio(book: book, klass: 'folio-appendix-left')
72+
appendix_folio(book: book, klass: 'folio-appendix-right')
73+
74+
eob_folio(book: book, klass: 'folio-eob-left')
75+
eob_folio(book: book, klass: 'folio-eob-right')
76+
end
77+
78+
def chapter_folio(chapter:)
79+
chapter_number = chapter.search('$[data-type="document-title"] > .os-number').first
80+
chapter_para = FolioParaWithNumber.new(
81+
klass: 'folio-chapter',
82+
title: chapter.title_text,
83+
title_number: chapter_number&.text,
84+
append_to: chapter.title
85+
)
86+
87+
chapter_para.create_para
88+
end
89+
90+
def module_folio(chapter:)
91+
# Introduction para
92+
chapter_number = chapter.search('$[data-type="document-title"] > .os-number').first
93+
chapter.pages('$.introduction').each do |page|
94+
title_element = page.search('h2[data-type="document-title"]').first
95+
intro_para = FolioParaWithNumber.new(
96+
klass: 'folio-module',
97+
title: page.title_text,
98+
title_number: chapter_number&.text,
99+
append_to: title_element
100+
)
101+
102+
intro_para.create_para
103+
end
104+
105+
# Module para
106+
chapter.non_introduction_pages.each do |page|
107+
title_element = page.search('h2[data-type="document-title"]').first
108+
title_number = title_element.search('$.os-number').first&.text
109+
module_para = FolioParaWithNumber.new(
110+
klass: 'folio-module',
111+
title: page.title_text,
112+
title_number: title_number,
113+
append_to: title_element
114+
)
115+
116+
module_para.create_para
117+
end
118+
end
119+
120+
def eoc_folio(chapter:, klass:)
121+
chapter_number = chapter.search('$[data-type="document-title"] > .os-number').first
122+
chapter.search(' > .os-eoc').each do |eoc_page|
123+
title_element = eoc_page.search('h2[data-type="document-title"]').first
124+
eoc_para = FolioParaWithNumber.new(
125+
klass: klass,
126+
title: title_element.text,
127+
title_number: chapter_number&.text,
128+
append_to: title_element
129+
)
130+
131+
eoc_para.create_para
132+
end
133+
end
134+
135+
def appendix_folio(book:, klass:)
136+
book.pages('$.appendix').each do |appendix_page|
137+
appendix_para = FolioParaWithNumber.new(
138+
klass: klass,
139+
title: appendix_page.title.text,
140+
title_number: [*('A'..'Z')][appendix_page.count_in(:book) - 1],
141+
append_to: appendix_page.title
142+
)
143+
144+
appendix_para.create_para
145+
end
146+
end
147+
148+
def eob_folio(book:, klass:)
149+
eob_selectors = '.os-eob[data-type="composite-chapter"], ' \
150+
'.os-eob[data-type="composite-page"]:not(.os-solutions-container)'
151+
152+
book.search(eob_selectors).each do |eob_page|
153+
eob_para = FolioPara.new(
154+
klass: klass,
155+
title: eob_page.search('h1[data-type="document-title"]').first
156+
)
157+
158+
eob_para.create_para
159+
end
160+
end
161+
end
162+
end
163+
end
164+
end

lib/recipes/algebra-1/recipe.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,7 @@
225225
}
226226
})
227227
BakeEquations.v1(book: book, number_decorator: :parentheses)
228-
BakeFolio.v1(book: book,
229-
chapters: book.units.chapters,
230-
options: { numbering_options: { mode: :unit_chapter_page } })
228+
BakeFolio.v2(book: book, chapters: book.units.chapters)
231229

232230
book.chapters.each do |chapter|
233231
BakeLearningObjectives.v2(chapter: chapter)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe Kitchen::Directions::BakeFolio do
6+
it 'calls v1' do
7+
expect_any_instance_of(Kitchen::Directions::BakeFolio::V1).to receive(:bake).with(
8+
book: 'book',
9+
chapters: %w[chapter1],
10+
options: {
11+
new_approach: false,
12+
numbering_options: { mode: :chapter_page, separator: '.' }
13+
}
14+
)
15+
described_class.v1(book: 'book', chapters: %w[chapter1])
16+
end
17+
18+
it 'calls v2' do
19+
expect_any_instance_of(Kitchen::Directions::BakeFolio::V2).to receive(:bake).with(book: 'book', chapters: %w[chapter1])
20+
described_class.v2(book: 'book', chapters: %w[chapter1])
21+
end
22+
end
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe Kitchen::Directions::BakeFolio do
6+
let(:book) do
7+
book_containing(html:
8+
<<~HTML
9+
<div class='hi'>Howdy</div>
10+
HTML
11+
)
12+
end
13+
14+
let(:book_with_folio) do
15+
book_containing(html:
16+
<<~HTML
17+
<div data-type="chapter">
18+
<h1 data-type="document-title">
19+
<span class="os-text">Chapter title</span>
20+
<span class="os-number">1</span>
21+
</h1>
22+
<div data-type="page" class="introduction">
23+
<h2 data-type="document-title">
24+
<span class="os-text">Introduction title</span>
25+
</h2>
26+
</div>
27+
<div data-type="page">
28+
<h2 data-type="document-title">
29+
<span class="os-text">Module title</span>
30+
<span class="os-number">1.1</span>
31+
</h2>
32+
</div>
33+
<div data-type="composite-page" class="os-eoc">
34+
<h2 data-type="document-title">
35+
<span class="os-text">Eoc title</span>
36+
</h2>
37+
</div>
38+
</div>
39+
<div data-type="page" class="appendix">
40+
<h1 data-type="document-title">
41+
<span class="os-text">Appendix title</span>
42+
</h1>
43+
</div>
44+
<div data-type="composite-chapter" class="os-eob os-solutions-container">
45+
<h1 data-type="document-title">
46+
<span class="os-text">Answer Key title</span>
47+
</h1>
48+
<div data-type="composite-page" class="os-eob os-solutions-container">
49+
<h2 data-type="document-title">
50+
<span class="os-text">Chapter title</span>
51+
</h2>
52+
</div>
53+
</div>
54+
<div data-type="composite-page" class="os-eob">
55+
<h1 data-type="document-title">
56+
<span class="os-text">Index title</span>
57+
</h1>
58+
</div>
59+
HTML
60+
)
61+
end
62+
63+
describe 'v2 folio pdf translations' do
64+
it 'works in english' do
65+
described_class.v2(book: book)
66+
expect(book).to match_snapshot_auto
67+
end
68+
69+
it 'works in spanish' do
70+
with_locale(:es) do
71+
described_class.v2(book: book)
72+
end
73+
74+
expect(book).to match_snapshot_auto
75+
end
76+
77+
context 'when book uses new approach' do
78+
it 'creates folio paras' do
79+
described_class.v2(book: book_with_folio)
80+
expect(book_with_folio).to match_snapshot_auto
81+
end
82+
end
83+
end
84+
end

0 commit comments

Comments
 (0)