Skip to content

Commit

Permalink
feat(diff): update html template
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Nov 1, 2024
1 parent ef6fbd7 commit 7432365
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 57 deletions.
5 changes: 5 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# TODO's

## rari
- [ ] Change compat macro to no args only and refactor `data-multiple`
- [ ] browser_compat sections should be able to contain content in yari ?!
- [ ] land curriculum has implementation
Expand All @@ -13,3 +14,7 @@
- [x] deal with summary() in templs
- [ ] list groups badges and titles
- [ ] WebKitCSSMatrix title

## translated content

- [ ] lower case codefences attributes
111 changes: 83 additions & 28 deletions crates/diff-test/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::borrow::Cow;
use std::cmp::max;
use std::collections::{BTreeMap, HashSet};
use std::fmt::Write;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::io::{BufWriter, Write as _};
use std::path::Path;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::Relaxed;
Expand Down Expand Up @@ -36,6 +37,10 @@ fn html(body: &str) -> String {
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body > ul {{
& > li {{
list-style: none;
}}
ul {{
display: flex;
flex-direction: column;
Expand Down Expand Up @@ -69,10 +74,13 @@ fn html(body: &str) -> String {
}}
}}
}}
}}
</style>
</head>
<body>
<ul>
{body}
</ul>
</body>
</html>
"#
Expand Down Expand Up @@ -156,6 +164,8 @@ struct BuildArgs {
verbose: bool,
#[arg(long)]
sidebars: bool,
#[arg(long)]
check_dts: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -259,9 +269,10 @@ static DIFF_MAP: LazyLock<Arc<DashMap<String, String>>> =
LazyLock::new(|| Arc::new(DashMap::new()));

/// Run html content through these handlers to clean up the html before minifying and diffing.
fn pre_diff_element_massaging_handlers<'a>() -> Vec<(Cow<'a, Selector>, ElementContentHandlers<'a>)>
{
vec![
fn pre_diff_element_massaging_handlers<'a>(
args: &BuildArgs,
) -> Vec<(Cow<'a, Selector>, ElementContentHandlers<'a>)> {
let mut handlers = vec![
// remove data-flaw-src attributes
element!("*[data-flaw-src]", |el| {
el.remove_attribute("data-flaw-src");
Expand All @@ -276,7 +287,20 @@ fn pre_diff_element_massaging_handlers<'a>() -> Vec<(Cow<'a, Selector>, ElementC
el.remove_attribute("id");
Ok(())
}),
]
];
if !args.check_dts {
handlers.extend([
element!("dt", |el| {
el.remove_attribute("id");
Ok(())
}),
element!("dt > a[href^=\"#\"", |el| {
el.remove_and_keep_content();
Ok(())
}),
]);
}
handlers
}

fn full_diff(
Expand All @@ -285,8 +309,7 @@ fn full_diff(
file: &str,
path: &[PathIndex],
diff: &mut BTreeMap<String, String>,
fast: bool,
sidebars: bool,
args: &BuildArgs,
) {
if path.len() == 1 {
if let PathIndex::Object(s) = &path[0] {
Expand All @@ -306,7 +329,8 @@ fn full_diff(
}

if lhs != rhs {
if IGNORED_KEYS.iter().any(|i| key.starts_with(i)) || key == "doc.sidebarHTML" && !sidebars
if IGNORED_KEYS.iter().any(|i| key.starts_with(i))
|| key == "doc.sidebarHTML" && !args.sidebars
{
return;
}
Expand All @@ -322,8 +346,7 @@ fn full_diff(
file,
&path,
diff,
fast,
sidebars,
args,
);
}
}
Expand All @@ -339,8 +362,7 @@ fn full_diff(
file,
&path,
diff,
fast,
sidebars,
args,
);
}
}
Expand All @@ -356,6 +378,14 @@ fn full_diff(
lhs = lhs.replace("\n ", "\n");
rhs = rhs.replace("\n ", "\n");
}
x if x.starts_with("doc.") && x.ends_with("value.id") => {
lhs = lhs
.trim_end_matches(|c: char| c == '_' || c.is_ascii_digit())
.to_string();
rhs = rhs
.trim_end_matches(|c: char| c == '_' || c.is_ascii_digit())
.to_string();
}
_ => {}
};
if is_html(&lhs) && is_html(&rhs) {
Expand All @@ -366,15 +396,15 @@ fn full_diff(
let lhs_t = rewrite_str(
&lhs_t,
RewriteStrSettings {
element_content_handlers: pre_diff_element_massaging_handlers(),
element_content_handlers: pre_diff_element_massaging_handlers(args),
..RewriteStrSettings::new()
},
)
.expect("lolhtml processing failed");
let rhs_t = rewrite_str(
&rhs_t,
RewriteStrSettings {
element_content_handlers: pre_diff_element_massaging_handlers(),
element_content_handlers: pre_diff_element_massaging_handlers(args),
..RewriteStrSettings::new()
},
)
Expand All @@ -394,7 +424,7 @@ fn full_diff(
DIFF_MAP.insert(diff_hash, "somewhere else".into());
diff.insert(
key,
ansi_to_html::convert(&if fast {
ansi_to_html::convert(&if args.fast {
diff_lines(&lhs, &rhs).to_string()
} else {
diff_words(&lhs, &rhs).to_string()
Expand Down Expand Up @@ -430,9 +460,7 @@ fn main() -> Result<(), anyhow::Error> {
let hits = max(a.len(), b.len());
let same = AtomicUsize::new(0);
if arg.html {
let mut out = Vec::new();
out.push("<ul>".to_string());
out.extend(a.par_iter().filter_map(|(k, v)| {
let list_items = a.par_iter().filter_map(|(k, v)| {
if b.get(k) == Some(v) {
same.fetch_add(1, Relaxed);
return None;
Expand All @@ -442,12 +470,12 @@ fn main() -> Result<(), anyhow::Error> {
let left = v;
let right = b.get(k).unwrap_or(&Value::Null);
let mut diff = BTreeMap::new();
full_diff(left, right, k, &[], &mut diff, arg.fast, arg.sidebars);
full_diff(left, right, k, &[], &mut diff, arg);
if !diff.is_empty() {
return Some(format!(
return Some((k.clone(), format!(
r#"<li><span>{k}</span><div class="r"><pre><code>{}</code></pre></div></li>"#,
serde_json::to_string_pretty(&diff).unwrap_or_default(),
));
)));
} else {
same.fetch_add(1, Relaxed);
}
Expand Down Expand Up @@ -483,16 +511,42 @@ fn main() -> Result<(), anyhow::Error> {
if arg.inline {
println!("{}", diff_words(&left, right));
}
Some(format!(
Some((k.clone(), format!(
r#"<li><span>{k}</span><div class="a">{}</div><div class="b">{}</div></li>"#,
left, right
))
)))
}
}
).collect::<Vec<_>>());
out.push("</ul>".to_string());
let mut file = File::create(&arg.out)?;
file.write_all(html(&out.into_iter().collect::<String>()).as_bytes())?;
).collect::<Vec<_>>();
let out: BTreeMap<String, Vec<_>> =
list_items
.into_iter()
.fold(BTreeMap::new(), |mut acc, (k, li)| {
let p = k.splitn(4, '/').collect::<Vec<_>>();
let cat = match &p[..] {
["docs", "web", cat, ..] => format!("docs/web/{cat}"),
["docs", cat, ..] => format!("docs/{cat}"),
[cat, ..] => cat.to_string(),
[] => "".to_string(),
};
acc.entry(cat).or_default().push(li);
acc
});

let out = out.into_iter().fold(String::new(), |mut acc, (k, v)| {
write!(
acc,
r#"<li><details><summary>[{}] {k}</summary><ul>{}</ul></details></li>"#,
v.len(),
v.into_iter().collect::<String>(),
)
.unwrap();
acc
});
let file = File::create(&arg.out)?;
let mut buffer = BufWriter::new(file);

buffer.write_all(html(&out).as_bytes())?;
}
if arg.csv {
let mut out = Vec::new();
Expand All @@ -508,7 +562,7 @@ fn main() -> Result<(), anyhow::Error> {
let left = v;
let right = b.get(k).unwrap_or(&Value::Null);
let mut diff = BTreeMap::new();
full_diff(left, right, k, &[], &mut diff, arg.fast, arg.sidebars);
full_diff(left, right, k, &[], &mut diff, arg);
if !diff.is_empty() {
return Some(format!(
"{}\n",
Expand All @@ -525,6 +579,7 @@ fn main() -> Result<(), anyhow::Error> {
.collect::<Vec<_>>(),
);
let mut file = File::create(&arg.out)?;

file.write_all(out.into_iter().collect::<String>().as_bytes())?;
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rari-doc/src/html/bubble_up.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use scraper::{ElementRef, Html, Selector};

use super::modifier::add_attribute;
use super::modifier::insert_attribute;
use crate::error::DocError;

pub fn bubble_up_curriculum_page(html: &mut Html) -> Result<(), DocError> {
Expand Down Expand Up @@ -45,7 +45,7 @@ pub fn bubble_up_curriculum_page(html: &mut Html) -> Result<(), DocError> {
//let next_ul = parent.next_siblings().find(|s| s.value().is_element());

for (id, value) in rews {
add_attribute(html, id, "class", value);
insert_attribute(html, id, "class", value);
}

Ok(())
Expand Down
6 changes: 3 additions & 3 deletions crates/rari-doc/src/html/modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use scraper::node::{self};
use scraper::{ElementRef, Html, Node, Selector};

use crate::error::DocError;
/// Adds an attribute to a specified HTML node.
/// Inserts an attribute to a specified HTML node.
///
/// # Parameters
/// - `html`: A mutable reference to the HTML document structure.
Expand All @@ -19,7 +19,7 @@ use crate::error::DocError;
///
/// If the node exists and is an element, this function adds or updates
/// the specified attribute in the node's attributes list.
pub fn add_attribute(html: &mut Html, node_id: NodeId, key: &str, value: &str) {
pub fn insert_attribute(html: &mut Html, node_id: NodeId, key: &str, value: &str) {
if let Some(mut details) = html.tree.get_mut(node_id) {
if let Node::Element(ref mut el) = details.value() {
el.attrs.insert(
Expand Down Expand Up @@ -250,7 +250,7 @@ pub fn add_missing_ids(html: &mut Html) -> Result<(), DocError> {
})
.collect::<Vec<_>>();
for (el_id, id) in subs {
add_attribute(html, el_id, "id", &id);
insert_attribute(html, el_id, "id", &id);
remove_attribute(html, el_id, "data-update-id");
}
Ok(())
Expand Down
41 changes: 23 additions & 18 deletions crates/rari-doc/src/html/rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,34 @@ pub fn post_process_html<T: PageLike>(
if let Some(id) = el.get_attribute("id") {
if id.contains(DELIM_START) {
el.set_attribute("data-update-id", "")?;
} else if ids.contains(id.as_str()) {
let (prefix, mut count) = if let Some((prefix, counter)) = id.rsplit_once('_') {
if counter.chars().all(|c| c.is_ascii_digit()) {
let count = counter.parse::<i64>().unwrap_or_default() + 1;
(prefix, count)
} else {
(id.as_str(), 2)
} else {
let id = id.to_lowercase();
if ids.contains(id.as_str()) {
let (prefix, mut count) =
if let Some((prefix, counter)) = id.rsplit_once('_') {
if counter.chars().all(|c| c.is_ascii_digit()) {
let count = counter.parse::<i64>().unwrap_or_default() + 1;
(prefix, count)
} else {
(id.as_str(), 2)
}
} else {
(id.as_str(), 2)
};
let mut id = format!("{prefix}_{count}");
while ids.contains(&id) && count < 666 {
count += 1;
id = format!("{prefix}_{count}");
}
} else {
(id.as_str(), 2)
};
let mut id = format!("{prefix}_{count}");
while ids.contains(&id) && count < 666 {
count += 1;
id = format!("{prefix}_{count}");
}

if !ids.contains(&id) && count < 666 {
if !ids.contains(&id) && count < 666 {
el.set_attribute("id", &id)?;
ids.insert(id);
}
} else {
el.set_attribute("id", &id)?;
ids.insert(id);
}
} else {
ids.insert(id);
}
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion crates/rari-doc/src/html/sections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub fn split_sections(html: &Html) -> Result<Splitted, DocError> {
id,
});
}
"section" if element.id() == Some("Quick_links") => {
"section" if matches!(element.id(), Some("Quick_links" | "quick_links")) => {
if let Some(section) = maybe_section.take() {
sections.push(section);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/rari-doc/src/html/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use scraper::{Html, Node, Selector};
use serde::{Deserialize, Serialize};

use super::links::{render_link_from_page, render_link_via_page, LinkModifier};
use super::modifier::add_attribute;
use super::modifier::insert_attribute;
use super::rewriter::post_process_html;
use crate::cached_readers::read_sidebar;
use crate::error::DocError;
Expand Down Expand Up @@ -77,10 +77,10 @@ fn expand_details_and_mark_current(html: &mut Html, a_selector: Selector) -> Res
}
}
if let Some(parent_id) = parent_id {
add_attribute(html, parent_id, "data-rewriter", "em");
insert_attribute(html, parent_id, "data-rewriter", "em");
}
for details in details {
add_attribute(html, details, "open", "");
insert_attribute(html, details, "open", "");
}

Ok(())
Expand Down
Loading

0 comments on commit 7432365

Please sign in to comment.