txt-cleaner 0.1.0

A smarter text cleanup library for Rust that trims whitespace, removes BOM/zero-width chars, and cleans markdown/html artifacts.
Documentation
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/// Options for `txt-cleaner` operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CleanOptions {
    /// When true, preserves paragraph structure by collapsing consecutive blank
    /// lines to a single blank line (`\n\n`).
    pub preserve_paragraphs: bool,

    /// When true, remove common markdown/html artifacts from the edges of the text.
    pub strip_markdown_artifacts: bool,

    /// When true, remove invisible characters like BOM and zero-width spaces.
    pub strip_invisible_chars: bool,

    /// When true, collapse all whitespace sequences to a single space.
    pub collapse_all_whitespace: bool,
}

impl CleanOptions {
    /// Create a new builder for `CleanOptions`.
    pub fn builder() -> CleanBuilder {
        CleanBuilder::default()
    }
}

impl Default for CleanOptions {
    fn default() -> Self {
        Self {
            preserve_paragraphs: true,
            strip_markdown_artifacts: true,
            strip_invisible_chars: true,
            collapse_all_whitespace: false,
        }
    }
}

/// Builder for cleaning options.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CleanBuilder {
    preserve_paragraphs: bool,
    strip_markdown_artifacts: bool,
    strip_invisible_chars: bool,
    collapse_all_whitespace: bool,
}

impl Default for CleanBuilder {
    fn default() -> Self {
        Self {
            preserve_paragraphs: true,
            strip_markdown_artifacts: true,
            strip_invisible_chars: true,
            collapse_all_whitespace: false,
        }
    }
}

impl CleanBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn preserve_paragraphs(mut self, preserve: bool) -> Self {
        self.preserve_paragraphs = preserve;
        self
    }

    pub fn strip_markdown_artifacts(mut self, strip: bool) -> Self {
        self.strip_markdown_artifacts = strip;
        self
    }

    pub fn strip_invisible_chars(mut self, strip: bool) -> Self {
        self.strip_invisible_chars = strip;
        self
    }

    pub fn collapse_all_whitespace(mut self, collapse: bool) -> Self {
        self.collapse_all_whitespace = collapse;
        self
    }

    pub fn build(self) -> CleanOptions {
        CleanOptions {
            preserve_paragraphs: self.preserve_paragraphs,
            strip_markdown_artifacts: self.strip_markdown_artifacts,
            strip_invisible_chars: self.strip_invisible_chars,
            collapse_all_whitespace: self.collapse_all_whitespace,
        }
    }

    pub fn clean(self, text: &str) -> String {
        clean_with_options(text, self.build())
    }
}

/// Clean text with the default safe options.
///
/// This includes:
/// - removing BOM and zero-width characters,
/// - stripping common markdown/html edge artifacts,
/// - normalizing whitespace and collapsing consecutive spaces/tabs/newlines.
pub fn clean(text: &str) -> String {
    clean_with_options(text, CleanOptions::default())
}

/// Clean text using a builder configuration.
pub fn clean_with_builder(text: &str, builder: CleanBuilder) -> String {
    builder.clean(text)
}

/// Clean text using explicit options.
pub fn clean_with_options(text: &str, options: CleanOptions) -> String {
    let mut result = text.to_string();

    if options.strip_invisible_chars {
        result = trim_bom_and_zero_width(&result);
    }

    if options.strip_markdown_artifacts {
        result = trim_html_markdown_artifacts(&result);
    }

    if options.collapse_all_whitespace {
        collapse_whitespace_to_single_space(&result)
    } else if options.preserve_paragraphs {
        trim_consecutive_whitespaces(&result)
    } else {
        collapse_whitespace_to_single_space(&result)
    }
}

/// Remove BOM and zero-width characters from the input text.
pub fn trim_bom_and_zero_width(text: &str) -> String {
    const REMOVED: [char; 5] = ['\u{feff}', '\u{200b}', '\u{200c}', '\u{200d}', '\u{2060}'];

    text.chars()
        .filter(|ch| !REMOVED.contains(ch))
        .collect()
}

/// Collapse whitespace while preserving paragraph structure.
///
/// - consecutive spaces/tabs become a single space
/// - a single newline stays a newline
/// - multiple blank lines collapse to a single blank line (`\n\n`)
/// - leading/trailing whitespace is removed
pub fn trim_consecutive_whitespaces(text: &str) -> String {
    let normalized = normalize_newlines(text);
    let mut output = String::with_capacity(normalized.len());
    let mut newline_count = 0;
    let mut pending_space = false;

    for ch in normalized.chars() {
        match ch {
            '\n' => {
                newline_count += 1;
                pending_space = false;
            }
            ' ' | '\t' => {
                if newline_count == 0 {
                    pending_space = true;
                }
            }
            _ => {
                if newline_count > 0 {
                    if !output.ends_with('\n') {
                        if newline_count == 1 {
                            output.push('\n');
                        } else {
                            output.push_str("\n\n");
                        }
                    }
                    newline_count = 0;
                } else if pending_space && !output.ends_with(' ') && !output.ends_with('\n') {
                    output.push(' ');
                }

                pending_space = false;
                output.push(ch);
            }
        }
    }

    output.trim_matches(|c: char| c.is_whitespace()).to_string()
}

fn collapse_whitespace_to_single_space(text: &str) -> String {
    let mut output = String::with_capacity(text.len());
    let mut pending_space = false;

    for ch in normalize_newlines(text).chars() {
        if ch.is_whitespace() {
            pending_space = true;
            continue;
        }

        if pending_space && !output.is_empty() {
            output.push(' ');
        }

        pending_space = false;
        output.push(ch);
    }

    output.trim().to_string()
}

fn normalize_newlines(text: &str) -> String {
    text.replace("\r\n", "\n").replace('\r', "\n")
}

/// Remove stray markdown/html artifacts from the text edges.
///
/// This is heuristic cleanup for broken input such as:
/// - `** broken sentence`
/// - `# heading text`
/// - `<div>lorem ipsum</div>`
/// - stray backticks, asterisks, underscores, or tildes at the text edges
pub fn trim_html_markdown_artifacts(text: &str) -> String {
    let mut current = text.trim().to_string();

    loop {
        let before = current.clone();
        current = strip_edge_artifacts(&current);
        current = current.trim().to_string();
        if current == before {
            break;
        }
    }

    current
}

fn strip_edge_artifacts(text: &str) -> String {
    if let Some(stripped) = strip_html_tag_edge(text) {
        return stripped;
    }

    if let Some(stripped) = strip_heading_prefix(text) {
        return stripped.trim_start().to_string();
    }

    if let Some(stripped) = strip_blockquote_or_list_prefix(text) {
        return stripped.trim_start().to_string();
    }

    if let Some(stripped) = strip_unpaired_edge_markers(text, true) {
        return stripped;
    }

    if let Some(stripped) = strip_unpaired_edge_markers(text, false) {
        return stripped;
    }

    if let Some(stripped) = strip_matching_wrappers(text) {
        return stripped.trim().to_string();
    }

    text.to_string()
}

fn strip_html_tag_edge(text: &str) -> Option<String> {
    let lower = text.to_lowercase();

    let html_tags = ["<div>", "<p>", "<span>", "<strong>", "<em>", "<b>", "<i>"];
    for tag in html_tags {
        if lower.starts_with(tag) {
            return Some(text[tag.len()..].trim_start().to_string());
        }
    }

    let trimmed_end = text.trim_end();
    let lower_end = trimmed_end.to_lowercase();
    for tag in html_tags {
        if lower_end.ends_with(tag) {
            return Some(trimmed_end[..trimmed_end.len() - tag.len()].trim_end().to_string());
        }
    }

    let closing_tags = ["</div>", "</p>", "</span>", "</strong>", "</em>", "</b>", "</i>"];
    for tag in closing_tags {
        if lower.starts_with(tag) {
            return Some(text[tag.len()..].trim_start().to_string());
        }
    }

    for tag in closing_tags {
        if lower_end.ends_with(tag) {
            return Some(trimmed_end[..trimmed_end.len() - tag.len()].trim_end().to_string());
        }
    }

    if lower.starts_with("<!--") {
        return Some(text[4..].trim_start().to_string());
    }

    if lower_end.ends_with("-->") {
        return Some(trimmed_end[..trimmed_end.len() - 3].trim_end().to_string());
    }

    None
}

fn strip_blockquote_or_list_prefix(text: &str) -> Option<String> {
    let trimmed = text.trim_start();

    if trimmed.starts_with("> ") {
        return Some(trimmed[2..].to_string());
    }

    if trimmed == ">" {
        return Some(String::new());
    }

    for prefix in ["- ", "* ", "+ "] {
        if trimmed.starts_with(prefix) {
            return Some(trimmed[prefix.len()..].to_string());
        }
    }

    None
}

fn strip_matching_wrappers(text: &str) -> Option<&str> {
    const MARKERS: [char; 4] = ['*', '_', '~', '`'];

    for marker in MARKERS {
        let prefix = count_leading_chars(text, marker);
        let suffix = count_trailing_chars(text, marker);

        if prefix > 0 && suffix > 0 {
            let matched = prefix.min(suffix);
            if matched > 0 {
                let start = text.char_indices().nth(matched).map(|(idx, _)| idx).unwrap_or(text.len());
                let end = text.len() - text.chars().rev().take(matched).map(|c| c.len_utf8()).sum::<usize>();
                if start < end {
                    return Some(&text[start..end]);
                }
            }
        }
    }

    None
}

fn strip_unpaired_edge_markers(text: &str, leading: bool) -> Option<String> {
    const MARKERS: [char; 4] = ['*', '_', '~', '`'];

    if leading {
        let count = count_leading_chars_set(text, &MARKERS);
        if count > 0 {
            let after = &text[text.char_indices().nth(count).map(|(idx, _)| idx).unwrap_or(text.len())..];
            if after.is_empty()
                || after.chars().next().map_or(false, |c| {
                    c.is_whitespace() || c == '#' || c == '>' || c == '<'
                })
            {
                return Some(after.trim_start().to_string());
            }
        }
    } else {
        let count = count_trailing_chars_set(text, &MARKERS);
        if count > 0 {
            let before_end = text.char_indices().rev().nth(count - 1).map(|(idx, _ch)| idx).unwrap_or(0);
            let before = &text[..before_end];
            if before.is_empty()
                || before.chars().rev().next().map_or(false, |c| {
                    c.is_whitespace() || c == '#' || c == '>' || c == '<'
                })
            {
                return Some(before.trim_end().to_string());
            }
        }
    }

    None
}

fn count_leading_chars(text: &str, marker: char) -> usize {
    text.chars().take_while(|&c| c == marker).count()
}

fn count_trailing_chars(text: &str, marker: char) -> usize {
    text.chars().rev().take_while(|&c| c == marker).count()
}

fn count_leading_chars_set(text: &str, markers: &[char]) -> usize {
    text.chars().take_while(|c| markers.contains(c)).count()
}

fn count_trailing_chars_set(text: &str, markers: &[char]) -> usize {
    text.chars().rev().take_while(|c| markers.contains(c)).count()
}

fn strip_heading_prefix(text: &str) -> Option<&str> {
    for level in (1..=6).rev() {
        let prefix = "#".repeat(level);
        if let Some(stripped) = text.strip_prefix(&prefix) {
            if stripped.starts_with(char::is_whitespace) {
                return Some(stripped);
            }
        }
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn trim_bom_and_zero_width_removes_invisible_chars() {
        let input = "\u{feff}Hello\u{200B} world\u{200C}!";
        assert_eq!(trim_bom_and_zero_width(input), "Hello world!");
    }

    #[test]
    fn trim_consecutive_whitespaces_collapses_space_tab_newline() {
        let input = "  foo   bar\t\tbaz\n\n\nqux   ";
        assert_eq!(trim_consecutive_whitespaces(input), "foo bar baz\n\nqux");
    }

    #[test]
    fn collapse_whitespace_to_single_space_works() {
        let input = "foo\n\nbar\t\t baz";
        assert_eq!(collapse_whitespace_to_single_space(input), "foo bar baz");
    }

    #[test]
    fn trim_html_markdown_artifacts_removes_edge_tokens() {
        let input = "  **# Hello *world* <div>  ";
        assert_eq!(trim_html_markdown_artifacts(input), "Hello *world*");
    }

    #[test]
    fn trim_html_markdown_artifacts_removes_heading_and_html_wrappers() {
        let input = "## <div>**Hello**</div>";
        assert_eq!(trim_html_markdown_artifacts(input), "Hello");
    }

    #[test]
    fn clean_with_builder_can_collapse_all_whitespace() {
        let input = "foo\n\nbar\t baz";
        let output = CleanBuilder::new().collapse_all_whitespace(true).clean(input);
        assert_eq!(output, "foo bar baz");
    }

    #[test]
    fn builder_api_allows_full_configuration() {
        let input = "\u{feff}  **# Hello  \n\n\n  world!  **\u{200B} ";
        let options = CleanOptions::builder()
            .strip_invisible_chars(true)
            .strip_markdown_artifacts(true)
            .preserve_paragraphs(false)
            .collapse_all_whitespace(true)
            .build();

        assert_eq!(clean_with_options(input, options), "Hello world!");
    }

    #[test]
    fn clean_applies_all_steps() {
        let input = "\u{feff}**\n  Hello   \n\n\n  world!  **\u{200B} ";
        assert_eq!(clean(input), "Hello\n\nworld!");
    }
}