.htmlgen/FunctionalPerl/Htmlgen/Linking.pm

filter: Statementstmt 54.1% 79 / 146 Branchbran 2.5% 1 / 40 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 75.7% 25 / 33 Podpod n/a totaltotal 41.1% 105 / 255 CC 30 SCAR 45.4
CC
30
Cov
38%
map_element
270.4
map_element
110.0
_ignore_module_name
12.0
CRAP 93.4
« lib/Chj/Class/methodnames.pm ↑ Summary lib/Chj/IO/Tempfile.pm »
1 #
2 # Copyright (c) 2014-2021 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 FunctionalPerl::Htmlgen::Linking
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18 Adding/changing links, currently:
19  
20 - check whether <code> parts contain things that are either module
21 names in our repository or on CPAN, if so link them.
22  
23 - also, map <code> contents with map_code_body if applicable
24  
25 - instead of linking non-markdown files locally, make them go to the
26 Github repo
27  
28 =head1 SEE ALSO
29  
30 These are L<FunctionalPerl::Htmlgen::PXMLMapper>s
31  
32 =head1 NOTE
33  
34 This is alpha software! Read the status section in the package README
35 or on the L<website|http://functional-perl.org/>.
36  
37 =cut
38  
39 package FunctionalPerl::Htmlgen::Linking;
40  
41 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶use experimental "signatures";
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46  
47  
48 2 package FunctionalPerl::Htmlgen::Linking::Anchors {
49  
50 # add anchors
51  
52 2 ▶ use PXML::XHTML ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 2 ▶ use FP::Struct [] => "FunctionalPerl::Htmlgen::PXMLMapper";
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 4 ▶ sub match_element_names($self) { [qw(h1 h2 h3 h4)] }
Subroutine match_element_names: called CC=1 SCAR=0.0
57  
58 6 ▶ sub map_element ($self, $e, $uplist) {
Subroutine map_element: called CC=1 SCAR=0.0
59 6 my $text = $e->text;
60 6 $text =~ s/ /_/sg;
61 6 A({ name => $text }, $e)
62 }
63 2 _END_
64 }
65  
66 2 package FunctionalPerl::Htmlgen::Linking::code {
67 2 ▶ use FP::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 2 ▶ use FP::Predicates;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 2 ▶ use FunctionalPerl::Htmlgen::PathUtil qw(path_diff);
Subroutine BEGIN: called CC=1 SCAR=0.0
70 2 ▶ use PXML::XHTML ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
71 2 ▶ use Chj::CPAN::ModulePODUrl 'perhaps_module_pod_url';
Subroutine BEGIN: called CC=1 SCAR=0.0
72 2 ▶ use FP::Memoizing 'memoizing_to_dir';
Subroutine BEGIN: called CC=1 SCAR=0.0
73 2 ▶ use FunctionalPerl::Indexing qw(identifierInfos_by_name);
Subroutine BEGIN: called CC=1 SCAR=0.0
74 2 ▶ use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 2 our $podurl_cache = ".ModulePODUrl-cache";
77 2 mkdir $podurl_cache;
78  
79 # NOTE: ignores are handled further down, see $ignore_module_name
80 0 ▶ *xmaybe_module_pod_url = memoizing_to_dir $podurl_cache, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
81 0 print STDERR "perhaps_module_pod_url(@_)..";
82 0 my @res = perhaps_module_pod_url @_;
83 0 print STDERR "@res\n";
84 0 ▶ wantarray ? @res : $res[-1] ## no critic
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
85 2 };
86  
87 sub maybe_module_pod_url($v) {
88 my $res;
89 eval {
90 $res = xmaybe_module_pod_url($v);
91 1
92 } || do {
93 my $e = $@;
94 my $firstline = "$e";
95 $firstline =~ s/\n.*//s;
96 $firstline =~ m/Can't connect/i
97 ? do {
98 warn "could not look up module '$v': $firstline";
99 return undef;
100 }
101 : die $e
102 };
103 $res
104 }
105  
106 sub is_likely_class_name($str) {
107  
108 # If $str contains an underscore but no "::" then it's much
109 # more likely to be a function or method name than a class
110 # name:
111 is_valid_class_name($str) and ($str =~ /::/ or not $str =~ /_/)
112 }
113  
114 2 ▶ use FP::Struct [
Subroutine BEGIN: called CC=1 SCAR=0.0
115 2 [\&is_string, "functional_perl_base_dir"],
116 [\&is_array, "static_ignore_names"]
117 2 ] => "FunctionalPerl::Htmlgen::PXMLMapper";
118  
119 0 ▶ sub _ignore_module_name($self) {
Subroutine _ignore_module_name: not called CC=3 SCAR=24.8
120 0 ▶ $self->{_ignore_module_name} //= do {
Condition0%0/3
$self->{"_ignore_module_name"} //= do { my $ignore_module_name = identifierInfos_by_name($self->functional_perl_base_dir, instance_of("FunctionalPerl::Indexing::Subroutine")); foreach $_ (@{$self->static_ignore_names;}) { push @{$$ignore_module_name{$_};}, 1; } ; $ignore_module_name->{"XXX"} = 1; $ignore_module_name }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_ignore_module_name"} //= do { my $ignore_module_name = identifierInfos_by_name($self->functional_perl_base_dir, instance_of("FunctionalPerl::Indexing::Subroutine")); foreach $_ (@{$self->static_ignore_names;}) { push @{$$ignore_module_name{$_};}, 1; } ; $ignore_module_name->{"XXX"} = 1; $ignore_module_name }
$self->{"_ignore_module_name"} do { my $ignore_module_name = identifierInfos_by_name($self->functional_perl_base_dir, instance_of("FunctionalPerl::Indexing::Subroutine")); foreach $_ (@{$self->static_ignore_names;}) { push @{$$ignore_module_name{$_};}, 1; } ; $ignore_module_name->{"XXX"} = 1; $ignore_module_name }
Truth table0%0/3
A //= B
A= $self->{"_ignore_module_name"}B= do { my $ignore_module_name = identifierInfos_by_name($self->functional_perl_base_dir, instance_of("FunctionalPerl::Indexing::Subroutine")); foreach $_ (@{$self->static_ignore_names;}) { push @{$$ignore_module_name{$_};}, 1; } ; $ignore_module_name->{"XXX"} = 1; $ignore_module_name }
ABresult
1X1
011
000
121 0 my $ignore_module_name
122 = identifierInfos_by_name($self->functional_perl_base_dir,
123 instance_of("FunctionalPerl::Indexing::Subroutine"));
124  
125 0 for (@{ $self->static_ignore_names }) {
126 0 push @{ $ignore_module_name->{$_} },
127 1; # that is not of the same type (Subroutine),
128 # but we just need a boolean.
129 }
130  
131 # To avoid tripping the search for to-do tags, this isn't part of
132 # the list above:
133 0 $$ignore_module_name{ "X" x 3 } = 1;
134  
135 0 $ignore_module_name
136 }
137 }
138  
139 0 ▶ sub ignore_module_name ($self, $name) {
Subroutine ignore_module_name: not called CC=1 SCAR=6.9
140 0 $self->_ignore_module_name->{$name}
141 }
142  
143 4 ▶ sub match_element_names($self) { ["code"] }
Subroutine match_element_names: called CC=1 SCAR=0.0
144  
145 0 ▶ sub map_element ($self, $e, $uplist) {
Subroutine map_element: not called CC=10 SCAR=47.0
146  
147 # possibly *map contents* of inline code or code sections
148 0 ▶ my $mapped_e = sub () {
Subroutine __ANON__: not called CC=2 SCAR=17.9
149 0 ▶ if (defined(my $f = $self->map_code_body)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $f = $self->map_code_body)) { } 0 0 0
150  
151 # rely on code never containing markup, at least *as long
152 # as there's no other mapper that introduced any*. XX how
153 # to make sure? (add a xtext method that dies when
154 # encountering non-sequence non-string parts?)
155 0 $e->body_set(&$f($e->text, $uplist, $self->path0))
156 } else {
157 0 $e
158 }
159 0 };
160  
161 # possibly *wrap* inline code or code sections: add links to
162 # '`Foo::Bar`' if found locally or on CPAN. XX perl specific, make
163 # addon.
164 0 ▶ if ( is_pair $uplist
Branch: 1 branch
BranchTrueFalseTotal
if (is_pair($uplist) and $uplist->first->lcname eq 'a' and $uplist->first->maybe_attribute('href')) { } 0 0 0
Condition0%0/6
is_pair($uplist) and $uplist->first->lcname eq "a" and $uplist->first->maybe_attribute("href")
!ll&&!rl&&r
000
is_pair($uplist) and $uplist->first->lcname eq "a"
!ll&&!rl&&r
000
MC/DC0%0/3
is_pair($uplist) and $uplist->first->lcname eq "a" and $uplist->first->maybe_attribute("href")
is_pair($uplist) $uplist->first->lcname eq "a" $uplist->first->maybe_attribute("href")
Truth table0%0/4
A and B and C
A= is_pair($uplist)B= $uplist->first->lcname eq "a"C= $uplist->first->maybe_attribute("href")
ABCresult
0XX0
10X0
1100
1111
165 and $uplist->first->lcname eq "a"
166 and $uplist->first->maybe_attribute("href"))
167 {
168 # already linked
169 0 &$mapped_e()
170 } else {
171 0 my $t = $e->text;
172 0 ▶ if (is_likely_class_name($t)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_likely_class_name($t)) { } 0 0 0
173 0 my $module_subpath = $t;
174 0 $module_subpath =~ s/::/\//sg;
175 0 $module_subpath .= ".pm";
176  
177 0 ▶ my $maybe_path =
Condition0%0/6
($self->maybe_have_path0->("lib/$module_subpath") // $self->maybe_have_path0->("meta/$module_subpath")) // $self->maybe_have_path0->("htmlgen/$module_subpath")
l!l&&r!l&&!r
000
$self->maybe_have_path0->("lib/$module_subpath") // $self->maybe_have_path0->("meta/$module_subpath")
l!l&&r!l&&!r
000
MC/DC0%0/3
($self->maybe_have_path0->("lib/$module_subpath") // $self->maybe_have_path0->("meta/$module_subpath")) // $self->maybe_have_path0->("htmlgen/$module_subpath")
$self->maybe_have_path0->("lib/$module_subpath") $self->maybe_have_path0->("meta/$module_subpath") $self->maybe_have_path0->("htmlgen/$module_subpath")
Truth table0%0/4
A // B // C
A= $self->maybe_have_path0->("lib/$module_subpath")B= $self->maybe_have_path0->("meta/$module_subpath")C= $self->maybe_have_path0->("htmlgen/$module_subpath")
ABCresult
1XX1
01X1
0011
0000
178  
179 # XX this should be moved to configuration of course.
180 ($self->maybe_have_path0->("lib/$module_subpath")
181 // $self->maybe_have_path0->("meta/$module_subpath")
182 // $self->maybe_have_path0->("htmlgen/$module_subpath")
183 );
184  
185 0 ▶ my $wrap_with_link = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
186 0 my ($url) = @_;
187 0 A { href => $url }, $e
188 0 };
189  
190 0 ▶ my $maybe_cpan_url
Branch: 1 branch
BranchTrueFalseTotal
$self->ignore_module_name($t) ? : 0 0 0
191 = $self->ignore_module_name($t)
192 ? undef
193 : maybe_module_pod_url($t);
194  
195 0 ▶ if (defined $maybe_cpan_url) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $maybe_cpan_url) { } 0 0 0
elsif (defined $maybe_path) { } 0 0 0
196 0 &$wrap_with_link($maybe_cpan_url)
197 } elsif (defined $maybe_path) {
198 0 &$wrap_with_link(path_diff $self->path0, $maybe_path)
199 } else {
200 0 &$mapped_e()
201 }
202 } else {
203 0 &$mapped_e()
204 }
205 }
206 }
207 2 _END_
208 }
209  
210 2 package FunctionalPerl::Htmlgen::Linking::a_href {
211 2 ▶ use FunctionalPerl::Htmlgen::UriUtil qw(URI_is_internal);
Subroutine BEGIN: called CC=1 SCAR=0.0
212 2 ▶ use Chj::xperlfunc qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
213 2 ▶ use FunctionalPerl::Htmlgen::PathUtil qw(path_add path_diff);
Subroutine BEGIN: called CC=1 SCAR=0.0
214 2 ▶ use FP::Show;
Subroutine BEGIN: called CC=1 SCAR=0.0
215  
216 2 ▶ use FP::Struct [] => "FunctionalPerl::Htmlgen::PXMLMapper";
Subroutine BEGIN: called CC=1 SCAR=0.0
217  
218 4 ▶ sub match_element_names($self) { ["a"] }
Subroutine match_element_names: called CC=1 SCAR=0.0
219  
220 # XX dito config, well all of the map_a_href (also, NOTE the 'XX'
221 # comment below about /tree/master instead; well, perhaps
222 # configure github repository base url and encode knowhow in the
223 # code)
224 2 our $github_base
225 = "https://github.com/pflanze/functional-perl/blob/master/";
226  
227 7 ▶ sub map_element ($self, $e, $uplist) {
Subroutine map_element: called CC=17 SCAR=56.0
228 7 ▶ if (my ($href) = $e->perhaps_attribute("href")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($href) = $e->perhaps_attribute('href')) { } 0 7 7
229  
230 0 my $uri = URI->new($href);
231  
232 0 ▶ if (URI_is_internal($uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (URI_is_internal($uri)) { } 0 0 0
233  
234 # * fix internal .md links
235  
236 0 my $selfpath0 = $self->path0;
237  
238 0 my ($path, $uri, $is_md) = do { # XX $uri can be
239 # unmodified there,
240 # right?
241 0 my $path = $uri->path;
242  
243 # '//' feature (see 'Formatting' section in htmlgen/README.md)
244 0 ▶ if ($href =~ m|^//|s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($href =~ m[^//]s) { } 0 0 0
elsif (length $path) { } 0 0 0
245 0 ▶ my ($op) = $uri->opaque() =~ m|^//([^/].*)$|s
Branch: 1 branch
BranchTrueFalseTotal
my($op) = $uri->opaque =~ m[^//([^/].*)$]s or die "bug" 0 0 0
246 or die "bug";
247 0 $uri->opaque(""); # mutation
248  
249 # Have a lookup hierarchy for the given path,
250 # since the same filename can exist multiple
251 # times. XX: `perhaps_filename_to_path0`
252 # should still be improved to never silently
253 # give wrong results!
254 0 my $path0_in_samedir = path_add dirname($selfpath0),
255 $op;
256 0 my $path0_docs = path_add "docs", $op;
257 0 ▶ if (
Branch: 4 branches
BranchTrueFalseTotal
if (my $p0 = ((($op eq $selfpath0 ? undef : $self->maybe_have_path0->($op)) // ($path0_in_samedir eq $selfpath0 ? undef : $self->maybe_have_path0->($path0_in_samedir))) // ($path0_docs eq $selfpath0 ? undef : $self->maybe_have_path0->($path0_docs))) // $self->perhaps_filename_to_path0->($op)) { } 0 0 0
$op eq $selfpath0 ? : 0 0 0
$path0_in_samedir eq $selfpath0 ? : 0 0 0
$path0_docs eq $selfpath0 ? : 0 0 0
Condition0%0/9
((($op eq $selfpath0 ? undef : $self->maybe_have_path0->($op)) // ($path0_in_samedir eq $selfpath0 ? undef : $self->maybe_have_path0->($path0_in_samedir))) // ($path0_docs eq $selfpath0 ? undef : $self->maybe_have_path0->($path0_docs))) // $self->perhaps_filename_to_path0->($op)
l!l&&r!l&&!r
000
(($op eq $selfpath0 ? undef : $self->maybe_have_path0->($op)) // ($path0_in_samedir eq $selfpath0 ? undef : $self->maybe_have_path0->($path0_in_samedir))) // ($path0_docs eq $selfpath0 ? undef : $self->maybe_have_path0->($path0_docs))
l!l&&r!l&&!r
000
($op eq $selfpath0 ? undef : $self->maybe_have_path0->($op)) // ($path0_in_samedir eq $selfpath0 ? undef : $self->maybe_have_path0->($path0_in_samedir))
l!l&&r!l&&!r
000
MC/DC0%0/4
((($op eq $selfpath0 ? undef : $self->maybe_have_path0->($op)) // ($path0_in_samedir eq $selfpath0 ? undef : $self->maybe_have_path0->($path0_in_samedir))) // ($path0_docs eq $selfpath0 ? undef : $self->maybe_have_path0->($path0_docs))) // $self->perhaps_filename_to_path0->($op)
($op eq $selfpath0 ? undef : $self->maybe_have_path0->($op)) ($path0_in_samedir eq $selfpath0 ? undef : $self->maybe_have_path0->($path0_in_samedir)) ($path0_docs eq $selfpath0 ? undef : $self->maybe_have_path0->($path0_docs)) $self->perhaps_filename_to_path0->($op)
Truth table0%0/5
A // B // C // D
A= ($op eq $selfpath0 ? undef : $self->maybe_have_path0->($op))B= ($path0_in_samedir eq $selfpath0 ? undef : $self->maybe_have_path0->($path0_in_samedir))C= ($path0_docs eq $selfpath0 ? undef : $self->maybe_have_path0->($path0_docs))D= $self->perhaps_filename_to_path0->($op)
ABCDresult
1XXX1
01XX1
001X1
00011
00000
258 my $p0 =
259  
260 # check as full path from root
261 (
262 $op eq $selfpath0
263 ? undef
264 : $self->maybe_have_path0->($op)
265 )
266 //
267  
268 # check in local dir
269 (
270 $path0_in_samedir eq $selfpath0
271 ? undef
272 : $self->maybe_have_path0->(
273 $path0_in_samedir)
274 )
275 //
276  
277 # check in docs
278 (
279 $path0_docs eq $selfpath0
280 ? undef
281 : $self->maybe_have_path0->($path0_docs)
282 )
283 //
284  
285 # check as filename anywhere (XX check
286 # against $selfpath0 here, too, or rather,
287 # use this to disambiguate?)
288 $self->perhaps_filename_to_path0->($op)
289 )
290 {
291 0 (path_diff($selfpath0, $p0), $uri, 1)
292 } else {
293 0 warn "unknown link target '$op' (from '$href')";
294 0 (path_diff($selfpath0, "UNKNOWN/$op"), $uri, 1)
295 }
296 } elsif (length $path) {
297 0 my $p0 = path_add(dirname($selfpath0), $path);
298 0 $p0 =~ s|^\./||; #hack. grr y
299 0 ▶ unless ($self->maybe_have_path0->($p0)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->maybe_have_path0->($p0)) 0 0 0
300 0 warn "link target does not exist: "
301 . show($p0)
302 . "('$path' from '$selfpath0', link '$href')";
303  
304 #use FP::Repl;repl;
305 }
306 0 ($path, $uri, $self->pathtranslate->is_md($path))
307 } else {
308 0 ($path, $uri, $self->pathtranslate->is_md($path))
309 }
310 };
311  
312 0 ▶ my $cont_uri = sub($uri) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
313 0 $e->attribute_set("href", "$uri")
314 0 };
315 0 ▶ my $cont_path = sub($path) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
316 0 $uri->path(
317 $self->pathtranslate->possibly_suffix_md_to_html($path)
318 );
319 0 &$cont_uri($uri);
320 0 };
321  
322 0 ▶ if (length $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $path) { } 0 0 0
323  
324 # * change links to non-.md files to go to Github
325 0 ▶ if ($is_md) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_md) { } 0 0 0
326 0 &$cont_path($path)
327 } else {
328 0 ▶ if (length(my $p = $uri->path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $p = $uri->path)) 0 0 0
329 0 $uri->path(path_add(dirname($self->path0), $p));
330  
331 # XX should use "/tree/master" instead of
332 # "/blob/master" Github url for
333 # directories, but Github redirects anyway
334 # so?
335 0 &$cont_uri($uri->abs($github_base))
336  
337 # yes, the `path` method is a mutator, `abs` is not!
338 }
339 }
340 } else {
341 0 $e
342 }
343  
344 } else {
345 0 $e
346 }
347 } else {
348 7 $e
349 }
350 }
351 2 _END_
352 }
353  
354 2 1
« lib/Chj/Class/methodnames.pm ↑ Summary lib/Chj/IO/Tempfile.pm »