rich-rs 1.2.0

Rich text and beautiful formatting for the terminal
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
//! Markup: BBCode-like markup parsing.
//!
//! Supports syntax like `[bold red]text[/]` for styling, links, and metadata.
//!
//! # Syntax
//!
//! - `[style]text[/style]` - Apply style to text
//! - `[style]text[/]` - Implicit close (closes most recent tag)
//! - `[link=url]text[/link]` - Hyperlink
//! - `[@handler=params]text[/@handler]` - Metadata (for Textual)
//! - `:emoji_name:` - Emoji codes (replaced with Unicode)
//! - `\[` - Escaped bracket (literal `[`)
//!
//! # Example
//!
//! ```
//! use rich_rs::markup::render;
//!
//! let text = render("[bold red]Hello[/] World", true);
//! assert!(text.is_ok());
//! ```

use once_cell::sync::Lazy;
use regex::Regex;
use std::collections::BTreeMap;
use std::sync::Arc;

use crate::emoji::Emoji;
use crate::error::{ParseError, Result};
use crate::style::{MetaValue, Style, StyleMeta};
use crate::text::{Span, Text};

/// A parsed markup tag.
///
/// Represents either a style tag or a special tag like link/metadata.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Tag {
    /// The tag name (e.g., "bold", "link", "@click").
    pub name: String,
    /// Optional parameters after `=` (e.g., "<https://example.com>" in `[link=https://example.com]`).
    pub parameters: Option<String>,
}

impl Tag {
    /// Create a new tag.
    pub fn new(name: impl Into<String>, parameters: Option<String>) -> Self {
        Tag {
            name: name.into(),
            parameters,
        }
    }

    /// Get the markup representation of this tag.
    pub fn markup(&self) -> String {
        match &self.parameters {
            Some(params) => format!("[{}={}]", self.name, params),
            None => format!("[{}]", self.name),
        }
    }
}

impl std::fmt::Display for Tag {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.parameters {
            Some(params) => write!(f, "{} {}", self.name, params),
            None => write!(f, "{}", self.name),
        }
    }
}

/// A token from the markup parser.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Token {
    /// Plain text.
    Text(String),
    /// A parsed tag.
    Tag(Tag),
}

/// Regex for matching markup tags.
///
/// Pattern: `((\\*)\[([a-z#/@][^[]*?)])`
/// - Group 1: Full match including backslashes
/// - Group 2: Backslash escapes
/// - Group 3: Tag content (without brackets)
static RE_TAGS: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"((?:\\)*)\[([a-z#/@][^\[]*?)]").expect("Invalid regex pattern"));

/// Parse markup into an iterator of (position, text, tag) tuples.
///
/// This is the core tokenizer. It yields:
/// - `(pos, Some(text), None)` for plain text (pos is where text ENDS, matching Python)
/// - `(pos, None, Some(tag))` for tags (pos is where tag STARTS)
///
/// # Note
///
/// Positions are **byte offsets**, not character offsets. For ASCII input this
/// matches Python's behavior, but for non-ASCII text the positions may differ.
/// Error messages use these byte positions.
///
/// # Arguments
///
/// * `markup` - The markup string to parse
pub fn parse(markup: &str) -> Vec<(usize, Option<String>, Option<Tag>)> {
    let mut result = Vec::new();
    let mut position = 0;

    for caps in RE_TAGS.captures_iter(markup) {
        let full_match = caps.get(0).unwrap();
        let escapes = caps.get(1).map(|m| m.as_str()).unwrap_or("");
        let tag_text = caps.get(2).map(|m| m.as_str()).unwrap_or("");

        let start = full_match.start();
        let end = full_match.end();

        // Emit any text before this match
        // Python yields `start` (where text ends) as position, not `position` (where it starts)
        if start > position {
            result.push((start, Some(markup[position..start].to_string()), None));
        }

        // Track adjusted start position for tags after backslashes
        let mut tag_start = start;

        if !escapes.is_empty() {
            let escape_len = escapes.len();
            let (backslashes, escaped) = (escape_len / 2, escape_len % 2);

            if backslashes > 0 {
                // Literal backslashes (pairs become singles)
                result.push((start, Some("\\".repeat(backslashes)), None));
                // Advance tag_start past the literal backslashes (matches Python's start += backslashes * 2)
                tag_start = start + backslashes * 2;
            }

            if escaped != 0 {
                // The tag is escaped - emit as literal text (without the escape backslash)
                // Python uses position after any literal backslashes
                let literal_start = tag_start + 1; // Skip the escape backslash
                let literal = &markup[literal_start..end];
                result.push((tag_start, Some(literal.to_string()), None));
                position = end;
                continue;
            }
        }

        // Parse tag: split on first `=`
        let (text, parameters) = match tag_text.find('=') {
            Some(idx) => (
                tag_text[..idx].to_string(),
                Some(tag_text[idx + 1..].to_string()),
            ),
            None => (tag_text.to_string(), None),
        };

        result.push((tag_start, None, Some(Tag::new(text, parameters))));
        position = end;
    }

    // Emit any remaining text
    // Note: Python uses `position` (start) for trailing text, not `len()` (end)
    // This is inconsistent with mid-text behavior but matches Python Rich
    if position < markup.len() {
        result.push((position, Some(markup[position..].to_string()), None));
    }

    result
}

/// Escape text for safe inclusion in markup.
///
/// Doubles existing backslashes before brackets and escapes opening brackets
/// that could be interpreted as tags.
///
/// # Example
///
/// ```
/// use rich_rs::markup::escape;
///
/// assert_eq!(escape("[bold]"), "\\[bold]");
/// assert_eq!(escape("\\[bold]"), "\\\\\\[bold]");
/// ```
pub fn escape(markup: &str) -> String {
    static ESCAPE_RE: Lazy<Regex> =
        Lazy::new(|| Regex::new(r"(\\*)(\[[a-z#/@][^\[]*?])").expect("Invalid escape regex"));

    let result = ESCAPE_RE.replace_all(markup, |caps: &regex::Captures| {
        let backslashes = caps.get(1).map(|m| m.as_str()).unwrap_or("");
        let text = caps.get(2).map(|m| m.as_str()).unwrap_or("");
        // Double the backslashes and add one more to escape the bracket
        format!("{}{}\\{}", backslashes, backslashes, text)
    });

    // Handle trailing backslash
    let result = result.to_string();
    if result.ends_with('\\') && !result.ends_with("\\\\") {
        format!("{}\\", result)
    } else {
        result
    }
}

/// Normalize a style name for comparison.
///
/// Strips whitespace and converts to lowercase.
fn normalize_style(style: &str) -> String {
    style.trim().to_lowercase()
}

/// Render markup into a Text object.
///
/// # Arguments
///
/// * `markup` - The markup string to render
/// * `emoji` - Whether to replace emoji codes (`:smile:` -> actual emoji)
///
/// # Returns
///
/// A `Text` object with styled spans, or an error if the markup is invalid.
///
/// # Example
///
/// ```
/// use rich_rs::markup::render;
///
/// let text = render("[bold red]Hello[/bold red] World", true).unwrap();
/// assert_eq!(text.plain_text(), "Hello World");
/// ```
pub fn render(markup: &str, emoji: bool) -> Result<Text> {
    render_with_emoji_variant(markup, emoji, None)
}

/// Render markup into a Text object with an optional emoji variant override.
///
/// # Arguments
///
/// * `markup` - The markup string to render
/// * `emoji` - Whether to replace emoji codes
/// * `emoji_variant` - Optional default variant to apply to all emojis
///
/// # Returns
///
/// A `Text` object with styled spans, or an error if the markup is invalid.
pub fn render_with_emoji_variant(
    markup: &str,
    emoji: bool,
    emoji_variant: Option<crate::emoji::EmojiVariant>,
) -> Result<Text> {
    // Helper to apply variant selector to emoji-replaced text
    let apply_emoji = |input: &str| -> String {
        let replaced = Emoji::replace(input);
        if let Some(variant) = emoji_variant {
            // Append variant selector to any emoji characters that don't already have one
            let selector = variant.selector();
            let mut result = String::with_capacity(replaced.len());
            let mut chars = replaced.chars().peekable();
            while let Some(c) = chars.next() {
                result.push(c);
                // If this is an emoji-like codepoint (above basic ASCII/Latin) and next
                // char is not already a variant selector, append one
                if c as u32 > 0x2000
                    && c != '\u{fe0e}'
                    && c != '\u{fe0f}'
                    && chars
                        .peek()
                        .map_or(true, |&next| next != '\u{fe0e}' && next != '\u{fe0f}')
                {
                    result.push_str(selector);
                }
            }
            result
        } else {
            replaced
        }
    };

    // Fast path: no brackets means no markup
    if !markup.contains('[') {
        let content = if emoji {
            apply_emoji(markup)
        } else {
            markup.to_string()
        };
        return Ok(Text::plain(content));
    }

    let mut text = Text::new();
    let mut style_stack: Vec<(usize, Tag)> = Vec::new();
    let mut spans: Vec<Span> = Vec::new();

    for (position, plain_text, tag) in parse(markup) {
        if let Some(mut content) = plain_text {
            // Handle escaped brackets: \[ -> [
            content = content.replace("\\[", "[");

            // Replace emoji codes if enabled
            if emoji {
                content = apply_emoji(&content);
            }

            text.append(&content, None);
        } else if let Some(tag) = tag {
            if tag.name.starts_with('/') {
                // Closing tag
                let style_name = tag.name[1..].trim();

                let (start, open_tag) = if !style_name.is_empty() {
                    // Explicit close: [/bold]
                    let normalized = normalize_style(style_name);
                    pop_style(&mut style_stack, &normalized).ok_or_else(|| {
                        ParseError::UnexpectedClosingTag(format!(
                            "closing tag '{}' at position {} doesn't match any open tag",
                            tag.markup(),
                            position
                        ))
                    })?
                } else {
                    // Implicit close: [/]
                    style_stack.pop().ok_or_else(|| {
                        ParseError::UnexpectedClosingTag(format!(
                            "closing tag '[/]' at position {} has nothing to close",
                            position
                        ))
                    })?
                };

                // Create span for the closed region
                if open_tag.name.starts_with('@') {
                    // Metadata tag - used by Textual for event handlers.
                    let mut meta_map = BTreeMap::new();
                    let value = match open_tag.parameters.as_deref().map(str::trim) {
                        None => MetaValue::None,
                        Some("") => MetaValue::None,
                        Some(params) => MetaValue::parse_python_literal(params)
                            .unwrap_or_else(|| MetaValue::str(params.to_string())),
                    };
                    meta_map.insert(open_tag.name.clone(), value);
                    let meta = StyleMeta {
                        meta: Some(Arc::new(meta_map)),
                        ..Default::default()
                    };
                    spans.push(Span::new_with_meta(
                        start,
                        text.len(),
                        Style::new(),
                        Some(meta),
                    ));
                } else if open_tag.name == "link" {
                    // Link tag - apply underline+cyan as visual indicator
                    let link_style = Style::new()
                        .with_underline(true)
                        .with_color(crate::color::SimpleColor::Standard(6)); // cyan
                    let meta = open_tag
                        .parameters
                        .as_ref()
                        .map(|url| StyleMeta::with_link(url.clone()))
                        .unwrap_or_default();
                    spans.push(Span::new_with_meta(
                        start,
                        text.len(),
                        link_style,
                        Some(meta),
                    ));
                } else {
                    // Regular style tag - parse and apply
                    let style_str = open_tag.to_string();
                    if let Some(style) = Style::parse(&style_str) {
                        if !style.is_null() {
                            spans.push(Span::new(start, text.len(), style));
                        }
                        // Note: Unknown style tokens are silently ignored, matching Python
                    }
                }
            } else {
                // Opening tag - push to stack with current position
                let normalized_tag = Tag::new(normalize_style(&tag.name), tag.parameters);
                style_stack.push((text.len(), normalized_tag));
            }
        }
    }

    // Handle unclosed tags - apply them as spans to end of text
    // Use same logic as explicit close: skip metadata, handle link specially, filter null styles
    let text_length = text.len();
    while let Some((start, tag)) = style_stack.pop() {
        if tag.name.starts_with('@') {
            let mut meta_map = BTreeMap::new();
            let value = match tag.parameters.as_deref().map(str::trim) {
                None => MetaValue::None,
                Some("") => MetaValue::None,
                Some(params) => MetaValue::parse_python_literal(params)
                    .unwrap_or_else(|| MetaValue::str(params.to_string())),
            };
            meta_map.insert(tag.name.clone(), value);
            let meta = StyleMeta {
                meta: Some(Arc::new(meta_map)),
                ..Default::default()
            };
            spans.push(Span::new_with_meta(
                start,
                text_length,
                Style::new(),
                Some(meta),
            ));
        } else if tag.name == "link" {
            // Link tag - apply underline+cyan (matches explicit close behavior)
            let link_style = Style::new()
                .with_underline(true)
                .with_color(crate::color::SimpleColor::Standard(6)); // cyan
            let meta = tag
                .parameters
                .as_ref()
                .map(|url| StyleMeta::with_link(url.clone()))
                .unwrap_or_default();
            spans.push(Span::new_with_meta(
                start,
                text_length,
                link_style,
                Some(meta),
            ));
        } else {
            // Regular style tag
            let style_str = tag.to_string();
            if let Some(style) = Style::parse(&style_str) {
                if !style.is_null() {
                    spans.push(Span::new(start, text_length, style));
                }
            }
        }
    }

    // Apply spans to text
    // Python does: sorted(spans[::-1], key=attrgetter("start"))
    // That's: reverse the list, then stable-sort by start
    // This preserves innermost-first ordering for overlapping spans
    spans.reverse();
    spans.sort_by(|a, b| a.start.cmp(&b.start)); // stable sort
    for span in spans {
        text.spans_mut().push(span);
    }

    Ok(text)
}

/// Pop a style from the stack by name.
///
/// Searches from the end of the stack for a matching tag name.
fn pop_style(stack: &mut Vec<(usize, Tag)>, style_name: &str) -> Option<(usize, Tag)> {
    for i in (0..stack.len()).rev() {
        if normalize_style(&stack[i].1.name) == style_name {
            return Some(stack.remove(i));
        }
    }
    None
}

/// Render markup with a base style applied.
///
/// # Arguments
///
/// * `markup` - The markup string to render
/// * `style` - Base style to apply
/// * `emoji` - Whether to replace emoji codes
pub fn render_with_style(markup: &str, style: Style, emoji: bool) -> Result<Text> {
    let mut text = render(markup, emoji)?;

    // Apply base style as a span covering the entire text
    if text.len() > 0 {
        text.stylize(0, text.len(), style);
    }

    Ok(text)
}

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

    #[test]
    fn test_tag_new() {
        let tag = Tag::new("bold", None);
        assert_eq!(tag.name, "bold");
        assert_eq!(tag.parameters, None);
        assert_eq!(tag.markup(), "[bold]");

        let tag_with_params = Tag::new("link", Some("https://example.com".to_string()));
        assert_eq!(tag_with_params.name, "link");
        assert_eq!(
            tag_with_params.parameters,
            Some("https://example.com".to_string())
        );
        assert_eq!(tag_with_params.markup(), "[link=https://example.com]");
    }

    #[test]
    fn test_tag_display() {
        let tag = Tag::new("bold", None);
        assert_eq!(tag.to_string(), "bold");

        let tag_with_params = Tag::new("link", Some("url".to_string()));
        assert_eq!(tag_with_params.to_string(), "link url");
    }

    #[test]
    fn test_parse_plain_text() {
        let tokens = parse("hello world");
        assert_eq!(tokens.len(), 1);
        assert_eq!(tokens[0].1, Some("hello world".to_string()));
    }

    #[test]
    fn test_parse_single_tag() {
        let tokens = parse("[bold]hello[/bold]");
        assert_eq!(tokens.len(), 3);

        // Opening tag
        assert!(tokens[0].2.is_some());
        assert_eq!(tokens[0].2.as_ref().unwrap().name, "bold");

        // Text
        assert_eq!(tokens[1].1, Some("hello".to_string()));

        // Closing tag
        assert!(tokens[2].2.is_some());
        assert_eq!(tokens[2].2.as_ref().unwrap().name, "/bold");
    }

    #[test]
    fn test_parse_tag_with_params() {
        let tokens = parse("[link=https://example.com]click[/link]");
        assert_eq!(tokens.len(), 3);

        let open_tag = tokens[0].2.as_ref().unwrap();
        assert_eq!(open_tag.name, "link");
        assert_eq!(open_tag.parameters, Some("https://example.com".to_string()));
    }

    #[test]
    fn test_parse_escaped_bracket() {
        let tokens = parse("\\[not a tag]");
        assert_eq!(tokens.len(), 1);
        // The escaped bracket should be plain text
        assert!(tokens[0].1.is_some());
        assert!(tokens[0].1.as_ref().unwrap().contains("[not a tag]"));
    }

    #[test]
    fn test_escape_simple() {
        assert_eq!(escape("[bold]"), "\\[bold]");
    }

    #[test]
    fn test_escape_with_backslash() {
        assert_eq!(escape("\\[bold]"), "\\\\\\[bold]");
    }

    #[test]
    fn test_escape_no_tag() {
        assert_eq!(escape("hello world"), "hello world");
    }

    #[test]
    fn test_escape_not_a_tag() {
        // Brackets that don't start tags are left alone
        assert_eq!(escape("[123]"), "[123]");
    }

    #[test]
    fn test_render_plain() {
        let text = render("hello world", false).unwrap();
        assert_eq!(text.plain_text(), "hello world");
        assert!(text.spans().is_empty());
    }

    #[test]
    fn test_render_bold() {
        let text = render("[bold]hello[/bold]", false).unwrap();
        assert_eq!(text.plain_text(), "hello");
        assert!(!text.spans().is_empty());
    }

    #[test]
    fn test_render_implicit_close() {
        let text = render("[bold]hello[/]", false).unwrap();
        assert_eq!(text.plain_text(), "hello");
        assert!(!text.spans().is_empty());
    }

    #[test]
    fn test_render_nested() {
        let text = render("[bold][italic]hello[/italic][/bold]", false).unwrap();
        assert_eq!(text.plain_text(), "hello");
        // Should have spans for both bold and italic
        assert!(text.spans().len() >= 2);
    }

    #[test]
    fn test_render_with_color() {
        let text = render("[red]hello[/red]", false).unwrap();
        assert_eq!(text.plain_text(), "hello");
        assert!(!text.spans().is_empty());
    }

    #[test]
    fn test_render_unclosed_tag() {
        // Unclosed tags should apply to end of text
        let text = render("[bold]hello", false).unwrap();
        assert_eq!(text.plain_text(), "hello");
        assert!(!text.spans().is_empty());
    }

    #[test]
    fn test_render_unclosed_link() {
        // Unclosed link should apply underline+cyan to end of text
        let text = render("[link=https://x]hi", false).unwrap();
        assert_eq!(text.plain_text(), "hi");
        assert!(!text.spans().is_empty());
        // Should have underline from link style
        assert!(text.spans()[0].style.underline == Some(true));
        assert_eq!(
            text.spans()[0]
                .meta
                .as_ref()
                .and_then(|m| m.link.as_deref()),
            Some("https://x")
        );
    }

    #[test]
    fn test_render_unclosed_metadata() {
        // Unclosed metadata tag should create non-visible spans with metadata attached
        let text = render("[@foo]bar", false).unwrap();
        assert_eq!(text.plain_text(), "bar");
        assert_eq!(text.spans().len(), 1);
        assert!(text.spans()[0].style.is_null());
        let meta = text.spans()[0].meta.as_ref().unwrap();
        let meta_map = meta.meta.as_ref().unwrap();
        assert_eq!(meta_map.get("@foo"), Some(&MetaValue::None));
    }

    #[test]
    fn test_render_unmatched_close_error() {
        let result = render("[bold]hello[/italic]", false);
        assert!(result.is_err());
    }

    #[test]
    fn test_render_empty_close_no_open_error() {
        let result = render("hello[/]", false);
        assert!(result.is_err());
    }

    #[test]
    fn test_render_escaped_bracket() {
        let text = render("\\[not bold]", false).unwrap();
        assert_eq!(text.plain_text(), "[not bold]");
    }

    #[test]
    fn test_render_with_emoji() {
        let text = render(":smile:", true).unwrap();
        // Should contain the smile emoji
        assert!(text.plain_text().contains('\u{1f604}') || text.plain_text() == ":smile:");
    }

    #[test]
    fn test_render_emoji_in_styled() {
        let text = render("[bold]:+1:[/bold]", true).unwrap();
        // Should contain thumbs up emoji
        assert!(!text.spans().is_empty());
    }

    #[test]
    fn test_render_link() {
        let text = render("[link=https://example.com]click here[/link]", false).unwrap();
        assert_eq!(text.plain_text(), "click here");
        // Link should create a span (underlined cyan)
        assert!(!text.spans().is_empty());
        assert_eq!(
            text.spans()[0]
                .meta
                .as_ref()
                .and_then(|m| m.link.as_deref()),
            Some("https://example.com")
        );
    }

    #[test]
    fn test_normalize_style() {
        assert_eq!(normalize_style("BOLD"), "bold");
        assert_eq!(normalize_style("  italic  "), "italic");
        assert_eq!(normalize_style("Bold Red"), "bold red");
    }

    #[test]
    fn test_render_with_style() {
        let base_style = Style::new().with_italic(true);
        let text = render_with_style("[bold]hello[/]", base_style, false).unwrap();
        assert_eq!(text.plain_text(), "hello");
        // Should have both bold and italic spans
        assert!(text.spans().len() >= 2);
    }

    #[test]
    fn test_render_multiple_tags_same_text() {
        let text = render("[bold red on blue]styled[/]", false).unwrap();
        assert_eq!(text.plain_text(), "styled");
        assert!(!text.spans().is_empty());
    }

    #[test]
    fn test_render_overlapping_styles() {
        let text = render("[bold]Hello [italic]World[/italic]![/bold]", false).unwrap();
        assert_eq!(text.plain_text(), "Hello World!");
    }

    #[test]
    fn test_parse_position_tracking() {
        let tokens = parse("abc[bold]def[/bold]ghi");
        // Positions: mid-text yields END position, trailing text yields START position (matches Python)
        assert_eq!(tokens[0].0, 3); // "abc" ends at 3 (where [bold] starts)
        assert_eq!(tokens[1].0, 3); // [bold] starts at 3
        assert_eq!(tokens[2].0, 12); // "def" ends at 12 (where [/bold] starts)
        assert_eq!(tokens[3].0, 12); // [/bold] starts at 12
        assert_eq!(tokens[4].0, 19); // "ghi" starts at 19 (trailing text uses start position)
    }

    #[test]
    fn test_escape_roundtrip() {
        let original = "[bold]text[/bold]";
        let escaped = escape(original);
        let text = render(&escaped, false).unwrap();
        assert_eq!(text.plain_text(), original);
    }
}