blib/lib/App/SlideServer.pm

filter: Statementstmt 63.4% 160 / 252 Branchbran 50.0% 57 / 114 Conditioncond 44.4% 44 / 99 MC/DCmcdc 20.0% 11 / 55 Subroutinesub 60.8% 28 / 46 Podpod 87.5% 14 / 16 totaltotal 53.9% 314 / 582 CC 109 SCAR 30.8
CC
109
Cov
56%
on_viewer_message
72.0
__ANON__
42.0
serve_page
42.0
CRAP 21.7
↑ Summary
1 package App::SlideServer;
2 3 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Mojo::Base 'Mojolicious';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Mojo::WebSocket 'WS_PING';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Mojo::File 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our $VERSION = '0.003'; #VERSION
12 #ABSTRACT: Mojo web server that serves slides and synchronization events
13  
14  
15 # Files supplied by the user to override the distribution
16 9 ▶has serve_dir => sub { path(shift->home) };
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18  
19 24 ▶sub slides_source_file($self, $value=undef) {
Subroutine slides_source_file: called CC=4 SCAR=17.9
Pod documented
20 24 ▶ $self->{slides_source_file}= $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 24 24
21 24 ▶ $self->{slides_source_file} // do {
Condition33%1/3
$self->{"slides_source_file"} // do { my($src) = grep((-f $_), $self->serve_dir->child("slides.html"), $self->serve_dir->child("slides.md"), $self->serve_dir->child("public", "slides.html"), $self->serve_dir->child("public", "slides.md")); $src }
l!l&&r!l&&!r
2400
MC/DC0%0/2
$self->{"slides_source_file"} // do { my($src) = grep((-f $_), $self->serve_dir->child("slides.html"), $self->serve_dir->child("slides.md"), $self->serve_dir->child("public", "slides.html"), $self->serve_dir->child("public", "slides.md")); $src }
$self->{"slides_source_file"} do { my($src) = grep((-f $_), $self->serve_dir->child("slides.html"), $self->serve_dir->child("slides.md"), $self->serve_dir->child("public", "slides.html"), $self->serve_dir->child("public", "slides.md")); $src }
Truth table33%1/3
A // B
A= $self->{"slides_source_file"}B= do { my($src) = grep((-f $_), $self->serve_dir->child("slides.html"), $self->serve_dir->child("slides.md"), $self->serve_dir->child("public", "slides.html"), $self->serve_dir->child("public", "slides.md")); $src }
ABresult
1X1
011
000
22 # Choose the first of ...
23 0 my ($src)= grep -f $_,
24 $self->serve_dir->child('slides.html'),
25 $self->serve_dir->child('slides.md'),
26 $self->serve_dir->child('public','slides.html'),
27 $self->serve_dir->child('public','slides.md');
28 0 $src;
29 };
30 }
31  
32  
33 3 has 'slides_source_monitor';
34  
35  
36 # Files that ship with the distribution
37 9 ▶has share_dir => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
38 9 ▶ if (-f path(__FILE__)->sibling('..','..','share','public','slides.js')) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f path(__FILE__)->sibling('..', '..', 'share', 'public', 'slides.js')) { } 0 9 9
39 0 return path(__FILE__)->sibling('..','..','share')->realpath;
40 } else {
41 9 require File::ShareDir;
42 9 return path(File::ShareDir::dist_dir('App-SlideServer'));
43 }
44 3 };
45  
46  
47 # A secret known only to whoever starts the server
48 # Clients must send this to gain presenter permission.
49 0 ▶has presenter_key => sub($self) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 my $key= sprintf "%06d", rand 1000000;
51 0 $self->log->info("Auto-generated presenter key: $key");
52 0 return $key;
53 3 };
54  
55  
56 # Hashref of { ID => $context } for every connected websocket
57 9 ▶has viewers => sub { +{} };
Subroutine __ANON__: called CC=1 SCAR=0.0
58  
59  
60 # Hashref of data to be pushed to all clients
61 0 ▶has published_state => sub { +{} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
62  
63  
64 3 has ['cache_token', 'page_dom', 'slides_dom'];
65  
66  
67 9 ▶sub build_slides($self, %opts) {
Subroutine build_slides: called CC=8 SCAR=24.4
Pod documented
68 9 my ($html, $token)= $self->load_slides_html(if_changed => $self->cache_token);
69 9 ▶ return 0 unless defined $html; # undef if source file unchanged
Branch: 1 branch
BranchTrueFalseTotal
unless defined $html 0 9 9
70 9 my ($page, @slides)= $self->extract_slides_dom($html);
71 9 $self->log->info("Loaded ".@slides." slides from ".$self->slides_source_file);
72 9 $page= $self->merge_page_assets($page);
73 9 $self->cache_token($token);
74 9 ▶ my $page_diff= !$self->page_dom || $self->page_dom ne $page;
Condition33%1/3
!$self->page_dom || $self->page_dom ne $page
l!l&&r!l&&!r
900
MC/DC0%0/2
!$self->page_dom || $self->page_dom ne $page
!$self->page_dom $self->page_dom ne $page
Truth table33%1/3
not(A) || B
A= !$self->page_domB= $self->page_dom ne $page
ABresult
1X1
011
000
75 my @slides_diff= !$self->slides_dom? (0..$#slides)
76 0 ▶ : grep { ($self->slides_dom->[$_]//'') ne ($slides[$_]//'') } 0..$#slides;
Branch: 1 branch
BranchTrueFalseTotal
!$self->slides_dom ? : 9 0 9
Condition0%0/4
$self->slides_dom->[$_] // ""
l!l
00
$slides[$_] // ""
l!l
00
MC/DC0%0/1
$self->slides_dom->[$_] // ""
$self->slides_dom->[$_]
MC/DC0%0/1
$slides[$_] // ""
$slides[$_]
Truth table0%0/2
A
A= $self->slides_dom->[$_]
Aresult
11
00
Truth table0%0/2
A
A= $slides[$_]
Aresult
11
00
77 9 $self->page_dom($page);
78 9 $self->slides_dom(\@slides);
79 9 ▶ $self->on_page_changed() if $page_diff;
Branch: 1 branch
BranchTrueFalseTotal
if $page_diff 9 0 9
80 9 ▶ $self->on_slides_changed(\@slides_diff) if @slides_diff;
Branch: 1 branch
BranchTrueFalseTotal
if @slides_diff 9 0 9
81 9 return \@slides;
82 }
83  
84  
85 15 ▶sub load_slides_html($self, %opts) {
Subroutine load_slides_html: called CC=20 SCAR=32.2
Pod documented
86 15 my $srcfile= $self->slides_source_file;
87 15 ▶ defined $srcfile
Branch: 1 branch
BranchTrueFalseTotal
defined $srcfile or croak(q[No source file; require slides.md or slides.html in serve_dir '] . $self->serve_dir . "'\n") 15 0 15
88 or croak "No source file; require slides.md or slides.html in serve_dir '".$self->serve_dir."'\n";
89 # Allow literal data with a scalar ref
90 15 my ($content, $change_token);
91 15 ▶ if (ref $srcfile eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $srcfile eq 'SCALAR') { } 7 8 15
elsif (ref $srcfile eq 'GLOB' or ref $srcfile and ref($srcfile)->isa('IO::Handle')) { } 4 4 8
92 return undef
93 7 ▶ if defined $opts{if_changed} && 0+$srcfile == $opts{if_changed};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"if_changed"} and 0 + $srcfile == $opts{"if_changed"} 0 7 7
Condition33%1/3
defined $opts{"if_changed"} and 0 + $srcfile == $opts{"if_changed"}
!ll&&!rl&&r
700
MC/DC0%0/2
defined $opts{"if_changed"} and 0 + $srcfile == $opts{"if_changed"}
defined $opts{"if_changed"} 0 + $srcfile == $opts{"if_changed"}
Truth table33%1/3
A and B
A= defined $opts{"if_changed"}B= 0 + $srcfile == $opts{"if_changed"}
ABresult
0X0
100
111
94 7 $content= $$srcfile;
95 7 $change_token= 0+$srcfile;
96 # Assume markdown unless first non-whitespace is the start of a tag
97 7 ▶ $content= $self->markdown_to_html($content, %opts)
Branch: 1 branch
BranchTrueFalseTotal
unless $content =~ /^\s*</u 2 5 7
98 unless $content =~ /^\s*</;
99 }
100 ▶ elsif (ref $srcfile eq 'GLOB' || (ref($srcfile) && ref($srcfile)->isa('IO::Handle'))) {
Condition66%4/6
ref $srcfile eq "GLOB" or ref $srcfile and ref($srcfile)->isa("IO::Handle")
l!l&&r!l&&!r
044
ref $srcfile and ref($srcfile)->isa("IO::Handle")
!ll&&!rl&&r
404
MC/DC33%1/3
ref $srcfile eq "GLOB" or ref $srcfile and ref($srcfile)->isa("IO::Handle")
ref $srcfile eq "GLOB" ref $srcfile ref($srcfile)->isa("IO::Handle")
Truth table50%2/4
A or B and C
A= ref $srcfile eq "GLOB"B= ref $srcfileC= ref($srcfile)->isa("IO::Handle")
ABCresult
1XX1
0111
00X0
0100
101 return undef
102 4 ▶ if defined $opts{if_changed} && (0+$srcfile) .'_'. tell($srcfile) eq $opts{if_changed};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"if_changed"} and (0 + $srcfile) . '_' . tell($srcfile) eq $opts{"if_changed"} 0 4 4
Condition33%1/3
defined $opts{"if_changed"} and (0 + $srcfile) . '_' . tell($srcfile) eq $opts{"if_changed"}
!ll&&!rl&&r
400
MC/DC0%0/2
defined $opts{"if_changed"} and (0 + $srcfile) . '_' . tell($srcfile) eq $opts{"if_changed"}
defined $opts{"if_changed"} (0 + $srcfile) . '_' . tell($srcfile) eq $opts{"if_changed"}
Truth table33%1/3
A and B
A= defined $opts{"if_changed"}B= (0 + $srcfile) . '_' . tell($srcfile) eq $opts{"if_changed"}
ABresult
0X0
100
111
103 4 ▶ seek($srcfile, 0, 0) || die "seek: $!"
Branch: 1 branch
BranchTrueFalseTotal
unless tell $srcfile <= 0 2 2 4
Condition60%3/5
tell $srcfile <= 0 || (seek($srcfile, 0, 0) || die("seek: $!"))
l!l&&r!l&&!r
220
seek $srcfile, 0, 0 or die "seek: $!"
l!l
20
MC/DC0%0/2
tell $srcfile <= 0 || (seek($srcfile, 0, 0) || die("seek: $!"))
tell $srcfile <= 0 seek $srcfile, 0, 0
Truth table66%2/3
A || B
A= tell $srcfile <= 0B= seek $srcfile, 0, 0
ABresult
1X1
011
000
104 unless tell($srcfile) <= 0;
105 4 $content= do { local $/= undef; <$srcfile> };
106 4 ▶ utf8::decode($content) unless PerlIO::get_layers($srcfile) =~ /encoding|utf8/i;
Branch: 1 branch
BranchTrueFalseTotal
unless &PerlIO::get_layers($srcfile) =~ /encoding|utf8/iu 0 4 4
107 # Assume markdown unless first non-whitespace is the start of a tag
108 4 ▶ $content= $self->markdown_to_html($content, %opts)
Branch: 1 branch
BranchTrueFalseTotal
unless $content =~ /^\s*</u 2 2 4
109 unless $content =~ /^\s*</;
110 4 $change_token= (0+$srcfile) .'_'. tell($srcfile);
111 }
112 else {
113 4 ▶ my $st= stat($srcfile)
Branch: 1 branch
BranchTrueFalseTotal
my $st = &App::SlideServer::stat($srcfile) or croak("Can't stat '${srcfile}'") 4 0 4
114 or croak "Can't stat '$srcfile'";
115 return undef
116 4 ▶ if defined $opts{if_changed} && $st->mtime == $opts{if_changed};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"if_changed"} and $st->mtime == $opts{"if_changed"} 0 4 4
Condition33%1/3
defined $opts{"if_changed"} and $st->mtime == $opts{"if_changed"}
!ll&&!rl&&r
400
MC/DC0%0/2
defined $opts{"if_changed"} and $st->mtime == $opts{"if_changed"}
defined $opts{"if_changed"} $st->mtime == $opts{"if_changed"}
Truth table33%1/3
A and B
A= defined $opts{"if_changed"}B= $st->mtime == $opts{"if_changed"}
ABresult
0X0
100
111
117
118 4 $content= path($srcfile)->slurp;
119 4 utf8::decode($content); # Could try to detect encoding, but people should just use utf-8
120  
121 4 ▶ $content= $self->markdown_to_html($content, %opts)
Branch: 1 branch
BranchTrueFalseTotal
if $srcfile =~ /[.]md$/iu 2 2 4
122 if $srcfile =~ /[.]md$/i;
123 4 $change_token= $st->mtime;
124 }
125 15 ▶ return wantarray? ($content, $change_token) : $content;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 9 6 15
126 }
127  
128  
129 0 ▶sub monitor_source_changes($self, $enable=1) {
Subroutine monitor_source_changes: not called CC=5 SCAR=34.0
Pod documented
130 0 ▶ if ($enable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enable) { } 0 0 0
131 0 my $f= $self->slides_source_file;
132 0 ▶ -f $f or croak "No such file '$f'";
Branch: 1 branch
BranchTrueFalseTotal
-f $f or croak("No such file '${f}'") 0 0 0
133 # TODO: wrap inotify in an object with a more convenient API and detect things like file renames
134 0 ▶ $self->{_inotify} //= do {
Condition0%0/3
$self->{"_inotify"} //= do { require Linux::Inotify2; my $inotify = "Linux::Inotify2"->new; my $i_fh = "IO::Handle"->new_from_fd($inotify->fileno, "r"); "Mojo::IOLoop"->singleton->reactor->io($i_fh, sub ($reactor, $writable) { $inotify->poll if $inotify and not $writable; } )->watch($i_fh, 1, 0); +{"inotify", $inotify, "inotify_fh", $i_fh} }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_inotify"} //= do { require Linux::Inotify2; my $inotify = "Linux::Inotify2"->new; my $i_fh = "IO::Handle"->new_from_fd($inotify->fileno, "r"); "Mojo::IOLoop"->singleton->reactor->io($i_fh, sub ($reactor, $writable) { $inotify->poll if $inotify and not $writable; } )->watch($i_fh, 1, 0); +{"inotify", $inotify, "inotify_fh", $i_fh} }
$self->{"_inotify"} do { require Linux::Inotify2; my $inotify = "Linux::Inotify2"->new; my $i_fh = "IO::Handle"->new_from_fd($inotify->fileno, "r"); "Mojo::IOLoop"->singleton->reactor->io($i_fh, sub ($reactor, $writable) { $inotify->poll if $inotify and not $writable; } )->watch($i_fh, 1, 0); +{"inotify", $inotify, "inotify_fh", $i_fh} }
Truth table0%0/3
A //= B
A= $self->{"_inotify"}B= do { require Linux::Inotify2; my $inotify = "Linux::Inotify2"->new; my $i_fh = "IO::Handle"->new_from_fd($inotify->fileno, "r"); "Mojo::IOLoop"->singleton->reactor->io($i_fh, sub ($reactor, $writable) { $inotify->poll if $inotify and not $writable; } )->watch($i_fh, 1, 0); +{"inotify", $inotify, "inotify_fh", $i_fh} }
ABresult
1X1
011
000
135 0 require Linux::Inotify2;
136 0 my $inotify= Linux::Inotify2->new;
137 0 my $i_fh= IO::Handle->new_from_fd($inotify->fileno, 'r');
138 Mojo::IOLoop->singleton->reactor
139 0 ▶ ->io( $i_fh, sub($reactor, $writable) { $inotify->poll if $inotify && !$writable })
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if $inotify and not $writable 0 0 0
Condition0%0/3
$inotify and not $writable
!ll&&!rl&&r
000
MC/DC0%0/2
$inotify and not $writable
$inotify not $writable
Truth table0%0/3
A and not(B)
A= $inotifyB= not $writable
ABresult
0X0
100
111
140 0 ->watch($i_fh, 1, 0);
141 0 { inotify => $inotify, inotify_fh => $i_fh }
142 };
143 0 Scalar::Util::weaken( my $app= $self );
144 0 ▶ my $watch= $self->{_inotify}{inotify}->watch("$f", Linux::Inotify2::IN_MODIFY(), sub { $app->build_slides });
Subroutine __ANON__: not called CC=1 SCAR=6.9
145 0 $self->slides_source_monitor($watch);
146 } else {
147 0 $self->slides_source_monitor(undef);
148 }
149 }
150  
151  
152 3 has markdown_processor => \&_build_markdown_processor;
153 5 ▶sub _build_markdown_processor {
Subroutine _build_markdown_processor: called CC=4 SCAR=25.0
154 5 ▶ if (eval { require Markdown::Perl }) {
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { require Markdown::Perl } }) { } 5 0 5
elsif (eval { do { require Text::Markdown::Hoedown } }) { } 0 0 0
155 5 my $mdp= Markdown::Perl->new(mode => 'github', disallowed_html_tags => []);
156 5 ▶ return sub { $mdp->convert(shift) };
Subroutine __ANON__: called CC=1 SCAR=0.0
157 0 } elsif (eval { require Text::Markdown::Hoedown }) {
158 0 my $opts= 0;
159 $opts |= Text::Markdown::Hoedown->$_
160 0 for qw( HOEDOWN_EXT_TABLES HOEDOWN_EXT_FENCED_CODE HOEDOWN_EXT_AUTOLINK
161 HOEDOWN_EXT_STRIKETHROUGH HOEDOWN_EXT_UNDERLINE HOEDOWN_EXT_QUOTE
162 HOEDOWN_EXT_SUPERSCRIPT HOEDOWN_EXT_NO_INTRA_EMPHASIS );
163 0 ▶ return sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
164 0 my $html= Text::Markdown::Hoedown::markdown(shift, extensions => $opts);
165 # The Hoedown markdown processor puts <p> tags on any raw html it wasn't expecting.
166 # We don't want them unless they contain actual content.
167 0 my %not_content= map +($_ => 1), qw( head title script link style base meta );
168 0 my $dom= Mojo::DOM->new($html);
169 0 for my $p ($dom->find('p')->each) {
170 # Remove it if the contents do not include printable text
171 # or span-type content.
172 0 my $keep= 0;
173 0 for ($p->child_nodes->each) {
174 0 ▶ next $not_content{$_->tag // ''};
Condition0%0/2
$_->tag // ""
l!l
00
MC/DC0%0/1
$_->tag // ""
$_->tag
Truth table0%0/2
A
A= $_->tag
Aresult
11
00
175 0 ▶ $keep= 1 if $_->text =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $_->text =~ /\S/u 0 0 0
176 }
177 0 ▶ $p->remove unless $keep;
Branch: 1 branch
BranchTrueFalseTotal
unless $keep 0 0 0
178 }
179 0 return "$dom";
180 0 };
181 } else {
182 0 croak "No markdown processor found, install Markdown::Perl\n";
183 }
184 };
185  
186  
187 6 ▶sub markdown_to_html($self, $md, %opts) {
Subroutine markdown_to_html: called CC=1 SCAR=0.0
Pod documented
188 # If the data contains an __END__ marker, discard the marker and all data after it.
189 6 $md =~ s/(\r?\n)__END__\r?\n.*\z/$1/s;
190  
191 6 return $self->markdown_processor->($md);
192 }
193  
194  
195 40 ▶sub _node_is_slide($self, $node, $tag) {
Subroutine _node_is_slide: called CC=3 SCAR=12.0
196 40 ▶ return $tag eq 'div' && ($node->{class}//'') =~ /\bslide\b/;
Condition60%3/5
$tag eq 'div' && ($node->{'class'} // '') =~ /\bslide\b/u
!ll&&!rl&&r
3703
$node->{"class"} // ""
l!l
30
MC/DC50%1/2
$tag eq 'div' && ($node->{'class'} // '') =~ /\bslide\b/u
$tag eq 'div' $node->{"class"}
Truth table66%2/3
A && B
A= $tag eq 'div'B= $node->{"class"}
ABresult
0X0
100
111
197 }
198 17 ▶sub _node_starts_slide($self, $node, $tag) {
Subroutine _node_starts_slide: called CC=3 SCAR=11.7
199 17 ▶ return $tag eq 'h1' || $tag eq 'h2' || $tag eq 'h3';
Condition66%4/6
$tag eq 'h1' || $tag eq 'h2' || $tag eq 'h3'
l!l&&r!l&&!r
1016
$tag eq "h1" or $tag eq "h2"
l!l&&r!l&&!r
1016
MC/DC33%1/3
$tag eq 'h1' || $tag eq 'h2' || $tag eq 'h3'
$tag eq "h1" $tag eq "h2" $tag eq 'h3'
Truth table50%2/4
A or B || C
A= $tag eq "h1"B= $tag eq "h2"C= $tag eq 'h3'
ABCresult
1XX1
01X1
0011
0000
200 }
201 37 ▶sub _node_splits_slide($self, $node, $tag) {
Subroutine _node_splits_slide: called CC=1 SCAR=0.0
202 37 return $tag eq 'hr';
203 }
204  
205 9 ▶sub extract_slides_dom($self, $html, %opts) {
Subroutine extract_slides_dom: called CC=30 SCAR=35.9
Pod documented
206 9 my $dom= Mojo::DOM->new($html);
207 9 my @head_tags= qw( head title script link style base meta );
208 9 my @move_to_head;
209 9 ▶ my $slide_root= $dom->at('div.slides') || $dom->at('body') || $dom;
Condition66%4/6
$dom->at('div.slides') || $dom->at('body') || $dom
l!l&&r!l&&!r
360
$dom->at("div.slides") or $dom->at("body")
l!l&&r!l&&!r
306
MC/DC0%0/3
$dom->at('div.slides') || $dom->at('body') || $dom
$dom->at("div.slides") $dom->at("body") $dom
Truth table50%2/4
A or B || C
A= $dom->at("div.slides")B= $dom->at("body")C= $dom
ABCresult
1XX1
01X1
0011
0000
210 9 for my $tag (@head_tags) {
211 63 for my $el ($slide_root->find($tag)->each) {
212 # special handling for script tags:
213 7 ▶ if ($tag eq 'script') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "script") 1 6 7
214 # If the script tag is a src="..." form, move it to <head>
215 # else for inline scripts, it might refer to elements on the
216 # slide, so load it alongside that slide.
217 1 ▶ next unless defined $el->{src};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $el->{"src"} 1 0 1
218 }
219 6 ▶ if ($tag eq 'head') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq 'head') { } 2 4 6
220 # the *contents* of the head tag need moved, not the head tag itself
221 2 push @move_to_head, $el->child_nodes->@*;
222 } else {
223 4 push @move_to_head, $el;
224 }
225 6 $el->remove;
226 }
227 }
228 9 for my $el ($slide_root->find('notes')->each) {
229 2 $el->tag('pre');
230 2 $el->{class}= 'notes';
231 2 my $parent= $el->parent;
232 2 ▶ $parent->strip if ($parent->tag//'') eq 'p'; # Hoedown markdown processor adds <p> tags
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->tag // "") eq "p" 0 2 2
Condition50%1/2
$parent->tag // ""
l!l
02
MC/DC0%0/1
$parent->tag // ""
$parent->tag
Truth table50%1/2
A
A= $parent->tag
Aresult
11
00
233 }
234
235 # Find each element that is an immediate child of body, and add it to
236 # the current slide until the next <h1> <h2> <h3> <hr> or <div class="slide">
237 # at which point, move to the next slide.
238 9 my (@slides, $cur_slide);
239 9 for my $node ($slide_root->@*) {
240 41 $node->remove;
241 41 ▶ my $tag= $node->tag // '';
Condition100%2/2
$node->tag // ""
l!l
1625
MC/DC100%1/1
$node->tag // ""
$node->tag
Truth table100%2/2
A
A= $node->tag
Aresult
11
00
242 # is it a script tag located before the first slide has begun?
243 41 ▶ if ($tag eq 'script' && !@slides) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "script" and not @slides) 1 40 41
Condition66%2/3
$tag eq "script" and not @slides
!ll&&!rl&&r
4001
MC/DC50%1/2
$tag eq "script" and not @slides
$tag eq "script" not @slides
Truth table66%2/3
A and not(B)
A= $tag eq "script"B= not @slides
ABresult
0X0
100
111
244 1 push @move_to_head, $node;
245 1 next;
246 }
247 # is it a whole pre-defined slide?
248 40 ▶ if ($self->_node_is_slide($node, $tag)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_node_is_slide($node, $tag)) { } 3 37 40
elsif ($self->_node_splits_slide($node, $tag)) { } 1 36 37
249 3 $cur_slide= undef;
250 3 push @slides, $node;
251 }
252 elsif ($self->_node_splits_slide($node, $tag)) {
253 1 $cur_slide= undef;
254 }
255 else {
256 # Ignore whitespace nodes when not in a current slide
257 36 ▶ next if !defined $cur_slide && $node->type eq 'text' && $node->text !~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cur_slide and $node->type eq "text" and not $node->text =~ /\S/u 13 23 36
Condition83%5/6
not defined $cur_slide and $node->type eq "text" and not $node->text =~ /\S/u
!ll&&!rl&&r
23013
not defined $cur_slide and $node->type eq "text"
!ll&&!rl&&r
17613
MC/DC66%2/3
not defined $cur_slide and $node->type eq "text" and not $node->text =~ /\S/u
not defined $cur_slide $node->type eq "text" not $node->text =~ /\S/u
Truth table75%3/4
not(A) and B and not(C)
A= not defined $cur_slideB= $node->type eq "text"C= not $node->text =~ /\S/u
ABCresult
0XX0
10X0
1100
1111
258 23 ▶ push @slides, ($cur_slide= Mojo::DOM->new_tag('div', class => 'slide')->[0])
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cur_slide or $self->_node_starts_slide($node, $tag) 7 16 23
Condition100%3/3
not defined $cur_slide or $self->_node_starts_slide($node, $tag)
l!l&&r!l&&!r
6116
MC/DC100%2/2
not defined $cur_slide or $self->_node_starts_slide($node, $tag)
not defined $cur_slide $self->_node_starts_slide($node, $tag)
Truth table100%3/3
not(A) or B
A= not defined $cur_slideB= $self->_node_starts_slide($node, $tag)
ABresult
1X1
011
000
259 if !defined $cur_slide
260 || $self->_node_starts_slide($node, $tag);
261 # Add "auto-step" to any <UL> tags
262 23 ▶ if (($tag eq 'ul' || $tag eq 'ol') && !$node->{class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "ul" || $tag eq "ol" and not $node->{"class"}) 3 20 23
Condition66%4/6
$tag eq "ul" || $tag eq "ol" and not $node->{"class"}
!ll&&!rl&&r
2003
$tag eq "ul" or $tag eq "ol"
l!l&&r!l&&!r
3020
MC/DC33%1/3
$tag eq "ul" || $tag eq "ol" and not $node->{"class"}
$tag eq "ul" $tag eq "ol" not $node->{"class"}
Truth table40%2/5
A or B and not(C)
A= $tag eq "ul"B= $tag eq "ol"C= not $node->{"class"}
ABCresult
00X0
1X00
0100
1X11
0111
263 3 $node->{class}= "auto-step";
264 # Now apply auto-step recursively to <ol> and <ul>
265 0 ▶ $node->find('ol')->map(sub{ $_->{class}= $_->{class}? "$_->{class} auto-step" : 'auto-step' });
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
$_->{'class'} ? : 0 0 0
266 0 ▶ $node->find('ul')->map(sub{ $_->{class}= $_->{class}? "$_->{class} auto-step" : 'auto-step' });
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
$_->{'class'} ? : 0 0 0
267 }
268 23 $cur_slide->append_content($node);
269 }
270 }
271
272 # Re-add things that belong in <head>
273 9 ▶ ($dom->at('html') || $dom)->prepend_content('<head></head>')
Branch: 1 branch
BranchTrueFalseTotal
unless $dom->at("head") 6 3 9
Condition33%1/3
$dom->at('html') || $dom
l!l&&r!l&&!r
060
MC/DC0%0/2
$dom->at('html') || $dom
$dom->at('html') $dom
Truth table33%1/3
A || B
A= $dom->at('html')B= $dom
ABresult
1X1
011
000
274 unless $dom->at('head');
275 9 for my $el (@move_to_head) {
276 9 ▶ if (($el->tag//'') eq 'head') {
Branch: 1 branch
BranchTrueFalseTotal
if (($el->tag // '') eq 'head') { } 0 9 9
Condition100%2/2
$el->tag // ""
l!l
72
MC/DC100%1/1
$el->tag // ""
$el->tag
Truth table100%2/2
A
A= $el->tag
Aresult
11
00
277 0 ▶ $el->child_nodes->each(sub{ $dom->at('head')->append_content($_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
278 } else {
279 9 $dom->at('head')->append_content($el);
280 }
281 }
282 9 return ($dom, @slides);
283 }
284  
285  
286 9 ▶sub merge_page_assets($self, $srcdom, %opts) {
Subroutine merge_page_assets: called CC=7 SCAR=20.2
Pod documented
287 9 my $page= Mojo::DOM->new($self->share_dir->child('page_template.html')->slurp);
288 9 ▶ if (my $srchead= $srcdom->at('head')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $srchead = $srcdom->at("head")) 9 0 9
289 9 my $pagehead= $page->at('head');
290 # Prevent conflicting tags (TODO, more...)
291 9 ▶ if (my $title= $srchead->at('title')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $title = $srchead->at("title")) 5 4 9
292 5 $pagehead->at('title')->remove;
293 }
294 9 $pagehead->append_content($_) for $srchead->@*;
295 }
296 9 ▶ if (my $srcbody= $srcdom->at('body')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $srcbody = $srcdom->at("body")) 3 6 9
297 3 ▶ if ($srcbody->child_nodes->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($srcbody->child_nodes->size) { } 3 0 3
298 3 $page->at('body')->replace($srcbody);
299 3 ▶ if (!$page->at('body div.slides')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($page->at("body div.slides")) 0 3 3
300 0 $page->at('body')->append_content('<div class="slides"></div>');
301 }
302 } else {
303 0 $page->at('body')->%*= $srcbody->%*;
304 }
305 }
306 9 return $page;
307 }
308  
309  
310 0 ▶sub update_published_state($self, @new_attrs) {
Subroutine update_published_state: not called CC=2 SCAR=17.9
Pod documented
311 0 $self->published_state->%* = ( $self->published_state->%*, @new_attrs );
312 $_->send({ json => { state => $self->published_state } })
313 0 for values $self->viewers->%*;
314 }
315  
316  
317 9 ▶sub startup($self) {
Subroutine startup: called CC=1 SCAR=0.2
Pod documented
318 9 $self->build_slides;
319 9 $self->presenter_key;
320 9 $self->static->paths([ $self->serve_dir->child('public'), $self->share_dir->child('public') ]);
321 0 ▶ $self->routes->get('/' => sub($c){ $c->app->serve_page($c) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
322 0 ▶ $self->routes->websocket('/slidelink.io' => sub($c){ $c->app->init_slidelink($c) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
323 }
324  
325 0 ▶sub serve_page($self, $c, %opts) {
Subroutine serve_page: not called CC=6 SCAR=37.4
Pod documented
326 0 ▶ if (!defined $self->page_dom || $self->cache_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->page_dom or $self->cache_token) 0 0 0
Condition0%0/3
not defined $self->page_dom or $self->cache_token
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $self->page_dom or $self->cache_token
not defined $self->page_dom $self->cache_token
Truth table0%0/3
not(A) or B
A= not defined $self->page_domB= $self->cache_token
ABresult
1X1
011
000
327 0 ▶ eval { $self->build_slides; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->build_slides; 1 } } or $self->log->error($@) 0 0 0
328 or $self->log->error($@);
329 }
330 # Merge the empty page with all currently-visible slides,
331 # which saves the client from needing a second request to fetch them.
332 # TODO: implement slide-by-slide loading
333 0 my $slide_max= $#{$self->slides_dom}; # $self->published_state->{slide_max} || 0;
334 0 my @slides= $self->slides_dom->@[0..$slide_max];
335 0 my $combined= Mojo::DOM->new($self->page_dom);
336 0 $combined->at('div.slides')->append_content(join '', @slides);
337  
338 # If this is for the presenter, set the config variable for that
339 0 ▶ if ($opts{presenter} || defined $c->req->param('presenter')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"presenter"} or defined $c->req->param("presenter")) 0 0 0
Condition0%0/3
$opts{"presenter"} or defined $c->req->param("presenter")
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"presenter"} or defined $c->req->param("presenter")
$opts{"presenter"} defined $c->req->param("presenter")
Truth table0%0/3
A or B
A= $opts{"presenter"}B= defined $c->req->param("presenter")
ABresult
1X1
011
000
340 0 my $key = $c->req->param('presenter');
341 0 $combined->at('head')->append_content(
342 "<script>\n window.slides.config.mode='presenter';\n window.slides.config.key='$key';\n</script>\n"
343 );
344 }
345  
346 0 $c->render(text => ''.$combined);
347 }
348  
349 0 ▶sub init_slidelink($self, $c) {
Subroutine init_slidelink: not called CC=5 SCAR=34.0
Pod documented
350 0 my $id= $c->req->request_id;
351 0 $self->viewers->{$id}= $c;
352 0 my $mode= $c->req->param('mode');
353 0 my $key= $c->req->param('key');
354 0 my %roles= ( follow => 1 );
355 0 ▶ if ($mode eq 'presenter') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "presenter") 0 0 0
356 0 ▶ if (($key||'') eq $self->presenter_key) {
Branch: 2 branches
BranchTrueFalseTotal
if (($key || '') eq $self->presenter_key) { } 0 0 0
elsif (defined $key) { } 0 0 0
Condition0%0/2
$key || ''
l!l
00
MC/DC0%0/1
$key || ''
$key
Truth table0%0/2
A
A= $key
Aresult
11
00
357 0 $roles{lead}= 1;
358 0 $roles{navigate}= 1;
359 0 $self->update_published_state(viewer_count => scalar keys $self->viewers->%*);
360 }
361 elsif (defined $key) {
362 0 $c->send({ json => { key_incorrect => 1 } });
363 }
364 }
365 0 $c->stash('roles', join ',', keys %roles);
366 0 $self->log->info(sprintf "%s (%s) connected as %s", $id, $c->tx->remote_address, $c->stash('roles'));
367 0 $c->send({ json => { roles => [ keys %roles ] } });
368
369 0 ▶ $c->on(json => sub($c, $msg, @) { $c->app->on_viewer_message($c, $msg) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
370 0 ▶ $c->on(finish => sub($c, @) { $c->app->on_viewer_disconnect($c) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
371 0 $c->inactivity_timeout(3600);
372 #my $keepalive= Mojo::IOLoop->recurring(60 => sub { $viewers{$id}->send([1, 0, 0, 0, WS_PING, '']); });
373 #$c->stash(keepalive => $keepalive);
374 }
375  
376  
377 0 ▶sub on_viewer_message($self, $c, $msg) {
Subroutine on_viewer_message: not called CC=8 SCAR=42.8
Pod documented
378 0 my $id= $c->req->request_id;
379 0 ▶ $self->log->debug(sprintf "client %s %s msg=%s", $id, $c->tx->original_remote_address//'', $msg//'');
Condition0%0/4
$c->tx->original_remote_address // ""
l!l
00
$msg // ""
l!l
00
MC/DC0%0/1
$c->tx->original_remote_address // ""
$c->tx->original_remote_address
MC/DC0%0/1
$msg // ""
$msg
Truth table0%0/2
A
A= $c->tx->original_remote_address
Aresult
11
00
Truth table0%0/2
A
A= $msg
Aresult
11
00
380 0 ▶ if ($c->stash('roles') =~ /\blead\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->stash("roles") =~ /\blead\b/u) 0 0 0
381 0 ▶ if (defined $msg->{extern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $msg->{"extern"}) 0 0 0
382 }
383 0 ▶ if (defined $msg->{slide_num}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $msg->{"slide_num"}) 0 0 0
384 $self->update_published_state(
385 slide_num => $msg->{slide_num},
386 step_num => $msg->{step_num},
387 ($msg->{slide_num} > ($self->published_state->{slide_max}//0)?
388 0 ▶ ( slide_max => $msg->{slide_num} ) : ()
Branch: 1 branch
BranchTrueFalseTotal
$msg->{'slide_num'} > ($self->published_state->{'slide_max'} // 0) ? : 0 0 0
Condition0%0/2
$self->published_state->{"slide_max"} // 0
l!l
00
MC/DC0%0/1
$self->published_state->{"slide_max"} // 0
$self->published_state->{"slide_max"}
Truth table0%0/2
A
A= $self->published_state->{"slide_max"}
Aresult
11
00
389 )
390 );
391 }
392 }
393 # if ($c->stash('roles') =~ /\b
394 }
395  
396 0 ▶sub on_viewer_disconnect($self, $c) {
Subroutine on_viewer_disconnect: not called CC=1 SCAR=6.9
Pod documented
397 0 my $id= $c->req->request_id;
398 #Mojo::IOLoop->remove($keepalive);
399 0 delete $self->viewers->{$id};
400 0 $self->update_published_state(viewer_count => scalar keys $self->viewers->%*);
401 }
402  
403 9 ▶sub on_page_changed($self) {
Subroutine on_page_changed: called CC=2 SCAR=6.9
Pod undocumented
404 $_->send({ json => { page_changed => 1 } })
405 9 for values $self->viewers->%*;
406 }
407  
408 9 ▶sub on_slides_changed($self, $changed) {
Subroutine on_slides_changed: called CC=2 SCAR=7.6
Pod undocumented
409 9 my @changes= map +{ idx => $_, html => $self->slides_dom->[$_] }, @$changed;
410 9 for my $viewer (values $self->viewers->%*) {
411 0 $viewer->send({ json => { slides_changed => \@changes } })
412 }
413 }
414  
415  
416 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
417 3 our @EXPORT_OK= qw( mojo2logany );
418  
419 # Utility method to create a Mojo logger that logs to Log::Any
420 9 ▶sub mojo2logany($logger= undef) {
Subroutine mojo2logany: called CC=5 SCAR=17.1
Pod documented
421 9 require Mojo::Log;
422 9 require Log::Any;
423 9 ▶ if (defined $logger && !ref $logger) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $logger and not ref $logger) { } 0 9 9
elsif (not defined $logger) { } 9 0 9
Condition33%1/3
defined $logger and not ref $logger
!ll&&!rl&&r
900
MC/DC0%0/2
defined $logger and not ref $logger
defined $logger not ref $logger
Truth table33%1/3
A and not(B)
A= defined $loggerB= not ref $logger
ABresult
0X0
100
111
424 0 $logger= Log::Any->get_logger(category => $logger);
425 } elsif (!defined $logger) {
426 9 $logger= Log::Any->get_logger;
427 }
428 9 my $mlog= Mojo::Log->new;
429 9 $mlog->unsubscribe('message');
430 9 ▶ $mlog->on(message => sub($app, $level, @lines) { $logger->$level(join ' ', @lines) });
Subroutine __ANON__: called CC=1 SCAR=0.0
431 9 $mlog;
432 }
433  
434 3 1;
435  
436 __END__
↑ Summary