Skip to content

Commit e287d44

Browse files
committed
fix(sidebars): support listsubpages with code
1 parent 48d540c commit e287d44

File tree

6 files changed

+96
-74
lines changed

6 files changed

+96
-74
lines changed

crates/rari-doc/src/helpers/subpages.rs

+40-18
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,24 @@ pub fn write_li_with_badges(
6363
out: &mut String,
6464
page: &Page,
6565
locale: Locale,
66+
code: bool,
6667
closed: bool,
6768
) -> Result<(), DocError> {
6869
let locale_page = if locale != Default::default() {
6970
&Page::from_url_with_locale_and_fallback(page.url(), locale)?
7071
} else {
7172
page
7273
};
73-
write!(
74-
out,
75-
"<li><a href=\"{}\">{}</a>",
74+
out.extend([
75+
"<li>",
76+
r#"<a href=""#,
7677
locale_page.url(),
77-
html_escape::encode_safe(locale_page.short_title().unwrap_or(locale_page.title()))
78-
)?;
78+
r#"">"#,
79+
if code { "<code>" } else { "" },
80+
&html_escape::encode_safe(locale_page.short_title().unwrap_or(locale_page.title())),
81+
if code { "</code>" } else { "" },
82+
"</a>",
83+
]);
7984
add_inline_badges(out, page, locale)?;
8085
if closed {
8186
write!(out, "</li>")?;
@@ -110,26 +115,37 @@ pub fn list_sub_pages_reverse_internal(
110115
locale: Locale,
111116
sorter: Option<SubPagesSorter>,
112117
page_types: &[PageType],
118+
code: bool,
113119
) -> Result<(), DocError> {
114120
let sub_pages = get_sub_pages(url, Some(1), sorter.unwrap_or_default())?;
115121

116122
for sub_page in sub_pages.iter().rev() {
117123
if !page_types.is_empty() && !page_types.contains(&sub_page.page_type()) {
118124
continue;
119125
}
120-
write_li_with_badges(out, sub_page, locale, true)?;
126+
write_li_with_badges(out, sub_page, locale, code, true)?;
121127
}
122128
Ok(())
123129
}
124130

131+
pub struct ListSubPagesContext<'a> {
132+
pub sorter: Option<SubPagesSorter>,
133+
pub page_types: &'a [PageType],
134+
pub code: bool,
135+
pub include_parent: bool,
136+
}
137+
125138
pub fn list_sub_pages_internal(
126139
out: &mut String,
127140
url: &str,
128141
locale: Locale,
129142
depth: Option<usize>,
130-
sorter: Option<SubPagesSorter>,
131-
page_types: &[PageType],
132-
include_parent: bool,
143+
ListSubPagesContext {
144+
sorter,
145+
page_types,
146+
code,
147+
include_parent,
148+
}: ListSubPagesContext<'_>,
133149
) -> Result<(), DocError> {
134150
let sub_pages = get_sub_pages(url, Some(1), sorter.unwrap_or_default())?;
135151
let depth = depth.map(|i| i.saturating_sub(1));
@@ -143,19 +159,22 @@ pub fn list_sub_pages_internal(
143159
}
144160
let sub_sub_pages = get_sub_pages(sub_page.url(), depth, sorter.unwrap_or_default())?;
145161
if sub_sub_pages.is_empty() {
146-
write_li_with_badges(out, &sub_page, locale, true)?;
162+
write_li_with_badges(out, &sub_page, locale, code, true)?;
147163
} else {
148-
write_li_with_badges(out, &sub_page, locale, false)?;
164+
write_li_with_badges(out, &sub_page, locale, code, false)?;
149165
out.push_str("<ol>");
150166

151167
list_sub_pages_internal(
152168
out,
153169
sub_page.url(),
154170
locale,
155171
depth,
156-
sorter,
157-
page_types,
158-
include_parent,
172+
ListSubPagesContext {
173+
sorter,
174+
page_types,
175+
code,
176+
include_parent,
177+
},
159178
)?;
160179
out.push_str("</ol>");
161180
out.push_str("</li>");
@@ -168,9 +187,12 @@ pub fn list_sub_pages_grouped_internal(
168187
out: &mut String,
169188
url: &str,
170189
locale: Locale,
171-
sorter: Option<SubPagesSorter>,
172-
page_types: &[PageType],
173-
include_parent: bool,
190+
ListSubPagesContext {
191+
sorter,
192+
page_types,
193+
code,
194+
include_parent,
195+
}: ListSubPagesContext<'_>,
174196
) -> Result<(), DocError> {
175197
let sub_pages = get_sub_pages(url, None, sorter.unwrap_or_default())?;
176198

@@ -206,7 +228,7 @@ pub fn list_sub_pages_grouped_internal(
206228
out.push_str("-*</summary><ol>");
207229
}
208230
for sub_page in group {
209-
write_li_with_badges(out, sub_page, locale, true)?;
231+
write_li_with_badges(out, sub_page, locale, code, true)?;
210232
}
211233
if keep_group {
212234
out.push_str("</ol></details></li>");

crates/rari-doc/src/html/sidebar.rs

+43-16
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ use super::rewriter::post_process_html;
2020
use crate::cached_readers::read_sidebar;
2121
use crate::error::DocError;
2222
use crate::helpers;
23-
use crate::helpers::subpages::{list_sub_pages_grouped_internal, list_sub_pages_internal};
23+
use crate::helpers::subpages::{
24+
list_sub_pages_grouped_internal, list_sub_pages_internal, ListSubPagesContext,
25+
};
2426
use crate::pages::page::{Page, PageLike};
2527
use crate::pages::types::doc::Doc;
2628
use crate::utils::{is_default, serialize_t_or_vec, t_or_vec};
@@ -292,6 +294,8 @@ pub struct SubPageEntry {
292294
#[serde(default, skip_serializing_if = "details_is_none")]
293295
pub details: Details,
294296
#[serde(default, skip_serializing_if = "is_default")]
297+
pub code: bool,
298+
#[serde(default, skip_serializing_if = "is_default")]
295299
pub include_parent: bool,
296300
}
297301

@@ -319,8 +323,13 @@ pub enum SidebarEntry {
319323
#[derive(Debug, Default)]
320324
pub enum MetaChildren {
321325
Children(Vec<SidebarMetaEntry>),
322-
ListSubPages(String, Vec<PageType>, bool),
323-
ListSubPagesGrouped(String, Vec<PageType>, bool),
326+
ListSubPages(String, Vec<PageType>, bool, bool),
327+
ListSubPagesGrouped {
328+
path: String,
329+
tags: Vec<PageType>,
330+
code: bool,
331+
include_parent: bool,
332+
},
324333
WebExtApi,
325334
#[default]
326335
None,
@@ -424,13 +433,14 @@ impl TryFrom<SidebarEntry> for SidebarMetaEntry {
424433
hash,
425434
title,
426435
path,
436+
code,
427437
include_parent,
428438
}) => SidebarMetaEntry {
429439
section: false,
430440
details,
431441
code: false,
432442
content: SidebarMetaEntryContent::from_link_title_hash(link, title, hash),
433-
children: MetaChildren::ListSubPages(path, tags, include_parent),
443+
children: MetaChildren::ListSubPages(path, tags, code, include_parent),
434444
},
435445
SidebarEntry::ListSubPagesGrouped(SubPageEntry {
436446
details,
@@ -439,13 +449,19 @@ impl TryFrom<SidebarEntry> for SidebarMetaEntry {
439449
hash,
440450
title,
441451
path,
452+
code,
442453
include_parent,
443454
}) => SidebarMetaEntry {
444455
section: false,
445456
details,
446457
code: false,
447458
content: SidebarMetaEntryContent::from_link_title_hash(link, title, hash),
448-
children: MetaChildren::ListSubPagesGrouped(path, tags, include_parent),
459+
children: MetaChildren::ListSubPagesGrouped {
460+
path,
461+
tags,
462+
code,
463+
include_parent,
464+
},
449465
},
450466
SidebarEntry::Default(BasicEntry {
451467
link,
@@ -565,7 +581,7 @@ impl SidebarMetaEntry {
565581
child.render(out, locale, slug, l10n)?;
566582
}
567583
}
568-
MetaChildren::ListSubPages(url, page_types, include_parent) => {
584+
MetaChildren::ListSubPages(url, page_types, code, include_parent) => {
569585
let url = if url.starts_with(concat!("/", default_locale().as_url_str(), "/")) {
570586
Cow::Borrowed(url)
571587
} else {
@@ -581,29 +597,40 @@ impl SidebarMetaEntry {
581597
&url,
582598
locale,
583599
Some(1),
584-
None,
585-
page_types,
586-
*include_parent,
600+
ListSubPagesContext {
601+
sorter: None,
602+
page_types,
603+
code: *code,
604+
include_parent: *include_parent,
605+
},
587606
)?
588607
}
589-
MetaChildren::ListSubPagesGrouped(url, page_types, include_parent) => {
590-
let url = if url.starts_with(concat!("/", default_locale().as_url_str(), "/")) {
591-
Cow::Borrowed(url)
608+
MetaChildren::ListSubPagesGrouped {
609+
path,
610+
tags,
611+
code,
612+
include_parent,
613+
} => {
614+
let url = if path.starts_with(concat!("/", default_locale().as_url_str(), "/")) {
615+
Cow::Borrowed(path)
592616
} else {
593617
Cow::Owned(concat_strs!(
594618
"/",
595619
Locale::default().as_url_str(),
596620
"/docs",
597-
url
621+
path
598622
))
599623
};
600624
list_sub_pages_grouped_internal(
601625
out,
602626
&url,
603627
locale,
604-
None,
605-
page_types,
606-
*include_parent,
628+
ListSubPagesContext {
629+
sorter: None,
630+
page_types: tags,
631+
code: *code,
632+
include_parent: *include_parent,
633+
},
607634
)?
608635
}
609636
MetaChildren::WebExtApi => {

crates/rari-doc/src/templ/templs/api_list_specs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn api_list_specs() -> Result<String, DocError> {
2222
env.locale,
2323
)?;
2424
let out = out_by_letter.entry(first_letter).or_default();
25-
write_li_with_badges(out, &page, env.locale, true)?;
25+
write_li_with_badges(out, &page, env.locale, false, true)?;
2626
}
2727
}
2828

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
use std::str::FromStr;
2-
31
use rari_templ_func::rari_f;
4-
use rari_types::fm_types::PageType;
52
use rari_types::AnyArg;
63

74
use crate::error::DocError;
8-
use crate::helpers::subpages::{self, SubPagesSorter};
5+
use crate::helpers::subpages::{self, ListSubPagesContext, SubPagesSorter};
96

107
/// List sub pages
118
#[rari_f]
@@ -33,50 +30,23 @@ pub fn list_sub_pages(
3330
env.locale,
3431
Some(SubPagesSorter::TitleNatural),
3532
&[],
33+
false,
3634
)?;
3735
} else {
3836
subpages::list_sub_pages_internal(
3937
&mut out,
4038
url,
4139
env.locale,
4240
Some(depth),
43-
Some(SubPagesSorter::TitleNatural),
44-
&[],
45-
false,
41+
ListSubPagesContext {
42+
sorter: Some(SubPagesSorter::TitleNatural),
43+
page_types: &[],
44+
code: false,
45+
include_parent: false,
46+
},
4647
)?;
4748
}
4849
out.push_str(if ordered { "</ol>" } else { "</ul>" });
4950

5051
Ok(out)
5152
}
52-
53-
#[rari_f]
54-
pub fn list_sub_pages_grouped(
55-
url: Option<String>,
56-
title: Option<String>,
57-
page_types: Option<String>,
58-
) -> Result<String, DocError> {
59-
let url = url.as_deref().unwrap_or(env.url);
60-
let title = title.as_deref().unwrap_or(env.title);
61-
let mut out = String::new();
62-
out.push_str("<details><summary>");
63-
out.push_str(&html_escape::encode_safe(title));
64-
out.push_str("</summary><ol>");
65-
subpages::list_sub_pages_grouped_internal(
66-
&mut out,
67-
url,
68-
env.locale,
69-
None,
70-
page_types
71-
.map(|pt| {
72-
pt.split(',')
73-
.filter_map(|pt| PageType::from_str(pt.trim()).ok())
74-
.collect::<Vec<_>>()
75-
})
76-
.as_deref()
77-
.unwrap_or_default(),
78-
false,
79-
)?;
80-
out.push_str("</ol></details>");
81-
Ok(out)
82-
}

crates/rari-doc/src/templ/templs/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ pub fn invoke(
7070
"glossary" => glossary::glossary_any,
7171
"csssyntax" => csssyntax::csssyntax_any,
7272
"listsubpages" => listsubpages::list_sub_pages_any,
73-
"listsubpagesgrouped" => listsubpages::list_sub_pages_grouped_any,
7473
"cssinfo" => cssinfo::cssinfo_any,
7574
"inheritancediagram" => inheritance_diagram::inheritance_diagram_any,
7675
"webextexamples" => web_ext_examples::web_ext_examples_any,

crates/rari-tools/src/sidebars.rs

+4
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ fn process_entry(entry: SidebarEntry, pairs: Pairs<'_>) -> SidebarEntry {
206206
title,
207207
path,
208208
include_parent,
209+
code,
209210
}) => {
210211
let new_path: Option<String> = replace_pairs(Some(path), pairs);
211212
if new_path.is_none() {
@@ -219,6 +220,7 @@ fn process_entry(entry: SidebarEntry, pairs: Pairs<'_>) -> SidebarEntry {
219220
title,
220221
path: new_path.unwrap(),
221222
include_parent,
223+
code,
222224
})
223225
}
224226
SidebarEntry::ListSubPagesGrouped(SubPageEntry {
@@ -229,6 +231,7 @@ fn process_entry(entry: SidebarEntry, pairs: Pairs<'_>) -> SidebarEntry {
229231
title,
230232
path,
231233
include_parent,
234+
code,
232235
}) => {
233236
let new_path: Option<String> = replace_pairs(Some(path), pairs);
234237
if new_path.is_none() {
@@ -242,6 +245,7 @@ fn process_entry(entry: SidebarEntry, pairs: Pairs<'_>) -> SidebarEntry {
242245
title,
243246
path: new_path.unwrap(),
244247
include_parent,
248+
code,
245249
})
246250
}
247251
SidebarEntry::Default(BasicEntry {

0 commit comments

Comments
 (0)