Skip to content

Commit

Permalink
fix(diff): pretty html diff and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Oct 10, 2024
1 parent b08e20b commit c32b5d8
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 62 deletions.
19 changes: 11 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/diff-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ prettydiff = "0.7"
html-minifier = "5"
ansi-to-html = "0.2"
similar = "2"
htmldiff = "0.1"
quick-xml = "0.36"
clap = { version = "4.5.1", features = ["derive"] }
28 changes: 13 additions & 15 deletions crates/diff-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ use std::sync::LazyLock;

use anyhow::{anyhow, Error};
use clap::{Args, Parser, Subcommand};
use htmldiff::htmldiff;
use ignore::types::TypesBuilder;
use ignore::WalkBuilder;
use itertools::Itertools;
use jsonpath_lib::Compiled;
use prettydiff::diff_words;
use prettydiff::{diff_lines, diff_words};
use rayon::prelude::*;
use regex::Regex;
use serde_json::Value;
use xml::fmt_html;

mod xml;

fn html(body: &str) -> String {
format!(
Expand Down Expand Up @@ -243,21 +245,17 @@ fn full_diff(lhs: &Value, rhs: &Value, path: &[PathIndex], diff: &mut BTreeMap<S
let rhs_t = WS_DIFF.replace_all(&rhs, "$x$y");
let lhs_t = DATA_FLAW_SRC.replace_all(&lhs_t, "");
let rhs_t = DATA_FLAW_SRC.replace_all(&rhs_t, "");
lhs = html_minifier::minify(lhs_t).unwrap();
rhs = html_minifier::minify(rhs_t).unwrap();
lhs = fmt_html(&html_minifier::minify(lhs_t).unwrap());
rhs = fmt_html(&html_minifier::minify(rhs_t).unwrap());
}
if lhs != rhs {
if key != "doc.sidebarHTML" {
diff.insert(
key,
ansi_to_html::convert(&diff_words(&lhs, &rhs).to_string()).unwrap(),
//similar::TextDiff::from_words(&lhs, &rhs)
// .unified_diff()
// .to_string(),
);
} else {
diff.insert(key, htmldiff(&lhs, &rhs));
}
diff.insert(
key,
ansi_to_html::convert(&diff_lines(&lhs, &rhs).to_string()).unwrap(),
//similar::TextDiff::from_words(&lhs, &rhs)
// .unified_diff()
// .to_string(),
);
}
}
(lhs, rhs) => {
Expand Down
22 changes: 22 additions & 0 deletions crates/diff-test/src/xml.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::io::Cursor;

use quick_xml::events::Event;
use quick_xml::reader::Reader;
use quick_xml::writer::Writer;

pub fn fmt_html(html: &str) -> String {
let mut reader = Reader::from_str(html);
reader.config_mut().trim_text(true);
let mut writer = Writer::new_with_indent(Cursor::new(Vec::new()), b' ', 0);
loop {
match reader.read_event() {
Ok(Event::Eof) => break,
// we can either move or borrow the event to write, depending on your use-case
Ok(e) => assert!(writer.write_event(e).is_ok()),
_ => {}
}
}

let result = writer.into_inner().into_inner();
String::from_utf8(result).unwrap()
}
15 changes: 7 additions & 8 deletions crates/rari-doc/src/templ/templs/embeds/embedyoutube.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
use std::borrow::Cow;

use rari_templ_func::rari_f;
use rari_utils::concat_strs;

use crate::error::DocError;

#[rari_f]
pub fn embed_youtube(video_id: String, title: Option<String>) -> Result<String, DocError> {
let title = title
.as_deref()
.map(|s| html_escape::encode_double_quoted_attribute(s));
let mut out = String::new();
out.extend([
.map(|s| html_escape::encode_double_quoted_attribute(s))
.unwrap_or(Cow::Borrowed("YouTube video"));
Ok(concat_strs!(
r#"<iframe width="560" height="315" "#,
r#"src="https://www.youtube-nocookie.com/embed/"#,
video_id.as_str(),
r#"" title=""#,
&title.unwrap_or(Cow::Borrowed("Youtube video")),
r#"" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>"#,
r#""></iframe>"#,
]);
Ok(out)
&title,
r#"" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>"#
))
}
4 changes: 2 additions & 2 deletions crates/rari-doc/src/templ/templs/embeds/jsfiddleembed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn embded_jsfiddle(
height: Option<AnyArg>,
) -> Result<String, DocError> {
let mut out = String::new();
out.push_str(r#"<iframe allowfullscreen="allowfullscreen" width="756" "#);
out.push_str(r#"<p><iframe allowfullscreen="allowfullscreen" width="756" "#);
if let Some(height) = height {
if !height.is_empty() {
write!(&mut out, r#"height="{}" "#, height)?;
Expand All @@ -28,7 +28,7 @@ pub fn embded_jsfiddle(
} else {
"/"
},
r#""></iframe>"#,
r#""></iframe></p>"#,
]);
Ok(out)
}
2 changes: 1 addition & 1 deletion crates/rari-doc/src/templ/templs/embeds/livesample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn live_sample(
if !height.is_empty() {
// TODO: fix this
if height.as_int() < 60 {
write!(&mut out, r#"height="{}" "#, height.as_int())?;
write!(&mut out, r#"height="60" "#)?;
} else {
write!(&mut out, r#"height="{}" "#, height)?;
}
Expand Down
60 changes: 33 additions & 27 deletions crates/rari-doc/src/templ/templs/previous_menu_next.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,43 @@ fn previous_next_menu_internal(
let mut out = String::new();
out.push_str(r#"<ul class="prev-next">"#);
if let Some(prev) = prev {
let page = RariApi::get_page(&concat_strs!(
"/",
locale.as_url_str(),
"/docs/",
prev.as_str()
))?;
let title = l10n_json_data("Template", "previous", locale)?;
generate_link(&mut out, page.slug(), locale, title)?;
if !prev.is_empty() {
let page = RariApi::get_page(&concat_strs!(
"/",
locale.as_url_str(),
"/docs/",
prev.as_str()
))?;
let title = l10n_json_data("Template", "previous", locale)?;
generate_link(&mut out, page.slug(), locale, title)?;
}
}
if let Some(menu) = menu {
let page = RariApi::get_page(&concat_strs!(
"/",
locale.as_url_str(),
"/docs/",
menu.as_str()
))?;
let title = concat_strs!(
l10n_json_data("Template", "prev_next_menu", locale)?,
page.title()
);
generate_link(&mut out, page.slug(), locale, &title)?;
if !menu.is_empty() {
let page = RariApi::get_page(&concat_strs!(
"/",
locale.as_url_str(),
"/docs/",
menu.as_str()
))?;
let title = concat_strs!(
l10n_json_data("Template", "prev_next_menu", locale)?,
page.title()
);
generate_link(&mut out, page.slug(), locale, &title)?;
}
}
if let Some(next) = next {
let page = RariApi::get_page(&concat_strs!(
"/",
locale.as_url_str(),
"/docs/",
next.as_str()
))?;
let title = l10n_json_data("Template", "next", locale)?;
generate_link(&mut out, page.slug(), locale, title)?;
if !next.is_empty() {
let page = RariApi::get_page(&concat_strs!(
"/",
locale.as_url_str(),
"/docs/",
next.as_str()
))?;
let title = l10n_json_data("Template", "next", locale)?;
generate_link(&mut out, page.slug(), locale, title)?;
}
}
out.push_str("</ul>");
Ok(out)
Expand Down

0 comments on commit c32b5d8

Please sign in to comment.