| 1 | package Markdown::Table; | ||||||||||||
| 2 | |||||||||||||
| 3 | # ABSTRACT: Create and parse tables in Markdown | ||||||||||||
| 4 | |||||||||||||
| 5 | 6 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 6 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 6 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 6 | ▶ | use Text::ASCIITable 0.22; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 6 | our $VERSION = 0.04; | |||||||||||
| 13 | |||||||||||||
| 14 | has cols => ( | ||||||||||||
| 15 | is => 'rwp', | ||||||||||||
| 16 | 8 | ▶ | isa => sub { ref $_[0] and 'ARRAY' eq ref $_[0] }, | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||
| 17 | 6 | ); | |||||||||||
| 18 | |||||||||||||
| 19 | has rows => ( | ||||||||||||
| 20 | is => 'rwp', | ||||||||||||
| 21 | 8 | ▶ | isa => sub { ref $_[0] and 'ARRAY' eq ref $_[0] }, | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||
| 22 | 6 | ); | |||||||||||
| 23 | |||||||||||||
| 24 | 4 | ▶ | sub parse { | ||||||||||
|
Subroutine
parse: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||
| 25 | 4 | my ($class, $markdown, $options) = @_; | |||||||||||
| 26 | |||||||||||||
| 27 | 4 | ▶ | $options //= {}; | ||||||||||
|
Condition100%2/2
$options //= {}
MC/DC100%1/1
$options //= {}
$options
Truth table100%2/2
A
A= $options
| |||||||||||||
| 28 | |||||||||||||
| 29 | 4 | my @found_tables = $markdown =~ m{ | |||||||||||
| 30 | ( | ||||||||||||
| 31 | ^\|[^\n]+\n | ||||||||||||
| 32 | ^\|(?:-{3,}\|)+\n | ||||||||||||
| 33 | (?: | ||||||||||||
| 34 | ^\|[^\n]+\n | ||||||||||||
| 35 | )+ | ||||||||||||
| 36 | ) | ||||||||||||
| 37 | }xmg; | ||||||||||||
| 38 | |||||||||||||
| 39 | 4 | ▶ | if ( $options->{nuclino} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 40 | 1 | push @found_tables, $markdown =~ m{ | |||||||||||
| 41 | ^\+(?:-+\+)+\n | ||||||||||||
| 42 | ( | ||||||||||||
| 43 | (?: | ||||||||||||
| 44 | ^\|[^\n]+\n | ||||||||||||
| 45 | (?:^(?:\+(?:-+\+)+)\n)? | ||||||||||||
| 46 | )+ | ||||||||||||
| 47 | ) | ||||||||||||
| 48 | ^(?:\+(?:-+\+)+) | ||||||||||||
| 49 | }xmsg; | ||||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | |||||||||||||
| 53 | 4 | my @tables; | |||||||||||
| 54 | |||||||||||||
| 55 | 4 | for my $table ( @found_tables ) { | |||||||||||
| 56 | 6 | my @lines = grep{ $_ =~ m{\A\|[^-]+} } split /\n/, $table; | |||||||||||
| 57 | |||||||||||||
| 58 | 6 | my $headerline = shift @lines; | |||||||||||
| 59 | 6 | $headerline =~ s{\A\|\s+}{}; | |||||||||||
| 60 | 6 | $headerline =~ s{\s+\|$}{}; | |||||||||||
| 61 | 6 | my @cols = split /\s+\|\s+/, $headerline; | |||||||||||
| 62 | |||||||||||||
| 63 | my @rows = map{ | ||||||||||||
| 64 | 6 | $_ =~ s{\A\|\s+}{}; | |||||||||||
| 65 | 16 | $_ =~ s{\s+\|$}{}; | |||||||||||
| 66 | |||||||||||||
| 67 | 16 | ▶ | $_ .= ' ' if '|' eq substr $_, -1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | |||||||||||||
| 69 | 16 | [ split /\s+\|\s+/, $_ ]; | |||||||||||
| 70 | } @lines; | ||||||||||||
| 71 | |||||||||||||
| 72 | 6 | push @tables, $class->new( | |||||||||||
| 73 | cols => \@cols, | ||||||||||||
| 74 | rows => \@rows, | ||||||||||||
| 75 | ); | ||||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | 4 | return @tables; | |||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | 1 | ▶ | sub set_cols { | ||||||||||
|
Subroutine
set_cols: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 82 | 1 | my ($self, @cols) = @_; | |||||||||||
| 83 | |||||||||||||
| 84 | 1 | $self->_set_cols( \@cols ); | |||||||||||
| 85 | 1 | return $self->cols; | |||||||||||
| 86 | } | ||||||||||||
| 87 | |||||||||||||
| 88 | 1 | ▶ | sub add_rows { | ||||||||||
|
Subroutine
add_rows: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 89 | 1 | my ($self, @new_rows) = @_; | |||||||||||
| 90 | |||||||||||||
| 91 | 1 | ▶ | my $rows = $self->rows || []; | ||||||||||
|
Condition50%1/2
$self->rows || []
MC/DC0%0/1
$self->rows || []
$self->rows
Truth table50%1/2
A
A= $self->rows
| |||||||||||||
| 92 | 1 | push @{ $rows }, @new_rows; | |||||||||||
| 93 | 1 | $self->_set_rows( $rows ); | |||||||||||
| 94 | |||||||||||||
| 95 | 1 | return $rows; | |||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | 6 | ▶ | sub get_table { | ||||||||||
|
Subroutine
get_table: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 99 | 6 | my ($self) = @_; | |||||||||||
| 100 | |||||||||||||
| 101 | 6 | my $ascii = Text::ASCIITable->new({ | |||||||||||
| 102 | hide_LastLine => 1, | ||||||||||||
| 103 | hide_FirstLine => 1, | ||||||||||||
| 104 | }); | ||||||||||||
| 105 | |||||||||||||
| 106 | 6 | $ascii->setCols( $self->cols ); | |||||||||||
| 107 | 6 | ▶ | for my $row ( @{ $self->rows || [] } ) { | ||||||||||
|
Condition50%1/2
$self->rows || []
MC/DC0%0/1
$self->rows || []
$self->rows
Truth table50%1/2
A
A= $self->rows
| |||||||||||||
| 108 | 14 | $ascii->addRow( @{ $row } ); | |||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | 6 | return $ascii->draw( undef, undef, [qw/| | - |/] ); | |||||||||||
| 112 | } | ||||||||||||
| 113 | |||||||||||||
| 114 | |||||||||||||
| 115 | 6 | 1; | |||||||||||
| 116 | |||||||||||||
| 117 | __END__ | ||||||||||||