Skip to content

Commit b5c7db8

Browse files
committed
Handle base URL in fonts and allow changing it
1 parent b51ddd7 commit b5c7db8

File tree

6 files changed

+51
-22
lines changed

6 files changed

+51
-22
lines changed

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);

static/scripts/languages.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ function langChange() {
2121

2222
nav_dropdown.onchange = langChange;
2323
footer_dropdown.onchange = langChange;
24+
// TODO: language redirect

0 commit comments

Comments
 (0)