Wiki/Node.pm

filter: Statementstmt 100% 79 / 79 Branchbran 100% 24 / 24 Conditioncond 76.9% 10 / 13 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 16 / 16 Podpod 77.7% 7 / 9 totaltotal 94.5% 140 / 148 CC 19 SCAR 14.9
CC
19
Cov
97%
new
9.0
interlink
6.0
_init
3.0
CRAP 4.4
« Wiki/Txt.pm ↑ Summary Wiki/Node/Head.pm »
1 #############################################################################
2 # (c) by Tels 2004. Part of Convert::Wiki
3 #
4 #############################################################################
5  
6 package Convert::Wiki::Node;
7  
8 8 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 $VERSION = '0.04';
15  
16 #############################################################################
17  
18 sub new
19 65 ▶ {
Subroutine new: called CC=9 SCAR=22.0
Pod undocumented
20 65 my $class = shift;
21  
22 65 my $args = $_[0];
23 65 ▶ $args = { @_ } if ref($args) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args ne "HASH" 20 45 65
24
25 65 my $self = bless {}, $class;
26  
27 # XXX TODO check arguments
28
29 65 ▶ if (defined $args->{type})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"type"}) 54 11 65
30 {
31 54 my $type = ucfirst($args->{type});
32 54 ▶ $type = 'Para' if $type eq 'Paragraph';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "Paragraph" 1 53 54
33  
34 54 ▶ if ($type =~ /(\d)\z/)
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /(\d)\z/) 15 39 54
35 {
36 # convert XX9 => XX (for Head1 etc)
37 15 ▶ $args->{level} = abs($1 || 1);
Condition50%1/2
$1 || 1
l!l
150
MC/DC0%0/1
$1 || 1
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
38 15 $type =~ s/\d\z//;
39 }
40  
41 54 ▶ $self->error('Node type must be one of Head, Item, Mono, Line or Para but is \'' . $type . "'") and return $self
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^(Head|Item|Line|Mono|Para)\z/ 1 53 54
Condition60%3/5
$type =~ /^(Head|Item|Line|Mono|Para)\z/ || $self->error(q[Node type must be one of Head, Item, Mono, Line or Para but is '] . $type . q[']) && (return $self)
l!l
5310
$self->error(q[Node type must be one of Head, Item, Mono, Line or Para but is '] . $type . q[']) and return $self
!ll
01
MC/DC0%0/2
$type =~ /^(Head|Item|Line|Mono|Para)\z/ || $self->error(q[Node type must be one of Head, Item, Mono, Line or Para but is '] . $type . q[']) && (return $self)
$type =~ /^(Head|Item|Line|Mono|Para)\z/ $self->error(q[Node type must be one of Head, Item, Mono, Line or Para but is '] . $type . q['])
Truth table66%2/3
A || B
A= $type =~ /^(Head|Item|Line|Mono|Para)\z/B= $self->error(q[Node type must be one of Head, Item, Mono, Line or Para but is '] . $type . q['])
ABresult
1X1
011
000
42 unless $type =~ /^(Head|Item|Line|Mono|Para)\z/;
43  
44 53 $class .= '::' . $type;
45 53 $self = bless $self, $class; # rebless
46 }
47  
48 64 ▶ if ($class ne __PACKAGE__)
Branch: 1 branch
BranchTrueFalseTotal
if ($class ne __PACKAGE__) 63 1 64
49 {
50 63 my $pm = $class; $pm =~ s/::/\//g; # :: => /
51 63 $pm .= '.pm';
52 63 require $pm; # XXX not very portable I am afraid
53 }
54  
55 64 $self->_init($args);
56 }
57  
58 sub _init
59 64 ▶ {
Subroutine _init: called CC=3 SCAR=11.0
60 # generic init, override in subclasses
61 64 my ($self,$args) = @_;
62  
63 64 foreach my $k (keys %$args)
64 {
65 167 $self->{$k} = $args->{$k};
66 }
67
68 64 $self->{error} = '';
69 64 ▶ $self->{txt} = '' unless defined $self->{txt};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"txt"} 10 54 64
70  
71 64 $self->{txt} =~ s/\n+\z//; # remove trailing newline
72 64 $self->{txt} =~ s/^\n+//; # remove newlines at start
73
74 64 $self->{prev} = undef;
75 64 $self->{next} = undef;
76  
77 64 $self;
78 }
79  
80 sub _as_wiki
81 3 ▶ {
Subroutine _as_wiki: called CC=1 SCAR=0.0
82 3 my ($self,$txt) = @_;
83  
84 3 $txt;
85 }
86  
87 sub as_wiki
88 55 ▶ {
Subroutine as_wiki: called CC=1 SCAR=0.0
Pod documented
89 55 my ($self, $wiki) = @_;
90  
91 55 $self->_as_wiki( $self->interlink($wiki) );
92 }
93  
94 sub interlink
95 48 ▶ {
Subroutine interlink: called CC=6 SCAR=17.9
Pod undocumented
96 # turn text in pragraph into links
97 48 my ($self, $wiki) = @_;
98  
99 48 my $txt = $self->{txt};
100 # for all phrases, find them case-insensitive, then link them
101 48 for my $link (@{$wiki->{interlink}})
102 {
103 # turn "Foo" into "Foo|Foo"
104 64 ▶ $link .= '|' . $link unless $link =~ /\|/;
Branch: 1 branch
BranchTrueFalseTotal
unless $link =~ /\|/ 5 59 64
105 # split "Foobar|Foo" into "Foobar", "Foo"
106 64 my ($target, $phrase) = split /\|/, $link;
107  
108 64 my $p = quotemeta(lc($phrase));
109  
110 64 ▶ if ($target =~ /^[a-z]+:/)
Branch: 1 branch
BranchTrueFalseTotal
if ($target =~ /^[a-z]+:/) { } 8 56 64
111 {
112 8 $txt =~ s/([^a-z])($p)([^a-z]|$)/${1}[$target ${2}]$3/i;
113  
114 }
115 else
116 {
117 # no /g, since we want to interlink the phrase only once per paragraph
118 # XXX TODO: this will turn "foo" into [[foo[[bar]]]] when searching
119 # for bar after "foobar|foo"
120 56 ▶ $txt =~ s/([^a-z]|^)($p)([^a-z]|$)/ "${1}[[$target" . ( ($2 eq $phrase && $2 eq $target) ? '' : "|$2") . "]]$3"/ie;
Branch: 1 branch
BranchTrueFalseTotal
$2 eq $phrase && $2 eq $target ? : 3 4 7
Condition100%3/3
$2 eq $phrase and $2 eq $target
!ll&&!rl&&r
313
MC/DC100%2/2
$2 eq $phrase and $2 eq $target
$2 eq $phrase $2 eq $target
Truth table100%3/3
A and B
A= $2 eq $phraseB= $2 eq $target
ABresult
0X0
100
111
121 }
122 }
123 48 $txt;
124 }
125  
126 sub error
127 10 ▶ {
Subroutine error: called CC=2 SCAR=6.9
Pod documented
128 10 my $self = shift;
129  
130 10 ▶ $self->{error} = $_[0] if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 3 7 10
131 10 $self->{error};
132 }
133  
134 sub type
135 18 ▶ {
Subroutine type: called CC=1 SCAR=0.0
Pod documented
136 18 my $self = shift;
137  
138 # XXX head1 => head
139 18 my $type = ref($self); $type =~ s/.*:://; # only last part
140 18 lc($type); # head, para, node etc
141 }
142  
143 sub prev_by_type
144 22 ▶ {
Subroutine prev_by_type: called CC=3 SCAR=11.0
Pod documented
145 # find a previous node with a certain type
146 22 my ($self,$type) = @_;
147  
148 22 my $prev = $self->{prev};
149  
150 # print "Looking for '$type'\n";
151 # print "# At $prev $prev->{type}\n" if defined $prev;
152  
153 22 ▶ while (defined $prev && $prev->{type} !~ /$type/)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev and not $prev->{"type"} =~ /$type/) 12 22 34
Condition100%3/3
defined $prev and not $prev->{"type"} =~ /$type/
!ll&&!rl&&r
13912
MC/DC100%2/2
defined $prev and not $prev->{"type"} =~ /$type/
defined $prev not $prev->{"type"} =~ /$type/
Truth table100%3/3
A and not(B)
A= defined $prevB= not $prev->{"type"} =~ /$type/
ABresult
0X0
100
111
154 {
155 12 $prev = $prev->{prev};
156 # print "# At $prev $prev->{type}\n" if defined $prev;
157 }
158 # found something, or hit the first node (aka undef)
159 22 $prev;
160 }
161  
162 sub prev
163 21 ▶ {
Subroutine prev: called CC=1 SCAR=0.0
Pod documented
164 21 my $self = shift;
165  
166 $self->{prev}
167 21 }
168  
169 sub next
170 6 ▶ {
Subroutine next: called CC=1 SCAR=0.0
Pod documented
171 6 my $self = shift;
172  
173 6 $self->{next};
174 }
175  
176 sub link
177 40 ▶ {
Subroutine link: called CC=1 SCAR=0.0
Pod documented
178 40 my $self = shift;
179  
180 40 $self->{next} = $_[0];
181 40 $self->{next}->{prev} = $self;
182  
183 40 $self;
184 }
185  
186 sub _remove_me
187 43 ▶ {
Subroutine _remove_me: called CC=1 SCAR=0.0
188 43 0;
189 }
190  
191 8 1;
192 __END__
« Wiki/Txt.pm ↑ Summary Wiki/Node/Head.pm »