line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Markdown::Table; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: Create and parse tables in Markdown |
4
|
|
|
|
|
|
|
|
5
|
5
|
|
|
5
|
|
2303
|
use strict; |
|
5
|
|
|
|
|
36
|
|
|
5
|
|
|
|
|
147
|
|
6
|
5
|
|
|
5
|
|
26
|
use warnings; |
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
120
|
|
7
|
|
|
|
|
|
|
|
8
|
5
|
|
|
5
|
|
2753
|
use Moo; |
|
5
|
|
|
|
|
65176
|
|
|
5
|
|
|
|
|
24
|
|
9
|
|
|
|
|
|
|
|
10
|
5
|
|
|
5
|
|
10464
|
use Text::ASCIITable 0.22; |
|
5
|
|
|
|
|
59236
|
|
|
5
|
|
|
|
|
3369
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = 0.03; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has cols => ( |
15
|
|
|
|
|
|
|
is => 'rwp', |
16
|
|
|
|
|
|
|
isa => sub { ref $_[0] and 'ARRAY' eq ref $_[0] }, |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has rows => ( |
20
|
|
|
|
|
|
|
is => 'rwp', |
21
|
|
|
|
|
|
|
isa => sub { ref $_[0] and 'ARRAY' eq ref $_[0] }, |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub parse { |
25
|
3
|
|
|
3
|
1
|
318
|
my ($class, $markdown, $options) = @_; |
26
|
|
|
|
|
|
|
|
27
|
3
|
|
100
|
|
|
25
|
$options //= {}; |
28
|
|
|
|
|
|
|
|
29
|
3
|
|
|
|
|
33
|
my @found_tables = $markdown =~ m{ |
30
|
|
|
|
|
|
|
( |
31
|
|
|
|
|
|
|
^\|[^\n]+\n |
32
|
|
|
|
|
|
|
^\|(?:-{3,}\|)+\n |
33
|
|
|
|
|
|
|
(?: |
34
|
|
|
|
|
|
|
^\|[^\n]+\n |
35
|
|
|
|
|
|
|
)+ |
36
|
|
|
|
|
|
|
) |
37
|
|
|
|
|
|
|
}xmg; |
38
|
|
|
|
|
|
|
|
39
|
3
|
100
|
|
|
|
14
|
if ( $options->{nuclino} ) { |
40
|
1
|
|
|
|
|
22
|
push @found_tables, $markdown =~ m{ |
41
|
|
|
|
|
|
|
^\+(?:-+\+)+\n |
42
|
|
|
|
|
|
|
( |
43
|
|
|
|
|
|
|
(?: |
44
|
|
|
|
|
|
|
^\|[^\n]+\n |
45
|
|
|
|
|
|
|
(?:^(?:\+(?:-+\+)+)\n)? |
46
|
|
|
|
|
|
|
)+ |
47
|
|
|
|
|
|
|
) |
48
|
|
|
|
|
|
|
^(?:\+(?:-+\+)+) |
49
|
|
|
|
|
|
|
}xmsg; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
3
|
|
|
|
|
7
|
my @tables; |
54
|
|
|
|
|
|
|
|
55
|
3
|
|
|
|
|
11
|
for my $table ( @found_tables ) { |
56
|
5
|
|
|
|
|
44
|
my @lines = grep{ $_ =~ m{\A\|[^-]+} } split /\n/, $table; |
|
25
|
|
|
|
|
86
|
|
57
|
|
|
|
|
|
|
|
58
|
5
|
|
|
|
|
18
|
my $headerline = shift @lines; |
59
|
5
|
|
|
|
|
22
|
$headerline =~ s{\A\|\s+}{}; |
60
|
5
|
|
|
|
|
30
|
$headerline =~ s{\s+\|$}{}; |
61
|
5
|
|
|
|
|
29
|
my @cols = split /\s+\|\s+/, $headerline; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my @rows = map{ |
64
|
5
|
|
|
|
|
14
|
$_ =~ s{\A\|\s+}{}; |
|
12
|
|
|
|
|
41
|
|
65
|
12
|
|
|
|
|
53
|
$_ =~ s{\s+\|$}{}; |
66
|
12
|
|
|
|
|
75
|
[ split /\s+\|\s+/, $_ ]; |
67
|
|
|
|
|
|
|
} @lines; |
68
|
|
|
|
|
|
|
|
69
|
5
|
|
|
|
|
77
|
push @tables, $class->new( |
70
|
|
|
|
|
|
|
cols => \@cols, |
71
|
|
|
|
|
|
|
rows => \@rows, |
72
|
|
|
|
|
|
|
); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
3
|
|
|
|
|
57
|
return @tables; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub set_cols { |
79
|
1
|
|
|
1
|
1
|
1784
|
my ($self, @cols) = @_; |
80
|
|
|
|
|
|
|
|
81
|
1
|
|
|
|
|
25
|
$self->_set_cols( \@cols ); |
82
|
1
|
|
|
|
|
15
|
return $self->cols; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub add_rows { |
86
|
1
|
|
|
1
|
1
|
11
|
my ($self, @new_rows) = @_; |
87
|
|
|
|
|
|
|
|
88
|
1
|
|
50
|
|
|
9
|
my $rows = $self->rows || []; |
89
|
1
|
|
|
|
|
2
|
push @{ $rows }, @new_rows; |
|
1
|
|
|
|
|
3
|
|
90
|
1
|
|
|
|
|
21
|
$self->_set_rows( $rows ); |
91
|
|
|
|
|
|
|
|
92
|
1
|
|
|
|
|
9
|
return $rows; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub get_table { |
96
|
6
|
|
|
6
|
1
|
3753
|
my ($self) = @_; |
97
|
|
|
|
|
|
|
|
98
|
6
|
|
|
|
|
63
|
my $ascii = Text::ASCIITable->new({ |
99
|
|
|
|
|
|
|
hide_LastLine => 1, |
100
|
|
|
|
|
|
|
hide_FirstLine => 1, |
101
|
|
|
|
|
|
|
}); |
102
|
|
|
|
|
|
|
|
103
|
6
|
|
|
|
|
259
|
$ascii->setCols( $self->cols ); |
104
|
6
|
50
|
|
|
|
583
|
for my $row ( @{ $self->rows || [] } ) { |
|
6
|
|
|
|
|
36
|
|
105
|
14
|
|
|
|
|
914
|
$ascii->addRow( @{ $row } ); |
|
14
|
|
|
|
|
40
|
|
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
6
|
|
|
|
|
584
|
return $ascii->draw( undef, undef, [qw/| | - |/] ); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
1; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
__END__ |