Skip to main content

Crate standout_bbparser

Crate standout_bbparser 

Source
Expand description

BBCode-style tag parser for terminal styling.

This crate provides a parser for [tag]content[/tag] style markup, designed for terminal output styling. It handles nested tags correctly and supports multiple output modes.

§Example

use standout_bbparser::{BBParser, TagTransform};
use console::Style;
use std::collections::HashMap;

let mut styles = HashMap::new();
styles.insert("bold".to_string(), Style::new().bold());
styles.insert("red".to_string(), Style::new().red());

// Apply ANSI codes
let parser = BBParser::new(styles.clone(), TagTransform::Apply);
let output = parser.parse("[bold]hello[/bold]");
// output contains ANSI escape codes for bold

// Strip tags (plain text)
let parser = BBParser::new(styles.clone(), TagTransform::Remove);
let output = parser.parse("[bold]hello[/bold]");
assert_eq!(output, "hello");

// Keep tags visible (debug mode)
let parser = BBParser::new(styles, TagTransform::Keep);
let output = parser.parse("[bold]hello[/bold]");
assert_eq!(output, "[bold]hello[/bold]");

§Unknown Tag Handling

Tags not found in the styles map can be handled in two ways:

For validation, use BBParser::validate to check for unknown tags before parsing.

§Tag Name Syntax

Tag names follow CSS identifier rules:

  • Start with a letter (a-z) or underscore (_)
  • Followed by letters, digits (0-9), underscores, or hyphens (-)
  • Cannot start with a digit or hyphen followed by digit
  • Case-sensitive (lowercase recommended)

Pattern: [a-z_][a-z0-9_-]*

Structs§

BBParser
A BBCode-style tag parser for terminal styling.
UnknownTagError
An error representing an unknown tag in the input.
UnknownTagErrors
A collection of unknown tag errors found during parsing.

Enums§

TagTransform
How to transform matched tags in the output.
UnknownTagBehavior
How to handle tags not found in the styles map.
UnknownTagKind
The kind of unknown tag encountered.