Skip to content

Commit

Permalink
fix(templ): fix wrong en-us-only
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Nov 8, 2024
1 parent fe7f5c2 commit 54d6359
Show file tree
Hide file tree
Showing 21 changed files with 92 additions and 52 deletions.
8 changes: 4 additions & 4 deletions crates/rari-doc/src/helpers/css_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub fn css_info_properties(
"animationType",
Cow::Owned(RariApi::link(
"/Web/CSS/CSS_animated_properties",
Some(locale),
locale,
Some(get_css_l10n_for_locale("animationType", locale)),
false,
None,
Expand Down Expand Up @@ -306,7 +306,7 @@ pub fn css_computed(locale: Locale) -> Result<String, DocError> {
let copy = l10n_json_data("Template", "xref_csscomputed", locale)?;
RariApi::link(
"/Web/CSS/computed_value",
Some(locale),
locale,
Some(copy),
false,
None,
Expand All @@ -318,7 +318,7 @@ pub fn css_inherited(locale: Locale) -> Result<String, DocError> {
let copy = l10n_json_data("Template", "xref_cssinherited", locale)?;
RariApi::link(
"/Web/CSS/inheritance",
Some(locale),
locale,
Some(copy),
false,
None,
Expand All @@ -330,7 +330,7 @@ pub fn css_inital(locale: Locale) -> Result<String, DocError> {
let copy = l10n_json_data("Template", "xref_cssinitial", locale)?;
RariApi::link(
"/Web/CSS/initial_value",
Some(locale),
locale,
Some(copy),
false,
None,
Expand Down
15 changes: 7 additions & 8 deletions crates/rari-doc/src/html/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tracing::warn;

use crate::error::DocError;
use crate::pages::page::{Page, PageLike};
use crate::resolve::locale_from_url;
use crate::templ::api::RariApi;
use crate::templ::templs::badges::{write_deprecated, write_experimental, write_non_standard};

Expand Down Expand Up @@ -81,19 +82,17 @@ pub fn render_link_from_page(
pub fn render_link_via_page(
out: &mut String,
link: &str,
locale: Option<Locale>,
locale: Locale,
content: Option<&str>,
code: bool,
title: Option<&str>,
with_badges: bool,
) -> Result<(), DocError> {
let mut url = Cow::Borrowed(link);
if let Some(link) = link.strip_prefix('/') {
if let Some(locale) = locale {
if !link.starts_with(Locale::default().as_url_str()) {
url = Cow::Owned(concat_strs!("/", locale.as_url_str(), "/docs/", link));
}
};
if locale_from_url(&url).is_none() {
url = Cow::Owned(concat_strs!("/", locale.as_url_str(), "/docs/", link));
}
let (url, anchor) = url.split_once('#').unwrap_or((&url, ""));
match RariApi::get_page(url) {
Ok(page) => {
Expand Down Expand Up @@ -122,9 +121,9 @@ pub fn render_link_via_page(
title,
&LinkModifier {
badges: if with_badges { page.status() } else { &[] },
badge_locale: locale.unwrap_or(page.locale()),
badge_locale: locale,
code,
only_en_us: page.locale() != locale.unwrap_or_default(),
only_en_us: page.locale() == Locale::EnUs && locale != Locale::EnUs,
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rari-doc/src/html/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ impl SidebarMetaEntry {
title,
} => {
let title = title.as_ref().map(|t| l10n.lookup(t.as_str(), locale));
render_link_via_page(out, link, Some(locale), title, self.code, None, true)?;
render_link_via_page(out, link, locale, title, self.code, None, true)?;
}
SidebarMetaEntryContent::Link { link: None, title } => {
let title = title.as_ref().map(|t| l10n.lookup(t.as_str(), locale));
Expand Down
31 changes: 31 additions & 0 deletions crates/rari-doc/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,37 @@ pub fn url_meta_from(url: &str) -> Result<UrlMeta<'_>, UrlError> {
})
}

/// Extracts the `Locale` from a given URL path.
///
/// This function takes a URL path as input and attempts to parse the first
/// path segment as a locale. If the first segment corresponds to a valid locale,
/// it returns `Some(Locale)`, otherwise, it returns `None`.
///
/// # Arguments
///
/// * `url` - A string slice that holds the URL path, potentially with a leading `/`.
///
/// # Returns
///
/// * `Option<Locale>` - `Some(Locale)` if the first path segment is a valid locale,
/// or `None` if it isn't.
///
/// # Examples
///
/// ```
/// # use rari_doc::resolve::locale_from_url;
/// # use rari_types::locale::Locale;
///
/// assert_eq!(locale_from_url("/en-US/some/path"), Some(Locale::EnUs));
/// assert_eq!(locale_from_url("fr/page"), Some(Locale::Fr));
/// assert_eq!(locale_from_url("invalid/path"), None);
/// ```
pub fn locale_from_url(url: &str) -> Option<Locale> {
let url = url.strip_prefix("/").unwrap_or(url);
url.split_once('/')
.and_then(|(l, _)| Locale::from_str(l).ok())
}

/// Builds a URL for a given slug, locale, and page category.
///
/// This function constructs a URL based on the provided slug, locale, and page category.
Expand Down
2 changes: 1 addition & 1 deletion crates/rari-doc/src/templ/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl RariApi {

pub fn link(
link: &str,
locale: Option<Locale>,
locale: Locale,
content: Option<&str>,
code: bool,
title: Option<&str>,
Expand Down
2 changes: 1 addition & 1 deletion crates/rari-doc/src/templ/templs/api_list_alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn api_list_alpha() -> Result<String, DocError> {
"<li>",
&RariApi::link(
page.url(),
Some(env.locale),
env.locale,
None,
true,
Some(page.short_title().unwrap_or(page.title())),
Expand Down
2 changes: 1 addition & 1 deletion crates/rari-doc/src/templ/templs/css_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ pub fn css_ref() -> Result<String, DocError> {
"<li>",
&RariApi::link(
&concat_strs!("/Web/CSS/", url.as_ref()),
Some(env.locale),
env.locale,
Some(&html_escape::encode_text(&label)),
true,
None,
Expand Down
2 changes: 1 addition & 1 deletion crates/rari-doc/src/templ/templs/firefox_for_developers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn generate_release_link<T: Display>(
render_link_via_page(
out,
&format!("/Mozilla/Firefox/Releases/{version}"),
Some(locale),
locale,
Some(&format!("Firefox {version} {for_developers}")),
false,
None,
Expand Down
2 changes: 1 addition & 1 deletion crates/rari-doc/src/templ/templs/glossary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn glossary(term_name: String, display_name: Option<String>) -> Result<Strin
);
RariApi::link(
&url,
Some(env.locale),
env.locale,
Some(&display_name.unwrap_or(term_name)),
false,
None,
Expand Down
9 changes: 8 additions & 1 deletion crates/rari-doc/src/templ/templs/links/csp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@ pub fn csp(directive: String) -> Result<String, DocError> {
"/{}/docs/Web/HTTP/Headers/Content-Security-Policy/{directive}",
env.locale.as_url_str()
);
RariApi::link(&url, None, Some(directive.as_ref()), true, None, false)
RariApi::link(
&url,
env.locale,
Some(directive.as_ref()),
true,
None,
false,
)
}
2 changes: 1 addition & 1 deletion crates/rari-doc/src/templ/templs/links/cssxref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ pub fn cssxref_internal(
} else {
maybe_display_name.to_string()
};
RariApi::link(&url, None, Some(&display_name), true, None, false)
RariApi::link(&url, locale, Some(&display_name), true, None, false)
}
2 changes: 1 addition & 1 deletion crates/rari-doc/src/templ/templs/links/domxref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn domxref(
let code = !no_code.map(|nc| nc.as_bool()).unwrap_or_default();
RariApi::link(
&url,
None,
env.locale,
Some(&display_with_fallback),
code,
display,
Expand Down
2 changes: 1 addition & 1 deletion crates/rari-doc/src/templ/templs/links/htmlxref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ pub fn htmlxref(
url.push_str(&anchor);
}

RariApi::link(&url, None, Some(display.as_ref()), code, None, false)
RariApi::link(&url, env.locale, Some(display.as_ref()), code, None, false)
}
10 changes: 6 additions & 4 deletions crates/rari-doc/src/templ/templs/links/http.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rari_templ_func::rari_f;
use rari_types::locale::Locale;
use rari_types::AnyArg;

use crate::error::DocError;
Expand All @@ -16,7 +17,7 @@ pub fn http_status(
env.locale.as_url_str(),
status
);
http(url, status, display, anchor, no_code)
http(url, status, display, anchor, no_code, env.locale)
}

#[rari_f]
Expand All @@ -31,7 +32,7 @@ pub fn http_header(
env.locale.as_url_str(),
status
);
http(url, status, display, anchor, no_code)
http(url, status, display, anchor, no_code, env.locale)
}

#[rari_f]
Expand All @@ -46,7 +47,7 @@ pub fn http_method(
env.locale.as_url_str(),
status
);
http(url, status, display, anchor, no_code)
http(url, status, display, anchor, no_code, env.locale)
}

fn http(
Expand All @@ -55,6 +56,7 @@ fn http(
display: Option<String>,
anchor: Option<String>,
no_code: Option<AnyArg>,
locale: Locale,
) -> Result<String, DocError> {
let mut display = display.unwrap_or(status.to_string());
if let Some(anchor) = anchor {
Expand All @@ -63,5 +65,5 @@ fn http(
display.push_str(&anchor);
}
let code = !no_code.map(|nc| nc.as_bool()).unwrap_or_default();
RariApi::link(&url, None, Some(display.as_ref()), code, None, false)
RariApi::link(&url, locale, Some(display.as_ref()), code, None, false)
}
2 changes: 1 addition & 1 deletion crates/rari-doc/src/templ/templs/links/jsxref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ pub fn jsxref(
}

let code = !no_code.map(|nc| nc.as_bool()).unwrap_or_default();
RariApi::link(&url, None, Some(display), code, None, false)
RariApi::link(&url, env.locale, Some(display), code, None, false)
}
2 changes: 1 addition & 1 deletion crates/rari-doc/src/templ/templs/links/mathmlxref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ pub fn mathmlxref(element_name: String) -> Result<String, DocError> {
element_name.as_str()
);

RariApi::link(&url, None, Some(&display), true, Some(&title), false)
RariApi::link(&url, env.locale, Some(&display), true, Some(&title), false)
}
2 changes: 1 addition & 1 deletion crates/rari-doc/src/templ/templs/links/svgattr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ pub fn svgattr(name: String) -> Result<String, DocError> {
name,
);

RariApi::link(&url, None, Some(&name), true, None, false)
RariApi::link(&url, env.locale, Some(&name), true, None, false)
}
2 changes: 1 addition & 1 deletion crates/rari-doc/src/templ/templs/links/svgxref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ pub fn svgxref_internal(element_name: &str, locale: Locale) -> Result<String, Do
element_name,
);

RariApi::link(&url, None, Some(display.as_ref()), true, None, false)
RariApi::link(&url, locale, Some(display.as_ref()), true, None, false)
}
2 changes: 1 addition & 1 deletion crates/rari-doc/src/templ/templs/links/webextapixref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn webextapixref(

RariApi::link(
&url,
None,
env.locale,
Some(display.as_ref()),
!no_code.map(|nc| nc.as_bool()).unwrap_or_default(),
None,
Expand Down
41 changes: 21 additions & 20 deletions crates/rari-doc/src/templ/templs/svginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,27 @@ pub fn svginfo() -> Result<String, DocError> {
));
out.push_str(r#"</td></tr>"#);

let (element_groups, elements): (Vec<String>, Vec<String>) =
info.content.elements.iter().try_fold(
(vec![], vec![]),
|(mut element_groups, mut elements), element| {
if element.contains("&lt;") {
let element_name = element.strip_prefix("&lt;").unwrap_or(element);
let element_name =
element_name.strip_suffix("&gt;").unwrap_or(element_name);
elements.push(svgxref_internal(element_name, env.locale)?)
} else {
let anchor = to_snake_case(element);
let url =
format!("/{}/docs/Web/SVG/Element#{anchor}", env.locale.as_url_str());
let display = l10n_json_data("SVG", element, env.locale).unwrap_or(element);
let link = RariApi::link(&url, None, Some(display), false, None, false)?;
element_groups.push(link);
}
Ok::<_, DocError>((element_groups, elements))
},
)?;
let (element_groups, elements): (Vec<String>, Vec<String>) = info
.content
.elements
.iter()
.try_fold(
(vec![], vec![]),
|(mut element_groups, mut elements), element| {
if element.contains("&lt;") {
let element_name = element.strip_prefix("&lt;").unwrap_or(element);
let element_name = element_name.strip_suffix("&gt;").unwrap_or(element_name);
elements.push(svgxref_internal(element_name, env.locale)?)
} else {
let anchor = to_snake_case(element);
let url = format!("/{}/docs/Web/SVG/Element#{anchor}", env.locale.as_url_str());
let display = l10n_json_data("SVG", element, env.locale).unwrap_or(element);
let link = RariApi::link(&url, env.locale, Some(display), false, None, false)?;
element_groups.push(link);
}
Ok::<_, DocError>((element_groups, elements))
},
)?;

out.extend([
r#"<tr><th scope="row">"#,
Expand Down
2 changes: 1 addition & 1 deletion crates/rari-doc/src/templ/templs/webext_all_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn web_ext_all_examples() -> Result<String, DocError> {
env.locale.as_url_str(),
&api.replace(' ', "_").replace("()", "").replace('.', "/"),
);
let link = RariApi::link(&url, None, None, true, None, false)?;
let link = RariApi::link(&url, env.locale, None, true, None, false)?;
out.push_str(&link);
out.push_str("<br/>")
}
Expand Down

0 comments on commit 54d6359

Please sign in to comment.