wicket 0.1.1

Wikipedia corpus knowledge extractor.
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
/// Wikitext cleaner module.
///
/// Converts MediaWiki markup (wikitext) into plain text by parsing the wikitext
/// into an AST using `parse_wiki_text_2` and extracting text content. Falls back
/// to regex-based cleaning when AST parsing fails.
///
/// The parser is configured with both English and Japanese Wikipedia namespaces
/// so that it can correctly handle dumps from either language edition without
/// changing the public API.
use std::sync::LazyLock;

use log::warn;
use parse_wiki_text_2::{Configuration, ConfigurationSource, Node};
use regex::Regex;

/// Pre-built parser configuration that recognises both English and Japanese
/// Wikipedia namespaces (Category/カテゴリ, File/Image/ファイル, REDIRECT/転送).
static WIKI_CONFIG: LazyLock<Configuration> = LazyLock::new(|| {
    Configuration::new(&ConfigurationSource {
        category_namespaces: &["category", "カテゴリ"],
        extension_tags: &[
            "categorytree",
            "ce",
            "charinsert",
            "chem",
            "gallery",
            "graph",
            "hiero",
            "imagemap",
            "indicator",
            "inputbox",
            "mapframe",
            "maplink",
            "math",
            "nowiki",
            "poem",
            "pre",
            "ref",
            "references",
            "score",
            "section",
            "source",
            "syntaxhighlight",
            "templatedata",
            "timeline",
        ],
        file_namespaces: &["file", "image", "ファイル"],
        link_trail: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
        magic_words: &[
            "DISAMBIG",
            "FORCETOC",
            "HIDDENCAT",
            "INDEX",
            "NEWSECTIONLINK",
            "NOCC",
            "NOCOLLABORATIONHUBTOC",
            "NOCONTENTCONVERT",
            "NOEDITSECTION",
            "NOGALLERY",
            "NOGLOBAL",
            "NOINDEX",
            "NONEWSECTIONLINK",
            "NOTC",
            "NOTITLECONVERT",
            "NOTOC",
            "STATICREDIRECT",
            "TOC",
        ],
        protocols: &[
            "//",
            "bitcoin:",
            "ftp://",
            "ftps://",
            "geo:",
            "git://",
            "gopher://",
            "http://",
            "https://",
            "irc://",
            "ircs://",
            "magnet:",
            "mailto:",
            "mms://",
            "news:",
            "nntp://",
            "redis://",
            "sftp://",
            "sip:",
            "sips:",
            "sms:",
            "ssh://",
            "svn://",
            "tel:",
            "telnet://",
            "urn:",
            "worldwind://",
            "xmpp:",
        ],
        redirect_magic_words: &["REDIRECT", "転送"],
    })
});

/// Tags whose content should be completely removed during text extraction.
const SKIP_TAGS: &[&str] = &[
    "ref",
    "references",
    "gallery",
    "source",
    "syntaxhighlight",
    "nowiki",
    "code",
    "math",
];

// Regex patterns for fallback cleaning, compiled once using LazyLock.

/// Matches `{{...}}` templates, including nested ones.
static RE_TEMPLATE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\{\{[^{}]*\}\}").expect("invalid regex"));

/// Matches `[[Category:...]]` and `[[カテゴリ:...]]` links.
static RE_CATEGORY: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\[\[(?:Category|カテゴリ):[^\]]*\]\]").expect("invalid regex"));

/// Matches `[[File:...]]`, `[[Image:...]]`, and `[[ファイル:...]]` links.
static RE_FILE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\[\[(?:File|Image|ファイル):[^\]]*\]\]").expect("invalid regex"));

/// Matches `[[target|text]]` piped links and captures the display text.
static RE_PIPED_LINK: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\[\[[^\]|]+\|([^\]]+)\]\]").expect("invalid regex"));

/// Matches `[[target]]` simple links and captures the target.
static RE_SIMPLE_LINK: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\[\[([^\]|]+)\]\]").expect("invalid regex"));

/// Matches `[url text]` external links and captures the display text.
static RE_EXTERNAL_LINK: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\[https?://[^\s\]]+ ([^\]]+)\]").expect("invalid regex"));

/// Matches `'''text'''` bold markup.
static RE_BOLD: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"'''([^']+)'''").expect("invalid regex"));

/// Matches `''text''` italic markup.
static RE_ITALIC: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"''([^']+)''").expect("invalid regex"));

/// Matches `== heading ==` markup (any level).
static RE_HEADING: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"=={1,6}\s*(.+?)\s*=={1,6}").expect("invalid regex"));

/// Matches HTML tags (opening and closing).
static RE_HTML_TAG: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"<[^>]+>").expect("invalid regex"));

/// Matches `<ref>...</ref>` and `<ref .../>` tags.
static RE_REF: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"<ref[^>]*(?:>[\s\S]*?</ref>|/>)").expect("invalid regex"));

/// Matches `{|...|}`wiki tables.
static RE_TABLE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"(?s)\{\|.*?\|\}").expect("invalid regex"));

// Post-processing patterns to catch markup remnants that survive AST/fallback cleaning.

/// Converts wikitext markup into plain text.
///
/// Parses the given wikitext using `parse_wiki_text_2` to build an AST, then
/// extracts plain text from the AST nodes. If parsing fails, falls back to
/// regex-based cleaning.
///
/// # Arguments
///
/// * `wikitext` - The raw wikitext markup string to clean.
///
/// # Returns
///
/// A `String` containing the extracted plain text.
pub fn clean_wikitext(wikitext: &str) -> String {
    let result = WIKI_CONFIG.parse(wikitext);

    match result {
        Ok(output) => {
            // Wikitext shrinks significantly after markup removal; half the
            // input length is a reasonable initial capacity that avoids most
            // reallocations without over-allocating.
            let mut text = String::with_capacity(wikitext.len() / 2);
            extract_text_from_nodes(&output.nodes, &mut text);
            clean_text(&text)
        }
        Err(_) => {
            warn!("Failed to parse wikitext with AST parser, using fallback cleaner");
            let cleaned = fallback_clean(wikitext);
            clean_text(&cleaned)
        }
    }
}

/// Recursively extracts plain text from a slice of AST nodes.
///
/// Walks the AST produced by `parse_wiki_text_2` and appends readable text
/// content to `output`, applying the extraction rules described in the module
/// documentation.
///
/// # Arguments
///
/// * `nodes` - The slice of AST nodes to process.
/// * `output` - The mutable string buffer to append extracted text to.
#[inline]
fn extract_text_from_nodes(nodes: &[Node], output: &mut String) {
    for node in nodes {
        match node {
            Node::Text { value, .. } => {
                output.push_str(value);
            }
            Node::CharacterEntity { character, .. } => {
                output.push(*character);
            }
            Node::Heading { nodes, .. } => {
                output.push('\n');
                extract_text_from_nodes(nodes, output);
                output.push('\n');
            }
            Node::Link { text, target, .. } => {
                if text.is_empty() {
                    output.push_str(target);
                } else {
                    extract_text_from_nodes(text, output);
                }
            }
            Node::ExternalLink { nodes, .. } => {
                // Extract only the label text, skipping URL nodes.
                // The AST may represent the external link content as a
                // single Text node containing "URL label_text", so we
                // strip the leading URL portion.
                for n in nodes {
                    match n {
                        Node::Text { value, .. } => {
                            if value.starts_with("http://") || value.starts_with("https://") {
                                // URL followed by optional label after the first space.
                                if let Some(pos) = value.find(' ') {
                                    output.push_str(value[pos + 1..].trim());
                                }
                            } else {
                                output.push_str(value);
                            }
                        }
                        _ => {
                            extract_text_from_nodes(std::slice::from_ref(n), output);
                        }
                    }
                }
            }
            Node::Bold { .. } | Node::Italic { .. } | Node::BoldItalic { .. } => {
                // These are just markers; actual text is in separate Text nodes
            }
            Node::Template { .. }
            | Node::Category { .. }
            | Node::Image { .. }
            | Node::Table { .. }
            | Node::Comment { .. }
            | Node::MagicWord { .. }
            | Node::Parameter { .. }
            | Node::Redirect { .. }
            | Node::StartTag { .. }
            | Node::EndTag { .. } => {
                // Skip entirely
            }
            Node::Tag { name, nodes, .. } => {
                if !SKIP_TAGS.contains(&name.as_ref()) {
                    extract_text_from_nodes(nodes, output);
                }
            }
            Node::ParagraphBreak { .. } => {
                output.push_str("\n\n");
            }
            Node::HorizontalDivider { .. } => {
                output.push('\n');
            }
            Node::UnorderedList { items, .. } | Node::OrderedList { items, .. } => {
                for item in items {
                    extract_text_from_nodes(&item.nodes, output);
                    output.push('\n');
                }
            }
            Node::DefinitionList { items, .. } => {
                for item in items {
                    extract_text_from_nodes(&item.nodes, output);
                    output.push('\n');
                }
            }
            Node::Preformatted { nodes, .. } => {
                extract_text_from_nodes(nodes, output);
            }
        }
    }
}

/// Removes HTML comment remnants (`!--...--`) from a string.
///
/// Reproduces the behaviour of the regex `!--.*?--` (non-greedy): each
/// `!--` is paired with the nearest following `--` and the entire span is
/// deleted.
///
/// # Arguments
///
/// * `s` - A string slice known to contain at least one `!--`.
///
/// # Returns
///
/// A new `String` with all comment remnants removed.
#[inline]
fn strip_comment_remnants(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut rest = s;

    while let Some(start) = rest.find("!--") {
        result.push_str(&rest[..start]);
        // Advance past `!--` and look for the closing `--`.
        let after_open = &rest[start + 3..];
        if let Some(end) = after_open.find("--") {
            rest = &after_open[end + 2..];
        } else {
            // No closing `--`; discard the rest (matches regex behaviour).
            return result;
        }
    }

    result.push_str(rest);
    result
}

/// Removes orphaned template closing sequences from a line.
///
/// Reproduces the behaviour of the regex `[^{]*\}\}` used in `replace_all`:
/// every maximal run of non-`{` characters followed by `}}` is deleted.
///
/// # Arguments
///
/// * `s` - A string slice known to contain at least one `}}`.
///
/// # Returns
///
/// A new `String` with orphaned template closes removed.
#[inline]
fn strip_orphaned_template_close(s: &str) -> String {
    let bytes = s.as_bytes();
    let len = bytes.len();
    let mut result = Vec::with_capacity(len);
    let mut i = 0;

    while i < len {
        // Look ahead for `}}`.
        if i + 1 < len && bytes[i] == b'}' && bytes[i + 1] == b'}' {
            // Walk backwards through result to remove preceding non-`{` bytes.
            while let Some(&last) = result.last() {
                if last == b'{' {
                    break;
                }
                result.pop();
            }
            // Skip the `}}`.
            i += 2;
        } else {
            result.push(bytes[i]);
            i += 1;
        }
    }

    // SAFETY: We only remove ASCII bytes (`}` = 0x7D and non-`{` ASCII) from
    // a valid UTF-8 sequence, which always yields valid UTF-8.
    unsafe { String::from_utf8_unchecked(result) }
}

/// Collapses runs of two or more ASCII spaces into a single space.
///
/// Scans the input as bytes (spaces are always `0x20` regardless of the
/// surrounding Unicode characters) and writes to a new `String` only when
/// consecutive spaces are found.  This avoids the overhead of a full regex
/// DFA while remaining correct for any UTF-8 input.
///
/// # Arguments
///
/// * `s` - A string slice that is known to contain at least one run of two
///   or more consecutive spaces (i.e. `s.contains("  ")` is true).
///
/// # Returns
///
/// A new `String` with all multi-space runs replaced by a single space.
#[inline]
fn collapse_spaces(s: &str) -> String {
    let bytes = s.as_bytes();
    let mut result = Vec::with_capacity(s.len());
    let mut last_was_space = false;
    for &b in bytes {
        if b == b' ' {
            if !last_was_space {
                result.push(b);
            }
            last_was_space = true;
        } else {
            result.push(b);
            last_was_space = false;
        }
    }
    // SAFETY: Removing duplicate space bytes (0x20, single-byte ASCII) from a
    // valid UTF-8 byte sequence always yields a valid UTF-8 byte sequence.
    unsafe { String::from_utf8_unchecked(result) }
}

/// Post-processes extracted text by removing markup remnants and normalizing
/// whitespace.
///
/// Performs the following cleanup steps:
/// 1. Removes orphaned template closing braces (`}}`) and preceding parameter text
/// 2. Removes lines consisting solely of template parameter syntax (`|key = value`)
/// 3. Removes HTML comment remnants where angle brackets were already stripped
/// 4. Trims leading and trailing whitespace from each line
/// 5. Collapses consecutive spaces into a single space
/// 6. Collapses three or more consecutive newlines into two (one blank line)
/// 7. Trims leading and trailing whitespace from the entire result
///
/// # Arguments
///
/// * `text` - The raw extracted text to clean up.
///
/// # Returns
///
/// A `String` with markup remnants removed and whitespace normalized.
#[inline]
fn clean_text(text: &str) -> String {
    // Single-pass approach: write directly into a pre-allocated buffer instead
    // of collecting into Vec<String> and then joining.  Blank-line runs are
    // tracked with a counter so that RE_MULTI_NEWLINE is no longer needed.
    let mut result = String::with_capacity(text.len());
    // Number of consecutive blank (or skipped) lines since the last content line.
    let mut blank_run: usize = 0;

    for line in text.lines() {
        let trimmed = line.trim();

        // Fast path: skip lines that start with `|`.  In cleaned wikitext
        // these are invariably template parameter remnants or table row
        // fragments, both of which should be discarded.
        if trimmed.starts_with('|') {
            blank_run += 1;
            continue;
        }

        // Remove orphaned template closes: strips every span of non-`{`
        // characters followed by `}}` without DFA overhead.
        let s1: std::borrow::Cow<str> = if trimmed.contains("}}") {
            std::borrow::Cow::Owned(strip_orphaned_template_close(trimmed))
        } else {
            std::borrow::Cow::Borrowed(trimmed)
        };
        // Remove HTML comment remnants (`!--...--`) without a regex engine.
        let s2: std::borrow::Cow<str> = if s1.contains("!--") {
            std::borrow::Cow::Owned(strip_comment_remnants(&s1))
        } else {
            s1
        };
        let s2_trimmed = s2.trim();
        let s3: std::borrow::Cow<str> = if s2_trimmed.contains("  ") {
            // Collapse consecutive spaces by scanning bytes directly.
            std::borrow::Cow::Owned(collapse_spaces(s2_trimmed))
        } else {
            std::borrow::Cow::Borrowed(s2_trimmed)
        };
        let cleaned = s3.trim();

        if cleaned.is_empty() {
            blank_run += 1;
            continue;
        }

        // Insert separator before the content line.
        if !result.is_empty() {
            if blank_run >= 1 {
                // One or more blank lines → paragraph break (\n\n).
                result.push_str("\n\n");
            } else {
                result.push('\n');
            }
        }

        result.push_str(cleaned);
        blank_run = 0;
    }

    result
}

/// Cleans wikitext using regex-based heuristics as a fallback.
///
/// Used when the AST parser fails. Applies a series of regex substitutions
/// to remove or simplify common wikitext constructs.
///
/// # Arguments
///
/// * `wikitext` - The raw wikitext markup to clean.
///
/// # Returns
///
/// A `String` with markup removed via regex patterns.
fn fallback_clean(wikitext: &str) -> String {
    use std::borrow::Cow;

    // Keep the text as Cow<str> throughout.  replace_all returns Cow::Borrowed
    // when the regex finds no match, so no String clone is made unless the
    // pattern actually matches.  The `sub!` macro scopes the borrow of `text`
    // strictly inside the replace_all call; only the owned result is assigned.
    let mut text: Cow<str> = Cow::Borrowed(wikitext);

    macro_rules! sub {
        ($re:expr, $rep:expr) => {
            if let Cow::Owned(s) = $re.replace_all(text.as_ref(), $rep) {
                text = Cow::Owned(s);
            }
        };
    }

    // Remove ref tags first (before general HTML tag removal).
    sub!(RE_REF, "");

    // Remove tables.
    sub!(RE_TABLE, "");

    // Remove templates (iterate for nested templates).
    for _ in 0..10 {
        match RE_TEMPLATE.replace_all(text.as_ref(), "") {
            Cow::Owned(s) => text = Cow::Owned(s),
            Cow::Borrowed(_) => break,
        }
    }

    // Remove category and file links.
    sub!(RE_CATEGORY, "");
    sub!(RE_FILE, "");

    // Convert piped links to display text.
    sub!(RE_PIPED_LINK, "$1");

    // Convert simple links to target text.
    sub!(RE_SIMPLE_LINK, "$1");

    // Convert external links to label text.
    sub!(RE_EXTERNAL_LINK, "$1");

    // Remove bold/italic markup.
    sub!(RE_BOLD, "$1");
    sub!(RE_ITALIC, "$1");

    // Convert headings to plain text.
    sub!(RE_HEADING, "$1");

    // Remove HTML tags.
    sub!(RE_HTML_TAG, "");

    text.into_owned()
}

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

    #[test]
    fn test_plain_text_passthrough() {
        let input = "This is plain text.";
        let result = clean_wikitext(input);
        assert_eq!(result, "This is plain text.");
    }

    #[test]
    fn test_bold_italic_removal() {
        let input = "This is '''bold''' and ''italic'' text.";
        let result = clean_wikitext(input);
        assert_eq!(result, "This is bold and italic text.");
    }

    #[test]
    fn test_internal_link_without_pipe() {
        let input = "Visit [[Main Page]] for more.";
        let result = clean_wikitext(input);
        assert_eq!(result, "Visit Main Page for more.");
    }

    #[test]
    fn test_internal_link_with_pipe() {
        let input = "See [[United States|the US]] for details.";
        let result = clean_wikitext(input);
        assert_eq!(result, "See the US for details.");
    }

    #[test]
    fn test_external_link() {
        let input = "Visit [http://example.com Example Site] for more.";
        let result = clean_wikitext(input);
        assert!(
            result.contains("Example Site"),
            "Expected label text, got: {result}"
        );
    }

    #[test]
    fn test_template_removal() {
        let input = "Before {{cite web|url=http://example.com|title=Test}} after.";
        let result = clean_wikitext(input);
        assert!(
            !result.contains("cite web"),
            "Template should be removed, got: {result}"
        );
        assert!(
            result.contains("Before"),
            "Text before template should remain, got: {result}"
        );
        assert!(
            result.contains("after."),
            "Text after template should remain, got: {result}"
        );
    }

    #[test]
    fn test_table_removal() {
        let input =
            "Before table.\n{| class=\"wikitable\"\n|-\n! Header\n|-\n| Cell\n|}\nAfter table.";
        let result = clean_wikitext(input);
        assert!(
            !result.contains("Header"),
            "Table content should be removed, got: {result}"
        );
        assert!(
            result.contains("Before table."),
            "Text before table should remain, got: {result}"
        );
    }

    #[test]
    fn test_comment_removal() {
        let input = "Visible <!-- hidden comment --> text.";
        let result = clean_wikitext(input);
        assert!(
            !result.contains("hidden comment"),
            "Comment should be removed, got: {result}"
        );
        assert!(
            result.contains("Visible"),
            "Visible text should remain, got: {result}"
        );
    }

    #[test]
    fn test_heading_extraction() {
        let input = "== Section Title ==\nContent here.";
        let result = clean_wikitext(input);
        assert!(
            result.contains("Section Title"),
            "Heading text should be extracted, got: {result}"
        );
        assert!(
            !result.contains("=="),
            "Heading markers should be removed, got: {result}"
        );
    }

    #[test]
    fn test_category_removal() {
        let input = "Text content.\n[[Category:Example]]";
        let result = clean_wikitext(input);
        assert!(
            !result.contains("Category"),
            "Category should be removed, got: {result}"
        );
        assert!(
            result.contains("Text content."),
            "Regular text should remain, got: {result}"
        );
    }

    #[test]
    fn test_image_removal() {
        let input = "Text content.\n[[File:Example.jpg|thumb|Caption]]";
        let result = clean_wikitext(input);
        assert!(
            !result.contains("Example.jpg"),
            "Image link should be removed, got: {result}"
        );
    }

    #[test]
    fn test_ref_tag_removal() {
        let input = "Fact<ref>Source: Book, 2024</ref> is stated.";
        let result = clean_wikitext(input);
        assert!(
            !result.contains("Source"),
            "Ref content should be removed, got: {result}"
        );
        assert!(
            result.contains("Fact"),
            "Text before ref should remain, got: {result}"
        );
    }

    #[test]
    fn test_unordered_list() {
        let input = "* Item one\n* Item two\n* Item three";
        let result = clean_wikitext(input);
        assert!(
            result.contains("Item one"),
            "List items should be extracted, got: {result}"
        );
        assert!(
            result.contains("Item two"),
            "List items should be extracted, got: {result}"
        );
    }

    #[test]
    fn test_paragraph_break() {
        let input = "First paragraph.\n\nSecond paragraph.";
        let result = clean_wikitext(input);
        assert!(
            result.contains("First paragraph."),
            "First paragraph should be present, got: {result}"
        );
        assert!(
            result.contains("Second paragraph."),
            "Second paragraph should be present, got: {result}"
        );
    }

    #[test]
    fn test_complex_wikitext() {
        let input = concat!(
            "'''Albert Einstein''' was a [[Germany|German]]-born ",
            "[[theoretical physics|theoretical physicist]]",
            "{{cite web|url=http://example.com}} ",
            "who developed the [[theory of relativity]].",
            "<!-- draft note -->",
            "<ref>Physics Today, 2024</ref>",
        );
        let result = clean_wikitext(input);
        assert!(
            result.contains("Albert Einstein"),
            "Name should be present, got: {result}"
        );
        assert!(
            result.contains("German"),
            "Link display text should be present, got: {result}"
        );
        assert!(
            result.contains("theoretical physicist"),
            "Link display text should be present, got: {result}"
        );
        assert!(
            result.contains("theory of relativity"),
            "Simple link text should be present, got: {result}"
        );
        assert!(
            !result.contains("cite web"),
            "Template should be removed, got: {result}"
        );
        assert!(
            !result.contains("draft note"),
            "Comment should be removed, got: {result}"
        );
        assert!(
            !result.contains("Physics Today"),
            "Ref should be removed, got: {result}"
        );
    }

    #[test]
    fn test_fallback_cleaning() {
        let input = "'''Bold''' and ''italic'' with [[Link|display]] and [[Simple]] link.";
        let result = fallback_clean(input);
        assert!(
            result.contains("Bold"),
            "Bold text should remain, got: {result}"
        );
        assert!(
            result.contains("italic"),
            "Italic text should remain, got: {result}"
        );
        assert!(
            result.contains("display"),
            "Piped link display should remain, got: {result}"
        );
        assert!(
            result.contains("Simple"),
            "Simple link target should remain, got: {result}"
        );
        assert!(
            !result.contains("'''"),
            "Bold markers should be removed, got: {result}"
        );
        assert!(
            !result.contains("''"),
            "Italic markers should be removed, got: {result}"
        );
    }

    #[test]
    fn test_fallback_template_removal() {
        let input = "Before {{outer|{{inner}}}} after.";
        let result = fallback_clean(input);
        assert!(
            !result.contains("{{"),
            "Nested templates should be removed, got: {result}"
        );
    }

    #[test]
    fn test_fallback_ref_removal() {
        let input = "Text<ref name=\"a\">Citation</ref> and <ref/> end.";
        let result = fallback_clean(input);
        assert!(
            !result.contains("Citation"),
            "Ref content should be removed, got: {result}"
        );
    }

    #[test]
    fn test_clean_text_whitespace() {
        let input = "  Line one  \n\n\n\n  Line two  \n  Line three  ";
        let result = clean_text(input);
        assert_eq!(result, "Line one\n\nLine two\nLine three");
    }

    #[test]
    fn test_orphaned_template_close_removal() {
        let input = "amp;}}";
        let result = clean_text(input);
        assert_eq!(
            result, "",
            "Orphaned template close should be removed, got: {result}"
        );
    }

    #[test]
    fn test_template_param_line_removal() {
        let input = "本文テキスト。\n|image5 = Example.jpg|caption5 = 説明文}}\n続きのテキスト。";
        let result = clean_text(input);
        assert!(
            !result.contains("image5"),
            "Template parameter line should be removed, got: {result}"
        );
        assert!(
            result.contains("本文テキスト。"),
            "Body text should remain, got: {result}"
        );
        assert!(
            result.contains("続きのテキスト。"),
            "Following text should remain, got: {result}"
        );
    }

    #[test]
    fn test_comment_remnant_removal() {
        let input = "!--tlh:Hol--";
        let result = clean_text(input);
        assert_eq!(
            result, "",
            "Comment remnant should be removed, got: {result}"
        );
    }

    #[test]
    fn test_comment_remnant_inline() {
        let input = "Visible text !--hidden comment-- more text.";
        let result = clean_text(input);
        assert!(
            !result.contains("hidden comment"),
            "Inline comment remnant should be removed, got: {result}"
        );
        assert!(
            result.contains("Visible text"),
            "Text before comment should remain, got: {result}"
        );
        assert!(
            result.contains("more text."),
            "Text after comment should remain, got: {result}"
        );
    }

    #[test]
    fn test_japanese_category_removal() {
        let input = "本文テキスト。\n[[カテゴリ:日本の都市]]";
        let result = clean_wikitext(input);
        assert!(
            !result.contains("カテゴリ"),
            "Japanese category should be removed, got: {result}"
        );
        assert!(
            result.contains("本文テキスト。"),
            "Body text should remain, got: {result}"
        );
    }

    #[test]
    fn test_japanese_file_removal() {
        let input = "本文テキスト。\n[[ファイル:Example.jpg|thumb|説明文]]";
        let result = clean_wikitext(input);
        assert!(
            !result.contains("Example.jpg"),
            "Japanese file link should be removed, got: {result}"
        );
        assert!(
            !result.contains("ファイル"),
            "Japanese file namespace should be removed, got: {result}"
        );
    }

    #[test]
    fn test_japanese_internal_link() {
        let input = "[[東京都|東京]]は日本の首都である。";
        let result = clean_wikitext(input);
        assert!(
            result.contains("東京"),
            "Japanese link display text should be present, got: {result}"
        );
        assert!(
            !result.contains("東京都"),
            "Japanese link target should be removed when pipe is used, got: {result}"
        );
    }

    #[test]
    fn test_japanese_simple_link() {
        let input = "[[大阪府]]は日本にある。";
        let result = clean_wikitext(input);
        assert!(
            result.contains("大阪府"),
            "Japanese simple link text should be present, got: {result}"
        );
        assert!(
            !result.contains("[["),
            "Link brackets should be removed, got: {result}"
        );
    }

    #[test]
    fn test_japanese_complex_wikitext() {
        let input = concat!(
            "'''日本語'''(にほんご、にっぽんご)は、",
            "[[日本]]国内や、かつての[[大日本帝国|日本統治地域]]で使われている",
            "[[言語]]である。",
            "{{lang|ja|日本語}}",
            "<!-- hidden -->",
            "<ref>出典情報</ref>",
            "\n[[カテゴリ:日本の言語]]",
            "\n[[ファイル:Japanese.png|thumb|日本語]]",
        );
        let result = clean_wikitext(input);
        assert!(
            result.contains("日本語"),
            "Title text should be present, got: {result}"
        );
        assert!(
            result.contains("にほんご"),
            "Furigana text should be present, got: {result}"
        );
        assert!(
            result.contains("日本統治地域"),
            "Piped link display text should be present, got: {result}"
        );
        assert!(
            result.contains("言語"),
            "Simple link text should be present, got: {result}"
        );
        assert!(
            !result.contains("hidden"),
            "Comment should be removed, got: {result}"
        );
        assert!(
            !result.contains("出典情報"),
            "Ref should be removed, got: {result}"
        );
        assert!(
            !result.contains("カテゴリ"),
            "Japanese category should be removed, got: {result}"
        );
        assert!(
            !result.contains("ファイル"),
            "Japanese file link should be removed, got: {result}"
        );
    }

    #[test]
    fn test_fallback_japanese_category_removal() {
        let input = "テキスト。\n[[カテゴリ:テスト]]";
        let result = fallback_clean(input);
        assert!(
            !result.contains("カテゴリ"),
            "Japanese category should be removed in fallback, got: {result}"
        );
    }

    #[test]
    fn test_fallback_japanese_file_removal() {
        let input = "テキスト。\n[[ファイル:Test.png|thumb|説明]]";
        let result = fallback_clean(input);
        assert!(
            !result.contains("ファイル"),
            "Japanese file should be removed in fallback, got: {result}"
        );
    }
}