NAME

Term::Fabulous::Text::Markup - Parse "[bold red]text[/]" markup into text, styled spans and links

SYNOPSIS

use Term::Fabulous::Text::Markup qw(parse_markup);

my ( $text, $spans ) = parse_markup('Press [bold]Enter[/] to [green on #202020]save[/green on #202020].');
# $text  is 'Press Enter to save.'
# $spans is [ [ 6, 11, { set => TB_BOLD, ... } ], [ 15, 19, { color => [ 0, 128, 0, 255 ], ... } ] ]

my ( $see, undef, $links ) = parse_markup('See [link=https://perl.org]perl.org[/link].');
# $see   is 'See perl.org.'
# $links is [ [ 4, 12, 'https://perl.org' ] ]

DESCRIPTION

Markup is text with style tags in square brackets, in the syntax of Python's rich library. Term::Fabulous::Widget::RichText takes it as its markup parameter; this module does the parsing.

[STYLE]

Opens a span with a style string of Term::Fabulous::Text::Style: [bold], [italic SteelBlue on #202020], [not bold]. An invalid style dies.

[link=TARGET]

Opens a link to TARGET: everything after the = up to the closing bracket, without the blanks at its ends ([link=https://perl.org], [link=perlfunc/open], [link=page two] for the target page two). The target is always a string, and cannot contain [ or ]; give other targets from Perl with add_link. What the target means is the program's business (see "LINKS" in Term::Fabulous::Widget::RichText). A link tag without a target, and a link inside another link, die. Close it with [/link] or [/]. A link is no span: style the words with a style tag inside or around it, as in [bold][link=faq]FAQ[/link][/].

[/]

Closes the innermost open span or link.

[/STYLE]

Closes the innermost open span that was opened with exactly that style string: [/bold] closes a [bold]. Dies when no such span is open.

\[

A literal [. Every other backslash is a backslash.

Brackets that do not look like a tag are text: a tag starts with a letter, # or / and contains no brackets, so [1], [ ] and [] stay as they are. Spans still open at the end run to the end of the text. Tags nest: an inner span is applied after the outer one, so its words win where they overlap.

FUNCTIONS

Nothing is exported by default.

parse_markup

my ( $text, $spans, $links ) = parse_markup($markup);

Returns the text without its tags and the spans as an array reference of [ $start, $end, $style ]: the character offsets of the span in $text ($end is the offset after its last character) and its normalized style hash (see "Style hashes" in Term::Fabulous::Text::Style). Spans are ordered by their start, outer spans before inner ones that start at the same offset; empty spans are dropped. The links are an array reference of [ $start, $end, $target ], ordered by their start; empty links are dropped as well. undef and references die with a message starting with Term::Fabulous::Text::Markup:.

SEE ALSO

Term::Fabulous::Text::Style, Term::Fabulous::Widget::RichText.