Repository navigation
Expand file tree
/
Copy pathxml.rs
More file actions
80 lines (70 loc) · 2.5 KB
/
Copy pathxml.rs
File metadata and controls
80 lines (70 loc) · 2.5 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! XML formatting utilities.
/// Format XML with proper indentation (Issue #1980).
/// Simple XML pretty-printer that adds indentation to make XML readable.
pub fn format_xml(xml: &str) -> String {
let mut result = String::new();
let mut indent_level: usize = 0;
let mut in_tag = false;
let mut current_tag = String::new();
let mut is_closing_tag;
let mut is_self_closing;
let mut text_content = String::new();
for c in xml.chars() {
match c {
'<' => {
// Output any accumulated text content
let trimmed = text_content.trim();
if !trimmed.is_empty() {
result.push_str(trimmed);
}
text_content.clear();
in_tag = true;
current_tag.clear();
}
'>' => {
if in_tag {
// Check if this is a closing tag
is_closing_tag = current_tag.starts_with('/');
// Check if self-closing
is_self_closing = current_tag.ends_with('/');
// For closing tags, decrease indent first
if is_closing_tag && indent_level > 0 {
indent_level -= 1;
}
// Add newline and indent before tag (except for first tag)
if !result.is_empty() {
result.push('\n');
result.push_str(&" ".repeat(indent_level));
}
// Output the tag
result.push('<');
result.push_str(¤t_tag);
result.push('>');
// For opening tags (not self-closing), increase indent
if !is_closing_tag
&& !is_self_closing
&& !current_tag.starts_with('?')
&& !current_tag.starts_with('!')
{
indent_level += 1;
}
in_tag = false;
current_tag.clear();
}
}
_ => {
if in_tag {
current_tag.push(c);
} else {
text_content.push(c);
}
}
}
}
// Handle any remaining text
let trimmed = text_content.trim();
if !trimmed.is_empty() {
result.push_str(trimmed);
}
result
}