Skip to main content

jouet_github/pr/threads/
condense.rs

1//! Condensing comment bodies for listing.
2//!
3//! Review bots wrap a few sentences of finding in HTML comments holding
4//! their state, badge images and `<details>` blocks of prompts and
5//! walkthroughs. Condensing removes the comments and badges, collapses each
6//! `<details>` to its summary and caps what is left at a number of lines,
7//! with a marker saying how much was cut. Suggestion blocks are code to
8//! apply, so they are always kept whole, even inside a collapsed `<details>`
9//! or past the cap. Other code blocks are kept intact by the HTML rules (an
10//! HTML comment inside a code block is code) but count toward the cap.
11
12/// Stands in for a fenced code block while the text around it is rewritten.
13const MARK: char = '\u{1}';
14
15#[derive(Debug, PartialEq, Eq)]
16pub struct Condensed {
17    pub body: String,
18    /// Whether anything other than comments and badges was left out: lines
19    /// past the cap or the contents of a `<details>`.
20    pub truncated: bool,
21}
22
23struct Fence {
24    lines: Vec<String>,
25    suggestion: bool,
26    closed: bool,
27}
28
29pub fn condense(body: &str, max_lines: usize) -> Condensed {
30    let body = body.replace("\r\n", "\n");
31    let (text, fences) = take_fences(&body);
32    let text = remove_html_comments(&text);
33    let (text, collapsed) = collapse_details(&text, &fences);
34    let text = remove_badges(&text);
35    let mut lines = Vec::new();
36    for line in text.lines() {
37        let line = line.trim_end();
38        if line.is_empty() && lines.last().is_none_or(|l: &&str| l.is_empty()) {
39            continue;
40        }
41        lines.push(line);
42    }
43    while lines.last().is_some_and(|l| l.is_empty()) {
44        lines.pop();
45    }
46    let (body, cut) = cap(&lines, &fences, max_lines);
47    Condensed {
48        body,
49        truncated: collapsed || cut,
50    }
51}
52
53/// The contents of the first suggestion block.
54pub fn suggestion(body: &str) -> Option<String> {
55    let body = body.replace("\r\n", "\n");
56    let (_, fences) = take_fences(&body);
57    let fence = fences.into_iter().find(|f| f.suggestion)?;
58    let end = fence.lines.len() - usize::from(fence.closed);
59    Some(fence.lines[1..end].join("\n"))
60}
61
62/// Replaces each fenced code block with a line holding its index.
63fn take_fences(body: &str) -> (String, Vec<Fence>) {
64    let mut text = String::new();
65    let mut fences: Vec<Fence> = Vec::new();
66    let mut open: Option<(char, usize)> = None;
67    for line in body.lines() {
68        let trimmed = line.trim_start();
69        if let Some((c, len)) = open {
70            let fence = fences.last_mut().expect("an open fence was pushed");
71            fence.lines.push(line.to_string());
72            let run = trimmed.chars().take_while(|&x| x == c).count();
73            if run >= len && trimmed[run..].trim().is_empty() {
74                fence.closed = true;
75                open = None;
76            }
77            continue;
78        }
79        let c = trimmed.chars().next().unwrap_or(' ');
80        let run = trimmed.chars().take_while(|&x| x == c).count();
81        if matches!(c, '`' | '~') && run >= 3 {
82            let info = trimmed[run..].trim();
83            open = Some((c, run));
84            text.push(MARK);
85            text.push_str(&fences.len().to_string());
86            text.push(MARK);
87            text.push('\n');
88            fences.push(Fence {
89                lines: vec![line.to_string()],
90                suggestion: info.split_whitespace().next() == Some("suggestion"),
91                closed: false,
92            });
93            continue;
94        }
95        text.push_str(line);
96        text.push('\n');
97    }
98    (text, fences)
99}
100
101fn remove_html_comments(text: &str) -> String {
102    let mut out = String::new();
103    let mut rest = text;
104    while let Some(start) = rest.find("<!--") {
105        let Some(len) = rest[start..].find("-->") else {
106            break;
107        };
108        out.push_str(&rest[..start]);
109        rest = &rest[start + len + 3..];
110    }
111    out.push_str(rest);
112    out
113}
114
115/// Collapses every outermost `<details>` to `[details: SUMMARY]`, followed by
116/// any suggestion blocks it held. Returns whether any was collapsed.
117fn collapse_details(text: &str, fences: &[Fence]) -> (String, bool) {
118    let mut out = String::new();
119    let mut rest = text;
120    let mut collapsed = false;
121    while let Some(start) = find_tag(rest, "details") {
122        let Some(end) = closing(&rest[start..], "details") else {
123            break;
124        };
125        let block = &rest[start..start + end];
126        out.push_str(&rest[..start]);
127        let summary = find_tag(block, "summary")
128            .and_then(|s| {
129                let open = s + block[s..].find('>')? + 1;
130                let close = open + find_close(&block[open..], "summary")?;
131                Some(plain(&block[open..close]))
132            })
133            .filter(|s| !s.is_empty())
134            .unwrap_or_else(|| "details".into());
135        out.push_str(&format!("\n[details: {summary}]\n"));
136        for index in marks(block) {
137            if fences.get(index).is_some_and(|f| f.suggestion) {
138                out.push_str(&format!("{MARK}{index}{MARK}\n"));
139            }
140        }
141        collapsed = true;
142        rest = &rest[start + end..];
143    }
144    out.push_str(rest);
145    (out, collapsed)
146}
147
148/// Removes badge images (SVGs and images from badge services) and
149/// `<picture>` elements, then the links they leave empty. An `<img>` badge
150/// with alt text, such as a reviewer's `P1`, becomes `[P1]`.
151fn remove_badges(text: &str) -> String {
152    let text = replace(text, &['<', '!'], |s| {
153        if find_tag(s, "picture") == Some(0) {
154            return closing(s, "picture").map(|len| (len, String::new()));
155        }
156        img_tag(s).or_else(|| markdown_image(s))
157    });
158    replace(&text, &['<', '['], |s| {
159        empty_anchor(s).or_else(|| empty_markdown_link(s))
160    })
161}
162
163/// Rewrites `text`, trying `at` wherever one of `starts` appears: it gives
164/// the length to replace and what to put in its place. Spaces after a
165/// removal are dropped where they would follow a line start or markup
166/// (`** Minor**` is not bold).
167fn replace(text: &str, starts: &[char], at: impl Fn(&str) -> Option<(usize, String)>) -> String {
168    let mut out = String::new();
169    let mut rest = text;
170    while let Some(i) = rest.find(starts) {
171        out.push_str(&rest[..i]);
172        rest = &rest[i..];
173        match at(rest) {
174            Some((len, with)) => {
175                out.push_str(&with);
176                rest = &rest[len..];
177                if with.is_empty() && out.chars().last().is_none_or(|c| "\n*_>([".contains(c)) {
178                    rest = rest.trim_start_matches([' ', '\t']);
179                }
180            }
181            None => {
182                out.push_str(&rest[..1]);
183                rest = &rest[1..];
184            }
185        }
186    }
187    out.push_str(rest);
188    out
189}
190
191/// A badge `<img ...>` tag at the start of `s`, and its alt text.
192fn img_tag(s: &str) -> Option<(usize, String)> {
193    if !s.get(..4)?.eq_ignore_ascii_case("<img") {
194        return None;
195    }
196    let len = s.find('>')? + 1;
197    let tag = &s[..len];
198    if !is_badge(attr(tag, "src")?) {
199        return None;
200    }
201    let alt = attr(tag, "alt").map(str::trim).unwrap_or_default();
202    Some((
203        len,
204        if alt.is_empty() {
205            String::new()
206        } else {
207            format!("[{alt}]")
208        },
209    ))
210}
211
212/// A badge `![alt](url)` at the start of `s`.
213fn markdown_image(s: &str) -> Option<(usize, String)> {
214    let s2 = s.strip_prefix("![")?;
215    let alt = s2.find("](")?;
216    if s2[..alt].contains('\n') {
217        return None;
218    }
219    let url_start = alt + 2;
220    let url_len = s2[url_start..].find(')')?;
221    let url = s2[url_start..url_start + url_len]
222        .split_whitespace()
223        .next()?;
224    is_badge(url).then(|| (2 + url_start + url_len + 1, String::new()))
225}
226
227fn is_badge(url: &str) -> bool {
228    let url = url.to_ascii_lowercase();
229    let path = url.split(['?', '#']).next().unwrap_or_default();
230    path.ends_with(".svg")
231        || ["shields.io", "badgen.net", "/badge"]
232            .iter()
233            .any(|b| url.contains(b))
234}
235
236/// An `<a ...>` at the start of `s` that holds only whitespace (removed) or
237/// links nowhere, `href="#"` (replaced by what it holds).
238fn empty_anchor(s: &str) -> Option<(usize, String)> {
239    let b = s.as_bytes();
240    let anchor = b.len() > 2
241        && b[0] == b'<'
242        && b[1].eq_ignore_ascii_case(&b'a')
243        && (b[2] == b'>' || b[2].is_ascii_whitespace());
244    if !anchor {
245        return None;
246    }
247    let open = s.find('>')? + 1;
248    let close = open + find_close(&s[open..], "a")?;
249    let inner = s[open..close].trim();
250    let len = close + "</a>".len();
251    if inner.is_empty() {
252        Some((len, String::new()))
253    } else {
254        (attr(&s[..open], "href") == Some("#")).then(|| (len, inner.to_string()))
255    }
256}
257
258/// A `[](url)` at the start of `s`.
259fn empty_markdown_link(s: &str) -> Option<(usize, String)> {
260    let r = s.strip_prefix('[')?.trim_start().strip_prefix("](")?;
261    let len = r.find(')')?;
262    (!r[..len].contains(char::is_whitespace)).then(|| (s.len() - r.len() + len + 1, String::new()))
263}
264
265/// Fits the lines into `max` lines (0 means no limit), expanding fences.
266/// Suggestion blocks are always kept and do not count. A code block cut
267/// short is closed so the rest of the text does not read as code.
268fn cap(lines: &[&str], fences: &[Fence], max: usize) -> (String, bool) {
269    let room = |kept: usize| {
270        if max == 0 {
271            usize::MAX
272        } else {
273            max.saturating_sub(kept)
274        }
275    };
276    let mut out: Vec<String> = Vec::new();
277    let (mut kept, mut cut) = (0, 0);
278    let mut marker_at = None;
279    for line in lines {
280        match mark(line).and_then(|i| fences.get(i)) {
281            Some(fence) if fence.suggestion => out.extend(fence.lines.iter().cloned()),
282            Some(fence) => {
283                let take = fence.lines.len().min(room(kept));
284                out.extend(fence.lines[..take].iter().cloned());
285                kept += take;
286                if take < fence.lines.len() {
287                    if take > 0 {
288                        let open = fence.lines[0].trim_start();
289                        let c = open.chars().next().unwrap_or('`');
290                        let len = open.chars().take_while(|&x| x == c).count();
291                        out.push(c.to_string().repeat(len));
292                    }
293                    marker_at.get_or_insert(out.len());
294                    cut += fence.lines.len() - take;
295                }
296            }
297            None if room(kept) > 0 => {
298                out.push((*line).to_string());
299                kept += 1;
300            }
301            None => {
302                marker_at.get_or_insert(out.len());
303                cut += 1;
304            }
305        }
306    }
307    if let Some(at) = marker_at {
308        let s = if cut == 1 { "" } else { "s" };
309        out.insert(at, format!("... {cut} more line{s} (--full shows all)"));
310    }
311    (out.join("\n"), cut > 0)
312}
313
314fn mark(line: &str) -> Option<usize> {
315    line.strip_prefix(MARK)?.strip_suffix(MARK)?.parse().ok()
316}
317
318fn marks(text: &str) -> Vec<usize> {
319    text.lines().filter_map(mark).collect()
320}
321
322/// The byte offset of the first `<NAME` tag (followed by `>` or whitespace),
323/// ignoring case.
324fn find_tag(s: &str, name: &str) -> Option<usize> {
325    let lower = s.to_ascii_lowercase();
326    let needle = format!("<{name}");
327    let mut from = 0;
328    while let Some(i) = lower[from..].find(&needle) {
329        let at = from + i;
330        let next = lower[at + needle.len()..].chars().next();
331        if next.is_some_and(|c| c == '>' || c.is_whitespace()) {
332            return Some(at);
333        }
334        from = at + needle.len();
335    }
336    None
337}
338
339/// The byte offset of the first `</NAME>` in `s`, ignoring case.
340fn find_close(s: &str, name: &str) -> Option<usize> {
341    s.to_ascii_lowercase().find(&format!("</{name}>"))
342}
343
344/// The length of the element that starts `s`, through its matching close
345/// tag, counting nested elements of the same name.
346fn closing(s: &str, name: &str) -> Option<usize> {
347    let close = format!("</{name}>");
348    let mut depth = 0;
349    let mut at = 0;
350    loop {
351        let next_open = find_tag(&s[at..], name).map(|i| at + i);
352        let next_close = find_close(&s[at..], name).map(|i| at + i)?;
353        match next_open {
354            Some(open) if open < next_close => {
355                depth += 1;
356                at = open + 1;
357            }
358            _ => {
359                depth -= 1;
360                at = next_close + close.len();
361                if depth == 0 {
362                    return Some(at);
363                }
364            }
365        }
366    }
367}
368
369/// An attribute's value in an HTML tag, quoted or not.
370fn attr<'a>(tag: &'a str, name: &str) -> Option<&'a str> {
371    let lower = tag.to_ascii_lowercase();
372    let mut from = 0;
373    loop {
374        let i = from + lower[from..].find(name)?;
375        from = i + name.len();
376        let before = lower[..i].chars().last();
377        if !before.is_some_and(char::is_whitespace) {
378            continue;
379        }
380        let rest = tag[from..].trim_start().strip_prefix('=')?.trim_start();
381        return Some(match rest.chars().next()? {
382            q @ ('"' | '\'') => {
383                let rest = &rest[1..];
384                &rest[..rest.find(q)?]
385            }
386            _ => rest.split(|c: char| c.is_whitespace() || c == '>').next()?,
387        });
388    }
389}
390
391/// Text with tags removed and whitespace collapsed.
392fn plain(html: &str) -> String {
393    let mut out = String::new();
394    let mut in_tag = false;
395    for c in html.chars() {
396        match c {
397            '<' => in_tag = true,
398            '>' if in_tag => in_tag = false,
399            _ if !in_tag => out.push(c),
400            _ => {}
401        }
402    }
403    out.split_whitespace().collect::<Vec<_>>().join(" ")
404}
405
406#[cfg(test)]
407mod tests {
408    use super::*;
409
410    fn condensed(body: &str) -> String {
411        condense(body, 30).body
412    }
413
414    #[test]
415    fn html_comments_go_but_code_keeps_them() {
416        let body = "Fix this.<!-- state: {\"a\":1}\nmore state -->\n\n```html\n<!-- kept -->\n```";
417        assert_eq!(condensed(body), "Fix this.\n\n```html\n<!-- kept -->\n```");
418    }
419
420    #[test]
421    fn details_collapse_to_their_summary() {
422        let body = "Problem.\n\n<details>\n<summary>๐Ÿค– Prompt for <b>AI</b> agents</summary>\n\n\
423                    Long prompt\n<details><summary>inner</summary>x</details>\n</details>\n\nAfter.";
424        let c = condense(body, 30);
425        assert_eq!(
426            c.body,
427            "Problem.\n\n[details: ๐Ÿค– Prompt for AI agents]\n\nAfter."
428        );
429        assert!(c.truncated);
430    }
431
432    #[test]
433    fn suggestions_survive_a_collapsed_details() {
434        let body = "Use a timeout.\n\n<details>\n<summary>๐Ÿ“ Committable suggestion</summary>\n\n\
435                    ```suggestion\nlet resp = get(url).timeout(t)?;\n```\n\n</details>";
436        assert_eq!(
437            condensed(body),
438            "Use a timeout.\n\n[details: ๐Ÿ“ Committable suggestion]\n\
439             ```suggestion\nlet resp = get(url).timeout(t)?;\n```"
440        );
441        assert_eq!(
442            suggestion(body).as_deref(),
443            Some("let resp = get(url).timeout(t)?;")
444        );
445    }
446
447    #[test]
448    fn badges_and_their_empty_links_go() {
449        let body = "<a href=\"https://cursor.com/open?x=1\"><picture><source srcset=\"a.svg\">\
450                    <img alt=\"Fix in Cursor\" src=\"https://cursor.com/fix-in-cursor.svg\"></picture></a> \
451                    [![CI](https://img.shields.io/badge/ci-passing-green)](https://ci) \
452                    <img src=\"https://example.com/badge.svg?x=1\" />\n\
453                    A real finding.\n\n![screenshot](https://example.com/shot.png)";
454        assert_eq!(
455            condensed(body),
456            "A real finding.\n\n![screenshot](https://example.com/shot.png)"
457        );
458    }
459
460    #[test]
461    fn badge_alt_text_is_kept_and_markup_stays_valid() {
462        // As greptile and CodeRabbit write them.
463        let body = "<a href=\"#\"><img alt=\"P1\" src=\"https://x.s3.amazonaws.com/badges/p1.svg?v=9\" \
464                    align=\"top\"></a> **One daemon stops another**\n\n\
465                    **<picture><img src=\"https://x/amber-16.svg\" alt=\"\"></picture> Minor** ยท **Quick win**";
466        assert_eq!(
467            condensed(body),
468            "[P1] **One daemon stops another**\n\n**Minor** ยท **Quick win**"
469        );
470    }
471
472    #[test]
473    fn the_cap_marks_the_cut_and_keeps_suggestions() {
474        let mut body: String = (1..=40).map(|i| format!("line {i}\n")).collect();
475        body.push_str("```suggestion\nfixed\n```\n");
476        let c = condense(&body, 30);
477        let lines: Vec<&str> = c.body.lines().collect();
478        assert_eq!(lines[29], "line 30");
479        assert_eq!(lines[30], "... 10 more lines (--full shows all)");
480        assert_eq!(&lines[31..], ["```suggestion", "fixed", "```"]);
481        assert!(c.truncated);
482        assert_eq!(condense(&body, 0).body.lines().count(), 43);
483    }
484
485    #[test]
486    fn a_code_block_cut_short_is_closed() {
487        let body = format!("intro\n```rust\n{}```\nafter", "x\n".repeat(10));
488        let c = condense(&body, 5);
489        assert_eq!(
490            c.body,
491            "intro\n```rust\nx\nx\nx\n```\n... 9 more lines (--full shows all)"
492        );
493    }
494
495    #[test]
496    fn short_plain_bodies_are_unchanged() {
497        let c = condense("good catch, is this also true\r\nfor the archive?", 30);
498        assert_eq!(c.body, "good catch, is this also true\nfor the archive?");
499        assert!(!c.truncated);
500    }
501
502    #[test]
503    fn unclosed_markup_is_left_alone() {
504        assert_eq!(condensed("a <!-- b"), "a <!-- b");
505        assert_eq!(
506            condensed("<details><summary>s</summary>"),
507            "<details><summary>s</summary>"
508        );
509    }
510}