Skip to content

Commit

Permalink
Merge pull request #1971 from rust-lang/remo/quqswvxktukm
Browse files Browse the repository at this point in the history
Update to handlebars v5
  • Loading branch information
Manishearth authored May 24, 2024
2 parents a3fae35 + 2c7dfee commit 3103b07
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 52 deletions.
54 changes: 32 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ edition = "2018"
[dependencies]
lazy_static = "1.2.0"
fluent = "0.16"
fluent-bundle = "0.11.0"
fluent-bundle = "0.15.0"
fluent-syntax = "0.11.0"
fluent-locale = "0.10.1"
handlebars-fluent = "0.3.1"
handlebars-fluent = "0.4.0"
rand = "0.8"
regex = "1"
rocket = "0.5.0"
rocket_dyn_templates = { version = "0.1.0", features = ["handlebars"] }
rocket = "0.5.1"
rocket_dyn_templates = { version = "0.2.0", features = ["handlebars"] }
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8.17"
sass-rs = "0.2.1"
reqwest = { version = "0.11.4", features = ["json"] }
toml = "0.5"
serde_json = "1.0"
rust_team_data = { git = "https://github.com/rust-lang/team" }
handlebars = "4.3.0"
handlebars = "5.1.0"
siphasher = "0.3.6"
percent-encoding = "2.1.0"
45 changes: 27 additions & 18 deletions src/i18n.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use handlebars::{
Context, Handlebars, Helper, HelperDef, HelperResult, Output, RenderContext, RenderError,
Context, Handlebars, Helper, HelperDef, HelperResult, Output, RenderContext, RenderErrorReason,
};

use rocket::request::FromParam;
Expand Down Expand Up @@ -149,55 +149,64 @@ impl TeamHelperParam {
impl HelperDef for TeamHelper {
fn call<'reg: 'rc, 'rc>(
&self,
h: &Helper<'reg, 'rc>,
h: &Helper<'rc>,
_: &'reg Handlebars,
context: &'rc Context,
rcx: &mut RenderContext<'reg, 'rc>,
out: &mut dyn Output,
) -> HelperResult {
let Some(name) = h.param(0) else {
return Err(RenderError::new(
return Err(RenderErrorReason::ParamNotFoundForIndex(
"{{team-text}} must have at least two parameters",
));
0,
)
.into());
};
let Some(name) = name.relative_path() else {
return Err(RenderError::new(
return Err(RenderErrorReason::InvalidParamType(
"{{team-text}} takes only identifier parameters",
));
)
.into());
};

let Some(id) = h.param(1) else {
return Err(RenderError::new(
return Err(RenderErrorReason::ParamNotFoundForIndex(
"{{team-text}} must have at least two parameters",
));
1,
)
.into());
};
let Some(id) = id.relative_path() else {
return Err(RenderError::new(
return Err(RenderErrorReason::InvalidParamType(
"{{team-text}} takes only identifier parameters",
));
)
.into());
};

let param = match id.as_str() {
"name" => TeamHelperParam::Name,
"description" => TeamHelperParam::Description,
"role" => {
let Some(role_id) = h.param(2) else {
return Err(RenderError::new(
return Err(RenderErrorReason::ParamNotFoundForIndex(
"{{team-text}} requires a third parameter for the role id",
));
2,
)
.into());
};
TeamHelperParam::Role(role_id.value().as_str().unwrap().to_owned())
}
unrecognized => {
return Err(RenderError::new(format!(
return Err(RenderErrorReason::Other(format!(
"unrecognized {{{{team-text}}}} param {unrecognized:?}",
)));
))
.into());
}
};

let team = rcx
.evaluate(context, name)
.map_err(|e| RenderError::from_error(&format!("Cannot find team {}", name), e))?;
.map_err(|e| RenderErrorReason::NestedError(Box::new(e)))?;
let lang = context
.data()
.get("lang")
Expand All @@ -210,18 +219,18 @@ impl HelperDef for TeamHelper {
if lang == "en-US" {
let english = param.english(team.as_json());
out.write(english)
.map_err(|e| RenderError::from_error("failed to render English team data", e))?;
.map_err(|e| RenderErrorReason::NestedError(Box::new(e)))?;
} else if let Some(value) = self.i18n.lookup_no_default_fallback(
&lang.parse().expect("language must be valid"),
&param.fluent_id(team_name),
None,
) {
out.write(&value)
.map_err(|e| RenderError::from_error("failed to render translated team data", e))?;
.map_err(|e| RenderErrorReason::NestedError(Box::new(e)))?;
} else {
let english = param.english(team.as_json());
out.write(english)
.map_err(|e| RenderError::from_error("failed to render", e))?;
.map_err(|e| RenderErrorReason::NestedError(Box::new(e)))?;
}
Ok(())
}
Expand Down
19 changes: 13 additions & 6 deletions src/teams.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use handlebars::{Context, Handlebars, Helper, HelperResult, Output, RenderContext, RenderError};
use handlebars::{
Context, Handlebars, Helper, HelperResult, Output, RenderContext, RenderErrorReason,
};
use percent_encoding::{utf8_percent_encode, AsciiSet, NON_ALPHANUMERIC};
use rust_team_data::v1::{Team, TeamKind, Teams, BASE_URL};
use std::cmp::Reverse;
Expand Down Expand Up @@ -212,16 +214,21 @@ pub fn encode_zulip_stream(
let zulip_stream = if let Some(p) = h.param(0) {
p.value()
} else {
return Err(RenderError::new(
return Err(RenderErrorReason::ParamNotFoundForIndex(
"{{encode-zulip-stream takes 1 parameter}}",
));
0,
)
.into());
};
let zulip_stream = if let Some(s) = zulip_stream.as_str() {
s
} else {
return Err(RenderError::new(
"{{encode-zulip-stream takes a string parameter}}",
));
return Err(RenderErrorReason::ParamTypeMismatchForName(
"encode-zulip-stream",
"0".into(),
"string".into(),
)
.into());
};

// https://github.com/zulip/zulip/blob/159641bab8c248f5b72a4e736462fb0b48e7fa24/static/js/hash_util.js#L20-L25
Expand Down
1 change: 0 additions & 1 deletion templates/learn/get-started.html.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,5 @@ fn main() {
src="/static/images/ferris.gif" />
</div>
</section>
{{> components/tools/install-script }}
{{/inline}}
{{~> (lookup this "parent")~}}

0 comments on commit 3103b07

Please sign in to comment.