blib/lib/Term/Fabulous/Text/Markup.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Term/Fabulous/Static.pm ↑ Summary blib/lib/Term/Fabulous/Widget.pm »
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.05';
9  
10 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 our @EXPORT_OK = qw(parse_markup escape_markup);
12  
13 0 ▶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 # A text that markup shows as it is: every [ escaped.
44 sub escape_markup ($text) {
45 die 'Term::Fabulous::Text::Markup: escape_markup needs a string' if !defined $text || ref $text;
46 return $text =~ s/\[/\\[/gr;
47 }
48  
49 sub parse_markup ($markup) {
50 die 'Term::Fabulous::Text::Markup: markup is a string' if !defined $markup || ref $markup;
51  
52 my $plain = '';
53 my @open; # [ $name, $style, $start, $link_target ] of every open tag, outermost first
54 my @closed; # [ $start, $end, $style ] in closing order
55 my @links; # [ $start, $end, $target ] in closing order
56 my $close = sub ($tag) {
57 my ( undef, $style, $start, $target ) = @$tag;
58 return unless $start < length $plain;
59 push @{ defined $target ? \@links : \@closed }, [ $start, length $plain, $target // $style ];
60 return;
61 };
62  
63 while ( $markup =~ /$TOKEN/gc ) {
64 my ( $text, $escaped, $tag ) = @+{qw(text escaped tag)}; # copied: the next match resets %+
65 if ( defined $text ) {
66 $plain .= $text;
67 }
68 elsif ( defined $escaped ) {
69 $plain .= '[';
70 }
71 elsif ( $tag =~ /\A\s*\// ) {
72 $close->( _closes( $tag, \@open, $markup ) );
73 }
74 else {
75 my $tag_text = $tag =~ s/\A\s+|\s+\z//gr;
76 my $target = _link_target( $tag_text, $markup );
77 if ( !defined $target ) {
78 push @open, [ $tag_text, parse_style($tag_text), length $plain, undef ];
79 next;
80 }
81 _fail( "link tag [$tag_text] inside another link", $markup ) if grep { defined $_->[3] } @open;
82 push @open, [ 'link', undef, length $plain, $target ];
83 }
84 }
85 $close->($_) foreach reverse @open;
86  
87 # Outer spans first, so that an inner span wins where they overlap.
88 my @spans = sort { $a->[0] <=> $b->[0] } reverse @closed;
89 return ( $plain, \@spans, [ sort { $a->[0] <=> $b->[0] } @links ] );
90 }
91  
92 1;
93  
94 __END__
« blib/lib/Term/Fabulous/Static.pm ↑ Summary blib/lib/Term/Fabulous/Widget.pm »