Muster/Hook/Links.pm

filter: Statementstmt 21.7% 23 / 106 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 46.1% 6 / 13 Podpod 100% 6 / 6 totaltotal 18.1% 35 / 193 CC 32 SCAR 48.3
CC
32
Cov
14%
htmllink
210.0
process
182.0
externallink
30.0
CRAP 124.8
« Muster/Hook/MindMap.pm ↑ Summary Muster/Hook/Table.pm »
1 package Muster::Hook::Links;
2 5 $Muster::Hook::Links::VERSION = '0.96';
3 =head1 NAME
4  
5 Muster::Hook::Links - Muster hook for links
6  
7 =head1 VERSION
8  
9 version 0.96
10  
11 =head1 SYNOPSIS
12  
13 # CamelCase plugin name
14 package Muster::Hook::Links;
15 use Mojo::Base 'Muster::Hook';
16  
17 =head1 DESCRIPTION
18  
19 L<Muster::Hook::Links> processes for links.
20  
21 =cut
22  
23 5 ▶use Mojo::Base 'Muster::Hook';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use Muster::LeafFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use Muster::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use YAML::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # ---------------------------------------------
31 # Class Variables
32  
33 # taken from IkiWiki code
34 5 my $Link_Regexp = qr{
35 \[\[(?=[^!]) # beginning of link
36 (?:
37 ([^\]\|]+) # 1: link text
38 \| # followed by '|'
39 )? # optional
40
41 ([^\n\r\]#]+) # 2: page to link to
42 (?:
43 \# # '#', beginning of anchor
44 ([^\s\]]+) # 3: anchor text
45 )? # optional
46
47 \]\] # end of link
48 }x;
49  
50 5 my $Email_Regexp = qr/^.+@.+\..+$/;
51 5 my $Url_Regexp = qr/^(?:[^:]+:\/\/|mailto:).*/i;
52  
53 =head1 METHODS
54  
55 =head2 register
56  
57 Initialize, and register hooks.
58  
59 =cut
60 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
Pod documented
61 0 my $self = shift;
62 0 my $hookmaster = shift;
63 0 my $conf = shift;
64  
65 # we need to be able to look things up in the database
66 0 $self->{metadb} = $hookmaster->{metadb};
67  
68 0 ▶ $hookmaster->add_hook('links' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 0 my %args = @_;
70  
71 0 return $self->process(%args);
72 },
73 0 );
74 0 return $self;
75 } # register
76  
77 =head2 process
78  
79 Process (scan or modify) a leaf object.
80 In scanning phase, it may update the meta-data,
81 in modify phase, it may update the content.
82 May leave the leaf untouched.
83  
84 my $new_leaf = $self->process(%args);
85  
86 =cut
87 0 ▶sub process {
Subroutine process: not called CC=13 SCAR=52.0
Pod documented
88 0 my $self = shift;
89 0 my %args = @_;
90  
91 0 my $leaf = $args{leaf};
92 0 my $phase = $args{phase};
93  
94 0 my $content = $leaf->cooked();
95 0 my $page = $leaf->pagename;
96  
97 0 ▶ if ($phase eq $Muster::Hooks::PHASE_SCAN)
Branch: 1 branch
BranchTrueFalseTotal
if ($phase eq $Muster::Hooks::PHASE_SCAN) { } 0 0 0
98 {
99 0 ▶ if (!$leaf->is_binary)
Branch: 1 branch
BranchTrueFalseTotal
unless ($leaf->is_binary) 0 0 0
100 {
101 0 my %links = ();
102  
103 0 ▶ while ($content =~ /(?<!\\)$Link_Regexp/g)
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /(?<!\\)$Link_Regexp/gu) 0 0 0
104 {
105 0 my $link = $2;
106 0 my $anchor = $3;
107 0 ▶ if (! $self->is_externallink($page, $link, $anchor)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_externallink($page, $link, $anchor)) 0 0 0
108 0 $links{$link}++;
109 }
110 }
111 0 my @links = sort keys %links;
112 0 ▶ if (scalar @links)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @links) 0 0 0
113 {
114 0 $leaf->{meta}->{links} = \@links;
115 }
116 }
117 }
118 else
119 {
120 0 $content =~ s{(\\?)$Link_Regexp}{
121 0 ▶ defined $2
Branch: 7 branches
BranchTrueFalseTotal
defined $2 ? : 0 0 0
$1 ? : 0 0 0
defined $4 ? : 0 0 0
$self->is_externallink($page, $3, $4) ? : 0 0 0
$1 ? : 0 0 0
defined $4 ? : 0 0 0
$self->is_externallink($page, $3, $4) ? : 0 0 0
122 ? ( $1
123 ? "[[$2|$3".(defined $4 ? "#$4" : "")."]]"
124 : $self->is_externallink($page, $3, $4)
125 ? $self->externallink($3, $4, $2)
126 : $self->htmllink($page, $self->linkpage($3),
127 anchor => $4, linktext => $2))
128 : ( $1
129 ? "[[$3".(defined $4 ? "#$4" : "")."]]"
130 : $self->is_externallink($page, $3, $4)
131 ? $self->externallink($3, $4)
132 : $self->htmllink($page, $self->linkpage($3),
133 anchor => $4))
134 }eg;
135 0 $leaf->{cooked} = $content;
136 }
137 0 return $leaf;
138 } # process
139  
140 =head2 is_externallink
141  
142 Is this an external link?
143  
144 =cut
145 0 ▶sub is_externallink {
Subroutine is_externallink: not called CC=2 SCAR=17.9
Pod documented
146 0 my $self = shift;
147 0 my $page = shift;
148 0 my $url = shift;
149 0 my $anchor = shift;
150  
151 0 ▶ if (defined $anchor) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $anchor) 0 0 0
152 0 $url.="#".$anchor;
153 }
154  
155 0 return ($url =~ /$Url_Regexp|$Email_Regexp/)
156 }
157  
158 =head2 linkpage
159  
160 Convert the link into a page-name.
161  
162 =cut
163 0 ▶sub linkpage {
Subroutine linkpage: not called CC=2 SCAR=17.9
Pod documented
164 0 my $self = shift;
165 0 my $link=shift;
166 #my $chars = defined $config{wiki_file_chars} ? $config{wiki_file_chars} : "-[:alnum:]+/.:_";
167 0 my $chars = "-[:alnum:]+/.:_";
168 0 ▶ $link=~s/([^$chars])/$1 eq ' ' ? '_' : "__".ord($1)."__"/eg;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq ' ' ? : 0 0 0
169 0 return $link;
170 }
171  
172 =head2 externallink
173  
174 Process an external link.
175  
176 =cut
177 0 ▶sub externallink {
Subroutine externallink: not called CC=5 SCAR=34.0
Pod documented
178 0 my $self = shift;
179 0 my $url = shift;
180 0 my $anchor = shift;
181 0 my $pagetitle = shift;
182  
183 0 ▶ if (defined $anchor) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $anchor) 0 0 0
184 0 $url.="#".$anchor;
185 }
186  
187 # build pagetitle
188 0 ▶ if (! $pagetitle) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pagetitle) 0 0 0
189 0 $pagetitle = $url;
190 # use only the email address as title for mailto: urls
191 0 ▶ if ($pagetitle =~ /^mailto:.*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pagetitle =~ /^mailto:.*/u) 0 0 0
192 0 $pagetitle =~ s/^mailto:([^?]+).*/$1/;
193 }
194 }
195  
196 0 ▶ if ($url !~ /$Url_Regexp/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url =~ /$Url_Regexp/u) 0 0 0
197 # handle email addresses (without mailto:)
198 0 $url = "mailto:" . $url;
199 }
200  
201 0 return "<a href=\"$url\">$pagetitle</a>";
202 }
203  
204 =head2 htmllink
205  
206 Generate the HTML for a link.
207  
208 =cut
209 0 ▶sub htmllink {
Subroutine htmllink: not called CC=14 SCAR=53.5
Pod documented
210 0 my $self = shift;
211 0 my $page=shift;
212 0 my $link=shift;
213 0 my %opts=@_;
214  
215 0 $link=~s/\/$//;
216  
217 0 my $bestlink;
218 0 ▶ if (! $opts{forcesubpage})
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts{'forcesubpage'}) { } 0 0 0
219 {
220 0 $bestlink=$self->{metadb}->bestlink($page, $link);
221 }
222 else
223 {
224 0 $bestlink="$page/".lc($link);
225 }
226 # assert: $bestlink contains the page to link to
227 0 my $page_exists = $self->{metadb}->page_exists($bestlink);
228 0 ▶ my $bl_info = $page_exists ? $self->{metadb}->page_or_file_info($bestlink) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$page_exists ? : 0 0 0
229  
230 0 my $linktext;
231 0 ▶ if (defined $opts{linktext})
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'linktext'}) { } 0 0 0
elsif ($page_exists) { } 0 0 0
232 {
233 0 $linktext=$opts{linktext};
234 }
235 elsif ($page_exists)
236 {
237 # use the actual page title
238 0 $linktext=$bl_info->{title};
239 }
240 else
241 {
242 0 $linktext=basename($link);
243 }
244  
245 0 ▶ if (length $bestlink
Branch: 1 branch
BranchTrueFalseTotal
if (length $bestlink and $page eq $bestlink and not defined $opts{"anchor"}) 0 0 0
Condition0%0/6
length $bestlink and $page eq $bestlink and not defined $opts{"anchor"}
!ll&&!rl&&r
000
length $bestlink and $page eq $bestlink
!ll&&!rl&&r
000
MC/DC0%0/3
length $bestlink and $page eq $bestlink and not defined $opts{"anchor"}
length $bestlink $page eq $bestlink not defined $opts{"anchor"}
Truth table0%0/4
A and B and not(C)
A= length $bestlinkB= $page eq $bestlinkC= not defined $opts{"anchor"}
ABCresult
0XX0
10X0
1100
1111
246 && $page eq $bestlink
247 && ! defined $opts{anchor})
248 {
249 0 return "<span class=\"selflink\">$linktext</span>";
250 }
251  
252 0 ▶ if (!$page_exists or !$bestlink)
Branch: 1 branch
BranchTrueFalseTotal
unless ($page_exists and $bestlink) 0 0 0
Condition0%0/3
$page_exists and $bestlink
!ll&&!rl&&r
000
MC/DC0%0/2
$page_exists and $bestlink
$page_exists $bestlink
Truth table0%0/3
A and B
A= $page_existsB= $bestlink
ABresult
0X0
100
111
253 {
254 0 return "<a class=\"createlink\" href=\"$link\">$linktext ?</a>";
255 }
256
257 0 ▶ $bestlink=File::Spec->abs2rel($bestlink, $page) if $page ne 'index';
Branch: 1 branch
BranchTrueFalseTotal
if $page ne "index" 0 0 0
258  
259 0 ▶ if (defined $opts{anchor}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"anchor"}) 0 0 0
260 0 $bestlink.="#".$opts{anchor};
261 }
262  
263 0 my @attrs;
264 0 foreach my $attr (qw{rel class title}) {
265 0 ▶ if (defined $opts{$attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{$attr}) 0 0 0
266 0 push @attrs, " $attr=\"$opts{$attr}\"";
267 }
268 }
269  
270 0 return "<a href=\"$bestlink\"@attrs>$linktext</a>";
271 }
272  
273 5 1;
« Muster/Hook/MindMap.pm ↑ Summary Muster/Hook/Table.pm »