| 1 | package Term::Fabulous::Text::Markup; | ||
| 2 | |||
| 3 | 1 | ▶ | use v5.32; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use feature 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | no warnings 'experimental::signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | our $VERSION = '0.02'; | ||
| 9 | |||
| 10 | 1 | ▶ | use Exporter 'import'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | our @EXPORT_OK = qw(parse_markup); | ||
| 12 | |||
| 13 | 1 | ▶ | use Term::Fabulous::Text::Style qw(parse_style); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | |||
| 15 | # A tag starts (after optional blanks) with a letter, a '#' (a hex color) | ||
| 16 | # or a '/' (a closing tag) and holds no brackets. Anything else between | ||
| 17 | # brackets is text. | ||
| 18 | my $TOKEN = qr/\G(?:(?<escaped>\\\[)|\[(?<tag>\s*[a-zA-Z#\/][^\[\]]*)\]|(?<text>[^\[\\]+|.))/s; | ||
| 19 | |||
| 20 | sub _fail ( $what, $markup ) { | ||
| 21 | die "Term::Fabulous::Text::Markup: $what in '$markup'"; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub _closes ( $tag, $open, $markup ) { | ||
| 25 | my $name = $tag =~ s/\A\s*\/\s*//r; | ||
| 26 | $name =~ s/\s+\z//; | ||
| 27 | return pop @$open if $name eq '' && @$open; | ||
| 28 | _fail( 'closing tag [/] without an open tag', $markup ) if $name eq ''; | ||
| 29 | |||
| 30 | my ($index) = grep { $open->[$_][0] eq $name } reverse 0 .. $#$open; | ||
| 31 | _fail( "closing tag [/$name] does not match an open tag", $markup ) unless defined $index; | ||
| 32 | return splice @$open, $index, 1; | ||
| 33 | } | ||
| 34 | |||
| 35 | # A link tag: 'link=' and the target, which may not be empty. | ||
| 36 | sub _link_target ( $tag_text, $markup ) { | ||
| 37 | my ($target) = $tag_text =~ /\Alink\s*=\s*(.*)\z/s; | ||
| 38 | return undef unless defined $target; | ||
| 39 | _fail( "link tag [$tag_text] without a target", $markup ) unless length $target; | ||
| 40 | return $target; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub parse_markup ($markup) { | ||
| 44 | die 'Term::Fabulous::Text::Markup: markup is a string' if !defined $markup || ref $markup; | ||
| 45 | |||
| 46 | my $plain = ''; | ||
| 47 | my @open; # [ $name, $style, $start, $link_target ] of every open tag, outermost first | ||
| 48 | my @closed; # [ $start, $end, $style ] in closing order | ||
| 49 | my @links; # [ $start, $end, $target ] in closing order | ||
| 50 | my $close = sub ($tag) { | ||
| 51 | my ( undef, $style, $start, $target ) = @$tag; | ||
| 52 | return unless $start < length $plain; | ||
| 53 | push @{ defined $target ? \@links : \@closed }, [ $start, length $plain, $target // $style ]; | ||
| 54 | return; | ||
| 55 | }; | ||
| 56 | |||
| 57 | while ( $markup =~ /$TOKEN/gc ) { | ||
| 58 | my ( $text, $escaped, $tag ) = @+{qw(text escaped tag)}; # copied: the next match resets %+ | ||
| 59 | if ( defined $text ) { | ||
| 60 | $plain .= $text; | ||
| 61 | } | ||
| 62 | elsif ( defined $escaped ) { | ||
| 63 | $plain .= '['; | ||
| 64 | } | ||
| 65 | elsif ( $tag =~ /\A\s*\// ) { | ||
| 66 | $close->( _closes( $tag, \@open, $markup ) ); | ||
| 67 | } | ||
| 68 | else { | ||
| 69 | my $tag_text = $tag =~ s/\A\s+|\s+\z//gr; | ||
| 70 | my $target = _link_target( $tag_text, $markup ); | ||
| 71 | if ( !defined $target ) { | ||
| 72 | push @open, [ $tag_text, parse_style($tag_text), length $plain, undef ]; | ||
| 73 | next; | ||
| 74 | } | ||
| 75 | _fail( "link tag [$tag_text] inside another link", $markup ) if grep { defined $_->[3] } @open; | ||
| 76 | push @open, [ 'link', undef, length $plain, $target ]; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | $close->($_) foreach reverse @open; | ||
| 80 | |||
| 81 | # Outer spans first, so that an inner span wins where they overlap. | ||
| 82 | my @spans = sort { $a->[0] <=> $b->[0] } reverse @closed; | ||
| 83 | return ( $plain, \@spans, [ sort { $a->[0] <=> $b->[0] } @links ] ); | ||
| 84 | } | ||
| 85 | |||
| 86 | 1; | ||
| 87 | |||
| 88 | __END__ | ||