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
13 changes: 8 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
<link rel="canonical" href="https://romanzdk.github.io/rust-for-pythonistas/">
<meta name="theme-color" content="#c2410c">
<meta property="og:type" content="website">
<meta property="og:site_name" content="Rust for Pythonistas">
<meta property="og:locale" content="en_US">
<meta property="og:title" content="Rust for Pythonistas">
<meta property="og:description" content="Learn to read Rust and make small changes to it, using the Python you already know. Side-by-side examples, quizzes and runnable exercises.">
<meta property="og:url" content="https://romanzdk.github.io/rust-for-pythonistas/">
<meta property="og:image" content="https://romanzdk.github.io/rust-for-pythonistas/docs/images/social-preview.png">
<meta property="og:image:width" content="1280">
<meta property="og:image:height" content="640">
<meta property="og:image:alt" content="Rust for Pythonistas: the same function in Python and in Rust, side by side">
<meta name="twitter:card" content="summary_large_image">
<link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>🦀</text></svg>">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.css" integrity="sha384-zaeBlB/vwYsDRSlFajnDd7OydJ0cWk+c2OWybl3eSUf6hW2EbhlCsQPqKr3gkznT" crossorigin="anonymous" referrerpolicy="no-referrer">
<link rel="stylesheet" href="style.css">
<script>
// Apply a saved light/dark choice before the first paint, so the page never flashes the wrong theme.
Expand All @@ -42,10 +45,10 @@
MIT license
</footer>

<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/addon/mode/simple.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/mode/rust/rust.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js" integrity="sha384-F/bZzf7p3Joyp5psL90p/p89AZJsndkSoGwRpXcZhleCWhd8SnRuoYo4d0yirjJp" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.js" integrity="sha384-ZYmwuq4n2gOcNxMSiJ6jyTj+BbIrilr7p6dlq6q5nmSWKmsH9UU4K1qqjycMkfmR" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/addon/mode/simple.min.js" integrity="sha384-5+aYjV0V2W3IwhAYp/9WOrGMv1TaYkCjnkkW7Hv3yJQo28MergRCSRaUIUzDUs2J" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/mode/rust/rust.min.js" integrity="sha384-SR4yBoZPBNymEiWYb4h8YbjccmCDvVyzr5DYXq4QWt7E/voRHmUbr/ja617eM6Rh" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<!-- Lessons appear in this order. scripts/check.mjs reads this list too. -->
<script src="lib.js"></script>
Expand Down
28 changes: 27 additions & 1 deletion scripts/build-pages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ const esc = (s) => String(s).replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&l
const strip = (s) => String(s).replace(/<[^>]+>/g, '').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&').replace(/\s+/g, ' ').trim();
const pre = (label, src) => `<figure><figcaption>${esc(label)}</figcaption><pre><code>${esc(src)}</code></pre></figure>`;

function page({ slug, title, description, body }) {
// JSON for a <script> tag: "<" escaped so text in it can never close the tag.
const ldJson = (data) => `<script type="application/ld+json">${JSON.stringify(data).replace(/</g, '\\u003c')}</script>`;

function page({ slug, title, description, body, head = '' }) {
const url = slug ? `${SITE}${slug}/` : SITE;
const set = (s, re, value) => {
if (!re.test(s)) throw new Error(`index.html: ${re} not found`);
Expand All @@ -39,6 +42,7 @@ function page({ slug, title, description, body }) {
s = set(s, /<meta property="og:title" content="[^"]*">/, `<meta property="og:title" content="${esc(title)}">`);
s = set(s, /<meta property="og:description" content="[^"]*">/, `<meta property="og:description" content="${esc(description)}">`);
s = set(s, /<meta property="og:url" content="[^"]*">/, `<meta property="og:url" content="${url}">`);
if (head) s = set(s, /<\/head>/, `${head}\n</head>`);
if (slug) s = set(s, /<body>/, `<body data-page="${slug}">`);
s = set(s, /<main id="main"><\/main>/, `<main id="main">${body}</main>`);
const file = path.join(out, slug, 'index.html');
Expand All @@ -56,6 +60,19 @@ urls.push(
description: 'Learn to read Rust and make small changes to it, using the Python you already know. Side-by-side examples, quizzes and runnable exercises.',
body: `<h1>Learn to read Rust using the Python you already know</h1><ol>${lessonLinks}</ol>
<p><a href="phrasebook/">Python → Rust phrasebook</a> · <a href="errors/">Rust compiler error index</a></p>`,
head: ldJson({
'@context': 'https://schema.org',
'@type': 'Course',
name: 'Rust for Pythonistas',
description: 'Learn to read Rust and make small changes to it, using the Python you already know.',
url: SITE,
inLanguage: 'en',
isAccessibleForFree: true,
educationalLevel: 'Beginner',
teaches: 'Reading and changing Rust code, for Python developers',
provider: { '@type': 'Person', name: 'Roman Žydyk', url: 'https://romanzdk.com' },
hasPart: LESSONS.map((l) => ({ '@type': 'LearningResource', name: strip(l.title), url: `${SITE}${l.id}/` })),
}),
}),
);

Expand Down Expand Up @@ -93,6 +110,15 @@ urls.push(
}),
);

// GitHub Pages serves this for any missing URL, at any depth: absolute base, not indexed, the app shows the course start.
fs.writeFileSync(
path.join(out, '404.html'),
html
.replace('<meta charset="utf-8">', '<meta charset="utf-8">\n <base href="/rust-for-pythonistas/">\n <meta name="robots" content="noindex">')
.replace(/<title>[^<]*<\/title>/, '<title>Page not found · Rust for Pythonistas</title>')
.replace(/\s*<link rel="canonical" href="[^"]*">/, ''),
);

fs.writeFileSync(path.join(out, 'sitemap.xml'), `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${urls.map((u) => ` <url><loc>${u}</loc></url>`).join('\n')}
Expand Down
Loading