line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Markdown::Table; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: Create and parse tables in Markdown |
4
|
|
|
|
|
|
|
|
5
|
6
|
|
|
6
|
|
3439
|
use strict; |
|
6
|
|
|
|
|
45
|
|
|
6
|
|
|
|
|
169
|
|
6
|
6
|
|
|
6
|
|
31
|
use warnings; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
144
|
|
7
|
|
|
|
|
|
|
|
8
|
6
|
|
|
6
|
|
3436
|
use Moo; |
|
6
|
|
|
|
|
79220
|
|
|
6
|
|
|
|
|
28
|
|
9
|
|
|
|
|
|
|
|
10
|
6
|
|
|
6
|
|
12694
|
use Text::ASCIITable 0.22; |
|
6
|
|
|
|
|
71904
|
|
|
6
|
|
|
|
|
4151
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = 0.04; |
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
|
4
|
|
|
4
|
1
|
375
|
my ($class, $markdown, $options) = @_; |
26
|
|
|
|
|
|
|
|
27
|
4
|
|
100
|
|
|
32
|
$options //= {}; |
28
|
|
|
|
|
|
|
|
29
|
4
|
|
|
|
|
51
|
my @found_tables = $markdown =~ m{ |
30
|
|
|
|
|
|
|
( |
31
|
|
|
|
|
|
|
^\|[^\n]+\n |
32
|
|
|
|
|
|
|
^\|(?:-{3,}\|)+\n |
33
|
|
|
|
|
|
|
(?: |
34
|
|
|
|
|
|
|
^\|[^\n]+\n |
35
|
|
|
|
|
|
|
)+ |
36
|
|
|
|
|
|
|
) |
37
|
|
|
|
|
|
|
}xmg; |
38
|
|
|
|
|
|
|
|
39
|
4
|
100
|
|
|
|
19
|
if ( $options->{nuclino} ) { |
40
|
1
|
|
|
|
|
20
|
push @found_tables, $markdown =~ m{ |
41
|
|
|
|
|
|
|
^\+(?:-+\+)+\n |
42
|
|
|
|
|
|
|
( |
43
|
|
|
|
|
|
|
(?: |
44
|
|
|
|
|
|
|
^\|[^\n]+\n |
45
|
|
|
|
|
|
|
(?:^(?:\+(?:-+\+)+)\n)? |
46
|
|
|
|
|
|
|
)+ |
47
|
|
|
|
|
|
|
) |
48
|
|
|
|
|
|
|
^(?:\+(?:-+\+)+) |
49
|
|
|
|
|
|
|
}xmsg; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
4
|
|
|
|
|
10
|
my @tables; |
54
|
|
|
|
|
|
|
|
55
|
4
|
|
|
|
|
14
|
for my $table ( @found_tables ) { |
56
|
6
|
|
|
|
|
54
|
my @lines = grep{ $_ =~ m{\A\|[^-]+} } split /\n/, $table; |
|
31
|
|
|
|
|
104
|
|
57
|
|
|
|
|
|
|
|
58
|
6
|
|
|
|
|
19
|
my $headerline = shift @lines; |
59
|
6
|
|
|
|
|
28
|
$headerline =~ s{\A\|\s+}{}; |
60
|
6
|
|
|
|
|
41
|
$headerline =~ s{\s+\|$}{}; |
61
|
6
|
|
|
|
|
38
|
my @cols = split /\s+\|\s+/, $headerline; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my @rows = map{ |
64
|
6
|
|
|
|
|
16
|
$_ =~ s{\A\|\s+}{}; |
|
16
|
|
|
|
|
56
|
|
65
|
16
|
|
|
|
|
83
|
$_ =~ s{\s+\|$}{}; |
66
|
|
|
|
|
|
|
|
67
|
16
|
100
|
|
|
|
65
|
$_ .= ' ' if '|' eq substr $_, -1; |
68
|
|
|
|
|
|
|
|
69
|
16
|
|
|
|
|
106
|
[ split /\s+\|\s+/, $_ ]; |
70
|
|
|
|
|
|
|
} @lines; |
71
|
|
|
|
|
|
|
|
72
|
6
|
|
|
|
|
79
|
push @tables, $class->new( |
73
|
|
|
|
|
|
|
cols => \@cols, |
74
|
|
|
|
|
|
|
rows => \@rows, |
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
4
|
|
|
|
|
96
|
return @tables; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub set_cols { |
82
|
1
|
|
|
1
|
1
|
1841
|
my ($self, @cols) = @_; |
83
|
|
|
|
|
|
|
|
84
|
1
|
|
|
|
|
23
|
$self->_set_cols( \@cols ); |
85
|
1
|
|
|
|
|
16
|
return $self->cols; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub add_rows { |
89
|
1
|
|
|
1
|
1
|
12
|
my ($self, @new_rows) = @_; |
90
|
|
|
|
|
|
|
|
91
|
1
|
|
50
|
|
|
9
|
my $rows = $self->rows || []; |
92
|
1
|
|
|
|
|
2
|
push @{ $rows }, @new_rows; |
|
1
|
|
|
|
|
3
|
|
93
|
1
|
|
|
|
|
20
|
$self->_set_rows( $rows ); |
94
|
|
|
|
|
|
|
|
95
|
1
|
|
|
|
|
10
|
return $rows; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub get_table { |
99
|
6
|
|
|
6
|
1
|
3475
|
my ($self) = @_; |
100
|
|
|
|
|
|
|
|
101
|
6
|
|
|
|
|
52
|
my $ascii = Text::ASCIITable->new({ |
102
|
|
|
|
|
|
|
hide_LastLine => 1, |
103
|
|
|
|
|
|
|
hide_FirstLine => 1, |
104
|
|
|
|
|
|
|
}); |
105
|
|
|
|
|
|
|
|
106
|
6
|
|
|
|
|
214
|
$ascii->setCols( $self->cols ); |
107
|
6
|
50
|
|
|
|
536
|
for my $row ( @{ $self->rows || [] } ) { |
|
6
|
|
|
|
|
35
|
|
108
|
14
|
|
|
|
|
876
|
$ascii->addRow( @{ $row } ); |
|
14
|
|
|
|
|
43
|
|
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
6
|
|
|
|
|
562
|
return $ascii->draw( undef, undef, [qw/| | - |/] ); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__END__ |