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
|
|
2414
|
use strict; |
|
5
|
|
|
|
|
41
|
|
|
5
|
|
|
|
|
149
|
|
6
|
5
|
|
|
5
|
|
26
|
use warnings; |
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
136
|
|
7
|
|
|
|
|
|
|
|
8
|
5
|
|
|
5
|
|
2973
|
use Moo; |
|
5
|
|
|
|
|
65581
|
|
|
5
|
|
|
|
|
24
|
|
9
|
|
|
|
|
|
|
|
10
|
5
|
|
|
5
|
|
10510
|
use Text::ASCIITable 0.22; |
|
5
|
|
|
|
|
60101
|
|
|
5
|
|
|
|
|
3297
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = 0.02; |
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
|
286
|
my ($class, $markdown, $options) = @_; |
26
|
|
|
|
|
|
|
|
27
|
3
|
|
100
|
|
|
27
|
$options //= {}; |
28
|
|
|
|
|
|
|
|
29
|
3
|
|
|
|
|
31
|
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
|
|
|
|
|
40
|
my @lines = grep{ $_ =~ m{\A\|[^-]+} } split /\n/, $table; |
|
25
|
|
|
|
|
80
|
|
57
|
|
|
|
|
|
|
|
58
|
5
|
|
|
|
|
16
|
my $headerline = shift @lines; |
59
|
5
|
|
|
|
|
21
|
$headerline =~ s{\A\|\s+}{}; |
60
|
5
|
|
|
|
|
29
|
$headerline =~ s{\s+\|$}{}; |
61
|
5
|
|
|
|
|
23
|
my @cols = split /\s+\|\s+/, $headerline; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my @rows = map{ |
64
|
5
|
|
|
|
|
14
|
$_ =~ s{\A\|\s+}{}; |
|
12
|
|
|
|
|
40
|
|
65
|
12
|
|
|
|
|
46
|
$_ =~ s{\s+\|$}{}; |
66
|
12
|
|
|
|
|
68
|
[ split /\s+\|\s+/, $_ ]; |
67
|
|
|
|
|
|
|
} @lines; |
68
|
|
|
|
|
|
|
|
69
|
5
|
|
|
|
|
67
|
push @tables, $class->new( |
70
|
|
|
|
|
|
|
cols => \@cols, |
71
|
|
|
|
|
|
|
rows => \@rows, |
72
|
|
|
|
|
|
|
); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
3
|
|
|
|
|
56
|
return @tables; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub set_cols { |
79
|
1
|
|
|
1
|
1
|
1927
|
my ($self, @cols) = @_; |
80
|
|
|
|
|
|
|
|
81
|
1
|
|
|
|
|
23
|
$self->_set_cols( \@cols ); |
82
|
1
|
|
|
|
|
17
|
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
|
|
|
|
|
3
|
push @{ $rows }, @new_rows; |
|
1
|
|
|
|
|
3
|
|
90
|
1
|
|
|
|
|
21
|
$self->_set_rows( $rows ); |
91
|
|
|
|
|
|
|
|
92
|
1
|
|
|
|
|
12
|
return $rows; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub get_table { |
96
|
6
|
|
|
6
|
1
|
3720
|
my ($self) = @_; |
97
|
|
|
|
|
|
|
|
98
|
6
|
|
|
|
|
56
|
my $ascii = Text::ASCIITable->new({ |
99
|
|
|
|
|
|
|
hide_LastLine => 1, |
100
|
|
|
|
|
|
|
hide_FirstLine => 1, |
101
|
|
|
|
|
|
|
}); |
102
|
|
|
|
|
|
|
|
103
|
6
|
|
|
|
|
258
|
$ascii->setCols( $self->cols ); |
104
|
6
|
50
|
|
|
|
914
|
for my $row ( @{ $self->rows || [] } ) { |
|
6
|
|
|
|
|
34
|
|
105
|
14
|
|
|
|
|
915
|
$ascii->addRow( @{ $row } ); |
|
14
|
|
|
|
|
39
|
|
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
6
|
|
|
|
|
569
|
return $ascii->draw( undef, undef, [qw/| | - |/] ); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
1; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
__END__ |