Skip to content

Commit 4675517

Browse files
committed
feat(templs): lots of sidebars
and fixes
1 parent fe7d2e8 commit 4675517

File tree

7 files changed

+171
-62
lines changed

7 files changed

+171
-62
lines changed

Diff for: crates/rari-doc/src/helpers/subpages.rs

+39-17
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pub fn write_li_with_badges(
6464
out: &mut impl Write,
6565
page: &Page,
6666
locale: Locale,
67+
closed: bool,
6768
) -> Result<(), DocError> {
6869
let locale_page = if locale != Default::default() {
6970
&url_path_to_page_with_other_locale_and_fallback(page.url(), Some(locale))?
@@ -85,33 +86,54 @@ pub fn write_li_with_badges(
8586
if page.status().contains(&FeatureStatus::Deprecated) {
8687
write_deprecated(out, locale)?;
8788
}
88-
Ok(write!(out, "</li>")?)
89+
if closed {
90+
write!(out, "</li>")?;
91+
}
92+
Ok(())
93+
}
94+
95+
pub fn list_sub_pages_reverse_internal(
96+
out: &mut String,
97+
url: &str,
98+
locale: Locale,
99+
sorter: Option<SubPagesSorter>,
100+
page_types: &[PageType],
101+
) -> Result<(), DocError> {
102+
let sub_pages = get_sub_pages(url, Some(1), sorter.unwrap_or_default())?;
103+
104+
for sub_page in sub_pages {
105+
if !page_types.is_empty() && !page_types.contains(&sub_page.page_type()) {
106+
continue;
107+
}
108+
write_li_with_badges(out, &sub_page, locale, true)?;
109+
}
110+
Ok(())
89111
}
90112

91113
pub fn list_sub_pages_internal(
92-
out: &mut impl Write,
114+
out: &mut String,
93115
url: &str,
94116
locale: Locale,
95117
depth: Option<usize>,
96-
reverse: bool,
97118
sorter: Option<SubPagesSorter>,
98119
page_types: &[PageType],
99120
) -> Result<(), DocError> {
100-
let sub_pages = get_sub_pages(url, depth, sorter.unwrap_or_default())?;
121+
let sub_pages = get_sub_pages(url, Some(1), sorter.unwrap_or_default())?;
101122

102-
if reverse {
103-
for sub_page in sub_pages.into_iter().rev() {
104-
if !page_types.is_empty() && !page_types.contains(&sub_page.page_type()) {
105-
continue;
106-
}
107-
write_li_with_badges(out, &sub_page, locale)?;
123+
let depth = depth.map(|i| i.saturating_sub(1));
124+
for sub_page in sub_pages {
125+
if !page_types.is_empty() && !page_types.contains(&sub_page.page_type()) {
126+
continue;
108127
}
109-
} else {
110-
for sub_page in sub_pages {
111-
if !page_types.is_empty() && !page_types.contains(&sub_page.page_type()) {
112-
continue;
113-
}
114-
write_li_with_badges(out, &sub_page, locale)?;
128+
let sub_sub_pages = get_sub_pages(sub_page.url(), depth, sorter.unwrap_or_default())?;
129+
if sub_sub_pages.is_empty() {
130+
write_li_with_badges(out, &sub_page, locale, true)?;
131+
} else {
132+
write_li_with_badges(out, &sub_page, locale, false)?;
133+
out.push_str("<ol>");
134+
list_sub_pages_internal(out, sub_page.url(), locale, depth, sorter, page_types)?;
135+
out.push_str("</ol>");
136+
out.push_str("</li>");
115137
}
116138
}
117139
Ok(())
@@ -154,7 +176,7 @@ pub fn list_sub_pages_grouped_internal(
154176
out.push_str("-*</summary><ol>");
155177
}
156178
for sub_page in group {
157-
write_li_with_badges(out, sub_page, locale)?;
179+
write_li_with_badges(out, sub_page, locale, true)?;
158180
}
159181
if keep_group {
160182
out.push_str("</ol></details></li>");

Diff for: crates/rari-doc/src/html/sidebar.rs

+35-35
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ fn cache_side_bar(sidebar: &str) -> bool {
3232
| "htmlsidebar"
3333
| "accessibilitysidebar"
3434
| "firefoxsidebar"
35+
| "webassemblysidebar"
36+
| "xsltsidebar"
37+
| "mdnsidebar"
38+
| "gamessidebar"
39+
| "mathmlref"
40+
| "pwasidebar"
3541
)
3642
}
3743

@@ -203,6 +209,8 @@ pub struct BasicEntry {
203209
pub code: bool,
204210
#[serde(default)]
205211
pub children: Vec<SidebarEntry>,
212+
#[serde(default)]
213+
pub details: Details,
206214
}
207215

208216
#[derive(Serialize, Deserialize, Default, Debug)]
@@ -214,7 +222,7 @@ pub struct SubPageEntry {
214222
#[serde(deserialize_with = "t_or_vec", default)]
215223
pub tags: Vec<PageType>,
216224
#[serde(default)]
217-
pub details: bool,
225+
pub details: Details,
218226
}
219227

220228
#[derive(Serialize, Deserialize, Default, Debug)]
@@ -227,7 +235,6 @@ pub struct WebExtApiEntry {
227235
#[serde(rename_all = "camelCase", tag = "type")]
228236
pub enum SidebarEntry {
229237
Section(BasicEntry),
230-
Details(BasicEntry),
231238
ListSubPages(SubPageEntry),
232239
ListSubPagesGrouped(SubPageEntry),
233240
WebExtApi(WebExtApiEntry),
@@ -265,7 +272,8 @@ impl Default for SidebarMetaEntryContent {
265272
}
266273
}
267274

268-
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
275+
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Default, PartialEq, Eq)]
276+
#[serde(rename_all = "camelCase")]
269277
pub enum Details {
270278
#[default]
271279
None,
@@ -283,16 +291,6 @@ impl Details {
283291
}
284292
}
285293

286-
impl From<bool> for Details {
287-
fn from(value: bool) -> Self {
288-
if value {
289-
Self::Closed
290-
} else {
291-
Self::None
292-
}
293-
}
294-
}
295-
296294
#[derive(Debug, Default)]
297295
pub struct SidebarMetaEntry {
298296
pub details: Details,
@@ -310,25 +308,10 @@ impl From<SidebarEntry> for SidebarMetaEntry {
310308
title,
311309
code,
312310
children,
311+
details,
313312
}) => SidebarMetaEntry {
314313
section: true,
315-
details: Details::None,
316-
code,
317-
content: SidebarMetaEntryContent::Link { link, title },
318-
children: if children.is_empty() {
319-
MetaChildren::None
320-
} else {
321-
MetaChildren::Children(children.into_iter().map(Into::into).collect())
322-
},
323-
},
324-
SidebarEntry::Details(BasicEntry {
325-
link,
326-
title,
327-
code,
328-
children,
329-
}) => SidebarMetaEntry {
330-
section: false,
331-
details: Details::Closed,
314+
details,
332315
code,
333316
content: SidebarMetaEntryContent::Link { link, title },
334317
children: if children.is_empty() {
@@ -345,7 +328,7 @@ impl From<SidebarEntry> for SidebarMetaEntry {
345328
path,
346329
}) => SidebarMetaEntry {
347330
section: false,
348-
details: details.into(),
331+
details,
349332
code: false,
350333
content: SidebarMetaEntryContent::Link { link, title },
351334
children: MetaChildren::ListSubPages(path, tags),
@@ -358,7 +341,7 @@ impl From<SidebarEntry> for SidebarMetaEntry {
358341
path,
359342
}) => SidebarMetaEntry {
360343
section: false,
361-
details: details.into(),
344+
details,
362345
code: false,
363346
content: SidebarMetaEntryContent::Link { link, title },
364347
children: MetaChildren::ListSubPagesGrouped(path, tags),
@@ -368,12 +351,17 @@ impl From<SidebarEntry> for SidebarMetaEntry {
368351
title,
369352
code,
370353
children,
354+
details,
371355
}) => SidebarMetaEntry {
372356
section: false,
373-
details: Details::None,
357+
details,
374358
code,
375359
content: SidebarMetaEntryContent::Link { link, title },
376-
children: MetaChildren::Children(children.into_iter().map(Into::into).collect()),
360+
children: if children.is_empty() {
361+
MetaChildren::None
362+
} else {
363+
MetaChildren::Children(children.into_iter().map(Into::into).collect())
364+
},
377365
},
378366
SidebarEntry::Link(link) => SidebarMetaEntry {
379367
section: false,
@@ -469,7 +457,7 @@ impl SidebarMetaEntry {
469457
}
470458
}
471459
MetaChildren::ListSubPages(url, page_types) => {
472-
list_sub_pages_internal(out, url, locale, Some(1), false, None, page_types)?
460+
list_sub_pages_internal(out, url, locale, Some(1), None, page_types)?
473461
}
474462
MetaChildren::ListSubPagesGrouped(url, page_types) => {
475463
list_sub_pages_grouped_internal(out, url, locale, None, page_types)?
@@ -495,3 +483,15 @@ impl SidebarMetaEntry {
495483
Ok(())
496484
}
497485
}
486+
487+
#[cfg(test)]
488+
mod test {
489+
use super::*;
490+
491+
#[test]
492+
fn test_details_ser() {
493+
let yaml_str = r#"details: closed"#;
494+
let entry: BasicEntry = serde_yaml::from_str(yaml_str).unwrap();
495+
assert_eq!(entry.details, Details::Closed);
496+
}
497+
}

Diff for: crates/rari-doc/src/templ/templs/listsubpages.rs

+25-9
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,35 @@ pub fn list_sub_pages(
1515
reverse: Option<AnyArg>,
1616
ordered: Option<AnyArg>,
1717
) -> Result<String, DocError> {
18+
let depth = depth.map(|d| d.as_int() as usize).unwrap_or(1);
1819
let url = url.as_deref().filter(|s| !s.is_empty()).unwrap_or(env.url);
1920
let ordered = ordered.as_ref().map(AnyArg::as_bool).unwrap_or_default();
2021
let mut out = String::new();
2122
out.push_str(if ordered { "<ol>" } else { "<ul>" });
22-
subpages::list_sub_pages_internal(
23-
&mut out,
24-
url,
25-
env.locale,
26-
Some(depth.map(|d| d.as_int() as usize).unwrap_or(1)),
27-
reverse.map(|r| r.as_int() != 0).unwrap_or_default(), // Yes the old marco checks for == 0 not === 0.
28-
Some(SubPagesSorter::SlugNatural),
29-
&[],
30-
)?;
23+
if reverse.map(|r| r.as_int() != 0).unwrap_or_default() {
24+
// Yes the old marco checks for == 0 not === 0.
25+
if depth > 1 {
26+
return Err(DocError::InvalidTempl(
27+
"listsubpages with reverse set and depth != 1".to_string(),
28+
));
29+
}
30+
subpages::list_sub_pages_reverse_internal(
31+
&mut out,
32+
url,
33+
env.locale,
34+
Some(SubPagesSorter::SlugNatural),
35+
&[],
36+
)?;
37+
} else {
38+
subpages::list_sub_pages_internal(
39+
&mut out,
40+
url,
41+
env.locale,
42+
Some(depth),
43+
Some(SubPagesSorter::SlugNatural),
44+
&[],
45+
)?;
46+
}
3147
out.push_str(if ordered { "</ol>" } else { "</ul>" });
3248

3349
Ok(out)

Diff for: crates/rari-doc/src/templ/templs/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub mod sidebars;
1818
pub mod specification;
1919
pub mod svginfo;
2020
pub mod web_ext_examples;
21+
pub mod xsltref;
2122

2223
use rari_types::globals::deny_warnings;
2324
use rari_types::{Arg, RariEnv};
@@ -50,6 +51,12 @@ pub fn invoke(
5051
| "htmlsidebar"
5152
| "accessibilitysidebar"
5253
| "firefoxsidebar"
54+
| "webassemblysidebar"
55+
| "xsltsidebar"
56+
| "mdnsidebar"
57+
| "gamessidebar"
58+
| "mathmlref"
59+
| "pwasidebar"
5360
);
5461
let f = match name.as_str() {
5562
"compat" => compat::compat_any,
@@ -64,6 +71,7 @@ pub fn invoke(
6471
"firefox_for_developers" => firefox_for_developers::firefox_for_developers_any,
6572
"js_property_attributes" => js_property_attributes::js_property_attributes_any,
6673
"svginfo" => svginfo::svginfo_any,
74+
"xsltref" => xsltref::xsltref_any,
6775

6876
// hacky
6977
"glossarydisambiguation" => glossarydisambiguation::glossarydisambiguation_any,
@@ -130,6 +138,12 @@ pub fn invoke(
130138
"htmlsidebar" => sidebars::htmlsidebar_any,
131139
"accessibilitysidebar" => sidebars::accessibilitysidebar_any,
132140
"firefoxsidebar" => sidebars::firefoxsidebar_any,
141+
"webassemblysidebar" => sidebars::webassemblysidebar_any,
142+
"xsltsidebar" => sidebars::xsltsidebar_any,
143+
"mdnsidebar" => sidebars::mdnsidebar_any,
144+
"gamessidebar" => sidebars::gamessidebar_any,
145+
"mathmlref" => sidebars::mathmlref_any,
146+
"pwasidebar" => sidebars::pwasidebar_any,
133147

134148
// unknown
135149
_ if deny_warnings() => return Err(DocError::UnknownMacro(ident.to_string())),

Diff for: crates/rari-doc/src/templ/templs/quick_links_with_subpages.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,13 @@ use crate::templ::legacy::fix_broken_legacy_url;
99
#[rari_f]
1010
pub fn quick_links_with_subpages(url: Option<String>) -> Result<String, DocError> {
1111
let url = url.map(|s| fix_broken_legacy_url(&s, env.locale).to_string());
12-
list_sub_pages(env, url, Some(AnyArg { value: Arg::Int(2) }), None, None)
12+
list_sub_pages(
13+
env,
14+
url,
15+
Some(AnyArg { value: Arg::Int(2) }),
16+
None,
17+
Some(AnyArg {
18+
value: Arg::Bool(true),
19+
}),
20+
)
1321
}

Diff for: crates/rari-doc/src/templ/templs/sidebars.rs

+30
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,33 @@ pub fn accessibilitysidebar() -> Result<String, DocError> {
6868
pub fn firefoxsidebar() -> Result<String, DocError> {
6969
sidebar::render_sidebar("firefoxsidebar", env.slug, env.locale)
7070
}
71+
72+
#[rari_f]
73+
pub fn webassemblysidebar() -> Result<String, DocError> {
74+
sidebar::render_sidebar("webassemblysidebar", env.slug, env.locale)
75+
}
76+
77+
#[rari_f]
78+
pub fn xsltsidebar() -> Result<String, DocError> {
79+
sidebar::render_sidebar("xsltsidebar", env.slug, env.locale)
80+
}
81+
82+
#[rari_f]
83+
pub fn mdnsidebar() -> Result<String, DocError> {
84+
sidebar::render_sidebar("mdnsidebar", env.slug, env.locale)
85+
}
86+
87+
#[rari_f]
88+
pub fn gamessidebar() -> Result<String, DocError> {
89+
sidebar::render_sidebar("gamessidebar", env.slug, env.locale)
90+
}
91+
92+
#[rari_f]
93+
pub fn mathmlref() -> Result<String, DocError> {
94+
sidebar::render_sidebar("mathmlref", env.slug, env.locale)
95+
}
96+
97+
#[rari_f]
98+
pub fn pwasidebar() -> Result<String, DocError> {
99+
sidebar::render_sidebar("pwasidebar", env.slug, env.locale)
100+
}

0 commit comments

Comments
 (0)