umd 0.1.0

Universal Markdown - A post-Markdown superset with Bootstrap 5 integration and extensible syntax
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
//! Inline decoration functions for LukiWiki
//!
//! Provides inline formatting functions:
//! - &color(fg,bg){text};
//! - &size(rem){text};
//! - &sup(text); (superscript)
//! - &sub(text); (subscript)
//! - &lang(locale){text};
//! - &abbr(text){description};
//! - &ruby(reading){text}; (furigana)
//! - Semantic HTML elements: dfn, kbd, samp, var, cite, q, small
//! - &time(datetime){text};
//! - &data(value){text};
//! - &bdi(text); &bdo(dir){text};
//! - &wbr; (word break opportunity)
//! - &br; (manual line break)
//! - %%text%% → <s>text</s> (strikethrough)
//!
//! Note: For underline, use Discord-style __text__ syntax instead

use once_cell::sync::Lazy;
use regex::Regex;

// Badge pattern with optional link support
static INLINE_BADGE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"&badge\(([^)]+?)\)\{([^}]+?)\};").unwrap());

// Link pattern for detecting [text](url) inside badge content
static MARKDOWN_LINK: Lazy<Regex> = Lazy::new(|| Regex::new(r"\[([^\]]+)\]\(([^)]+)\)").unwrap());

static INLINE_COLOR: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"&color\(([^,)]*?)(?:,([^)]*?))?\)\{([^}]+?)\};").unwrap());

static INLINE_SIZE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"&size\(([^)]+?)\)\{([^}]+?)\};").unwrap());

static INLINE_SUP: Lazy<Regex> = Lazy::new(|| Regex::new(r"&sup\(([^)]+?)\);").unwrap());

static INLINE_SUB: Lazy<Regex> = Lazy::new(|| Regex::new(r"&sub\(([^)]+?)\);").unwrap());

static INLINE_LANG: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"&lang\(([^)]+?)\)\{([^}]+?)\};").unwrap());

static INLINE_ABBR: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"&abbr\(([^)]+?)\)\{([^}]+?)\};").unwrap());

static INLINE_RUBY: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"&ruby\(([^)]+?)\)\{([^}]+?)\};").unwrap());

// Semantic HTML elements - simple wrapper tags
static INLINE_DFN: Lazy<Regex> = Lazy::new(|| Regex::new(r"&dfn\(([^)]+?)\);").unwrap());
static INLINE_KBD: Lazy<Regex> = Lazy::new(|| Regex::new(r"&kbd\(([^)]+?)\);").unwrap());
static INLINE_SAMP: Lazy<Regex> = Lazy::new(|| Regex::new(r"&samp\(([^)]+?)\);").unwrap());
static INLINE_VAR: Lazy<Regex> = Lazy::new(|| Regex::new(r"&var\(([^)]+?)\);").unwrap());
static INLINE_CITE: Lazy<Regex> = Lazy::new(|| Regex::new(r"&cite\(([^)]+?)\);").unwrap());
static INLINE_Q: Lazy<Regex> = Lazy::new(|| Regex::new(r"&q\(([^)]+?)\);").unwrap());
static INLINE_SMALL: Lazy<Regex> = Lazy::new(|| Regex::new(r"&small\(([^)]+?)\);").unwrap());

// Elements with attributes
static INLINE_TIME: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"&time\(([^)]+?)\)\{([^}]+?)\};").unwrap());
static INLINE_DATA: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"&data\(([^)]+?)\)\{([^}]+?)\};").unwrap());

// Bidirectional text
static INLINE_BDI: Lazy<Regex> = Lazy::new(|| Regex::new(r"&bdi\(([^)]+?)\);").unwrap());
static INLINE_BDO: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"&bdo\(([^)]+?)\)\{([^}]+?)\};").unwrap());

// Word break opportunity (self-closing)
static INLINE_WBR: Lazy<Regex> = Lazy::new(|| Regex::new(r"&wbr;").unwrap());

// Manual line break (self-closing) - mainly for table cells where trailing spaces don't work
static INLINE_BR: Lazy<Regex> = Lazy::new(|| Regex::new(r"&br;").unwrap());

/// Regex for LukiWiki strikethrough: %%text%% → <s>text</s>
static LUKIWIKI_STRIKETHROUGH: Lazy<Regex> = Lazy::new(|| Regex::new(r"%%([^%]+)%%").unwrap());

/// Regex for Discord-style spoiler: || text || → <span class="spoiler">text</span>
static DISCORD_SPOILER: Lazy<Regex> = Lazy::new(|| Regex::new(r"\|\|([^|]+)\|\|").unwrap());

/// Regex for UMD spoiler function: &spoiler(text); or &spoiler{text};
static INLINE_SPOILER: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"&spoiler(?:\(([^)]+?)\)|\{([^}]+?)\});").unwrap());

/// Map font size value to Bootstrap class or inline style
fn map_font_size(value: &str) -> (bool, String) {
    // Check if value has unit (rem, em, px, etc.)
    if value.contains("rem") || value.contains("em") || value.contains("px") {
        return (false, value.to_string()); // Return as inline style
    }

    // Map to Bootstrap fs-* classes (unitless values)
    let class = match value {
        "2.5" => "fs-1",
        "2" | "2.0" => "fs-2",
        "1.75" => "fs-3",
        "1.5" => "fs-4",
        "1.25" => "fs-5",
        "0.875" => "fs-6",
        _ => return (false, format!("{}rem", value)), // Custom value as inline style
    };

    (true, class.to_string())
}

/// Map color value to Bootstrap class or inline style
/// Returns Some((is_class, value)) if valid, None if invalid
/// Only accepts Bootstrap color names and HEX format (#RRGGBB or #RGB)
fn map_color(value: &str, is_background: bool) -> Option<(bool, String)> {
    let trimmed = value.trim();

    // Bootstrap theme colors
    let bootstrap_colors = [
        // Theme colors
        "primary",
        "secondary",
        "success",
        "danger",
        "warning",
        "info",
        "light",
        "dark",
        "body",
        "body-secondary",
        "body-tertiary",
        "body-emphasis",
        // Custom colors (Bootstrap 5.3+)
        "blue",
        "indigo",
        "purple",
        "pink",
        "red",
        "orange",
        "yellow",
        "green",
        "teal",
        "cyan",
        // Theme colors with suffixes
        "primary-subtle",
        "secondary-subtle",
        "success-subtle",
        "danger-subtle",
        "warning-subtle",
        "info-subtle",
        "light-subtle",
        "dark-subtle",
        "primary-emphasis",
        "secondary-emphasis",
        "success-emphasis",
        "danger-emphasis",
        "warning-emphasis",
        "info-emphasis",
        "light-emphasis",
        "dark-emphasis",
        // Custom colors with suffixes
        "blue-subtle",
        "indigo-subtle",
        "purple-subtle",
        "pink-subtle",
        "red-subtle",
        "orange-subtle",
        "yellow-subtle",
        "green-subtle",
        "teal-subtle",
        "cyan-subtle",
        "blue-emphasis",
        "indigo-emphasis",
        "purple-emphasis",
        "pink-emphasis",
        "red-emphasis",
        "orange-emphasis",
        "yellow-emphasis",
        "green-emphasis",
        "teal-emphasis",
        "cyan-emphasis",
    ];

    // Check if it's a Bootstrap color
    for color in &bootstrap_colors {
        if trimmed == *color || trimmed.starts_with(&format!("{}-", color)) {
            let prefix = if is_background { "bg" } else { "text" };
            return Some((true, format!("{}-{}", prefix, trimmed)));
        }
    }

    // Check if it's a HEX color (#RRGGBB or #RGB)
    if trimmed.starts_with('#') && (trimmed.len() == 4 || trimmed.len() == 7) {
        // Basic validation: check if all characters after # are hex digits
        if trimmed[1..].chars().all(|c| c.is_ascii_hexdigit()) {
            return Some((false, trimmed.to_string()));
        }
    }

    // Future: Support rgb() and hsl() formats
    // if trimmed.starts_with("rgb(") || trimmed.starts_with("rgba(") ||
    //    trimmed.starts_with("hsl(") || trimmed.starts_with("hsla(") {
    //     return Some((false, trimmed.to_string()));
    // }

    // Invalid color specification (e.g., HTML color names are not supported)
    None
}

/// Map badge type to Bootstrap badge classes
fn map_badge_type(badge_type: &str) -> String {
    // Check if it's a pill badge
    if badge_type.ends_with("-pill") {
        let color = badge_type.trim_end_matches("-pill");
        format!("badge rounded-pill bg-{}", color)
    } else {
        // Regular badge
        format!("badge bg-{}", badge_type)
    }
}

/// Apply inline decoration functions to HTML
///
/// # Arguments
///
/// * `html` - The HTML content to process
///
/// # Returns
///
/// HTML with inline decorations applied
pub fn apply_inline_decorations(html: &str) -> String {
    let mut result = html.to_string();

    // Decode HTML entities for UMD inline syntax
    // Comrak escapes & to &amp;, which prevents our regexes from matching
    // We need to convert &amp; back to & for UMD syntax only
    result = result.replace("&amp;color(", "&color(");
    result = result.replace("&amp;badge(", "&badge(");
    result = result.replace("&amp;size(", "&size(");
    result = result.replace("&amp;sup(", "&sup(");
    result = result.replace("&amp;sub(", "&sub(");
    result = result.replace("&amp;lang(", "&lang(");
    result = result.replace("&amp;abbr(", "&abbr(");
    result = result.replace("&amp;ruby(", "&ruby(");
    result = result.replace("&amp;spoiler(", "&spoiler(");
    result = result.replace("&amp;spoiler{", "&spoiler{");
    result = result.replace("&amp;dfn(", "&dfn(");
    result = result.replace("&amp;kbd(", "&kbd(");
    result = result.replace("&amp;samp(", "&samp(");
    result = result.replace("&amp;var(", "&var(");
    result = result.replace("&amp;cite(", "&cite(");
    result = result.replace("&amp;q(", "&q(");
    result = result.replace("&amp;small(", "&small(");
    result = result.replace("&amp;time(", "&time(");
    result = result.replace("&amp;data(", "&data(");
    result = result.replace("&amp;bdi(", "&bdi(");
    result = result.replace("&amp;bdo(", "&bdo(");
    result = result.replace("&amp;wbr", "&wbr");
    result = result.replace("&amp;br", "&br");

    // Apply %%text%% → <s>text</s> (LukiWiki strikethrough)
    result = LUKIWIKI_STRIKETHROUGH
        .replace_all(&result, "<s>$1</s>")
        .to_string();

    // Apply || text || → <span class="spoiler">text</span> (Discord spoiler)
    result = DISCORD_SPOILER
        .replace_all(
            &result,
            r#"<span class="spoiler" role="button" tabindex="0" aria-expanded="false">$1</span>"#,
        )
        .to_string();

    // Apply &spoiler(text); or &spoiler{text}; → <span class="spoiler">text</span>
    result = INLINE_SPOILER
        .replace_all(&result, |caps: &regex::Captures| {
            let text = caps.get(1).or_else(|| caps.get(2)).map_or("", |m| m.as_str());
            format!(r#"<span class="spoiler" role="button" tabindex="0" aria-expanded="false">{}</span>"#, text)
        })
        .to_string();

    // Apply &badge(type){text}; with optional link support
    result = INLINE_BADGE
        .replace_all(&result, |caps: &regex::Captures| {
            let badge_type = caps.get(1).map_or("", |m| m.as_str());
            let content = caps.get(2).map_or("", |m| m.as_str());
            let badge_class = map_badge_type(badge_type);

            // Check if content contains a Markdown link: [text](url)
            if let Some(link_caps) = MARKDOWN_LINK.captures(content) {
                let text = link_caps.get(1).map_or("", |m| m.as_str());
                let url = link_caps.get(2).map_or("", |m| m.as_str());
                format!("<a href=\"{}\" class=\"{}\">{}</a>", url, badge_class, text)
            } else {
                format!("<span class=\"{}\">{}</span>", badge_class, content)
            }
        })
        .to_string();

    // Apply &color(fg,bg){text}; with Bootstrap support
    result = INLINE_COLOR
        .replace_all(&result, |caps: &regex::Captures| {
            let fg = caps.get(1).map_or("", |m| m.as_str().trim());
            let bg = caps.get(2).map_or("", |m| m.as_str().trim());
            let text = caps.get(3).map_or("", |m| m.as_str());

            let mut classes = Vec::new();
            let mut styles = Vec::new();

            if !fg.is_empty() && fg != "inherit" {
                if let Some((is_class, value)) = map_color(fg, false) {
                    if is_class {
                        classes.push(value);
                    } else {
                        styles.push(format!("color: {}", value));
                    }
                }
            }

            if !bg.is_empty() && bg != "inherit" {
                if let Some((is_class, value)) = map_color(bg, true) {
                    if is_class {
                        classes.push(value);
                    } else {
                        styles.push(format!("background-color: {}", value));
                    }
                }
            }

            if classes.is_empty() && styles.is_empty() {
                text.to_string()
            } else {
                let mut attrs = Vec::new();
                if !classes.is_empty() {
                    attrs.push(format!("class=\"{}\"", classes.join(" ")));
                }
                if !styles.is_empty() {
                    attrs.push(format!("style=\"{}\"", styles.join("; ")));
                }
                format!("<span {}>{}</span>", attrs.join(" "), text)
            }
        })
        .to_string();

    // Apply &size(value){text}; with Bootstrap support
    result = INLINE_SIZE
        .replace_all(&result, |caps: &regex::Captures| {
            let size = caps.get(1).map_or("", |m| m.as_str());
            let text = caps.get(2).map_or("", |m| m.as_str());

            let (is_class, value) = map_font_size(size);
            if is_class {
                format!("<span class=\"{}\">{}</span>", value, text)
            } else {
                format!("<span style=\"font-size: {}\">{}</span>", value, text)
            }
        })
        .to_string();

    // Apply &sup(text);
    result = INLINE_SUP
        .replace_all(&result, "<sup>$1</sup>;")
        .to_string();

    // Apply &sub(text);
    result = INLINE_SUB
        .replace_all(&result, "<sub>$1</sub>;")
        .to_string();

    // Apply &lang(locale){text};
    result = INLINE_LANG
        .replace_all(&result, "<span lang=\"$1\">$2</span>;")
        .to_string();

    // Apply &abbr(text){description};
    result = INLINE_ABBR
        .replace_all(&result, "<abbr title=\"$2\">$1</abbr>;")
        .to_string();

    // Apply &ruby(reading){text};
    result = INLINE_RUBY
        .replace_all(&result, "<ruby>$2<rp>(</rp><rt>$1</rt><rp>)</rp></ruby>;")
        .to_string();

    // Semantic HTML elements - simple wrappers
    result = INLINE_DFN
        .replace_all(&result, "<dfn>$1</dfn>;")
        .to_string();
    result = INLINE_KBD
        .replace_all(&result, "<kbd>$1</kbd>;")
        .to_string();
    result = INLINE_SAMP
        .replace_all(&result, "<samp>$1</samp>;")
        .to_string();
    result = INLINE_VAR
        .replace_all(&result, "<var>$1</var>;")
        .to_string();
    result = INLINE_CITE
        .replace_all(&result, "<cite>$1</cite>;")
        .to_string();
    result = INLINE_Q.replace_all(&result, "<q>$1</q>;").to_string();
    result = INLINE_SMALL
        .replace_all(&result, "<small>$1</small>;")
        .to_string();

    // Elements with attributes
    result = INLINE_TIME
        .replace_all(&result, "<time datetime=\"$1\">$2</time>;")
        .to_string();
    result = INLINE_DATA
        .replace_all(&result, "<data value=\"$1\">$2</data>;")
        .to_string();

    // Bidirectional text
    result = INLINE_BDI
        .replace_all(&result, "<bdi>$1</bdi>;")
        .to_string();
    result = INLINE_BDO
        .replace_all(&result, "<bdo dir=\"$1\">$2</bdo>;")
        .to_string();

    // Word break opportunity
    result = INLINE_WBR.replace_all(&result, "<wbr />").to_string();

    // Manual line break (mainly for table cells)
    result = INLINE_BR.replace_all(&result, "<br />").to_string();

    result
}

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

    #[test]
    fn test_map_color_blue() {
        let result = map_color("blue", false);
        assert!(result.is_some(), "blue should be recognized as a valid color");
        let (is_class, class_or_style) = result.unwrap();
        assert!(is_class, "blue should be recognized as a Bootstrap class");
        assert_eq!(class_or_style, "text-blue", "Expected text-blue, got {}", class_or_style);
    }

    #[test]
    fn test_map_color_hex() {
        let result = map_color("#FF5733", false);
        assert!(result.is_some(), "#FF5733 should be recognized as a valid HEX color");
        let (is_class, value) = result.unwrap();
        assert!(!is_class, "HEX color should not be a Bootstrap class");
        assert_eq!(value, "#FF5733", "Expected #FF5733, got {}", value);
    }

    #[test]
    fn test_map_color_invalid_html_name() {
        // HTML color names like "white" or "black" are not in Bootstrap color list
        // and should be rejected
        let result = map_color("white", false);
        assert!(result.is_none(), "HTML color name 'white' should be rejected");

        let result = map_color("black", false);
        assert!(result.is_none(), "HTML color name 'black' should be rejected");
    }

    #[test]
    fn test_inline_color_foreground() {
        let input = "This is &color(red){red text};";
        let output = apply_inline_decorations(input);
        // red is now a Bootstrap color, so it should output a class
        assert!(output.contains(r#"<span class="text-red">red text</span>"#));
    }

    #[test]
    fn test_inline_color_background() {
        let input = "&color(,yellow){yellow bg};";
        let output = apply_inline_decorations(input);
        // yellow is now a Bootstrap color, so it should output a class
        assert!(output.contains(r#"<span class="bg-yellow">yellow bg</span>"#));
    }

    #[test]
    fn test_inline_color_both() {
        // Test with valid Bootstrap colors
        let input = "&color(cyan,yellow){cyan on yellow};";
        let output = apply_inline_decorations(input);
        // cyan and yellow are Bootstrap custom colors
        assert!(output.contains(r#"class="text-cyan bg-yellow""#), "Expected both colors as classes, got: {}", output);
    }

    #[test]
    fn test_inline_color_invalid() {
        // white and black are not in Bootstrap color list, so they should be rejected
        let input = "&color(white,black){white on black};";
        let output = apply_inline_decorations(input);
        // Invalid colors should be ignored, text remains as-is
        assert_eq!(output, "white on black", "Invalid colors should be ignored, got: {}", output);
    }

    #[test]
    fn test_inline_color_hex() {
        // Test with HEX color
        let input = "&color(#FF5733){Custom hex color};";
        let output = apply_inline_decorations(input);
        assert!(output.contains(r#"style="color: #FF5733""#), "Expected HEX color as inline style, got: {}", output);
    }

    #[test]
    fn test_inline_size() {
        let input = "&size(1.5){larger};";
        let output = apply_inline_decorations(input);
        // 1.5 maps to Bootstrap fs-4 class
        assert!(output.contains("<span class=\"fs-4\">larger</span>"));
    }

    #[test]
    fn test_inline_sup() {
        let input = "x&sup(2);";
        let output = apply_inline_decorations(input);
        assert_eq!(output, "x<sup>2</sup>;");
    }

    #[test]
    fn test_inline_sub() {
        let input = "H&sub(2);O";
        let output = apply_inline_decorations(input);
        assert_eq!(output, "H<sub>2</sub>;O");
    }

    #[test]
    fn test_inline_lang() {
        let input = "&lang(en){Hello};";
        let output = apply_inline_decorations(input);
        assert_eq!(output, "<span lang=\"en\">Hello</span>;");
    }

    #[test]
    fn test_inline_abbr() {
        let input = "&abbr(HTML){HyperText Markup Language};";
        let output = apply_inline_decorations(input);
        assert_eq!(
            output,
            "<abbr title=\"HyperText Markup Language\">HTML</abbr>;"
        );
    }

    #[test]
    fn test_multiple_inline_decorations() {
        let input = "&color(red){Red}; and &size(2){Big}; and &sup(superscript);";
        let output = apply_inline_decorations(input);
        // red is now a Bootstrap color, so it should use a class instead of inline style
        assert!(output.contains(&"text-red"));
        // 2 maps to Bootstrap fs-2 class
        assert!(output.contains("fs-2"));
        assert!(output.contains("<sup>superscript</sup>"));
    }

    #[test]
    fn test_lukiwiki_strikethrough() {
        let input = "This is %%strikethrough%% text.";
        let output = apply_inline_decorations(input);
        assert_eq!(output, "This is <s>strikethrough</s> text.");
    }

    #[test]
    fn test_lukiwiki_strikethrough_multiple() {
        let input = "%%first%% and %%second%%";
        let output = apply_inline_decorations(input);
        assert_eq!(output, "<s>first</s> and <s>second</s>");
    }

    #[test]
    fn test_inline_ruby() {
        let input = "&ruby(Ashita){明日};";
        let output = apply_inline_decorations(input);
        assert_eq!(
            output,
            "<ruby>明日<rp>(</rp><rt>Ashita</rt><rp>)</rp></ruby>;"
        );
    }

    #[test]
    fn test_semantic_elements() {
        let input = "&dfn(term); &kbd(Ctrl); &samp(output); &var(x);";
        let output = apply_inline_decorations(input);
        assert!(output.contains("<dfn>term</dfn>"));
        assert!(output.contains("<kbd>Ctrl</kbd>"));
        assert!(output.contains("<samp>output</samp>"));
        assert!(output.contains("<var>x</var>"));
    }

    #[test]
    fn test_cite_q_small() {
        let input = "&cite(Book Title); &q(quote); &small(note);";
        let output = apply_inline_decorations(input);
        assert!(output.contains("<cite>Book Title</cite>"));
        assert!(output.contains("<q>quote</q>"));
        assert!(output.contains("<small>note</small>"));
    }

    #[test]
    fn test_time_and_data() {
        let input = "&time(2026-01-26){today}; &data(12345){value};";
        let output = apply_inline_decorations(input);
        assert!(output.contains("<time datetime=\"2026-01-26\">today</time>"));
        assert!(output.contains("<data value=\"12345\">value</data>"));
    }

    #[test]
    fn test_bidirectional_text() {
        let input = "&bdi(مرحبا); &bdo(rtl){right-to-left};";
        let output = apply_inline_decorations(input);
        assert!(output.contains("<bdi>مرحبا</bdi>"));
        assert!(output.contains("<bdo dir=\"rtl\">right-to-left</bdo>"));
    }

    #[test]
    fn test_wbr() {
        let input = "Very&wbr;Long&wbr;Word";
        let output = apply_inline_decorations(input);
        assert_eq!(output, "Very<wbr />Long<wbr />Word");
    }

    #[test]
    fn test_br() {
        let input = "Line 1&br;Line 2&br;Line 3";
        let output = apply_inline_decorations(input);
        assert_eq!(output, "Line 1<br />Line 2<br />Line 3");
    }

    #[test]
    fn test_badge_basic() {
        let input = "&badge(primary){New};";
        let output = apply_inline_decorations(input);
        assert!(output.contains("<span class=\"badge bg-primary\">New</span>"));
    }

    #[test]
    fn test_badge_pill() {
        let input = "&badge(success-pill){Active};";
        let output = apply_inline_decorations(input);
        assert!(output.contains("badge rounded-pill"));
        assert!(output.contains("bg-success"));
    }

    #[test]
    fn test_badge_with_link() {
        let input = "&badge(danger){[Error](/error)};";
        let output = apply_inline_decorations(input);
        assert!(output.contains("<a href=\"/error\" class=\"badge bg-danger\">Error</a>"));
    }

    #[test]
    fn test_color_bootstrap_class() {
        let input = "&color(primary){Primary text};";
        let output = apply_inline_decorations(input);
        assert!(output.contains("class=\"text-primary\""));
    }

    #[test]
    fn test_color_custom_value() {
        let input = "&color(#FF0000){Red text};";
        let output = apply_inline_decorations(input);
        assert!(output.contains("style=\"color: #FF0000\""));
    }

    #[test]
    fn test_spoiler_discord_syntax() {
        let input = "This is ||hidden text|| in a sentence.";
        let output = apply_inline_decorations(input);
        assert!(output.contains(r#"<span class="spoiler" role="button" tabindex="0" aria-expanded="false">hidden text</span>"#));
    }

    #[test]
    fn test_spoiler_umd_function_parentheses() {
        let input = "This is &spoiler(hidden text); in a sentence.";
        let output = apply_inline_decorations(input);
        assert!(output.contains(r#"<span class="spoiler" role="button" tabindex="0" aria-expanded="false">hidden text</span>"#));
    }

    #[test]
    fn test_spoiler_umd_function_braces() {
        let input = "This is &spoiler{hidden text}; in a sentence.";
        let output = apply_inline_decorations(input);
        assert!(output.contains(r#"<span class="spoiler" role="button" tabindex="0" aria-expanded="false">hidden text</span>"#));
    }

    #[test]
    fn test_multiple_spoilers() {
        let input = "||spoiler1|| and ||spoiler2|| and &spoiler{spoiler3};";
        let output = apply_inline_decorations(input);
        let spoiler_count = output.matches(r#"<span class="spoiler""#).count();
        assert_eq!(spoiler_count, 3);
    }

    #[test]
    fn test_size_bootstrap_class() {
        let input = "&size(1.5){Medium text};";
        let output = apply_inline_decorations(input);
        assert!(output.contains("class=\"fs-4\""));
    }

    #[test]
    fn test_size_custom_value() {
        let input = "&size(3rem){Custom size};";
        let output = apply_inline_decorations(input);
        assert!(output.contains("style=\"font-size: 3rem\""));
    }
}