Skip to content

Commit 648becb

Browse files
committed
feat(templ): post process inline sidebar
1 parent c9a7591 commit 648becb

File tree

6 files changed

+71
-24
lines changed

6 files changed

+71
-24
lines changed

crates/rari-doc/src/docs/build.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use super::title::{page_title, transform_title};
2222
use crate::baseline::get_baseline;
2323
use crate::error::DocError;
2424
use crate::html::modifier::add_missing_ids;
25-
use crate::html::rewriter::post_process_html;
26-
use crate::html::sections::{split_sections, BuildSection, BuildSectionType};
27-
use crate::html::sidebar::build_sidebars;
25+
use crate::html::rewriter::{post_process_html, post_process_inline_sidebar};
26+
use crate::html::sections::{split_sections, BuildSection, BuildSectionType, Splitted};
27+
use crate::html::sidebar::{build_sidebars, expand_details_and_mark_current_for_inline_sidebar};
2828
use crate::specs::extract_specifications;
2929
use crate::templ::render::{decode_ref, render};
3030

@@ -144,7 +144,17 @@ pub fn build_content<T: PageLike>(doc: &T) -> Result<PageContent, DocError> {
144144
let post_processed_html = post_process_html(&html, doc, false)?;
145145
let mut fragment = Html::parse_fragment(&post_processed_html);
146146
add_missing_ids(&mut fragment)?;
147-
let (sections, summary, sidebar) = split_sections(&fragment).expect("DOOM");
147+
expand_details_and_mark_current_for_inline_sidebar(&mut fragment, doc.url())?;
148+
let Splitted {
149+
sections,
150+
summary,
151+
sidebar,
152+
} = split_sections(&fragment).expect("DOOM");
153+
let sidebar = if let Some(sidebar) = sidebar {
154+
Some(post_process_inline_sidebar(&sidebar)?)
155+
} else {
156+
None
157+
};
148158
let toc = make_toc(&sections, matches!(doc.page_type(), PageType::Curriculum));
149159
let body = sections.into_iter().map(Into::into).collect();
150160
Ok(PageContent {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@ fn title_natural_sorter(a: &Page, b: &Page) -> Ordering {
2626
natural_compare_with_floats(a.title(), b.title())
2727
}
2828

29+
fn slug_natural_sorter(a: &Page, b: &Page) -> Ordering {
30+
natural_compare_with_floats(a.slug(), b.slug())
31+
}
32+
2933
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
3034
pub enum SubPagesSorter {
3135
#[default]
3236
Title,
3337
Slug,
3438
TitleNatural,
39+
SlugNatural,
3540
}
3641

3742
impl SubPagesSorter {
@@ -40,6 +45,7 @@ impl SubPagesSorter {
4045
SubPagesSorter::Title => title_sorter,
4146
SubPagesSorter::Slug => slug_sorter,
4247
SubPagesSorter::TitleNatural => title_natural_sorter,
48+
SubPagesSorter::SlugNatural => slug_natural_sorter,
4349
}
4450
}
4551
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::borrow::Cow;
22
use std::collections::HashSet;
33

44
use lol_html::html_content::ContentType;
5-
use lol_html::{element, text, HtmlRewriter, Settings};
5+
use lol_html::{element, rewrite_str, text, HtmlRewriter, RewriteStrSettings, Settings};
66
use rari_md::bq::NoteCard;
77
use rari_types::fm_types::PageType;
88
use rari_types::locale::Locale;
@@ -14,6 +14,22 @@ use crate::error::DocError;
1414
use crate::redirects::resolve_redirect;
1515
use crate::resolve::strip_locale_from_url;
1616

17+
pub fn post_process_inline_sidebar(input: &str) -> Result<String, DocError> {
18+
let element_content_handlers = vec![element!("*[data-rewriter=em]", |el| {
19+
el.prepend("<em>", ContentType::Html);
20+
el.append("</em>", ContentType::Html);
21+
el.remove_attribute("data-rewriter");
22+
Ok(())
23+
})];
24+
Ok(rewrite_str(
25+
input,
26+
RewriteStrSettings {
27+
element_content_handlers,
28+
..Default::default()
29+
},
30+
)?)
31+
}
32+
1733
pub fn post_process_html<T: PageLike>(
1834
input: &str,
1935
page: &T,

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ pub struct BuildSection<'a> {
2222
pub id: Option<String>,
2323
}
2424

25-
pub fn split_sections(
26-
html: &Html,
27-
) -> Result<(Vec<BuildSection<'_>>, Option<String>, Option<String>), DocError> {
25+
pub struct Splitted<'a> {
26+
pub sections: Vec<BuildSection<'a>>,
27+
pub summary: Option<String>,
28+
pub sidebar: Option<String>,
29+
}
30+
31+
pub fn split_sections(html: &Html) -> Result<Splitted, DocError> {
2832
let root_children = html.root_element().children();
2933
let raw_sections = root_children;
3034
let summary_selector = Selector::parse("p").unwrap();
@@ -107,7 +111,7 @@ pub fn split_sections(
107111
if let Some(section) = maybe_section.take() {
108112
sections.push(section);
109113
}
110-
let html = ElementRef::wrap(current).unwrap().html();
114+
let html = ElementRef::wrap(current).unwrap().inner_html();
111115
sidebar = Some(html)
112116
}
113117
_ => {
@@ -233,5 +237,9 @@ pub fn split_sections(
233237
if let Some(section) = last.take() {
234238
sections.push(section);
235239
}
236-
Ok((sections, summary, sidebar))
240+
Ok(Splitted {
241+
sections,
242+
summary,
243+
sidebar,
244+
})
237245
}

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,19 @@ type SidebarCache = Arc<RwLock<HashMap<Locale, HashMap<String, String>>>>;
3333

3434
static SIDEBAR_CACHE: Lazy<SidebarCache> = Lazy::new(|| Arc::new(RwLock::new(HashMap::new())));
3535

36-
fn expand_details_to_for_current(mut html: Html, url: &str) -> Result<String, DocError> {
36+
pub fn expand_details_and_mark_current_for_inline_sidebar(
37+
html: &mut Html,
38+
url: &str,
39+
) -> Result<(), DocError> {
40+
let a_selector = Selector::parse(&format!("#Quick_links a[href=\"{url}\"]")).unwrap();
41+
expand_details_and_mark_current(html, a_selector)
42+
}
43+
fn expand_details_and_mark_current_for_sidebar(html: &mut Html, url: &str) -> Result<(), DocError> {
3744
let a_selector = Selector::parse(&format!("a[href=\"{url}\"]")).unwrap();
45+
expand_details_and_mark_current(html, a_selector)
46+
}
47+
48+
fn expand_details_and_mark_current(html: &mut Html, a_selector: Selector) -> Result<(), DocError> {
3849
let mut details = vec![];
3950
let mut parent_id = None;
4051
if let Some(a) = html.select(&a_selector).next() {
@@ -52,19 +63,20 @@ fn expand_details_to_for_current(mut html: Html, url: &str) -> Result<String, Do
5263
}
5364
}
5465
if let Some(parent_id) = parent_id {
55-
add_attribute(&mut html, parent_id, "data-rewriter", "em");
66+
add_attribute(html, parent_id, "data-rewriter", "em");
5667
}
5768
for details in details {
58-
add_attribute(&mut html, details, "open", "");
69+
add_attribute(html, details, "open", "");
5970
}
6071

61-
Ok(html.html())
72+
Ok(())
6273
}
6374

6475
fn postprocess_sidebar(ks_rendered_sidebar: &str, doc: &Doc) -> Result<String, DocError> {
65-
let fragment = Html::parse_fragment(ks_rendered_sidebar);
66-
let pre_processed_html = expand_details_to_for_current(fragment, &doc.meta.url)?;
67-
let post_processed_html = post_process_html(&pre_processed_html, doc, true)?;
76+
let mut fragment = Html::parse_fragment(ks_rendered_sidebar);
77+
78+
expand_details_and_mark_current_for_sidebar(&mut fragment, &doc.meta.url)?;
79+
let post_processed_html = post_process_html(&fragment.html(), doc, true)?;
6880
Ok::<_, DocError>(post_processed_html)
6981
}
7082

crates/rari-doc/src/templ/macros/listsubpages.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ use crate::error::DocError;
88
use crate::helpers::subpages::{self, SubPagesSorter};
99

1010
/// List sub pages
11-
///
12-
/// Parameters:
13-
/// $0 Base url
14-
/// $1 Title
15-
/// $3 Page types
1611
#[rari_f]
1712
pub fn list_sub_pages(
1813
url: Option<String>,
@@ -29,8 +24,8 @@ pub fn list_sub_pages(
2924
url,
3025
env.locale,
3126
Some(depth.map(|d| d.as_int() as usize).unwrap_or(1)),
32-
reverse.map(|r| r.as_bool()).unwrap_or_default(),
33-
Some(SubPagesSorter::TitleNatural),
27+
reverse.map(|r| r.as_int() != 0).unwrap_or_default(), // Yes the old marco checks for == 0 not === 0.
28+
Some(SubPagesSorter::SlugNatural),
3429
&[],
3530
)?;
3631
out.push_str(if ordered { "</ol>" } else { "</ul>" });

0 commit comments

Comments
 (0)