Skip to content

Commit 309f6bf

Browse files
committed
fix(diff): fix subpages and banners
Also add support for --sidebars in diff-test.
1 parent 149e313 commit 309f6bf

File tree

13 files changed

+120
-46
lines changed

13 files changed

+120
-46
lines changed

Diff for: Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ reqwest = { version = "0.12", default-features = false, features = [
8080
"gzip",
8181
] }
8282
indoc = "2"
83+
base64 = "0.22"
8384

8485

8586
[dependencies]

Diff for: crates/diff-test/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ itertools.workspace = true
1414
regex.workspace = true
1515
serde.workspace = true
1616
serde_json.workspace = true
17+
base64.workspace = true
1718

1819
jsonpath_lib = "0.3.0"
1920
prettydiff = "0.7"
2021
html-minifier = "5"
2122
ansi-to-html = "0.2"
2223
similar = "2"
2324
quick-xml = "0.36"
25+
sha2 = "0.10"
2426
clap = { version = "4.5.1", features = ["derive"] }

Diff for: crates/diff-test/src/main.rs

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use std::cmp::max;
2-
use std::collections::{BTreeMap, HashSet};
2+
use std::collections::{BTreeMap, HashMap, HashSet};
33
use std::fs;
44
use std::fs::File;
55
use std::io::Write;
66
use std::path::Path;
77
use std::sync::atomic::AtomicUsize;
88
use std::sync::atomic::Ordering::Relaxed;
9-
use std::sync::LazyLock;
9+
use std::sync::{Arc, LazyLock, RwLock};
1010

1111
use anyhow::{anyhow, Error};
12+
use base64::prelude::{Engine as _, BASE64_STANDARD_NO_PAD};
1213
use clap::{Args, Parser, Subcommand};
1314
use ignore::types::TypesBuilder;
1415
use ignore::WalkBuilder;
@@ -18,6 +19,7 @@ use prettydiff::{diff_lines, diff_words};
1819
use rayon::prelude::*;
1920
use regex::Regex;
2021
use serde_json::Value;
22+
use sha2::{Digest, Sha256};
2123
use xml::fmt_html;
2224

2325
mod xml;
@@ -147,6 +149,8 @@ struct BuildArgs {
147149
value: bool,
148150
#[arg(short, long)]
149151
verbose: bool,
152+
#[arg(long)]
153+
sidebars: bool,
150154
}
151155

152156
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -175,7 +179,7 @@ const IGNORE: &[&str] = &[
175179
"doc.popularity",
176180
"doc.source.github_url",
177181
"doc.source.last_commit_url",
178-
"doc.sidebarHTML",
182+
//"doc.sidebarHTML",
179183
"doc.sidebarMacro",
180184
"doc.hasMathML",
181185
"doc.other_translations",
@@ -187,12 +191,16 @@ static WS_DIFF: LazyLock<Regex> =
187191
static DATA_FLAW_SRC: LazyLock<Regex> =
188192
LazyLock::new(|| Regex::new(r#" data-flaw-src="[^"]+""#).unwrap());
189193

194+
static DIFF_MAP: LazyLock<Arc<RwLock<HashMap<String, String>>>> =
195+
LazyLock::new(|| Arc::new(RwLock::new(HashMap::new())));
196+
190197
fn full_diff(
191198
lhs: &Value,
192199
rhs: &Value,
193200
path: &[PathIndex],
194201
diff: &mut BTreeMap<String, String>,
195202
fast: bool,
203+
sidebars: bool,
196204
) {
197205
if path.len() == 1 {
198206
if let PathIndex::Object(s) = &path[0] {
@@ -203,7 +211,7 @@ fn full_diff(
203211
}
204212
if lhs != rhs {
205213
let key = make_key(path);
206-
if IGNORE.iter().any(|i| key.starts_with(i)) {
214+
if IGNORE.iter().any(|i| key.starts_with(i)) || key == "doc.sidebarHTML" && !sidebars {
207215
return;
208216
}
209217
match (lhs, rhs) {
@@ -218,6 +226,7 @@ fn full_diff(
218226
&path,
219227
diff,
220228
fast,
229+
sidebars,
221230
);
222231
}
223232
}
@@ -233,6 +242,7 @@ fn full_diff(
233242
&path,
234243
diff,
235244
fast,
245+
sidebars,
236246
);
237247
}
238248
}
@@ -259,6 +269,15 @@ fn full_diff(
259269
rhs = fmt_html(&html_minifier::minify(rhs_t).unwrap());
260270
}
261271
if lhs != rhs {
272+
let mut diff_hash = Sha256::new();
273+
diff_hash.write_all(lhs.as_bytes()).unwrap();
274+
diff_hash.write_all(rhs.as_bytes()).unwrap();
275+
let diff_hash = BASE64_STANDARD_NO_PAD.encode(&diff_hash.finalize()[..]);
276+
if let Some(hash) = (*DIFF_MAP.read().unwrap()).get(&diff_hash) {
277+
diff.insert(key, format!("See {hash}"));
278+
return;
279+
}
280+
(*DIFF_MAP.write().unwrap()).insert(diff_hash, "somewhere else".into());
262281
diff.insert(
263282
key,
264283
ansi_to_html::convert(&if fast {
@@ -309,7 +328,7 @@ fn main() -> Result<(), anyhow::Error> {
309328
let left = v;
310329
let right = b.get(k).unwrap_or(&Value::Null);
311330
let mut diff = BTreeMap::new();
312-
full_diff(left, right, &[], &mut diff, arg.fast);
331+
full_diff(left, right, &[], &mut diff, arg.fast, arg.sidebars);
313332
if !diff.is_empty() {
314333
return Some(format!(
315334
r#"<li><span>{k}</span><div class="r"><pre><code>{}</code></pre></div></li>"#,

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

+29-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rari_types::fm_types::{FeatureStatus, PageType};
88
use rari_types::globals::{cache_content, deny_warnings};
99
use rari_types::locale::Locale;
1010

11+
use super::l10n::l10n_json_data;
1112
use super::titles::api_page_title;
1213
use crate::error::DocError;
1314
use crate::pages::page::{Page, PageLike, PageReader};
@@ -95,6 +96,14 @@ pub fn add_inline_badges(out: &mut String, page: &Page, locale: Locale) -> Resul
9596
Ok(())
9697
}
9798

99+
pub fn write_parent_li(out: &mut String, page: &Page, locale: Locale) -> Result<(), DocError> {
100+
let title = l10n_json_data("Template", "overview", locale)?;
101+
write!(out, "<li><a href=\"{}\">{}</a>", page.url(), title)?;
102+
add_inline_badges(out, page, locale)?;
103+
write!(out, "</li>")?;
104+
Ok(())
105+
}
106+
98107
pub fn list_sub_pages_reverse_internal(
99108
out: &mut String,
100109
url: &str,
@@ -120,10 +129,14 @@ pub fn list_sub_pages_internal(
120129
depth: Option<usize>,
121130
sorter: Option<SubPagesSorter>,
122131
page_types: &[PageType],
132+
include_parent: bool,
123133
) -> Result<(), DocError> {
124134
let sub_pages = get_sub_pages(url, Some(1), sorter.unwrap_or_default())?;
125-
126135
let depth = depth.map(|i| i.saturating_sub(1));
136+
if include_parent {
137+
let page = Page::from_url_with_other_locale_and_fallback(url, Some(locale))?;
138+
write_parent_li(out, &page, locale)?;
139+
}
127140
for sub_page in sub_pages {
128141
if !page_types.is_empty() && !page_types.contains(&sub_page.page_type()) {
129142
continue;
@@ -134,7 +147,16 @@ pub fn list_sub_pages_internal(
134147
} else {
135148
write_li_with_badges(out, &sub_page, locale, false)?;
136149
out.push_str("<ol>");
137-
list_sub_pages_internal(out, sub_page.url(), locale, depth, sorter, page_types)?;
150+
151+
list_sub_pages_internal(
152+
out,
153+
sub_page.url(),
154+
locale,
155+
depth,
156+
sorter,
157+
page_types,
158+
include_parent,
159+
)?;
138160
out.push_str("</ol>");
139161
out.push_str("</li>");
140162
}
@@ -148,6 +170,7 @@ pub fn list_sub_pages_grouped_internal(
148170
locale: Locale,
149171
sorter: Option<SubPagesSorter>,
150172
page_types: &[PageType],
173+
include_parent: bool,
151174
) -> Result<(), DocError> {
152175
let sub_pages = get_sub_pages(url, None, sorter.unwrap_or_default())?;
153176

@@ -171,6 +194,10 @@ pub fn list_sub_pages_grouped_internal(
171194
grouped.insert(sub_page.title(), vec![sub_page]);
172195
}
173196
}
197+
if include_parent {
198+
let page = Page::from_url_with_other_locale_and_fallback(url, Some(locale))?;
199+
write_parent_li(out, &page, locale)?;
200+
}
174201
for (prefix, group) in grouped {
175202
let keep_group = group.len() > 2;
176203
if keep_group {

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

+23-23
Original file line numberDiff line numberDiff line change
@@ -277,29 +277,21 @@ pub fn post_process_html<T: PageLike>(
277277
el.after("</div>", ContentType::Html);
278278
Ok(())
279279
}),
280-
element!("div.notecard.callout > p:first-child", |el| {
281-
el.prepend(
282-
&concat_strs!(
283-
"<strong>",
284-
NoteCard::Callout.prefix_for_locale(page.locale()),
285-
"</strong>"
286-
),
287-
ContentType::Html,
288-
);
289-
Ok(())
290-
}),
291-
element!("div.notecard.warning > p:first-child", |el| {
292-
el.prepend(
293-
&concat_strs!(
294-
"<strong>",
295-
NoteCard::Warning.prefix_for_locale(page.locale()),
296-
"</strong>"
297-
),
298-
ContentType::Html,
299-
);
300-
Ok(())
301-
}),
302-
element!("div.notecard.note > p:first-child", |el| {
280+
element!(
281+
"div.notecard.warning[data-add-warning] > p:first-child",
282+
|el| {
283+
el.prepend(
284+
&concat_strs!(
285+
"<strong>",
286+
NoteCard::Warning.prefix_for_locale(page.locale()),
287+
"</strong>"
288+
),
289+
ContentType::Html,
290+
);
291+
Ok(())
292+
}
293+
),
294+
element!("div.notecard.note[data-add-note] > p:first-child", |el| {
303295
el.prepend(
304296
&concat_strs!(
305297
"<strong>",
@@ -330,6 +322,14 @@ pub fn post_process_html<T: PageLike>(
330322
el.remove_attribute("data-sourcepos");
331323
Ok(())
332324
}),
325+
element!("*[data-add-note]", |el| {
326+
el.remove_attribute("data-add-note");
327+
Ok(())
328+
}),
329+
element!("*[data-add-warning]", |el| {
330+
el.remove_attribute("data-add-warning");
331+
Ok(())
332+
}),
333333
];
334334
if sidebar {
335335
element_content_handlers.push(element!("html", |el| {

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

+26-9
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ pub struct SubPageEntry {
224224
pub tags: Vec<PageType>,
225225
#[serde(default)]
226226
pub details: Details,
227+
#[serde(default)]
228+
pub include_parent: bool,
227229
}
228230

229231
#[derive(Serialize, Deserialize, Default, Debug)]
@@ -248,8 +250,8 @@ pub enum SidebarEntry {
248250
#[derive(Debug, Default)]
249251
pub enum MetaChildren {
250252
Children(Vec<SidebarMetaEntry>),
251-
ListSubPages(String, Vec<PageType>),
252-
ListSubPagesGrouped(String, Vec<PageType>),
253+
ListSubPages(String, Vec<PageType>, bool),
254+
ListSubPagesGrouped(String, Vec<PageType>, bool),
253255
WebExtApi,
254256
#[default]
255257
None,
@@ -327,25 +329,27 @@ impl From<SidebarEntry> for SidebarMetaEntry {
327329
link,
328330
title,
329331
path,
332+
include_parent,
330333
}) => SidebarMetaEntry {
331334
section: false,
332335
details,
333336
code: false,
334337
content: SidebarMetaEntryContent::Link { link, title },
335-
children: MetaChildren::ListSubPages(path, tags),
338+
children: MetaChildren::ListSubPages(path, tags, include_parent),
336339
},
337340
SidebarEntry::ListSubPagesGrouped(SubPageEntry {
338341
details,
339342
tags,
340343
link,
341344
title,
342345
path,
346+
include_parent,
343347
}) => SidebarMetaEntry {
344348
section: false,
345349
details,
346350
code: false,
347351
content: SidebarMetaEntryContent::Link { link, title },
348-
children: MetaChildren::ListSubPagesGrouped(path, tags),
352+
children: MetaChildren::ListSubPagesGrouped(path, tags, include_parent),
349353
},
350354
SidebarEntry::Default(BasicEntry {
351355
link,
@@ -457,11 +461,24 @@ impl SidebarMetaEntry {
457461
child.render(out, locale, slug, l10n)?;
458462
}
459463
}
460-
MetaChildren::ListSubPages(url, page_types) => {
461-
list_sub_pages_internal(out, url, locale, Some(1), None, page_types)?
462-
}
463-
MetaChildren::ListSubPagesGrouped(url, page_types) => {
464-
list_sub_pages_grouped_internal(out, url, locale, None, page_types)?
464+
MetaChildren::ListSubPages(url, page_types, include_parent) => list_sub_pages_internal(
465+
out,
466+
url,
467+
locale,
468+
Some(1),
469+
None,
470+
page_types,
471+
*include_parent,
472+
)?,
473+
MetaChildren::ListSubPagesGrouped(url, page_types, include_parent) => {
474+
list_sub_pages_grouped_internal(
475+
out,
476+
url,
477+
locale,
478+
None,
479+
page_types,
480+
*include_parent,
481+
)?
465482
}
466483
MetaChildren::WebExtApi => {
467484
let children = &helpers::webextapi::children(

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ fn build_interface_list(entries: &mut Vec<SidebarMetaEntry>, interfaces: &[&str]
166166
.map(|interface| SidebarMetaEntry {
167167
code: true,
168168
content: SidebarMetaEntryContent::Link {
169-
title: None,
169+
title: Some(interface.to_string()),
170170
link: Some(format!(
171171
"/Web/API/{}",
172172
interface.replace("()", "").replace('.', "/")

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn available_in_workers(typ: Option<String>) -> Result<String, DocError> {
3737
.unwrap_or(l10n_json_data("Template", default_typ, env.locale)?);
3838

3939
Ok(concat_strs!(
40-
r#"<div class="notecard note"><p> "#,
40+
r#"<div class="notecard note" data-add-note><p> "#,
4141
copy,
4242
"</p></div>"
4343
))

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

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub fn list_sub_pages(
4242
Some(depth),
4343
Some(SubPagesSorter::SlugNatural),
4444
&[],
45+
false,
4546
)?;
4647
}
4748
out.push_str(if ordered { "</ol>" } else { "</ul>" });
@@ -74,6 +75,7 @@ pub fn list_sub_pages_grouped(
7475
})
7576
.as_deref()
7677
.unwrap_or_default(),
78+
false,
7779
)?;
7880
out.push_str("</ol></details>");
7981
Ok(out)

0 commit comments

Comments
 (0)