From de3ef2d7019eb30c0e2dd002bd926d7bf27952e2 Mon Sep 17 00:00:00 2001 From: Leynos Date: Mon, 4 Aug 2025 12:01:00 +0100 Subject: [PATCH 1/5] Add diagnostics and document Token lifetime --- src/wrap.rs | 8 +++++++- tests/wrap_renumber.rs | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/wrap.rs b/src/wrap.rs index 314fa572..4c62ae97 100644 --- a/src/wrap.rs +++ b/src/wrap.rs @@ -15,6 +15,10 @@ mod tokenize; /// Token emitted by [`tokenize::segment_inline`] and used by higher-level /// wrappers. /// +/// Downstream callers inspect [`Token<'a>`] when implementing bespoke +/// wrapping logic. The `'a` lifetime parameter ties each token to the source +/// text, avoiding unnecessary allocation. +/// /// Re-export these so callers of [`crate::textproc`] can implement custom /// transformations without depending on internal modules. pub use tokenize::Token; @@ -164,7 +168,9 @@ fn wrap_preserving_code(text: &str, width: usize) -> Vec { } #[doc(hidden)] -pub fn is_fence(line: &str) -> bool { FENCE_RE.is_match(line) } +pub fn is_fence(line: &str) -> bool { + FENCE_RE.is_match(line) +} pub(crate) fn is_markdownlint_directive(line: &str) -> bool { MARKDOWNLINT_DIRECTIVE_RE.is_match(line) diff --git a/tests/wrap_renumber.rs b/tests/wrap_renumber.rs index ae7c0ad6..44a1b6ea 100644 --- a/tests/wrap_renumber.rs +++ b/tests/wrap_renumber.rs @@ -13,5 +13,11 @@ fn wrap_then_renumber_preserves_order() { let mut out = process_stream(&input); out = renumber_lists(&out); - assert_eq!(out, expected); + assert_eq!( + out, + expected, + "renumbered output mismatch:\nexpected: {expected:?}\nactual: {out:?}", + expected = expected, + out = out, + ); } From b34b28e228ae0639990de2e333508906386c32f4 Mon Sep 17 00:00:00 2001 From: Leynos Date: Mon, 4 Aug 2025 14:40:02 +0100 Subject: [PATCH 2/5] Compact fence detection helper --- src/html.rs | 12 +++++++----- src/wrap.rs | 6 +++--- tests/wrap_renumber.rs | 5 +---- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/html.rs b/src/html.rs index c3372881..b148e314 100644 --- a/src/html.rs +++ b/src/html.rs @@ -84,7 +84,9 @@ fn is_element(handle: &Handle, tag: &str) -> bool { } /// Returns `true` if `handle` represents a `` or `` element. -fn is_table_cell(handle: &Handle) -> bool { is_element(handle, "td") || is_element(handle, "th") } +fn is_table_cell(handle: &Handle) -> bool { + is_element(handle, "td") || is_element(handle, "th") +} /// Walks the DOM tree collecting `` nodes under `handle`. fn collect_tables(handle: &Handle, tables: &mut Vec) { @@ -112,10 +114,10 @@ fn is_bold_tag(tag: &str) -> bool { /// Returns `true` if `handle` contains a `` or `` descendant. fn contains_strong(handle: &Handle) -> bool { - if let NodeData::Element { name, .. } = &handle.data - && is_bold_tag(name.local.as_ref()) - { - return true; + if let NodeData::Element { name, .. } = &handle.data { + if is_bold_tag(name.local.as_ref()) { + return true; + } } let children = handle.children.borrow(); children.iter().any(contains_strong) diff --git a/src/wrap.rs b/src/wrap.rs index 4c62ae97..83c0f620 100644 --- a/src/wrap.rs +++ b/src/wrap.rs @@ -167,10 +167,10 @@ fn wrap_preserving_code(text: &str, width: usize) -> Vec { lines } +/// Detect whether a line opens or closes a fenced code block (````` or `~~~`), +/// ignoring leading whitespace. #[doc(hidden)] -pub fn is_fence(line: &str) -> bool { - FENCE_RE.is_match(line) -} +pub fn is_fence(line: &str) -> bool { FENCE_RE.is_match(line) } pub(crate) fn is_markdownlint_directive(line: &str) -> bool { MARKDOWNLINT_DIRECTIVE_RE.is_match(line) diff --git a/tests/wrap_renumber.rs b/tests/wrap_renumber.rs index 44a1b6ea..73d0d6f3 100644 --- a/tests/wrap_renumber.rs +++ b/tests/wrap_renumber.rs @@ -14,10 +14,7 @@ fn wrap_then_renumber_preserves_order() { out = renumber_lists(&out); assert_eq!( - out, - expected, + out, expected, "renumbered output mismatch:\nexpected: {expected:?}\nactual: {out:?}", - expected = expected, - out = out, ); } From 516d607b855dfa9bc9d8fde240c746e1cff1e669 Mon Sep 17 00:00:00 2001 From: Leynos Date: Mon, 4 Aug 2025 23:32:49 +0100 Subject: [PATCH 3/5] Keep is_fence compact --- src/wrap.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wrap.rs b/src/wrap.rs index 83c0f620..3a1b06c8 100644 --- a/src/wrap.rs +++ b/src/wrap.rs @@ -167,9 +167,9 @@ fn wrap_preserving_code(text: &str, width: usize) -> Vec { lines } -/// Detect whether a line opens or closes a fenced code block (````` or `~~~`), -/// ignoring leading whitespace. +/// Checks if a line is a fence marker (````` or `~~~`), ignoring leading whitespace. #[doc(hidden)] +#[rustfmt::skip] pub fn is_fence(line: &str) -> bool { FENCE_RE.is_match(line) } pub(crate) fn is_markdownlint_directive(line: &str) -> bool { From fc22bbc2de3464fd1334582827ea5fd85cf5e154 Mon Sep 17 00:00:00 2001 From: Payton McIntosh Date: Mon, 4 Aug 2025 23:49:42 +0100 Subject: [PATCH 4/5] Apply formatting --- src/html.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/html.rs b/src/html.rs index b148e314..2742d7b3 100644 --- a/src/html.rs +++ b/src/html.rs @@ -84,9 +84,7 @@ fn is_element(handle: &Handle, tag: &str) -> bool { } /// Returns `true` if `handle` represents a `
` or `` element. -fn is_table_cell(handle: &Handle) -> bool { - is_element(handle, "td") || is_element(handle, "th") -} +fn is_table_cell(handle: &Handle) -> bool { is_element(handle, "td") || is_element(handle, "th") } /// Walks the DOM tree collecting `` nodes under `handle`. fn collect_tables(handle: &Handle, tables: &mut Vec) { From 1ce83cf33760058be1a7b96564893036d56e424e Mon Sep 17 00:00:00 2001 From: Payton McIntosh Date: Tue, 5 Aug 2025 00:01:20 +0100 Subject: [PATCH 5/5] Fix lint issue --- src/html.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/html.rs b/src/html.rs index 2742d7b3..c3372881 100644 --- a/src/html.rs +++ b/src/html.rs @@ -112,10 +112,10 @@ fn is_bold_tag(tag: &str) -> bool { /// Returns `true` if `handle` contains a `` or `` descendant. fn contains_strong(handle: &Handle) -> bool { - if let NodeData::Element { name, .. } = &handle.data { - if is_bold_tag(name.local.as_ref()) { - return true; - } + if let NodeData::Element { name, .. } = &handle.data + && is_bold_tag(name.local.as_ref()) + { + return true; } let children = handle.children.borrow(); children.iter().any(contains_strong)