Skip to content

Commit

Permalink
fix(diff): fast diff and various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Oct 10, 2024
1 parent c32b5d8 commit 560f198
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 22 deletions.
30 changes: 18 additions & 12 deletions crates/diff-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ struct BuildArgs {
#[arg(long)]
ignore_html_whitespace: bool,
#[arg(long)]
fast: bool,
#[arg(long)]
value: bool,
#[arg(short, long)]
verbose: bool,
Expand Down Expand Up @@ -185,7 +187,13 @@ static WS_DIFF: LazyLock<Regex> =
static DATA_FLAW_SRC: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r#" data-flaw-src="[^"]+""#).unwrap());

fn full_diff(lhs: &Value, rhs: &Value, path: &[PathIndex], diff: &mut BTreeMap<String, String>) {
fn full_diff(
lhs: &Value,
rhs: &Value,
path: &[PathIndex],
diff: &mut BTreeMap<String, String>,
fast: bool,
) {
if path.len() == 1 {
if let PathIndex::Object(s) = &path[0] {
if s == "url" {
Expand All @@ -209,6 +217,7 @@ fn full_diff(lhs: &Value, rhs: &Value, path: &[PathIndex], diff: &mut BTreeMap<S
rhs.get(i).unwrap_or(&Value::Null),
&path,
diff,
fast,
);
}
}
Expand All @@ -223,6 +232,7 @@ fn full_diff(lhs: &Value, rhs: &Value, path: &[PathIndex], diff: &mut BTreeMap<S
rhs.get(key).unwrap_or(&Value::Null),
&path,
diff,
fast,
);
}
}
Expand Down Expand Up @@ -251,10 +261,12 @@ fn full_diff(lhs: &Value, rhs: &Value, path: &[PathIndex], diff: &mut BTreeMap<S
if 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(),
ansi_to_html::convert(&if fast {
diff_lines(&lhs, &rhs).to_string()
} else {
diff_words(&lhs, &rhs).to_string()
})
.unwrap(),
);
}
}
Expand All @@ -264,12 +276,6 @@ fn full_diff(lhs: &Value, rhs: &Value, path: &[PathIndex], diff: &mut BTreeMap<S
if lhs != rhs {
diff.insert(
key,
//ansi_to_html::convert(
// &similar::TextDiff::from_words(&lhs, &rhs)
// .unified_diff()
// .to_string(),
//)
//.unwrap(),
ansi_to_html::convert(&diff_words(&lhs, &rhs).to_string()).unwrap(),
);
}
Expand Down Expand Up @@ -303,7 +309,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, &[], &mut diff);
full_diff(left, right, &[], &mut diff, arg.fast);
if !diff.is_empty() {
return Some(format!(
r#"<li><span>{k}</span><div class="r"><pre><code>{}</code></pre></div></li>"#,
Expand Down
11 changes: 9 additions & 2 deletions crates/rari-doc/src/helpers/summary_hack.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rari_md::{m2h_internal, M2HOptions};

use crate::error::DocError;
use crate::pages::page::{Page, PageLike};
use crate::pages::types::doc::render_md_to_html;
use crate::templ::render::render_for_summary;

/// There's a few places were we still tansplant content.
Expand All @@ -14,7 +15,13 @@ pub fn get_hacky_summary_md(page: &Page) -> Result<String, DocError> {
|| line.starts_with("##"))
})
.map(|line| {
render_for_summary(line).and_then(|md| render_md_to_html(md.trim(), page.locale()))
render_for_summary(line).and_then(|md| {
Ok(m2h_internal(
md.trim(),
page.locale(),
M2HOptions { sourcepos: false },
)?)
})
})
.unwrap_or_else(|| Ok(String::from("No summray found.")))
}
Expand Down
6 changes: 6 additions & 0 deletions crates/rari-doc/src/html/rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ pub fn post_process_html<T: PageLike>(
}
Ok(())
}),
element!("pre:not(.notranslate)", |el| {
let mut class = el.get_attribute("class").unwrap_or_default();
class.push_str(" notranslate");
el.set_attribute("class", &class)?;
Ok(())
}),
element!("pre[class*=brush]:not(.hidden)", |el| {
let class = el.get_attribute("class");
let class = class.as_deref().unwrap_or_default();
Expand Down
2 changes: 1 addition & 1 deletion crates/rari-doc/src/templ/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub(crate) fn decode_ref(input: &str, templs: &[String]) -> Result<String, DocEr

fn push_text(out: &mut String, slice: &str) {
let mut last = 0;
for (i, _) in slice.match_indices("\\{{") {
for (i, _) in slice.match_indices("\\{") {
out.push_str(&slice[last..i]);
last = i + 1;
}
Expand Down
3 changes: 2 additions & 1 deletion crates/rari-doc/src/templ/templs/glossarydisambiguation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ pub fn glossarydisambiguation() -> Result<String, DocError> {
out.push_str("<dl>");

for page in pages {
let summary = get_hacky_summary_md(&page)?;
out.extend([
r#"<dt><a href=""#,
page.url(),
r#"">"#,
page.title(),
r#"</a></dt><dd>"#,
strip_paragraph_unckecked(get_hacky_summary_md(&page)?.as_str()),
strip_paragraph_unckecked(summary.as_str()),
r#"</dd>"#,
]);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rari-md/src/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use regex::Regex;

pub fn anchorize(content: &str) -> Cow<'_, str> {
static REJECTED_CHARS: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r#"[<>"$#%&+,/:;=?@\[\]^`{|}~')(\\]"#).unwrap());
LazyLock::new(|| Regex::new(r#"[*<>"$#%&+,/:;=?@\[\]^`{|}~')(\\]"#).unwrap());

let id = REJECTED_CHARS.replace_all(content, "");
let mut id = id.trim().to_lowercase();
Expand Down
20 changes: 19 additions & 1 deletion crates/rari-md/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,31 @@ where
}
}

pub struct M2HOptions {
pub sourcepos: bool,
}

impl Default for M2HOptions {
fn default() -> Self {
Self { sourcepos: true }
}
}

/// rari's custom markdown parser. This implements the MDN markdown extensions.
/// See [MDN Markdown](https://developer.mozilla.org/en-US/docs/MDN/Writing_guidelines/Howto/Markdown_in_MDN)
pub fn m2h(input: &str, locale: Locale) -> Result<String, MarkdownError> {
m2h_internal(input, locale, Default::default())
}

pub fn m2h_internal(
input: &str,
locale: Locale,
m2h_options: M2HOptions,
) -> Result<String, MarkdownError> {
let arena = Arena::new();
let mut options = ComrakOptions::default();
options.extension.tagfilter = false;
options.render.sourcepos = true;
options.render.sourcepos = m2h_options.sourcepos;
options.render.experimental_inline_sourcepos = true;
options.render.unsafe_ = true;
options.extension.table = true;
Expand Down
21 changes: 17 additions & 4 deletions crates/rari-md/src/node_card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,30 @@ pub(crate) fn is_callout<'a>(block_quote: &'a AstNode<'a>, locale: Locale) -> Op
}
if let Some(child) = block_quote.first_child() {
if let Some(marker) = child.first_child() {
if let NodeValue::Text(ref text) = marker.data.borrow().value {
let mut data = marker.data.borrow_mut();
if let NodeValue::Text(ref text) = data.value {
if text.starts_with(NoteCard::Callout.new_prefix()) {
marker.detach();
if text.trim() == NoteCard::Callout.new_prefix() {
marker.detach();
} else if let Some(tail) = text.strip_prefix(NoteCard::Callout.new_prefix()) {
data.value = NodeValue::Text(tail.trim().to_string());
}
return Some(NoteCard::Callout);
}
if text.starts_with(NoteCard::Warning.new_prefix()) {
marker.detach();
if text.trim() == NoteCard::Warning.new_prefix() {
marker.detach();
} else if let Some(tail) = text.strip_prefix(NoteCard::Warning.new_prefix()) {
data.value = NodeValue::Text(tail.trim().to_string());
}
return Some(NoteCard::Warning);
}
if text.starts_with(NoteCard::Note.new_prefix()) {
marker.detach();
if text.trim() == NoteCard::Note.new_prefix() {
marker.detach();
} else if let Some(tail) = text.strip_prefix(NoteCard::Note.new_prefix()) {
data.value = NodeValue::Text(tail.trim().to_string());
}
return Some(NoteCard::Note);
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/rari-types/src/fm_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ pub enum PageType {
JavascriptStaticMethod,
MathmlAttribute,
MathmlElement,
MdnCommunityGuide,
MdnWritingGuide,
SvgAttribute,
SvgElement,
WebApiOverview,
Expand Down

0 comments on commit 560f198

Please sign in to comment.