Skip to content

Commit 95080ec

Browse files
Revert changes to BakeIframes (#411)
* Revert changes to `BakeIframes` * Update algebra-1 expected output
1 parent 09d6939 commit 95080ec

File tree

26 files changed

+388
-203
lines changed

26 files changed

+388
-203
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [v2.26.1] - 2025-05-22
10+
11+
* Revert changes to `BakeIframes`
12+
913
## [v2.26.0] - 2025-05-20
1014

1115
* Add `suppress_solution_title` to `bake_numbered_exercise`

lib/kitchen/directions/bake_iframes/v1.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ def bake(book:)
1010
iframes.each do |iframe|
1111
next if iframe.has_class?('os-is-iframe') # don't double-bake
1212

13-
iframe_link = '/404-this-link-should-be-replaced'
14-
13+
iframe_link = \
14+
begin
15+
iframe.rex_link
16+
rescue StandardError
17+
warn "Unable to find rex link for iframe #{iframe}"
18+
iframe[:src]
19+
end
1520
iframe.wrap('<div class="os-has-iframe" data-type="switch">')
1621
iframe.add_class('os-is-iframe')
1722

@@ -20,7 +25,7 @@ def bake(book:)
2025

2126
iframe.prepend(child:
2227
<<~HTML
23-
<a class="os-is-link" data-needs-rex-link="true" href="#{iframe_link}" target="_blank" rel="noopener nofollow">#{I18n.t(:iframe_link_text)}</a>
28+
<a class="os-is-link" href="#{iframe_link}" target="_blank" rel="noopener nofollow">#{I18n.t(:iframe_link_text)}</a>
2429
HTML
2530
)
2631

lib/kitchen/element_base.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,39 @@ def as_enumerator
967967
enumerator_class.new(search_query: search_query_that_found_me) { |block| block.yield(self) }
968968
end
969969

970+
def rex_link
971+
self[:'data-is-for-rex-linking'] = 'true'
972+
973+
element_with_ancestors = document.book.chapters.search_with(
974+
Kitchen::PageElementEnumerator, Kitchen::CompositePageElementEnumerator
975+
).search('[data-is-for-rex-linking="true"]').first
976+
977+
remove_attribute('data-is-for-rex-linking')
978+
979+
unless element_with_ancestors
980+
raise("Cannot create rex link to element #{self} - needs ancestors of both types chapter & page/composite_page" \
981+
"#{say_source_or_nil}"
982+
)
983+
end
984+
985+
book_slug = document.slug
986+
chapter_count = element_with_ancestors.ancestor(:chapter).count_in(:book)
987+
page_string = ''
988+
page_title = ''
989+
page = element_with_ancestors.ancestor(:page) if element_with_ancestors.has_ancestor?(:page)
990+
if page&.is_introduction?
991+
page_title = page.first('[data-type="document-title"]').text.slugify
992+
elsif page
993+
page_string = "#{page.count_in(:chapter) - 1}-"
994+
page_title = page.title_text.slugify
995+
else
996+
page = element_with_ancestors.ancestor(:composite_page)
997+
page_title = page.title.text.slugify
998+
end
999+
1000+
"https://openstax.org/books/#{book_slug}/pages/#{chapter_count}-#{page_string}#{page_title}"
1001+
end
1002+
9701003
def add_platform_media(format)
9711004
valid_formats = %w[screen print screenreader]
9721005
raise "Media format invalid; valid formats are #{valid_formats}" unless valid_formats.include?(format)

lib/kitchen/patches/string.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,18 @@ class String
1010
def uncapitalize
1111
sub(/^[A-Z]/, &:downcase)
1212
end
13+
14+
# Transform self to kebab case, returning a new string
15+
# Example: "Star Wars: The Empire Strikes Back" -> "star-wars-the-empire-strikes-back"
16+
#
17+
# @return [String]
18+
#
19+
def slugify
20+
I18n.transliterate(
21+
strip.downcase
22+
.gsub(/'/, '')
23+
.gsub(/®/, ' r')
24+
.gsub(/\u2014+/, '-')
25+
).gsub(/[^(\w\s)-]/, '').gsub(/[\s-]+/, '-')
26+
end
1327
end

spec/kitchen_spec/directions/bake_iframes/v1_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,48 @@
6363
described_class.new.bake(book: book_with_baked_iframes)
6464
expect(book_with_baked_iframes).to match_normalized_html(book_with_baked_iframes_snapshot)
6565
end
66+
67+
context 'with exceptions' do
68+
let(:book_with_iframe_no_slug) do
69+
book_containing(html:
70+
<<~HTML
71+
<div data-type="chapter">
72+
<div data-type="page" class="introduction"></div>
73+
<div data-type="page" id="page_1234">
74+
<h1 data-type="document-title">The Document: Title!</h1>
75+
<div data-alt="atoms_isotopes" data-type="media" id="1234">
76+
<iframe height="371.4" src="https://openstax.org/l/atoms_isotopes" width="660"><!-- no-selfclose -->
77+
</iframe>
78+
</div>
79+
</div>
80+
</div>
81+
HTML
82+
)
83+
end
84+
85+
let(:book_with_iframe_no_id_on_media) do
86+
book_containing(html:
87+
<<~HTML
88+
<span data-type="slug" data-value="the-book-slug">
89+
<div data-type="chapter">
90+
<div data-type="page" class="introduction"></div>
91+
<div data-type="page" id="page_1234">
92+
<h1 data-type="document-title">The Document: Title!</h1>
93+
<div data-alt="atoms_isotopes" data-type="media">
94+
<iframe height="371.4" src="https://openstax.org/l/atoms_isotopes" width="660"><!-- no-selfclose -->
95+
</iframe>
96+
</div>
97+
</div>
98+
</div>
99+
HTML
100+
)
101+
end
102+
103+
it 'warns when rex link can\'t be made - no slug' do
104+
expect(Warning).to receive(:warn).with(
105+
/Unable to find rex link for iframe <iframe/, { category: nil }
106+
)
107+
described_class.new.bake(book: book_with_iframe_no_slug)
108+
end
109+
end
66110
end

spec/kitchen_spec/element_base_spec.rb

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,91 @@
755755
end
756756
end
757757

758+
describe '#rex_link' do
759+
let(:book_rex_linkable) do
760+
book_containing(html:
761+
<<~HTML
762+
<div data-type="metadata" style="display: none;">
763+
<h1 data-type="document-title" itemprop="name">Title Of The Book</h1>
764+
<span data-type="slug" data-value="test-book-slug"></span>
765+
</div>
766+
<div data-type="page" id="not-in-chapter"></div>
767+
<div data-type="chapter">
768+
<div data-type="page" class="introduction">
769+
<div data-type="metadata" style="display: none;">
770+
<h1 data-type="document-title" itemprop="name">Introduction: Fre Sha Vocado</h1>
771+
</div>
772+
<div class="intro-text">
773+
<h2 data-type="document-title">
774+
<span data-type="" itemprop="" class="os-text">Introduction</span>
775+
</h2>
776+
</div>
777+
<div id="element1"></div>
778+
</div>
779+
<div data-type="page">
780+
<div data-type="metadata" style="display: none;">
781+
<h1 data-type="document-title" itemprop="name">Test Page: It's, An, Avocado</h1>
782+
</div>
783+
<h2 data-type="document-title">
784+
<span class="os-number">1.1</span>
785+
<span class="os-divider"> </span>
786+
<span data-type="" itemprop="" class="os-text">Test Page: It's, An, Avocado</span>
787+
</h2>
788+
<div id="element2"></div>
789+
</div>
790+
<div data-type="page">
791+
<div data-type="metadata" style="display: none;">
792+
<h1 data-type="document-title" itemprop="name">I have hyphen — and latin letter ć</h1>
793+
</div>
794+
<h2 data-type="document-title">
795+
<span class="os-number">1.2</span>
796+
<span class="os-divider"> </span>
797+
<span data-type="" itemprop="" class="os-text">I have hyphen — and latin letter ć</span>
798+
</h2>
799+
<div id="element4"></div>
800+
</div>
801+
</div>
802+
<div data-type="chapter">
803+
<div class="os-eoc os-summary-container" data-type="composite-page" data-uuid-key=".summary" id="composite-page-1">
804+
<h2 data-type="document-title">
805+
<span class="os-text">Summary Or Something</span>
806+
</h2>
807+
<div data-type="metadata" style="display: none;">
808+
<h1 data-type="document-title" itemprop="name">Summary</h1>
809+
<span data-type="slug" data-value="introduction-political-science"></span>
810+
</div>
811+
<div id="element3"></div>
812+
</div>
813+
HTML
814+
)
815+
end
816+
817+
it 'returns rex link for element in section page' do
818+
expect(book_rex_linkable.first('div#element2').rex_link).to \
819+
eq('https://openstax.org/books/test-book-slug/pages/1-1-test-page-its-an-avocado')
820+
end
821+
822+
it 'returns rex link for element in intro page' do
823+
expect(book_rex_linkable.first('div#element1').rex_link).to \
824+
eq('https://openstax.org/books/test-book-slug/pages/1-introduction-fre-sha-vocado')
825+
end
826+
827+
it 'returns rex link for element in composite page' do
828+
expect(book_rex_linkable.first('div#element3').rex_link).to \
829+
eq('https://openstax.org/books/test-book-slug/pages/2-summary-or-something')
830+
end
831+
832+
it 'returns rex link for element with hyphen and latin letter' do
833+
expect(book_rex_linkable.first('div#element4').rex_link).to \
834+
eq('https://openstax.org/books/test-book-slug/pages/1-2-i-have-hyphen-and-latin-letter-c')
835+
end
836+
837+
it 'raises error when ancestors can\'t be found' do
838+
expect { book_rex_linkable.pages('$#not-in-chapter').first.rex_link }.to \
839+
raise_error(/Cannot create rex link to element[^>]+id="not-in-chapter"/)
840+
end
841+
end
842+
758843
describe '#add_platform_media' do
759844
let(:no_media) do
760845
book_containing(html:

0 commit comments

Comments
 (0)