Skip to content
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
17 changes: 17 additions & 0 deletions _includes/home/code_examples/cities.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% highlight ruby %}
# Ruby knows what you
# mean, even if you
# want to do math on
# an entire Array
cities = %w[ London
Oslo
Paris
Amsterdam
Berlin ]
visited = %w[Berlin Oslo]

puts "I still need " +
"to visit the " +
"following cities:",
cities - visited
{% endhighlight %}
18 changes: 18 additions & 0 deletions _includes/home/code_examples/greeter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% highlight ruby %}
# The Greeter class
class Greeter
def initialize(name)
@name = name.capitalize
end

def salute
puts "Hello #{@name}!"
end
end

# Create a new object
g = Greeter.new("world")

# Output "Hello World!"
g.salute
{% endhighlight %}
13 changes: 13 additions & 0 deletions _includes/home/code_examples/i_love_ruby.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% highlight ruby %}
# Output "I love Ruby"
say = "I love Ruby"
puts say

# Output "I *LOVE* RUBY"
say = say.sub("love", "*love*")
puts say.upcase

# Output "I *love* Ruby"
# five times
5.times { puts say }
{% endhighlight %}
8 changes: 5 additions & 3 deletions _includes/home/try_ruby.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
{% assign home = site.data.locales.home[page.lang] %}
{% assign examples = "i_love_ruby,cities,greeter" | split: "," %}

<!-- Try Ruby Section -->
<section id="try-ruby-section" class="try-ruby-section pb-0 relative opacity-0 max-h-0 overflow-hidden transition-all duration-700 ease-out">
<div class="container mx-auto px-6 lg:px-8">
<!-- Code Examples Grid -->
<div class="grid grid-cols-1 lg:grid-cols-3 gap-8 lg:gap-12 2xl:gap-16 mb-16">
{% for i in (1..3) %}
{% for example in examples %}
{% assign i = forloop.index %}
<div class="try-ruby-card group bg-white dark:bg-stone-900 rounded-2xl overflow-hidden flex flex-col shadow-sm border border-gold-400 dark:border-gold-500 has-[a:hover]:border-ruby-600 dark:has-[a:hover]:border-ruby-600 transition-colors box-border">
<!-- Code Area -->
<div class="code-example-wrapper relative flex-grow min-h-[200px]">
<!-- Code Content (will fade in after frame animation) -->
<!-- Code Content (inlined at build time) -->
<div id="try-ruby-example-{{ i }}" class="try-ruby-content code-example px-8 py-6 font-mono text-sm overflow-x-auto h-full opacity-0 transition-opacity duration-500">
<!-- Code will be loaded dynamically -->
{% include home/code_examples/{{ example }}.html %}
</div>
</div>

Expand Down
15 changes: 15 additions & 0 deletions _layouts/homepage.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@
<script type="text/javascript" src="/javascripts/theme-toggle.js" defer></script>
<script type="text/javascript" src="/javascripts/try-ruby-examples.js"></script>
<script type="text/javascript" src="/javascripts/hero-animation.js" defer></script>

<!-- Fallback styles when JavaScript is disabled -->
<noscript>
<style>
#hero-loader { display: none !important; }
[data-hero-layer="gem"],
[data-hero-layer="content"],
[data-hero-layer="sunburst"],
[data-hero-layer="illust-main"],
[data-hero-layer="illust-sub"],
[data-hero-layer="illust-main"] img { opacity: 1 !important; }
#try-ruby-section { opacity: 1 !important; max-height: none !important; overflow: visible !important; }
.try-ruby-content { opacity: 1 !important; }
</style>
</noscript>
</head>

<body id="home-page-layout">
Expand Down
30 changes: 7 additions & 23 deletions javascripts/hero-animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ document.addEventListener('DOMContentLoaded', () => {
const totalImages = images.length;
let loadedImages = 0;

// Track loading states for hero images and Try Ruby
// Track loading state for hero images
// Note: TryRuby examples are now inlined at build time, no async loading needed
let heroImagesReady = false;
let tryRubyReady = false;

const layers = {
finalGem: document.querySelector('[data-hero-layer="gem"]'),
Expand All @@ -45,9 +45,9 @@ document.addEventListener('DOMContentLoaded', () => {
return;
}

// Check if both hero images and Try Ruby are ready
// Check if hero images are ready to start animation
const checkAllReady = () => {
if (heroImagesReady && tryRubyReady) {
if (heroImagesReady) {
// Update to 100% and start animation
if (loaderNumber) {
loaderNumber.textContent = '100';
Expand All @@ -58,8 +58,8 @@ document.addEventListener('DOMContentLoaded', () => {

const updateProgress = () => {
loadedImages++;
// Show progress up to 80% for hero images, reserve 20% for Try Ruby
const percentage = Math.round((loadedImages / totalImages) * 80);
// Show progress up to 100% for hero images
const percentage = Math.round((loadedImages / totalImages) * 100);
if (loaderNumber) {
loaderNumber.textContent = `${percentage}`;
}
Expand All @@ -82,23 +82,7 @@ document.addEventListener('DOMContentLoaded', () => {
heroImagesReady = true;
}

// Subscribe to Try Ruby examples loading
if (typeof TryRubyExamples !== 'undefined') {
TryRubyExamples.setOnReady(() => {
tryRubyReady = true;
// Update progress to show Try Ruby is loaded (80% -> 95%)
if (loaderNumber && !heroImagesReady) {
const currentPercent = parseInt(loaderNumber.textContent) || 0;
loaderNumber.textContent = `${Math.max(currentPercent, 95)}`;
}
checkAllReady();
});
} else {
// TryRubyExamples not available, skip waiting for it
tryRubyReady = true;
}

// Initial check in case both are already ready
// Initial check in case images are already loaded
checkAllReady();

function startAnimation() {
Expand Down
97 changes: 21 additions & 76 deletions javascripts/try-ruby-examples.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
"use strict";

var TryRubyExamples = {
loadedCount: 0,
totalExamples: 3,
isReady: false,
onReadyCallback: null,

extractCodeFromHtml: function(html) {
// Create a temporary element to parse the HTML
var temp = document.createElement('div');
temp.innerHTML = html;
isReady: true, // Examples are inlined at build time, always ready

extractCodeFromElement: function(element) {
// Find the code element and extract its text content
var codeElement = temp.querySelector('code');
var codeElement = element.querySelector('code');
if (!codeElement) {
return '';
}
Expand All @@ -26,25 +19,10 @@ var TryRubyExamples = {
return 'https://try.ruby-lang.org/playground/#code=' + encodedCode;
},

onExampleLoaded: function() {
TryRubyExamples.loadedCount++;

// When all examples are loaded, mark as ready and notify
if (TryRubyExamples.loadedCount >= TryRubyExamples.totalExamples) {
TryRubyExamples.isReady = true;
if (TryRubyExamples.onReadyCallback) {
TryRubyExamples.onReadyCallback();
}
}
},

// Set callback to be called when all examples are ready
// Since examples are inlined, call immediately
setOnReady: function(callback) {
TryRubyExamples.onReadyCallback = callback;
// If already ready, call immediately
if (TryRubyExamples.isReady) {
callback();
}
callback();
},

showAllExamples: function() {
Expand Down Expand Up @@ -88,59 +66,26 @@ var TryRubyExamples = {
}
},

loadExample: function(exampleName, targetId, buttonId) {
var lang = document.location.pathname.split('/')[1] || 'en';
var target = document.getElementById(targetId);
var button = document.getElementById(buttonId);

if (!target) {
console.error('Target element not found:', targetId);
return;
}

// Use fetch API to load the example
fetch('/' + lang + '/examples/' + exampleName + '/')
.then(function(response) {
if (!response.ok) {
throw new Error('Failed to load example: ' + exampleName);
}
return response.text();
})
.then(function(html) {
target.innerHTML = html;

// Update button URL if button exists
if (button) {
// Extract Ruby code from the loaded HTML
var rubyCode = TryRubyExamples.extractCodeFromHtml(html);
if (rubyCode) {
var tryRubyUrl = TryRubyExamples.generateTryRubyUrl(rubyCode);
button.href = tryRubyUrl;
}
// Set up TryRuby button URLs based on inlined code examples
setupButtons: function() {
for (var i = 1; i <= 3; i++) {
var exampleElement = document.getElementById('try-ruby-example-' + i);
var button = document.getElementById('try-ruby-button-' + i);

if (exampleElement && button) {
var rubyCode = TryRubyExamples.extractCodeFromElement(exampleElement);
if (rubyCode) {
var tryRubyUrl = TryRubyExamples.generateTryRubyUrl(rubyCode);
button.href = tryRubyUrl;
}

// Notify that this example has loaded
TryRubyExamples.onExampleLoaded();
})
.catch(function(error) {
console.error('Error loading example:', error);
target.innerHTML = '<p class="text-stone-500">Failed to load example</p>';
// Still count as loaded to prevent infinite loading state
TryRubyExamples.onExampleLoaded();
});
},

loadAll: function() {
// Load each example into its corresponding container
TryRubyExamples.loadExample('i_love_ruby', 'try-ruby-example-1', 'try-ruby-button-1');
TryRubyExamples.loadExample('cities', 'try-ruby-example-2', 'try-ruby-button-2');
TryRubyExamples.loadExample('greeter', 'try-ruby-example-3', 'try-ruby-button-3');
}
}
}
};

// Load examples when DOM is ready
// Set up button URLs when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', TryRubyExamples.loadAll);
document.addEventListener('DOMContentLoaded', TryRubyExamples.setupButtons);
} else {
TryRubyExamples.loadAll();
TryRubyExamples.setupButtons();
}
10 changes: 10 additions & 0 deletions stylesheets/compiled.css
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,16 @@ body:is(.dark *){
color: rgb(250 250 249 / var(--tw-text-opacity, 1));
}

/* Ensure syntax highlighting colors are not overridden by body color */

.highlight code {
color: inherit;
}

.highlight pre {
color: var(--color-text-default);
}

.container{
width: 100%;
margin-right: auto;
Expand Down
8 changes: 8 additions & 0 deletions stylesheets/tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@
@apply bg-white text-stone-900 text-base leading-5 font-default;
@apply dark:bg-stone-900 dark:text-stone-50;
}

/* Ensure syntax highlighting colors are not overridden by body color */
.highlight code {
color: inherit;
}
.highlight pre {
color: var(--color-text-default);
}
}
Loading