Skip to content

Revert changes to BakeIframes #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [v2.26.1] - 2025-05-22

* Revert changes to `BakeIframes`

## [v2.26.0] - 2025-05-20

* Add `suppress_solution_title` to `bake_numbered_exercise`
Expand Down
11 changes: 8 additions & 3 deletions lib/kitchen/directions/bake_iframes/v1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ def bake(book:)
iframes.each do |iframe|
next if iframe.has_class?('os-is-iframe') # don't double-bake

iframe_link = '/404-this-link-should-be-replaced'

iframe_link = \
begin
iframe.rex_link
rescue StandardError
warn "Unable to find rex link for iframe #{iframe}"
iframe[:src]
end
iframe.wrap('<div class="os-has-iframe" data-type="switch">')
iframe.add_class('os-is-iframe')

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

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

Expand Down
33 changes: 33 additions & 0 deletions lib/kitchen/element_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,39 @@ def as_enumerator
enumerator_class.new(search_query: search_query_that_found_me) { |block| block.yield(self) }
end

def rex_link
self[:'data-is-for-rex-linking'] = 'true'

element_with_ancestors = document.book.chapters.search_with(
Kitchen::PageElementEnumerator, Kitchen::CompositePageElementEnumerator
).search('[data-is-for-rex-linking="true"]').first

remove_attribute('data-is-for-rex-linking')

unless element_with_ancestors
raise("Cannot create rex link to element #{self} - needs ancestors of both types chapter & page/composite_page" \
"#{say_source_or_nil}"
)
end

book_slug = document.slug
chapter_count = element_with_ancestors.ancestor(:chapter).count_in(:book)
page_string = ''
page_title = ''
page = element_with_ancestors.ancestor(:page) if element_with_ancestors.has_ancestor?(:page)
if page&.is_introduction?
page_title = page.first('[data-type="document-title"]').text.slugify
elsif page
page_string = "#{page.count_in(:chapter) - 1}-"
page_title = page.title_text.slugify
else
page = element_with_ancestors.ancestor(:composite_page)
page_title = page.title.text.slugify
end

"https://openstax.org/books/#{book_slug}/pages/#{chapter_count}-#{page_string}#{page_title}"
end

def add_platform_media(format)
valid_formats = %w[screen print screenreader]
raise "Media format invalid; valid formats are #{valid_formats}" unless valid_formats.include?(format)
Expand Down
14 changes: 14 additions & 0 deletions lib/kitchen/patches/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,18 @@ class String
def uncapitalize
sub(/^[A-Z]/, &:downcase)
end

# Transform self to kebab case, returning a new string
# Example: "Star Wars: The Empire Strikes Back" -> "star-wars-the-empire-strikes-back"
#
# @return [String]
#
def slugify
I18n.transliterate(
strip.downcase
.gsub(/'/, '')
.gsub(/®/, ' r')
.gsub(/\u2014+/, '-')
).gsub(/[^(\w\s)-]/, '').gsub(/[\s-]+/, '-')
end
end
44 changes: 44 additions & 0 deletions spec/kitchen_spec/directions/bake_iframes/v1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,48 @@
described_class.new.bake(book: book_with_baked_iframes)
expect(book_with_baked_iframes).to match_normalized_html(book_with_baked_iframes_snapshot)
end

context 'with exceptions' do
let(:book_with_iframe_no_slug) do
book_containing(html:
<<~HTML
<div data-type="chapter">
<div data-type="page" class="introduction"></div>
<div data-type="page" id="page_1234">
<h1 data-type="document-title">The Document: Title!</h1>
<div data-alt="atoms_isotopes" data-type="media" id="1234">
<iframe height="371.4" src="https://openstax.org/l/atoms_isotopes" width="660"><!-- no-selfclose -->
</iframe>
</div>
</div>
</div>
HTML
)
end

let(:book_with_iframe_no_id_on_media) do
book_containing(html:
<<~HTML
<span data-type="slug" data-value="the-book-slug">
<div data-type="chapter">
<div data-type="page" class="introduction"></div>
<div data-type="page" id="page_1234">
<h1 data-type="document-title">The Document: Title!</h1>
<div data-alt="atoms_isotopes" data-type="media">
<iframe height="371.4" src="https://openstax.org/l/atoms_isotopes" width="660"><!-- no-selfclose -->
</iframe>
</div>
</div>
</div>
HTML
)
end

it 'warns when rex link can\'t be made - no slug' do
expect(Warning).to receive(:warn).with(
/Unable to find rex link for iframe <iframe/, { category: nil }
)
described_class.new.bake(book: book_with_iframe_no_slug)
end
end
end
85 changes: 85 additions & 0 deletions spec/kitchen_spec/element_base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,91 @@
end
end

describe '#rex_link' do
let(:book_rex_linkable) do
book_containing(html:
<<~HTML
<div data-type="metadata" style="display: none;">
<h1 data-type="document-title" itemprop="name">Title Of The Book</h1>
<span data-type="slug" data-value="test-book-slug"></span>
</div>
<div data-type="page" id="not-in-chapter"></div>
<div data-type="chapter">
<div data-type="page" class="introduction">
<div data-type="metadata" style="display: none;">
<h1 data-type="document-title" itemprop="name">Introduction: Fre Sha Vocado</h1>
</div>
<div class="intro-text">
<h2 data-type="document-title">
<span data-type="" itemprop="" class="os-text">Introduction</span>
</h2>
</div>
<div id="element1"></div>
</div>
<div data-type="page">
<div data-type="metadata" style="display: none;">
<h1 data-type="document-title" itemprop="name">Test Page: It's, An, Avocado</h1>
</div>
<h2 data-type="document-title">
<span class="os-number">1.1</span>
<span class="os-divider"> </span>
<span data-type="" itemprop="" class="os-text">Test Page: It's, An, Avocado</span>
</h2>
<div id="element2"></div>
</div>
<div data-type="page">
<div data-type="metadata" style="display: none;">
<h1 data-type="document-title" itemprop="name">I have hyphen — and latin letter ć</h1>
</div>
<h2 data-type="document-title">
<span class="os-number">1.2</span>
<span class="os-divider"> </span>
<span data-type="" itemprop="" class="os-text">I have hyphen — and latin letter ć</span>
</h2>
<div id="element4"></div>
</div>
</div>
<div data-type="chapter">
<div class="os-eoc os-summary-container" data-type="composite-page" data-uuid-key=".summary" id="composite-page-1">
<h2 data-type="document-title">
<span class="os-text">Summary Or Something</span>
</h2>
<div data-type="metadata" style="display: none;">
<h1 data-type="document-title" itemprop="name">Summary</h1>
<span data-type="slug" data-value="introduction-political-science"></span>
</div>
<div id="element3"></div>
</div>
HTML
)
end

it 'returns rex link for element in section page' do
expect(book_rex_linkable.first('div#element2').rex_link).to \
eq('https://openstax.org/books/test-book-slug/pages/1-1-test-page-its-an-avocado')
end

it 'returns rex link for element in intro page' do
expect(book_rex_linkable.first('div#element1').rex_link).to \
eq('https://openstax.org/books/test-book-slug/pages/1-introduction-fre-sha-vocado')
end

it 'returns rex link for element in composite page' do
expect(book_rex_linkable.first('div#element3').rex_link).to \
eq('https://openstax.org/books/test-book-slug/pages/2-summary-or-something')
end

it 'returns rex link for element with hyphen and latin letter' do
expect(book_rex_linkable.first('div#element4').rex_link).to \
eq('https://openstax.org/books/test-book-slug/pages/1-2-i-have-hyphen-and-latin-letter-c')
end

it 'raises error when ancestors can\'t be found' do
expect { book_rex_linkable.pages('$#not-in-chapter').first.rex_link }.to \
raise_error(/Cannot create rex link to element[^>]+id="not-in-chapter"/)
end
end

describe '#add_platform_media' do
let(:no_media) do
book_containing(html:
Expand Down
Loading