Skip to content

Commit 042895b

Browse files
committed
WIP
1 parent c3c85fc commit 042895b

25 files changed

+145
-88
lines changed

.github/workflows/main.yml

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: CI
2-
on: [push, pull_request]
2+
on: [ push ]
33
env:
44
RUST_BACKTRACE: 1
55
jobs:
@@ -12,8 +12,7 @@ jobs:
1212
with:
1313
components: rustfmt
1414

15-
- name: Formatting
16-
run: cargo fmt --all -- --check
15+
- uses: Swatinem/rust-cache@v2
1716

1817
- name: Test
1918
run: |
@@ -22,3 +21,33 @@ jobs:
2221
cargo build --all --locked
2322
cargo clippy -- --deny warnings
2423
cargo test --all --locked
24+
25+
- name: Formatting
26+
run: cargo fmt --all -- --check
27+
28+
- name: Build website
29+
run: cargo run
30+
env:
31+
BASE_URL: "/www.rust-lang.org"
32+
33+
- name: Configure GitHub Pages
34+
uses: actions/configure-pages@v5
35+
36+
- name: Upload website
37+
uses: actions/upload-pages-artifact@v3
38+
with:
39+
path: html
40+
41+
deploy:
42+
needs: [ build ]
43+
environment:
44+
name: github-pages
45+
url: ${{ steps.deployment.outputs.page_url }}
46+
permissions:
47+
contents: read
48+
pages: write
49+
id-token: write
50+
runs-on: ubuntu-latest
51+
steps:
52+
- name: Deploy to GitHub Pages
53+
uses: actions/deploy-pages@v4

src/assets.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::BaseUrl;
12
use crate::fs::ensure_directory;
23
use anyhow::Context;
34
use sass_rs::{Options, compile_file};
@@ -22,7 +23,12 @@ fn relative_url(path: &Path, out_dir: &Path) -> anyhow::Result<String> {
2223
}
2324

2425
/// Compiles SASS file, stores it in `out_dir` and returns the relative URL to it.
25-
fn compile_sass(root_dir: &Path, out_dir: &Path, filename: &str) -> anyhow::Result<String> {
26+
fn compile_sass(
27+
root_dir: &Path,
28+
out_dir: &Path,
29+
filename: &str,
30+
base_url: &str,
31+
) -> anyhow::Result<String> {
2632
let scss_file = root_dir
2733
.join("src")
2834
.join("styles")
@@ -31,6 +37,7 @@ fn compile_sass(root_dir: &Path, out_dir: &Path, filename: &str) -> anyhow::Resu
3137
let css = compile_file(&scss_file, Options::default())
3238
.map_err(|e| anyhow::anyhow!("{e}"))
3339
.with_context(|| anyhow::anyhow!("couldn't compile sass: {}", scss_file.display()))?;
40+
let css = css.replace("$BASEURL", base_url);
3441

3542
let css_sha = format!("{filename}_{}", hash_string(&css));
3643
let out_css_path = out_dir
@@ -41,7 +48,7 @@ fn compile_sass(root_dir: &Path, out_dir: &Path, filename: &str) -> anyhow::Resu
4148
write_file(&out_css_path, &css.into_bytes())
4249
.with_context(|| anyhow::anyhow!("couldn't write css file: {}", out_css_path.display()))?;
4350

44-
Ok(relative_url(&out_css_path, &out_dir)?)
51+
relative_url(&out_css_path, out_dir)
4552
}
4653

4754
fn concat_files(
@@ -71,7 +78,7 @@ fn concat_files(
7178
write_file(Path::new(&out_file_path), concatted.as_bytes())
7279
.with_context(|| anyhow::anyhow!("couldn't write vendor {extension}"))?;
7380

74-
Ok(relative_url(&out_file_path, &out_dir)?)
81+
relative_url(&out_file_path, out_dir)
7582
}
7683

7784
fn concat_vendor_css(root_dir: &Path, out_dir: &Path, files: Vec<&str>) -> anyhow::Result<String> {
@@ -105,21 +112,21 @@ pub struct AssetFiles {
105112
pub fn compile_assets(
106113
root_dir: &Path,
107114
out_dir: &Path,
108-
baseurl: &str,
115+
base_url: &str,
109116
) -> anyhow::Result<AssetFiles> {
110-
let app_css_file = compile_sass(root_dir, out_dir, "app")?;
111-
let fonts_css_file = compile_sass(root_dir, out_dir, "fonts")?;
117+
let app_css_file = compile_sass(root_dir, out_dir, "app", base_url)?;
118+
let fonts_css_file = compile_sass(root_dir, out_dir, "fonts", base_url)?;
112119
let vendor_css_file = concat_vendor_css(root_dir, out_dir, vec!["tachyons"])?;
113120
let app_js_file = concat_app_js(root_dir, out_dir, vec!["tools-install"])?;
114121

115122
Ok(AssetFiles {
116123
css: CSSFiles {
117-
app: format!("{baseurl}{app_css_file}"),
118-
fonts: format!("{baseurl}{fonts_css_file}"),
119-
vendor: format!("{baseurl}{vendor_css_file}"),
124+
app: format!("{base_url}/{app_css_file}"),
125+
fonts: format!("{base_url}/{fonts_css_file}"),
126+
vendor: format!("{base_url}/{vendor_css_file}"),
120127
},
121128
js: JSFiles {
122-
app: format!("{baseurl}{app_js_file}"),
129+
app: format!("{base_url}/{app_js_file}"),
123130
},
124131
})
125132
}

src/i18n.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use handlebars::{
55
use serde::Serialize;
66
use std::{collections::HashSet, sync::LazyLock};
77

8+
use crate::ENGLISH;
89
use handlebars_fluent::{
910
fluent_bundle::{FluentResource, FluentValue, concurrent::FluentBundle},
1011
loader::SimpleLoader,
@@ -215,7 +216,7 @@ impl HelperDef for TeamHelper {
215216
let team_name = team.as_json()["name"].as_str().unwrap();
216217

217218
// English uses the team data directly, so that it gets autoupdated
218-
if lang == "en-US" {
219+
if lang == ENGLISH {
219220
let english = param.english(team.as_json());
220221
out.write(english)
221222
.map_err(|e| RenderErrorReason::NestedError(Box::new(e)))?;

src/main.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
#![allow(unused)]
2+
13
use crate::assets::compile_assets;
24
use crate::i18n::{TeamHelper, create_loader};
35
use crate::redirect::create_redirects;
46
use crate::render::{RenderCtx, render_directory, render_governance, render_index};
5-
use crate::rust_version::RustVersion;
7+
use crate::rust_version::fetch_rust_version;
68
use crate::teams::{encode_zulip_stream, load_rust_teams};
79
use anyhow::Context;
810
use handlebars::{DirectorySourceOptions, Handlebars};
@@ -24,11 +26,24 @@ const ZULIP_DOMAIN: &str = "https://rust-lang.zulipchat.com";
2426
static LAYOUT: &str = "components/layout";
2527
static ENGLISH: &str = "en-US";
2628

27-
fn baseurl(lang: &str) -> String {
28-
if lang == "en-US" {
29-
String::new()
30-
} else {
31-
format!("/{lang}")
29+
/// Relative base url from the root of the website
30+
/// `url` can be e.g. `` or `/foo-bar`.
31+
struct BaseUrl {
32+
url: String,
33+
}
34+
35+
impl BaseUrl {
36+
fn new(url: &str) -> Self {
37+
let url = url.strip_suffix('/').unwrap_or(url).to_string();
38+
Self { url }
39+
}
40+
41+
fn resolve(&self, lang: &str) -> String {
42+
if lang == ENGLISH {
43+
self.url.clone()
44+
} else {
45+
format!("{}/{lang}", self.url)
46+
}
3247
}
3348
}
3449

@@ -52,6 +67,9 @@ fn setup_handlebars() -> anyhow::Result<Handlebars<'static>> {
5267

5368
#[tokio::main]
5469
async fn main() -> anyhow::Result<()> {
70+
let base_url = std::env::var("BASE_URL").unwrap_or_else(|_| "".to_string());
71+
let base_url = BaseUrl::new(&base_url);
72+
5573
let rust_version = fetch_rust_version().await?;
5674
let teams = load_rust_teams().await?;
5775

@@ -61,7 +79,7 @@ async fn main() -> anyhow::Result<()> {
6179
std::fs::create_dir_all(&output_dir)?;
6280

6381
let root_dir = Path::new(".");
64-
let assets = compile_assets(root_dir, &output_dir, "/")?;
82+
let assets = compile_assets(root_dir, &output_dir, &base_url.resolve(ENGLISH))?;
6583
let handlebars = setup_handlebars()?;
6684

6785
let ctx = RenderCtx {
@@ -72,6 +90,7 @@ async fn main() -> anyhow::Result<()> {
7290
teams,
7391
handlebars,
7492
output_dir,
93+
base_url,
7594
};
7695
ctx.copy_asset_dir("static", "static")?;
7796
ctx.copy_asset_file(

src/render.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::fs::{copy_dir_all, ensure_directory};
33
use crate::i18n::{EXPLICIT_LOCALE_INFO, LocaleInfo, SUPPORTED_LOCALES};
44
use crate::rust_version::RustVersion;
55
use crate::teams::{PageData, RustTeams};
6-
use crate::{ENGLISH, LAYOUT, PONTOON_ENABLED, baseurl};
6+
use crate::{BaseUrl, ENGLISH, LAYOUT, PONTOON_ENABLED};
77
use anyhow::Context;
88
use handlebars::Handlebars;
99
use handlebars_fluent::{Loader, SimpleLoader};
@@ -72,6 +72,7 @@ pub struct RenderCtx<'a> {
7272
pub rust_version: RustVersion,
7373
pub teams: RustTeams,
7474
pub assets: AssetFiles,
75+
pub base_url: BaseUrl,
7576
}
7677

7778
impl<'a> RenderCtx<'a> {
@@ -95,8 +96,8 @@ impl<'a> RenderCtx<'a> {
9596
parent: LAYOUT,
9697
is_landing: false,
9798
data,
98-
baseurl: baseurl(&lang),
99-
is_translation: lang != "en-US",
99+
baseurl: self.base_url.resolve(lang),
100+
is_translation: lang != ENGLISH,
100101
lang: lang.to_string(),
101102
pontoon_enabled: PONTOON_ENABLED,
102103
assets: &self.assets,

src/styles/fonts.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ $format-names: (
119119
@function urls($filename, $range, $extensions) {
120120
$urls: ();
121121
@each $extension in $extensions {
122-
$url: url("/static/fonts/#{$extension}/#{$filename}.#{$range}.#{$extension}");
122+
$url: url("$BASEURL/static/fonts/#{$extension}/#{$filename}.#{$range}.#{$extension}");
123123
$format: format(map-get($format-names, $extension));
124124

125125
$urls: append($urls, $url $format, comma);

templates/404.html.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<h2 class="subtitle">{{fluent "error404-subtitle"}}</h2>
88
</div>
99
<div class="w-30-l w-100 mt5 mt0-l">
10-
<img id="ferris-error" src="/static/images/ferris-error.png" alt='{{fluent "error404-img-alt"}}'>
10+
<img id="ferris-error" src="{{baseurl}}/static/images/ferris-error.png" alt='{{fluent "error404-img-alt"}}'>
1111
</div>
1212
</div>
1313
</div>

templates/community/index.html.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
</ul>
171171
</div>
172172
</div>
173-
<img src="/static/images/rustfest.jpg" alt="{{fluent " community-rustfest-alt"}}">
173+
<img src="{{baseurl}}/static/images/rustfest.jpg" alt="{{fluent " community-rustfest-alt"}}">
174174
</div>
175175
</section>
176176

templates/components/footer.html.hbs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333
<div class="flex flex-column mw8 w-100 measure-wide-l pv2 pv5-m pv2-ns ph4-m ph4-l">
3434
<h4>{{fluent "footer-social"}}</h4>
3535
<div class="flex flex-row flex-wrap items-center">
36-
<a rel="me" href="https://social.rust-lang.org/@rust" target="_blank"><img src="/static/images/mastodon.svg"
36+
<a rel="me" href="https://social.rust-lang.org/@rust" target="_blank"><img src="{{baseurl}}/static/images/mastodon.svg"
3737
alt="{{fluent "mastodon"}}" title="{{fluent "mastodon"}}" /></a>
3838
<a rel="me" href="https://bsky.app/profile/rust-lang.org" target="_blank"><img
39-
src="/static/images/bluesky.svg" alt="{{fluent "bluesky"}}" title="{{fluent "bluesky"}}" /></a>
39+
src="{{baseurl}}/static/images/bluesky.svg" alt="{{fluent "bluesky"}}" title="{{fluent "bluesky"}}" /></a>
4040
<a href="https://www.youtube.com/channel/UCaYhcUwRBNscFNUKTjgPFiA" target="_blank"><img class="pv2"
41-
src="/static/images/youtube.svg" alt="{{fluent "footer-alt-youtube"}}" title="YouTube" /></a>
42-
<a href="https://github.com/rust-lang" target="_blank"><img src="/static/images/github.svg" alt="github logo"
41+
src="{{baseurl}}/static/images/youtube.svg" alt="{{fluent "footer-alt-youtube"}}" title="YouTube" /></a>
42+
<a href="https://github.com/rust-lang" target="_blank"><img src="{{baseurl}}/static/images/github.svg" alt="github logo"
4343
title="{{fluent "footer-github-alt"}}" /></a>
4444
</div>
4545
</div>

templates/components/layout.html.hbs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@
2828
<meta property="og:locale" content="en_US" />
2929

3030
<!-- styles -->
31-
<link rel="stylesheet" href="/static/styles/a11y-dark.css"/>
31+
<link rel="stylesheet" href="{{baseurl}}/static/styles/a11y-dark.css"/>
3232
<link rel="stylesheet" href="{{assets.css.vendor}}"/>
3333
<link rel="stylesheet" href="{{assets.css.fonts}}"/>
3434
<link rel="stylesheet" href="{{assets.css.app}}"/>
3535

3636
<!-- favicon -->
37-
<link rel="apple-touch-icon" sizes="180x180" href="/static/images/apple-touch-icon.png?v=ngJW8jGAmR">
38-
<link rel="icon" sizes="16x16" type="image/png" href="/static/images/favicon-16x16.png">
39-
<link rel="icon" sizes="32x32" type="image/png" href="/static/images/favicon-32x32.png">
40-
<link rel="icon" type="image/svg+xml" href="/static/images/favicon.svg">
41-
<link rel="manifest" href="/static/images/site.webmanifest?v=ngJW8jGAmR">
42-
<link rel="mask-icon" href="/static/images/safari-pinned-tab.svg?v=ngJW8jGAmR" color="#000000">
37+
<link rel="apple-touch-icon" sizes="180x180" href="{{baseurl}}/static/images/apple-touch-icon.png?v=ngJW8jGAmR">
38+
<link rel="icon" sizes="16x16" type="image/png" href="{{baseurl}}/static/images/favicon-16x16.png">
39+
<link rel="icon" sizes="32x32" type="image/png" href="{{baseurl}}/static/images/favicon-32x32.png">
40+
<link rel="icon" type="image/svg+xml" href="{{baseurl}}/static/images/favicon.svg">
41+
<link rel="manifest" href="{{baseurl}}/static/images/site.webmanifest?v=ngJW8jGAmR">
42+
<link rel="mask-icon" href="{{baseurl}}/static/images/safari-pinned-tab.svg?v=ngJW8jGAmR" color="#000000">
4343
<meta name="msapplication-TileColor" content="#ffffff">
44-
<meta name="msapplication-config" content="/static/images/browserconfig.xml?v=ngJW8jGAmR">
44+
<meta name="msapplication-config" content="{{baseurl}}/static/images/browserconfig.xml?v=ngJW8jGAmR">
4545
<meta name="theme-color" content="#ffffff">
4646

4747
{{#if is_landing}}
@@ -54,8 +54,8 @@
5454

5555
<!-- Custom Highlight pack with: Rust, Markdown, TOML, Bash, JSON, YAML,
5656
and plaintext. -->
57-
<script src="/static/scripts/highlight.pack.js" defer></script>
58-
<script src="/static/scripts/init.js" defer></script>
57+
<script src="{{baseurl}}/static/scripts/highlight.pack.js" defer></script>
58+
<script src="{{baseurl}}/static/scripts/init.js" defer></script>
5959
</head>
6060
<body>
6161
{{> components/nav}}
@@ -66,6 +66,6 @@
6666
{{#if pontoon_enabled}}
6767
<script src="https://pontoon.rust-lang.org/pontoon.js"></script>
6868
{{/if}}
69-
<script src="/static/scripts/languages.js"></script>
69+
<script src="{{baseurl}}/static/scripts/languages.js"></script>
7070
</body>
7171
</html>

0 commit comments

Comments
 (0)