-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fences2.rs
More file actions
36 lines (31 loc) · 1.39 KB
/
test_fences2.rs
File metadata and controls
36 lines (31 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
pub fn strip_code_fences(text: &str) -> String {
let trimmed = text.trim();
// Check if the entire response is a single code block
let lines: Vec<&str> = trimmed.lines().collect();
if lines.len() >= 3 {
let first = lines.first().copied().unwrap_or("").trim();
let last = lines.last().copied().unwrap_or("").trim();
if first.starts_with("```") && last == "```" {
return lines[1..lines.len() - 1].join("\n").trim().to_string();
}
}
// Look for a code block anywhere in the text
if let Some(start_idx) = trimmed.find("```") {
let after_ticks = &trimmed[start_idx + 3..];
if let Some(newline_idx) = after_ticks.find('\n') {
let code_start = start_idx + 3 + newline_idx + 1;
if let Some(end_idx) = trimmed[code_start..].find("```") {
return trimmed[code_start..code_start + end_idx].trim().to_string();
}
}
}
trimmed.to_string()
}
fn main() {
println!("1: {}", strip_code_fences("Here is the command:\n```bash\nls -la\n```"));
println!("2: {}", strip_code_fences("```\ngit status\n```"));
println!("3: {}", strip_code_fences("ls -la"));
println!("4: {}", strip_code_fences("```bash\nls -la"));
println!("5: {}", strip_code_fences("Here:\n```\nls\n```\nAnd more text"));
println!("6: {}", strip_code_fences("```\nls\n```\nAnd more text"));
}