Skip to content

Commit 792c407

Browse files
committed
feat(templs): more on subpages and fixes
1 parent 4675517 commit 792c407

File tree

18 files changed

+182
-38
lines changed

18 files changed

+182
-38
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/diff-test/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ itertools = "0.13"
2020
similar = "2"
2121
regex = "1"
2222
htmldiff = "0.1"
23+
rayon = "1"

crates/diff-test/src/main.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use std::fs;
44
use std::fs::File;
55
use std::io::Write;
66
use std::path::Path;
7+
use std::sync::atomic::AtomicUsize;
8+
use std::sync::atomic::Ordering::Relaxed;
79
use std::sync::LazyLock;
810

911
use anyhow::{anyhow, Error};
@@ -14,6 +16,7 @@ use ignore::WalkBuilder;
1416
use itertools::Itertools;
1517
use jsonpath_lib::Compiled;
1618
use prettydiff::diff_words;
19+
use rayon::prelude::*;
1720
use regex::Regex;
1821
use serde_json::Value;
1922

@@ -172,6 +175,7 @@ const IGNORE: &[&str] = &[
172175
"doc.sidebarMacro",
173176
"doc.hasMathML",
174177
"doc.other_translations",
178+
"doc.summary",
175179
];
176180

177181
static WS_DIFF: LazyLock<Regex> =
@@ -287,14 +291,14 @@ fn main() -> Result<(), anyhow::Error> {
287291
let b = gather(&arg.root_b, arg.query.as_deref())?;
288292

289293
let hits = max(a.len(), b.len());
290-
let mut same = 0;
294+
let same = AtomicUsize::new(0);
291295
if arg.html {
292296
let mut out = Vec::new();
293297
out.push("<ul>".to_string());
294-
for (k, v) in a.iter() {
298+
out.extend(a.par_iter().filter_map(|(k, v)| {
295299
if b.get(k) == Some(v) {
296-
same += 1;
297-
continue;
300+
same.fetch_add(1, Relaxed);
301+
return None;
298302
}
299303

300304
if arg.value {
@@ -303,14 +307,14 @@ fn main() -> Result<(), anyhow::Error> {
303307
let mut diff = BTreeMap::new();
304308
full_diff(left, right, &[], &mut diff);
305309
if !diff.is_empty() {
306-
out.push(format!(
310+
return Some(format!(
307311
r#"<li><span>{k}</span><div class="r"><pre><code>{}</code></pre></div></li>"#,
308312
serde_json::to_string_pretty(&diff).unwrap_or_default(),
309313
));
310314
} else {
311-
same += 1;
315+
same.fetch_add(1, Relaxed);
312316
}
313-
continue;
317+
None
314318
} else {
315319
let left = &v.as_str().unwrap_or_default();
316320
let right = b
@@ -336,24 +340,29 @@ fn main() -> Result<(), anyhow::Error> {
336340
let left = left.replace(broken_link, "");
337341
if left == right {
338342
println!("only broken links differ");
339-
same += 1;
340-
continue;
343+
same.fetch_add(1, Relaxed);
344+
return None;
341345
}
342346
if arg.inline {
343347
println!("{}", diff_words(&left, right));
344348
}
345-
out.push(format!(
349+
Some(format!(
346350
r#"<li><span>{k}</span><div class="a">{}</div><div class="b">{}</div></li>"#,
347351
left, right
348352
))
349353
}
350354
}
355+
).collect::<Vec<_>>());
351356
out.push("</ul>".to_string());
352357
let mut file = File::create(&arg.out)?;
353358
file.write_all(html(&out.into_iter().collect::<String>()).as_bytes())?;
354359
}
355360

356-
println!("Took: {:?} - {same}/{hits}", start.elapsed());
361+
println!(
362+
"Took: {:?} - {}/{hits}",
363+
start.elapsed(),
364+
same.load(Relaxed)
365+
);
357366
}
358367
}
359368
Ok(())

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use super::title::{page_title, transform_title};
2222
use crate::baseline::get_baseline;
2323
use crate::error::DocError;
2424
use crate::html::bubble_up::bubble_up_curriculum_page;
25-
use crate::html::modifier::add_missing_ids;
25+
use crate::html::modifier::{add_missing_ids, remove_empty_p};
2626
use crate::html::rewriter::{post_process_html, post_process_inline_sidebar};
2727
use crate::html::sections::{split_sections, BuildSection, BuildSectionType, Splitted};
2828
use crate::html::sidebar::{
@@ -153,6 +153,7 @@ pub fn build_content<T: PageLike>(page: &T) -> Result<PageContent, DocError> {
153153
if page.page_type() == PageType::Curriculum {
154154
bubble_up_curriculum_page(&mut fragment)?;
155155
}
156+
remove_empty_p(&mut fragment)?;
156157
add_missing_ids(&mut fragment)?;
157158
expand_details_and_mark_current_for_inline_sidebar(&mut fragment, page.url())?;
158159
let Splitted {

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl SubPagesSorter {
6161
}
6262

6363
pub fn write_li_with_badges(
64-
out: &mut impl Write,
64+
out: &mut String,
6565
page: &Page,
6666
locale: Locale,
6767
closed: bool,
@@ -77,6 +77,14 @@ pub fn write_li_with_badges(
7777
locale_page.url(),
7878
html_escape::encode_safe(locale_page.short_title().unwrap_or(locale_page.title()))
7979
)?;
80+
add_inline_badges(out, page, locale)?;
81+
if closed {
82+
write!(out, "</li>")?;
83+
}
84+
Ok(())
85+
}
86+
87+
pub fn add_inline_badges(out: &mut String, page: &Page, locale: Locale) -> Result<(), DocError> {
8088
if page.status().contains(&FeatureStatus::Experimental) {
8189
write_experimental(out, locale)?;
8290
}
@@ -86,9 +94,6 @@ pub fn write_li_with_badges(
8694
if page.status().contains(&FeatureStatus::Deprecated) {
8795
write_deprecated(out, locale)?;
8896
}
89-
if closed {
90-
write!(out, "</li>")?;
91-
}
9297
Ok(())
9398
}
9499

@@ -101,11 +106,11 @@ pub fn list_sub_pages_reverse_internal(
101106
) -> Result<(), DocError> {
102107
let sub_pages = get_sub_pages(url, Some(1), sorter.unwrap_or_default())?;
103108

104-
for sub_page in sub_pages {
109+
for sub_page in sub_pages.iter().rev() {
105110
if !page_types.is_empty() && !page_types.contains(&sub_page.page_type()) {
106111
continue;
107112
}
108-
write_li_with_badges(out, &sub_page, locale, true)?;
113+
write_li_with_badges(out, sub_page, locale, true)?;
109114
}
110115
Ok(())
111116
}

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ pub fn render_internal_link(
4141
if modifier.code {
4242
out.push_str("<code>");
4343
}
44-
let content = html_escape::decode_html_entities(content);
45-
let content = html_escape::encode_safe(&content);
46-
out.push_str(&content);
44+
out.push_str(content);
4745
if modifier.code {
4846
out.push_str("</code>");
4947
}
@@ -67,7 +65,14 @@ pub fn render_link_from_page(
6765
page: &Page,
6866
modifier: &LinkModifier,
6967
) -> Result<(), DocError> {
70-
let content = html_escape::encode_safe(page.short_title().unwrap_or(page.title()));
68+
let content = page.short_title().unwrap_or(page.title());
69+
let decoded_content = html_escape::decode_html_entities(content);
70+
let encoded_content = html_escape::encode_safe(&decoded_content);
71+
let content = if content != encoded_content {
72+
Cow::Owned(encoded_content.into_owned())
73+
} else {
74+
Cow::Borrowed(content)
75+
};
7176
render_internal_link(out, page.url(), None, &content, None, modifier)
7277
}
7378

@@ -91,7 +96,18 @@ pub fn render_link_via_page(
9196
match RariApi::get_page(url) {
9297
Ok(page) => {
9398
let url = page.url();
94-
let content = content.unwrap_or(page.short_title().unwrap_or(page.title()));
99+
let content = if let Some(content) = content {
100+
Cow::Borrowed(content)
101+
} else {
102+
let content = page.short_title().unwrap_or(page.title());
103+
let decoded_content = html_escape::decode_html_entities(content);
104+
let encoded_content = html_escape::encode_safe(&decoded_content);
105+
if content != encoded_content {
106+
Cow::Owned(encoded_content.into_owned())
107+
} else {
108+
Cow::Borrowed(content)
109+
}
110+
};
95111
return render_internal_link(
96112
out,
97113
url,
@@ -100,7 +116,7 @@ pub fn render_link_via_page(
100116
} else {
101117
Some(anchor)
102118
},
103-
content,
119+
&content,
104120
title,
105121
&LinkModifier {
106122
badges: if with_badges { page.status() } else { &[] },

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,14 @@ pub fn add_missing_ids(html: &mut Html) -> Result<(), DocError> {
8484
}
8585
Ok(())
8686
}
87+
88+
pub fn remove_empty_p(html: &mut Html) -> Result<(), DocError> {
89+
let selector = Selector::parse("p:empty").unwrap();
90+
let dels = html.select(&selector).map(|el| el.id()).collect::<Vec<_>>();
91+
92+
for id in dels {
93+
html.tree.get_mut(id).unwrap().detach();
94+
}
95+
96+
Ok(())
97+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub fn non_standard_header() -> Result<String, DocError> {
8080
let copy = rari_l10n::l10n_json_data("Template", "non_standard_header_copy", env.locale)?;
8181

8282
Ok([
83-
r#"<div class="notecard nonstadard"><p><strong>"#,
83+
r#"<div class="notecard nonstandard"><p><strong>"#,
8484
title,
8585
":</strong> ",
8686
copy,

crates/rari-doc/src/templ/templs/embeds/livesample.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rari_types::AnyArg;
55

66
use crate::error::DocError;
77
use crate::templ::api::RariApi;
8-
use crate::utils::trim_ws;
8+
use crate::utils::dedup_ws;
99

1010
#[allow(clippy::too_many_arguments)]
1111
#[rari_f]
@@ -18,7 +18,7 @@ pub fn live_sample(
1818
_deprecated_5: Option<String>,
1919
allowed_features: Option<String>,
2020
) -> Result<String, DocError> {
21-
let title = trim_ws(&id.replace('_', " "));
21+
let title = dedup_ws(&id.replace('_', " "));
2222
let id = RariApi::anchorize(&id);
2323
let mut out = String::new();
2424
out.push_str(r#"<div class="code-example"><div class="example-header"></div><iframe class="sample-code-frame" title=""#);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use rari_templ_func::rari_f;
22

33
use crate::error::DocError;
44
use crate::templ::api::RariApi;
5-
use crate::utils::trim_ws;
5+
use crate::utils::dedup_ws;
66

77
#[rari_f]
88
pub fn glossary(term_name: String, display_name: Option<String>) -> Result<String, DocError> {
9-
let url = format!("/Glossary/{}", trim_ws(&term_name).replace(' ', "_"));
9+
let url = format!("/Glossary/{}", dedup_ws(&term_name).replace(' ', "_"));
1010
RariApi::link(
1111
&url,
1212
Some(env.locale),

0 commit comments

Comments
 (0)