| 1 | # You may distribute under the terms of the GNU General Public License | ||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||
| 3 | # (C) Paul Evans, 2017 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | package Circle::Net::Matrix::Utils; | ||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 2 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 2 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||
| 16 | parse_markdownlike | ||||||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use String::Tagged; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 2 | my %DEFAULT_PATTERNS = ( | |||||||||||||||||||||||||||||||
| 22 | '*' => 'italic', | ||||||||||||||||||||||||||||||||
| 23 | '**' => 'bold', | ||||||||||||||||||||||||||||||||
| 24 | '`' => 'monospace', | ||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | =head2 parse_markdownlike | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | $st = parse_markdownlike( $str ) | ||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | Attempts to parse some markdown-like formatting tags from the input C<$str>, | ||||||||||||||||||||||||||||||||
| 36 | returning a L<String::Tagged::Formatting> instance representing it. | ||||||||||||||||||||||||||||||||
| 37 | In particular, the following markup is recognised | ||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | *italic* | ||||||||||||||||||||||||||||||||
| 40 | **bold** | ||||||||||||||||||||||||||||||||
| 41 | `monospace` | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | This parser does not attempt to be a complete, nor a fully-compatible Markdown | ||||||||||||||||||||||||||||||||
| 44 | parser, but simply tries to provide a useful function for entering formatted | ||||||||||||||||||||||||||||||||
| 45 | messages. | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | sub parse_markdownlike | ||||||||||||||||||||||||||||||||
| 50 | 4 | ▶ | { | ||||||||||||||||||||||||||||||
|
Subroutine
parse_markdownlike: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 51 | 4 | my ( $str, $patterns ) = @_; | |||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 4 | ▶ | $patterns //= \%DEFAULT_PATTERNS; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$patterns //= \%DEFAULT_PATTERNS
MC/DC0%0/1
$patterns //= \%DEFAULT_PATTERNS
$patterns
Truth table50%1/2
A
A= $patterns
| |||||||||||||||||||||||||||||||||
| 54 | 4 | ▶ | keys %$patterns or croak "Cannot parse_markdownlike with no marker patterns"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 4 | my %tags = (); | |||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 12 | my $match_marker = join "|", map { quotemeta } | |||||||||||||||||||||||||||||||
| 59 | 4 | sort { length $b <=> length $a } # longest first so ** wins over * | |||||||||||||||||||||||||||||||
| 60 | keys %$patterns; | ||||||||||||||||||||||||||||||||
| 61 | 4 | $match_marker = qr/$match_marker/; | |||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 4 | my $ret = String::Tagged->new; | |||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | 4 | ▶ | while( length $str ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | 10 | ▶ | if( $str =~ m/(\w?)($match_marker)(\w?)/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 67 | 7 | my $word_before = length $1; | |||||||||||||||||||||||||||||||
| 68 | 7 | my $marker = $2; | |||||||||||||||||||||||||||||||
| 69 | 7 | my $markstart = $-[2]; | |||||||||||||||||||||||||||||||
| 70 | 7 | my $markend = $+[2]; | |||||||||||||||||||||||||||||||
| 71 | 7 | my $word_after = length $3; | |||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | # prefix before marker | ||||||||||||||||||||||||||||||||
| 74 | 7 | ▶ | $ret->append_tagged( substr( $str, 0, $markstart ), %tags ) if $markstart > 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | 7 | ▶ | if( $word_after and !$word_before ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$word_after and not $word_before
MC/DC50%1/2
$word_after and not $word_before
$word_after
not $word_before
Truth table66%2/3
A and not(B)
A= $word_afterB= not $word_before
| |||||||||||||||||||||||||||||||||
| 77 | 3 | $tags{ $patterns->{$marker} } = 1; | |||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||
| 79 | ▶ | elsif( $word_before and !$word_after ) { | |||||||||||||||||||||||||||||||
|
Condition66%2/3
$word_before and not $word_after
MC/DC50%1/2
$word_before and not $word_after
$word_before
not $word_after
Truth table66%2/3
A and not(B)
A= $word_beforeB= not $word_after
| |||||||||||||||||||||||||||||||||
| 80 | 3 | delete $tags{ $patterns->{$marker} }; | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | else { | ||||||||||||||||||||||||||||||||
| 83 | # This isn't actually a tag start/stop so ignore it and continue | ||||||||||||||||||||||||||||||||
| 84 | 1 | $ret->append_tagged( substr( $str, $markstart, $markend - $markstart ), %tags ); | |||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 7 | substr( $str, 0, $markend ) = ""; | |||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | else { | ||||||||||||||||||||||||||||||||
| 90 | 3 | $ret->append_tagged( $str, %tags ); | |||||||||||||||||||||||||||||||
| 91 | 3 | last; | |||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 4 | ▶ | return $ret if $ret->tagnames; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 2 | return $ret->str; | |||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 2 | 0x55AA; | |||||||||||||||||||||||||||||||