blib/lib/Text/Xatena.pm

filter: Statementstmt 98.7% 81 / 82 Branchbran 86.3% 19 / 22 Conditioncond 72.7% 8 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 15 / 15 Podpod 33.3% 1 / 3 totaltotal 89.2% 125 / 140 CC 17 SCAR 12.3
CC
17
Cov
94%
new
7.1
_parse
4.0
__ANON__
3.0
CRAP 3.4
« blib/lib/Text/Xatena/Inline.pm ↑ Summary lib/Text/Xatena/Node/StopP.pm »
1 package Text::Xatena;
2  
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 20 ▶use Text::Xatena::LineScanner;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use Text::Xatena::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use Text::Xatena::Node::Root;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use Text::Xatena::Inline;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 20 ▶use Text::Xatena::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 20 our $VERSION = '0.18';
14  
15 20 our $SYNTAXES = [
16 'Text::Xatena::Node::SeeMore',
17 'Text::Xatena::Node::SuperPre',
18 'Text::Xatena::Node::StopP',
19 'Text::Xatena::Node::Blockquote',
20 'Text::Xatena::Node::Pre',
21 'Text::Xatena::Node::List',
22 'Text::Xatena::Node::DefinitionList',
23 'Text::Xatena::Node::Table',
24 'Text::Xatena::Node::Section',
25 'Text::Xatena::Node::Comment',
26 ];
27  
28 124 ▶sub new {
Subroutine new: called CC=7 SCAR=19.7
Pod undocumented
29 124 my ($class, %opts) = @_;
30  
31 124 my $self = bless { %opts }, $class;
32  
33 124 ▶ $self->{templates} ||= {};
Condition100%2/2
$self->{"templates"} ||= {}
l!l
1123
MC/DC100%1/1
$self->{"templates"} ||= {}
$self->{"templates"}
Truth table100%2/2
A
A= $self->{"templates"}
Aresult
11
00
34 $self->{templates} = {
35 map {
36 2 ▶ my $pkg = $_ =~ /::/ ? $_ : "Text::Xatena::Node::$_";
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /::/ ? : 0 2 2
37 2 $pkg => $self->{templates}->{$_};
38 }
39 124 keys %{ $self->{templates} }
40 };
41  
42 $self->{syntaxes} = [
43 map {
44 1221 ▶ my $pkg = $_ =~ /::/ ? $_ : "Text::Xatena::Node::$_";
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /::/ ? : 1220 1 1221
45 1221 ▶ $pkg->use or die $@;
Branch: 1 branch
BranchTrueFalseTotal
$pkg->use or die $@ 1221 0 1221
46 1221 $pkg;
47 }
48 124 ▶ @{ $opts{syntaxes} || $SYNTAXES }
Condition66%2/3
$opts{'syntaxes'} || $Text::Xatena::SYNTAXES
l!l&&r!l&&!r
21220
MC/DC0%0/2
$opts{'syntaxes'} || $Text::Xatena::SYNTAXES
$opts{'syntaxes'} $Text::Xatena::SYNTAXES
Truth table66%2/3
A || B
A= $opts{'syntaxes'}B= $Text::Xatena::SYNTAXES
ABresult
1X1
011
000
49 ];
50  
51 124 ▶ $self->{inline} ||= Text::Xatena::Inline->new;
Condition66%2/3
$self->{"inline"} ||= "Text::Xatena::Inline"->new
l!l&&r!l&&!r
221020
MC/DC0%0/2
$self->{"inline"} ||= "Text::Xatena::Inline"->new
$self->{"inline"} "Text::Xatena::Inline"->new
Truth table66%2/3
A ||= B
A= $self->{"inline"}B= "Text::Xatena::Inline"->new
ABresult
1X1
011
000
52  
53 124 $self;
54 }
55  
56 128 ▶sub format {
Subroutine format: called CC=3 SCAR=11.0
Pod undocumented
57 128 my ($self, $string, %opts) = @_;
58 128 $string =~ s{\r\n?|\n}{\n}g;
59  
60 128 ▶ $self->inline($opts{inline}) if $opts{inline};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"inline"} 1 127 128
61  
62 128 ▶ if ($self->{hatena_compatible}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'hatena_compatible'}) { } 9 119 128
63 9 $self->{templates}->{'Text::Xatena::Node::Section'} = q[
64 <h{{= $level + 2 }}>{{= $title }}</h{{= $level + 2 }}>
65 {{= $content }}
66 ];
67  
68 20 ▶ no warnings "once", "redefine";
Subroutine BEGIN: called CC=1 SCAR=0.0
69 14 ▶ local *Text::Xatena::Node::as_html_paragraph = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
70 14 my ($self, $context, $text, %opts) = @_;
71 14 $text = $context->inline->format($text, %opts);
72  
73 14 $text =~ s{\n$}{}g;
74 14 ▶ if ($opts{stopp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'stopp'}) { } 0 14 14
75 0 $text;
76 } else {
77 "<p>" . join("",
78 map {
79 14 ▶ if (/^(\n+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\n+)$/) { } 6 20 26
80 6 "</p>" . ("<br />\n" x (length($1) - 2)) . "<p>";
81 } else {
82 20 $_;
83 }
84 }
85 split(/(\n+)/, $text)
86 ) . "</p>\n";
87 }
88 9 };
89  
90 9 $self->_parse($string)->as_html($self);
91 } else {
92 119 $self->_parse($string)->as_html($self);
93 }
94 }
95  
96 283 ▶sub inline {
Subroutine inline: called CC=2 SCAR=6.9
Pod documented
97 283 my ($self, $new) = @_;
98 283 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 1 282 283
99 1 $self->{inline} = $new;
100 } else {
101 282 $self->{inline};
102 }
103 }
104  
105 141 ▶sub _parse {
Subroutine _parse: called CC=4 SCAR=13.9
106 141 my ($self, $string) = @_;
107  
108 141 my @syntaxes = @{ $self->{syntaxes} };
109 141 my $s = Text::Xatena::LineScanner->new($string);
110 141 my $root = Text::Xatena::Node::Root->new ;
111 141 my $stack = [ $root ];
112 141 ▶ loop: until ($s->eos) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s->eos) 429 141 570
113 429 my $parent = $stack->[-1];
114  
115 429 for my $pkg (@syntaxes) {
116 3599 ▶ $pkg->parse($s, $parent, $stack) and next loop;
Branch: 1 branch
BranchTrueFalseTotal
$pkg->parse($s, $parent, $stack) and next loop 155 3444 3599
117 }
118  
119 # plain lines
120 274 push @$parent, $s->next;
121 }
122  
123 141 $root;
124 }
125  
126 109 ▶sub _tmpl {
Subroutine _tmpl: called CC=3 SCAR=11.0
127 109 my ($self, $pkg, $default, $stash) = @_;
128 109 my $tmpl = $self->{templates}->{$pkg};
129 109 ▶ my $sub = ref($tmpl) eq 'CODE' ? $tmpl : template($tmpl || $default, [ keys %$stash ]);
Branch: 1 branch
BranchTrueFalseTotal
ref $tmpl eq 'CODE' ? : 30 79 109
Condition66%2/3
$tmpl || $default
l!l&&r!l&&!r
5740
MC/DC0%0/2
$tmpl || $default
$tmpl $default
Truth table66%2/3
A || B
A= $tmplB= $default
ABresult
1X1
011
000
130 109 $self->{templates}->{$pkg} = $sub;
131 109 $sub->($stash);
132 }
133  
134 20 1;
135 __END__
« blib/lib/Text/Xatena/Inline.pm ↑ Summary lib/Text/Xatena/Node/StopP.pm »