Skip to content

Core 845 postbake rex link #406

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 3 commits into from
Apr 1, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

* Fix iframes links for Polish books
* Remove `ElementBase#rex_link` in favor of generating links elsewhere
* Update `BakeIframes` to mark iframe links as needing rex linking

## [v2.25.1] - 2025-02-25

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

iframe_link = \
begin
iframe.rex_link
rescue StandardError
warn "Unable to find rex link for iframe #{iframe}"
iframe[:src]
end
iframe_link = '/404-this-link-should-be-replaced'

iframe.wrap('<div class="os-has-iframe" data-type="switch">')
iframe.add_class('os-is-iframe')

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So postbake uses this data attribute to do something? I tried looking it up but didn't find anything (maybe it's not found with the full attribute name?)

Copy link
Contributor Author

@TylerZeroMaster TylerZeroMaster Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jivey those changes can be found in the other PR: openstax/enki#405.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah thanks I did not notice the second link

HTML
)

Expand Down
33 changes: 0 additions & 33 deletions lib/kitchen/element_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -915,39 +915,6 @@ 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: 0 additions & 14 deletions lib/kitchen/patches/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,4 @@ 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: 0 additions & 44 deletions spec/kitchen_spec/directions/bake_iframes/v1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,48 +63,4 @@
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: 0 additions & 85 deletions spec/kitchen_spec/element_base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -624,91 +624,6 @@
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
8 changes: 4 additions & 4 deletions spec/recipes_spec/books/ap-history/expected_output.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ Europeans believed they discovered an entirely new place when they encountered t
<p id="auto_22e8aac4-82ad-474f-867d-3059dfde7829_fs-idm388862080">Watch this <a href="https://openstax.org/l/99ColonizeAmer" target="_blank" rel="noopener nofollow">BRI Homework Help video on The Colonization of America</a> to review of the reasons different European powers claimed portions of the New World:</p>
<div data-type="media" id="auto_22e8aac4-82ad-474f-867d-3059dfde7829_fs-idm405412048" data-alt="articles">
<div class="os-has-iframe os-has-link" data-type="switch">
<a class="os-is-link" href="https://openstax.org/books/llph-slug/pages/1-1--chapter-1-introductory-essay-14911607" target="_blank" rel="noopener nofollow" data-media="print">Access multimedia content</a>
<a class="os-is-link" data-needs-rex-link="true" href="/404-this-link-should-be-replaced" target="_blank" rel="noopener nofollow" data-media="print">Access multimedia content</a>
<iframe width="660" height="371.4" src="https://www.youtube.com/embed/e8my1VY3OGA" class="os-is-iframe" data-media="screen">
<!-- no-selfclose -->
</iframe>
Expand Down Expand Up @@ -2786,7 +2786,7 @@ Europeans believed they discovered an entirely new place when they encountered t
<p id="auto_ff3d527a-7f54-43b3-bc09-13d4a43894b4_fs-idm233945408">Watch this <a href="https://openstax.org/l/99ColumbianExch" target="_blank" rel="noopener nofollow">BRI Homework Help video on the Columbian Exchange</a> for a review of the main ideas in this essay.</p>
<div data-type="media" id="auto_ff3d527a-7f54-43b3-bc09-13d4a43894b4_fs-idm226476080" data-alt="articles">
<div class="os-has-iframe os-has-link" data-type="switch">
<a class="os-is-link" href="https://openstax.org/books/llph-slug/pages/1-4--columbian-exchange" target="_blank" rel="noopener nofollow" data-media="print">Access multimedia content</a>
<a class="os-is-link" data-needs-rex-link="true" href="/404-this-link-should-be-replaced" target="_blank" rel="noopener nofollow" data-media="print">Access multimedia content</a>
<iframe width="660" height="371.4" src="https://www.youtube.com/embed/ZU5gZeFMofM" class="os-is-iframe" data-media="screen">
<!-- no-selfclose -->
</iframe>
Expand Down Expand Up @@ -8286,7 +8286,7 @@ Through this inquiry, students will evaluate primary and secondary sources to ex
<p id="auto_e1363585-7bd7-4cf5-a3db-50c1647dfac4_fs-idm275328608">Watch this <a href="https://openstax.org/l/99ColonizeAmer2" target="_blank" rel="noopener nofollow">BRI Homework Help Video on The Colonization of America</a> for a review of the differences among the European colonies in the New World.</p>
<div data-type="media" id="auto_e1363585-7bd7-4cf5-a3db-50c1647dfac4_fs-idm265578080" data-alt="articles">
<div class="os-has-iframe os-has-link" data-type="switch">
<a class="os-is-link" href="https://openstax.org/books/llph-slug/pages/2-1--chapter-2-introductory-essay-16071763" target="_blank" rel="noopener nofollow" data-media="print">Access multimedia content</a>
<a class="os-is-link" data-needs-rex-link="true" href="/404-this-link-should-be-replaced" target="_blank" rel="noopener nofollow" data-media="print">Access multimedia content</a>
<iframe src="https://openstax.org/l/99ColonizeAmer2" class="os-is-iframe" data-media="screen">
<!-- no-selfclose -->
</iframe>
Expand Down Expand Up @@ -8366,7 +8366,7 @@ Through this inquiry, students will evaluate primary and secondary sources to ex
<p id="auto_e1363585-7bd7-4cf5-a3db-50c1647dfac4_fs-idm273017904">Watch this <a href="https://openstax.org/l/99SlaveryAmer" target="_blank" rel="noopener nofollow">BRI Homework Help Video on the Development of Slavery in North America</a> for a review of the main ideas covered in this section.</p>
<div data-type="media" id="auto_e1363585-7bd7-4cf5-a3db-50c1647dfac4_fs-idm272527504" data-alt="articles">
<div class="os-has-iframe os-has-link" data-type="switch">
<a class="os-is-link" href="https://openstax.org/books/llph-slug/pages/2-1--chapter-2-introductory-essay-16071763" target="_blank" rel="noopener nofollow" data-media="print">Access multimedia content</a>
<a class="os-is-link" data-needs-rex-link="true" href="/404-this-link-should-be-replaced" target="_blank" rel="noopener nofollow" data-media="print">Access multimedia content</a>
<iframe width="660" height="371.4" src="https://openstax.org/l/99SlaveryAmer" class="os-is-iframe" data-media="screen">
<!-- no-selfclose -->
</iframe>
Expand Down
10 changes: 5 additions & 5 deletions spec/recipes_spec/books/ap-physics-2e/expected_output.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@
<p id="auto_38303a4a-6f1d-464e-b350-9b6cafd4911d_fs-id1598123">Essential Knowledge 3.A.1 An observer in a particular reference frame can describe the motion of an object using such quantities as position, displacement, distance, velocity, speed, and acceleration.</p>
<div data-type="media" id="auto_38303a4a-6f1d-464e-b350-9b6cafd4911d_concept-trailer-2d-kinematics" data-alt="OpenStax Concept Trailer for Two-Dimensional Kinematics">
<div class="os-has-iframe os-has-link" data-type="switch">
<a class="os-is-link" href="https://openstax.org/books/physics-test/pages/1-connection-for-ap-r-courses" target="_blank" rel="noopener nofollow" data-media="print">Access multimedia content</a>
<a class="os-is-link" data-needs-rex-link="true" href="/404-this-link-should-be-replaced" target="_blank" rel="noopener nofollow" data-media="print">Access multimedia content</a>
<iframe src="https://www.youtube.com/embed/shFNnyLztWE" height="315" width="560" class="os-is-iframe" data-media="screen">
<!-- no-selfclose -->
</iframe>
Expand Down Expand Up @@ -601,7 +601,7 @@
<figure id="auto_137a5bbf-0ef3-4026-aa78-f61eb3ec87c2_eip-idm388746832">
<div data-type="media" id="auto_137a5bbf-0ef3-4026-aa78-f61eb3ec87c2_fs-id1167062339457" data-alt="This simulation allows you to adjust position, velocity, and acceleration of a ladybug.">
<div class="os-has-iframe os-has-link" data-type="switch">
<a class="os-is-link" href="https://openstax.org/books/physics-test/pages/1-1-kinematics-in-two-dimensions-an-introduction" target="_blank" rel="noopener nofollow" data-media="print">Access multimedia content</a>
<a class="os-is-link" data-needs-rex-link="true" href="/404-this-link-should-be-replaced" target="_blank" rel="noopener nofollow" data-media="print">Access multimedia content</a>
<iframe width="660" height="371.4" src="https://openstax.org/l/28ladybug" class="os-is-iframe" data-media="screen">
<!-- no-selfclose -->
</iframe>
Expand Down Expand Up @@ -1073,7 +1073,7 @@ is defined to be <span class="os-math-in-para"><m:math display="inline"><m:seman
<figure id="auto_5b264795-329f-44e0-8972-0133f0c98af0_eip-idm630235312">
<div data-type="media" id="auto_5b264795-329f-44e0-8972-0133f0c98af0_fs-id1167067104720" data-alt="In this game, students will use a vector arrow to explore position and velocity. The task is to get a ball into the pinpointed location without it hitting a wall.">
<div class="os-has-iframe os-has-link" data-type="switch">
<a class="os-is-link" href="https://openstax.org/books/physics-test/pages/1-2-vector-addition-and-subtraction-graphical-methods" target="_blank" rel="noopener nofollow" data-media="print">Access multimedia content</a>
<a class="os-is-link" data-needs-rex-link="true" href="/404-this-link-should-be-replaced" target="_blank" rel="noopener nofollow" data-media="print">Access multimedia content</a>
<iframe width="660" height="371.4" src="https://openstax.org/l/28mazegame" class="os-is-iframe" data-media="screen">
<!-- no-selfclose -->
</iframe>
Expand Down Expand Up @@ -3868,7 +3868,7 @@ size 12{A rSub { size 8{y} } } {}
<span data-type="newline"><br /></span></p>
<div data-type="media" id="auto_b7a2885d-9d3c-4e64-b382-b1da2b70d587_fs-id1167066847646" data-alt="This simulation provides the tools to experiment with adding vectors graphically.">
<div class="os-has-iframe os-has-link" data-type="switch">
<a class="os-is-link" href="https://openstax.org/books/physics-test/pages/1-3-vector-addition-and-subtraction-analytical-methods" target="_blank" rel="noopener nofollow" data-media="print">Access multimedia content</a>
<a class="os-is-link" data-needs-rex-link="true" href="/404-this-link-should-be-replaced" target="_blank" rel="noopener nofollow" data-media="print">Access multimedia content</a>
<iframe width="660" height="371.4" src="https://openstax.org/l/28vectoradd/" class="os-is-iframe" data-media="screen">
<!-- no-selfclose -->
</iframe>
Expand Down Expand Up @@ -8060,7 +8060,7 @@ size 12{t= { {2y} over { \( v rSub { size 8{0y} } +v rSub { size 8{y} } \) }
<figure id="auto_b46567e9-4987-49f5-ba7c-828a756c372c_eip-id1462984">
<div data-type="media" id="auto_b46567e9-4987-49f5-ba7c-828a756c372c_fs-id1167061400916" data-alt="This simulation provides an opportunity to learn through experimentation with projectiles.">
<div class="os-has-iframe os-has-link" data-type="switch">
<a class="os-is-link" href="https://openstax.org/books/physics-test/pages/1-4-projectile-motion" target="_blank" rel="noopener nofollow" data-media="print">Access multimedia content</a>
<a class="os-is-link" data-needs-rex-link="true" href="/404-this-link-should-be-replaced" target="_blank" rel="noopener nofollow" data-media="print">Access multimedia content</a>
<iframe width="660" height="371.4" src="https://openstax.org/l/28prjctilemtion" class="os-is-iframe" data-media="screen">
<!-- no-selfclose -->
</iframe>
Expand Down
Loading