Skip to content

Commit 18b23df

Browse files
Core 845 postbake rex link (#406)
* Remove rex_link and slugify Add a formatted anchor that will be updated in postbake * Update tests and snapshots * Update changelog
1 parent f1f99d7 commit 18b23df

File tree

25 files changed

+171
-350
lines changed

25 files changed

+171
-350
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88

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

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

lib/kitchen/directions/bake_iframes/v1.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@ 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 = \
14-
begin
15-
iframe.rex_link
16-
rescue StandardError
17-
warn "Unable to find rex link for iframe #{iframe}"
18-
iframe[:src]
19-
end
13+
iframe_link = '/404-this-link-should-be-replaced'
14+
2015
iframe.wrap('<div class="os-has-iframe" data-type="switch">')
2116
iframe.add_class('os-is-iframe')
2217

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

2621
iframe.prepend(child:
2722
<<~HTML
28-
<a class="os-is-link" href="#{iframe_link}" target="_blank" rel="noopener nofollow">#{I18n.t(:iframe_link_text)}</a>
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>
2924
HTML
3025
)
3126

lib/kitchen/element_base.rb

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

918-
def rex_link
919-
self[:'data-is-for-rex-linking'] = 'true'
920-
921-
element_with_ancestors = document.book.chapters.search_with(
922-
Kitchen::PageElementEnumerator, Kitchen::CompositePageElementEnumerator
923-
).search('[data-is-for-rex-linking="true"]').first
924-
925-
remove_attribute('data-is-for-rex-linking')
926-
927-
unless element_with_ancestors
928-
raise("Cannot create rex link to element #{self} - needs ancestors of both types chapter & page/composite_page" \
929-
"#{say_source_or_nil}"
930-
)
931-
end
932-
933-
book_slug = document.slug
934-
chapter_count = element_with_ancestors.ancestor(:chapter).count_in(:book)
935-
page_string = ''
936-
page_title = ''
937-
page = element_with_ancestors.ancestor(:page) if element_with_ancestors.has_ancestor?(:page)
938-
if page&.is_introduction?
939-
page_title = page.first('[data-type="document-title"]').text.slugify
940-
elsif page
941-
page_string = "#{page.count_in(:chapter) - 1}-"
942-
page_title = page.title_text.slugify
943-
else
944-
page = element_with_ancestors.ancestor(:composite_page)
945-
page_title = page.title.text.slugify
946-
end
947-
948-
"https://openstax.org/books/#{book_slug}/pages/#{chapter_count}-#{page_string}#{page_title}"
949-
end
950-
951918
def add_platform_media(format)
952919
valid_formats = %w[screen print screenreader]
953920
raise "Media format invalid; valid formats are #{valid_formats}" unless valid_formats.include?(format)

lib/kitchen/patches/string.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,4 @@ 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
2713
end

spec/kitchen_spec/directions/bake_iframes/v1_spec.rb

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -63,48 +63,4 @@
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
11066
end

spec/kitchen_spec/element_base_spec.rb

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -624,91 +624,6 @@
624624
end
625625
end
626626

627-
describe '#rex_link' do
628-
let(:book_rex_linkable) do
629-
book_containing(html:
630-
<<~HTML
631-
<div data-type="metadata" style="display: none;">
632-
<h1 data-type="document-title" itemprop="name">Title Of The Book</h1>
633-
<span data-type="slug" data-value="test-book-slug"></span>
634-
</div>
635-
<div data-type="page" id="not-in-chapter"></div>
636-
<div data-type="chapter">
637-
<div data-type="page" class="introduction">
638-
<div data-type="metadata" style="display: none;">
639-
<h1 data-type="document-title" itemprop="name">Introduction: Fre Sha Vocado</h1>
640-
</div>
641-
<div class="intro-text">
642-
<h2 data-type="document-title">
643-
<span data-type="" itemprop="" class="os-text">Introduction</span>
644-
</h2>
645-
</div>
646-
<div id="element1"></div>
647-
</div>
648-
<div data-type="page">
649-
<div data-type="metadata" style="display: none;">
650-
<h1 data-type="document-title" itemprop="name">Test Page: It's, An, Avocado</h1>
651-
</div>
652-
<h2 data-type="document-title">
653-
<span class="os-number">1.1</span>
654-
<span class="os-divider"> </span>
655-
<span data-type="" itemprop="" class="os-text">Test Page: It's, An, Avocado</span>
656-
</h2>
657-
<div id="element2"></div>
658-
</div>
659-
<div data-type="page">
660-
<div data-type="metadata" style="display: none;">
661-
<h1 data-type="document-title" itemprop="name">I have hyphen — and latin letter ć</h1>
662-
</div>
663-
<h2 data-type="document-title">
664-
<span class="os-number">1.2</span>
665-
<span class="os-divider"> </span>
666-
<span data-type="" itemprop="" class="os-text">I have hyphen — and latin letter ć</span>
667-
</h2>
668-
<div id="element4"></div>
669-
</div>
670-
</div>
671-
<div data-type="chapter">
672-
<div class="os-eoc os-summary-container" data-type="composite-page" data-uuid-key=".summary" id="composite-page-1">
673-
<h2 data-type="document-title">
674-
<span class="os-text">Summary Or Something</span>
675-
</h2>
676-
<div data-type="metadata" style="display: none;">
677-
<h1 data-type="document-title" itemprop="name">Summary</h1>
678-
<span data-type="slug" data-value="introduction-political-science"></span>
679-
</div>
680-
<div id="element3"></div>
681-
</div>
682-
HTML
683-
)
684-
end
685-
686-
it 'returns rex link for element in section page' do
687-
expect(book_rex_linkable.first('div#element2').rex_link).to \
688-
eq('https://openstax.org/books/test-book-slug/pages/1-1-test-page-its-an-avocado')
689-
end
690-
691-
it 'returns rex link for element in intro page' do
692-
expect(book_rex_linkable.first('div#element1').rex_link).to \
693-
eq('https://openstax.org/books/test-book-slug/pages/1-introduction-fre-sha-vocado')
694-
end
695-
696-
it 'returns rex link for element in composite page' do
697-
expect(book_rex_linkable.first('div#element3').rex_link).to \
698-
eq('https://openstax.org/books/test-book-slug/pages/2-summary-or-something')
699-
end
700-
701-
it 'returns rex link for element with hyphen and latin letter' do
702-
expect(book_rex_linkable.first('div#element4').rex_link).to \
703-
eq('https://openstax.org/books/test-book-slug/pages/1-2-i-have-hyphen-and-latin-letter-c')
704-
end
705-
706-
it 'raises error when ancestors can\'t be found' do
707-
expect { book_rex_linkable.pages('$#not-in-chapter').first.rex_link }.to \
708-
raise_error(/Cannot create rex link to element[^>]+id="not-in-chapter"/)
709-
end
710-
end
711-
712627
describe '#add_platform_media' do
713628
let(:no_media) do
714629
book_containing(html:

spec/recipes_spec/books/ap-history/expected_output.xhtml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ Europeans believed they discovered an entirely new place when they encountered t
12061206
<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>
12071207
<div data-type="media" id="auto_22e8aac4-82ad-474f-867d-3059dfde7829_fs-idm405412048" data-alt="articles">
12081208
<div class="os-has-iframe os-has-link" data-type="switch">
1209-
<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>
1209+
<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>
12101210
<iframe width="660" height="371.4" src="https://www.youtube.com/embed/e8my1VY3OGA" class="os-is-iframe" data-media="screen">
12111211
<!-- no-selfclose -->
12121212
</iframe>
@@ -2786,7 +2786,7 @@ Europeans believed they discovered an entirely new place when they encountered t
27862786
<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>
27872787
<div data-type="media" id="auto_ff3d527a-7f54-43b3-bc09-13d4a43894b4_fs-idm226476080" data-alt="articles">
27882788
<div class="os-has-iframe os-has-link" data-type="switch">
2789-
<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>
2789+
<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>
27902790
<iframe width="660" height="371.4" src="https://www.youtube.com/embed/ZU5gZeFMofM" class="os-is-iframe" data-media="screen">
27912791
<!-- no-selfclose -->
27922792
</iframe>
@@ -8286,7 +8286,7 @@ Through this inquiry, students will evaluate primary and secondary sources to ex
82868286
<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>
82878287
<div data-type="media" id="auto_e1363585-7bd7-4cf5-a3db-50c1647dfac4_fs-idm265578080" data-alt="articles">
82888288
<div class="os-has-iframe os-has-link" data-type="switch">
8289-
<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>
8289+
<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>
82908290
<iframe src="https://openstax.org/l/99ColonizeAmer2" class="os-is-iframe" data-media="screen">
82918291
<!-- no-selfclose -->
82928292
</iframe>
@@ -8366,7 +8366,7 @@ Through this inquiry, students will evaluate primary and secondary sources to ex
83668366
<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>
83678367
<div data-type="media" id="auto_e1363585-7bd7-4cf5-a3db-50c1647dfac4_fs-idm272527504" data-alt="articles">
83688368
<div class="os-has-iframe os-has-link" data-type="switch">
8369-
<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>
8369+
<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>
83708370
<iframe width="660" height="371.4" src="https://openstax.org/l/99SlaveryAmer" class="os-is-iframe" data-media="screen">
83718371
<!-- no-selfclose -->
83728372
</iframe>

spec/recipes_spec/books/ap-physics-2e/expected_output.xhtml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@
443443
<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>
444444
<div data-type="media" id="auto_38303a4a-6f1d-464e-b350-9b6cafd4911d_concept-trailer-2d-kinematics" data-alt="OpenStax Concept Trailer for Two-Dimensional Kinematics">
445445
<div class="os-has-iframe os-has-link" data-type="switch">
446-
<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>
446+
<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>
447447
<iframe src="https://www.youtube.com/embed/shFNnyLztWE" height="315" width="560" class="os-is-iframe" data-media="screen">
448448
<!-- no-selfclose -->
449449
</iframe>
@@ -601,7 +601,7 @@
601601
<figure id="auto_137a5bbf-0ef3-4026-aa78-f61eb3ec87c2_eip-idm388746832">
602602
<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.">
603603
<div class="os-has-iframe os-has-link" data-type="switch">
604-
<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>
604+
<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>
605605
<iframe width="660" height="371.4" src="https://openstax.org/l/28ladybug" class="os-is-iframe" data-media="screen">
606606
<!-- no-selfclose -->
607607
</iframe>
@@ -1073,7 +1073,7 @@ is defined to be <span class="os-math-in-para"><m:math display="inline"><m:seman
10731073
<figure id="auto_5b264795-329f-44e0-8972-0133f0c98af0_eip-idm630235312">
10741074
<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.">
10751075
<div class="os-has-iframe os-has-link" data-type="switch">
1076-
<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>
1076+
<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>
10771077
<iframe width="660" height="371.4" src="https://openstax.org/l/28mazegame" class="os-is-iframe" data-media="screen">
10781078
<!-- no-selfclose -->
10791079
</iframe>
@@ -3868,7 +3868,7 @@ size 12{A rSub { size 8{y} } } {}
38683868
<span data-type="newline"><br /></span></p>
38693869
<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.">
38703870
<div class="os-has-iframe os-has-link" data-type="switch">
3871-
<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>
3871+
<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>
38723872
<iframe width="660" height="371.4" src="https://openstax.org/l/28vectoradd/" class="os-is-iframe" data-media="screen">
38733873
<!-- no-selfclose -->
38743874
</iframe>
@@ -8060,7 +8060,7 @@ size 12{t= { {2y} over { \( v rSub { size 8{0y} } +v rSub { size 8{y} } \) }
80608060
<figure id="auto_b46567e9-4987-49f5-ba7c-828a756c372c_eip-id1462984">
80618061
<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.">
80628062
<div class="os-has-iframe os-has-link" data-type="switch">
8063-
<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>
8063+
<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>
80648064
<iframe width="660" height="371.4" src="https://openstax.org/l/28prjctilemtion" class="os-is-iframe" data-media="screen">
80658065
<!-- no-selfclose -->
80668066
</iframe>

0 commit comments

Comments
 (0)