| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
}
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
}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | my @slides_diff= !$self->slides_dom? (0..$#slides) | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | : grep { ($self->slides_dom->[$_]//'') ne ($slides[$_]//'') } 0..$#slides; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/4
$self->slides_dom->[$_] // ""
$slides[$_] // ""
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->[$_]
Truth table0%0/2
A
A= $slides[$_]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 9 | $self->page_dom($page); | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | 9 | $self->slides_dom(\@slides); | |||||||||||||||||||||||||||||||||||||||||||||
| 79 | 9 | ▶ | $self->on_page_changed() if $page_diff; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 9 | ▶ | $self->on_slides_changed(\@slides_diff) if @slides_diff; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 7 | ▶ | if defined $opts{if_changed} && 0+$srcfile == $opts{if_changed}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $opts{"if_changed"} and 0 + $srcfile == $opts{"if_changed"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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")
ref $srcfile and ref($srcfile)->isa("IO::Handle")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | return undef | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 4 | ▶ | if defined $opts{if_changed} && (0+$srcfile) .'_'. tell($srcfile) eq $opts{if_changed}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $opts{"if_changed"} and (0 + $srcfile) . '_' . tell($srcfile) eq $opts{"if_changed"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 4 | ▶ | seek($srcfile, 0, 0) || die "seek: $!" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
tell $srcfile <= 0 || (seek($srcfile, 0, 0) || die("seek: $!"))
seek $srcfile, 0, 0 or die "seek: $!"
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | unless $content =~ /^\s*</; | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 4 | $change_token= (0+$srcfile) .'_'. tell($srcfile); | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 4 | ▶ | my $st= stat($srcfile) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $opts{"if_changed"} and $st->mtime == $opts{"if_changed"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | if $srcfile =~ /[.]md$/i; | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 4 | $change_token= $st->mtime; | |||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 15 | ▶ | return wantarray? ($content, $change_token) : $content; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | my $f= $self->slides_source_file; | |||||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | -f $f or croak "No such file '$f'"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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}
}
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}
}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$inotify and not $writable
MC/DC0%0/2
$inotify and not $writable
$inotify
not $writable
Truth table0%0/3
A and not(B)
A= $inotifyB= not $writable
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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 // ""
MC/DC0%0/1
$_->tag // ""
$_->tag
Truth table0%0/2
A
A= $_->tag
| |||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | ▶ | $keep= 1 if $_->text =~ /\S/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | $p->remove unless $keep; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
$node->{"class"} // ""
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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'
$tag eq "h1" or $tag eq "h2"
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'
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
$dom->at("div.slides") or $dom->at("body")
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 6 | ▶ | if ($tag eq 'head') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
$parent->tag // ""
MC/DC0%0/1
$parent->tag // ""
$parent->tag
Truth table50%1/2
A
A= $parent->tag
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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 // ""
MC/DC100%1/1
$node->tag // ""
$node->tag
Truth table100%2/2
A
A= $node->tag
| |||||||||||||||||||||||||||||||||||||||||||||||
| 242 | # is it a script tag located before the first slide has begun? | ||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 41 | ▶ | if ($tag eq 'script' && !@slides) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$tag eq "script" and not @slides
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
not defined $cur_slide and $node->type eq "text" and not $node->text =~ /\S/u
not defined $cur_slide and $node->type eq "text"
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 23 | ▶ | push @slides, ($cur_slide= Mojo::DOM->new_tag('div', class => 'slide')->[0]) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $cur_slide or $self->_node_starts_slide($node, $tag)
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)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
$tag eq "ul" || $tag eq "ol" and not $node->{"class"}
$tag eq "ul" or $tag eq "ol"
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$dom->at('html') || $dom
MC/DC0%0/2
$dom->at('html') || $dom
$dom->at('html')
$dom
Truth table33%1/3
A || B
A= $dom->at('html')B= $dom
| |||||||||||||||||||||||||||||||||||||||||||||||
| 274 | unless $dom->at('head'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 9 | for my $el (@move_to_head) { | |||||||||||||||||||||||||||||||||||||||||||||
| 276 | 9 | ▶ | if (($el->tag//'') eq 'head') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$el->tag // ""
MC/DC100%1/1
$el->tag // ""
$el->tag
Truth table100%2/2
A
A= $el->tag
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 9 | my $pagehead= $page->at('head'); | |||||||||||||||||||||||||||||||||||||||||||||
| 290 | # Prevent conflicting tags (TODO, more...) | ||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 9 | ▶ | if (my $title= $srchead->at('title')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 3 | ▶ | if ($srcbody->child_nodes->size) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 3 | $page->at('body')->replace($srcbody); | |||||||||||||||||||||||||||||||||||||||||||||
| 299 | 3 | ▶ | if (!$page->at('body div.slides')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
not defined $self->page_dom or $self->cache_token
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | ▶ | eval { $self->build_slides; 1 } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$opts{"presenter"} or defined $c->req->param("presenter")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | ▶ | if (($key||'') eq $self->presenter_key) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/2
$key || ''
MC/DC0%0/1
$key || ''
$key
Truth table0%0/2
A
A= $key
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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 // ""
$msg // ""
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
Truth table0%0/2
A
A= $msg
| |||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | ▶ | if ($c->stash('roles') =~ /\blead\b/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | ▶ | if (defined $msg->{extern}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | ▶ | if (defined $msg->{slide_num}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/2
$self->published_state->{"slide_max"} // 0
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $logger and not ref $logger
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||