blib/lib/Text/Markdown/Slidy.pm

filter: Statementstmt 94.8% 74 / 78 Branchbran 76.9% 20 / 26 Conditioncond 70.5% 12 / 17 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 100% 13 / 13 Podpod 60.0% 3 / 5 totaltotal 85.2% 127 / 149 CC 21 SCAR 16.4
CC
21
Cov
88%
_separate_frontmater
8.8
split_markdown
8.0
markdown
4.0
CRAP 5.1
↑ Summary
1 package Text::Markdown::Slidy;
2 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use YAML::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = "0.04";
9 2 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our @EXPORT = qw/markdown split_markdown/;
12  
13 8 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod documented
14 8 my $class = shift;
15 0 ▶ my %args = @_ == 1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 8 8
16 8 bless {%args}, $class;
17 }
18  
19 16 ▶sub markdown {
Subroutine markdown: called CC=4 SCAR=13.9
Pod documented
20 16 my ($self, $text) = @_;
21  
22 # Detect functional mode, and create an instance for this run
23 16 ▶ unless (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 8 8 16
24 8 ▶ if ( $self ne __PACKAGE__ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self ne __PACKAGE__) { } 8 0 8
25 8 my $ob = __PACKAGE__->new();
26 # $self is text, $text is options
27 8 return $ob->markdown($self, $text);
28 }
29 else {
30 0 croak('Calling ' . $self . '->markdown (as a class method) is not supported.');
31 }
32 }
33 8 my ($body, $meta) = _separate_frontmater($text);
34 8 my @slides = $self->_sections($body);
35 8 my $html = join "\n", @slides;
36 8 ▶ return wantarray ? ($html, $meta) : $html;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 4 8
37 }
38  
39 26 ▶sub template {
Subroutine template: called CC=2 SCAR=6.9
Pod undocumented
40 26 my $self = shift;
41  
42 26 ▶ $self->{template} ||= qq[<div class="slide">\n%s</div>\n];
Condition100%2/2
$self->{"template"} ||= "<div class=\"slide\">\n%s</div>\n"
l!l
188
MC/DC100%1/1
$self->{"template"} ||= "<div class=\"slide\">\n%s</div>\n"
$self->{"template"}
Truth table100%2/2
A
A= $self->{"template"}
Aresult
11
00
43 }
44  
45 8 ▶sub _separate_frontmater {
Subroutine _separate_frontmater: called CC=8 SCAR=21.7
46 8 my $text = shift;
47  
48 8 my $delim = "---\n";
49 8 my $strict_frontmatter;
50 8 my ($raw_header, $body, $body2) = split /^$delim/ms, $text, 3;
51 8 ▶ if ($raw_header eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($raw_header eq '') { } 4 4 8
elsif ($body2) { } 4 0 4
52 4 my $strict_frontmatter = 1;
53 4 ($raw_header, $body) = ($body, $body2);
54 } elsif ($body2) {
55 4 $body = $body . $delim . $body2;
56 }
57 8 my $meta;
58 8 ▶ if (!$body) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $body) { } 0 8 8
59 0 $body = $raw_header;
60 } else {
61 8 eval {
62 8 $meta = YAML::PP::Load($raw_header);
63 };
64 8 ▶ if ($@ || !$meta || ref $meta ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $meta or ref $meta ne "HASH") 2 6 8
Condition50%3/6
$@ or not $meta or ref $meta ne "HASH"
l!l&&r!l&&!r
026
$@ or not $meta
l!l&&r!l&&!r
008
MC/DC33%1/3
$@ or not $meta or ref $meta ne "HASH"
$@ not $meta ref $meta ne "HASH"
Truth table50%2/4
A or not(B) or C
A= $@B= not $metaC= ref $meta ne "HASH"
ABCresult
1XX1
01X1
0011
0000
65 2 ▶ if ($strict_frontmatter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strict_frontmatter) 0 2 2
66 0 die sprintf("invalid frontmatter\n%s", $raw_header);
67 }
68 2 undef $meta;
69 2 $body = $raw_header . $delim . $body;
70 }
71 }
72 8 return ($body, $meta);
73 }
74  
75 26 ▶sub md {
Subroutine md: called CC=2 SCAR=7.0
Pod undocumented
76 26 my $self = shift;
77  
78 26 ▶ $self->{md} ||= do {
Condition66%2/3
$self->{"md"} ||= do { require Text::Markdown; "Text::Markdown"->new }
l!l&&r!l&&!r
1880
MC/DC0%0/2
$self->{"md"} ||= do { require Text::Markdown; "Text::Markdown"->new }
$self->{"md"} do { require Text::Markdown; "Text::Markdown"->new }
Truth table66%2/3
A ||= B
A= $self->{"md"}B= do { require Text::Markdown; "Text::Markdown"->new }
ABresult
1X1
011
000
79 8 require Text::Markdown;
80 8 Text::Markdown->new;
81 };
82 }
83  
84 26 ▶sub _process {
Subroutine _process: called CC=1 SCAR=0.0
85 26 my ($self, $slide_text) = @_;
86  
87 26 my $html = $self->md->markdown($slide_text);
88 26 sprintf $self->template, $html;
89 }
90  
91 8 ▶sub _sections {
Subroutine _sections: called CC=1 SCAR=0.0
92 8 my ($self, $text) = @_;
93  
94 8 map {$self->_process($_)} split_markdown($text);
95 }
96  
97 8 ▶sub split_markdown {
Subroutine split_markdown: called CC=8 SCAR=20.8
Pod documented
98 8 my $text = shift;
99 8 $text =~ s/^\A\s+//ms;
100 8 $text =~ s/\s+\z//ms;
101  
102 8 my @slides;
103 my @slide_lines;
104 8 for my $line (split /\r?\n/, $text) {
105 84 ▶ if ( my ($delim) = $line =~ /^([- ]+?|=+)\s*$/ and @slide_lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($delim) = $line =~ /^([- ]+?|=+)\s*$/ and @slide_lines) 20 64 84
Condition66%2/3
my($delim) = $line =~ /^([- ]+?|=+)\s*$/ and @slide_lines
!ll&&!rl&&r
64020
MC/DC50%1/2
my($delim) = $line =~ /^([- ]+?|=+)\s*$/ and @slide_lines
my($delim) = $line =~ /^([- ]+?|=+)\s*$/ @slide_lines
Truth table66%2/3
A and B
A= my($delim) = $line =~ /^([- ]+?|=+)\s*$/B= @slide_lines
ABresult
0X0
100
111
106 20 ▶ if ($delim =~ / / || !$slide_lines[$#slide_lines]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delim =~ / / or not $slide_lines[$#slide_lines]) 4 16 20
Condition100%3/3
$delim =~ / / or not $slide_lines[$#slide_lines]
l!l&&r!l&&!r
2216
MC/DC100%2/2
$delim =~ / / or not $slide_lines[$#slide_lines]
$delim =~ / / not $slide_lines[$#slide_lines]
Truth table100%3/3
A or not(B)
A= $delim =~ / /B= not $slide_lines[$#slide_lines]
ABresult
1X1
011
000
107 # <hr />
108 # `- - -`
109 # "\n\n----\n"
110 4 push @slides, join("\n", (@slide_lines, $line));
111 4 @slide_lines = ();
112 4 next;
113 }
114 # h1 or h2
115 16 my $prev = pop @slide_lines;
116 16 ▶ push @slides, join("\n", @slide_lines) if @slide_lines;
Branch: 1 branch
BranchTrueFalseTotal
if @slide_lines 14 2 16
117 16 @slide_lines = ($prev); # $prev is title;
118 }
119 80 push @slide_lines, $line;
120 }
121 8 ▶ push @slides, join("\n", @slide_lines) if @slide_lines;
Branch: 1 branch
BranchTrueFalseTotal
if @slide_lines 8 0 8
122  
123 8 @slides;
124 }
125  
126 2 1;
127 __END__
↑ Summary