blib/lib/Mojolicious/Plugin/RevealJS.pm

filter: Statementstmt 77.0% 67 / 87 Branchbran 72.7% 16 / 22 Conditioncond 58.8% 10 / 17 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 93.3% 14 / 15 Podpod 100% 1 / 1 totaltotal 72.8% 110 / 151 CC 19 SCAR 19.3
CC
19
Cov
74%
_export
12.0
_include_sample
11.4
_include_code
8.2
CRAP 6.9
↑ Summary
1 package Mojolicious::Plugin::RevealJS;
2  
3 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use 5.12.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.21';
8 1 $VERSION = eval $VERSION;
9  
10 1 ▶use Mojo::Home;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Mojo::ByteStream 'b';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Mojo::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use File::Basename 'dirname';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Share ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶has home => sub { Mojo::Home->new(File::Share::dist_dir('Mojolicious-Plugin-RevealJS')) };
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 1 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
20 1 my ($plugin, $app, $conf) = @_;
21 1 my $home = $plugin->home;
22 1 push @{ $app->static->paths }, $home->child('public');
23 1 push @{ $app->renderer->paths }, $home->child('templates');
24  
25 1 $app->defaults('revealjs.init' => {
26 controls => \1,
27 progress => \1,
28 history => \1,
29 center => \1,
30 transition => 'slide', #none/fade/slide/convex/concave/zoom
31 });
32  
33 1 $app->helper('revealjs.export' => \&_export);
34  
35 1 $app->helper(include_code => \&_include_code);
36 1 $app->helper(include_sample => \&_include_sample);
37 1 ▶ $app->helper(section => sub { shift->tag(section => @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
38 3 ▶ $app->helper(markdown_section => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 3 my ($c, @args) = @_;
40 3 ▶ return $c->tag(section => data => { markdown => undef } => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
41 3 return $c->tag(script => (type => 'text/template') => @args);
42 3 });
43 1 });
44 }
45  
46 15 ▶sub _include_code {
Subroutine _include_code: called CC=8 SCAR=21.0
47 15 my ($c, $filename, %opts) = @_;
48 15 ▶ my $file = $c->stash->{'revealjs.private.files'}{$filename}
Condition66%2/3
$c->stash->{"revealjs.private.files"}{$filename} ||= $c->app->home->rel_file($filename)->slurp
l!l&&r!l&&!r
690
MC/DC0%0/2
$c->stash->{"revealjs.private.files"}{$filename} ||= $c->app->home->rel_file($filename)->slurp
$c->stash->{"revealjs.private.files"}{$filename} $c->app->home->rel_file($filename)->slurp
Truth table66%2/3
A ||= B
A= $c->stash->{"revealjs.private.files"}{$filename}B= $c->app->home->rel_file($filename)->slurp
ABresult
1X1
011
000
49 ||= $c->app->home->rel_file($filename)->slurp;
50 15 my $mark = qr'^\h*(?:#+|-{2,}|/{2,}|<!--)\h*reveal'm;
51  
52 15 ▶ if (my $section = delete $opts{section}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $section = delete $opts{"section"}) 3 12 15
53 3 my @sections = split /$mark\h+(?:begin|end)\h+\Q$section\E\N*\R/ms, $file, 3;
54 3 ▶ $file = Mojo::Util::trim($sections[1]) if @sections > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @sections > 1 3 0 3
55 }
56  
57 15 $file =~ s/$mark\N*\R//mg;
58  
59 15 my $template = <<' INCLUDE';
60 % my $text = stash 'revealjs.private.text';
61 % my $file = stash 'revealjs.private.file';
62 % my $lang = stash 'revealjs.private.lang';
63 <div class="mojo include-code">
64 <pre><code class="<%= $lang %>" data-trim>
65 <%= $text =%>
66 </code></pre>
67 % if (defined $file) {
68 <p class="filename mojo sample-annotation" style="float: right; text-color: white; font-size: small;"><%= $file %></p>
69 % }
70 </div>
71 INCLUDE
72  
73 15 ▶ $filename = undef if exists $opts{include_filename} && !$opts{include_filename};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"include_filename"} and not $opts{"include_filename"} 3 12 15
Condition66%2/3
exists $opts{"include_filename"} and not $opts{"include_filename"}
!ll&&!rl&&r
1203
MC/DC50%1/2
exists $opts{"include_filename"} and not $opts{"include_filename"}
exists $opts{"include_filename"} not $opts{"include_filename"}
Truth table66%2/3
A and not(B)
A= exists $opts{"include_filename"}B= not $opts{"include_filename"}
ABresult
0X0
100
111
74  
75 my $html = $c->render_to_string(
76 inline => $template,
77 'revealjs.private.text' => $file,
78 'revealjs.private.file' => $filename,
79 15 ▶ 'revealjs.private.lang' => delete($opts{language}) // $c->stash('language') // 'perl',
Condition80%4/5
(delete $opts{"language"} // $c->stash("language")) // "perl"
l!l
312
delete $opts{"language"} // $c->stash("language")
l!l&&r!l&&!r
3012
MC/DC50%1/2
(delete $opts{"language"} // $c->stash("language")) // "perl"
delete $opts{"language"} $c->stash("language")
Truth table66%2/3
A // B
A= delete $opts{"language"}B= $c->stash("language")
ABresult
1X1
011
000
80 %opts
81 );
82 15 return b $html;
83 }
84  
85 9 ▶sub _include_sample {
Subroutine _include_sample: called CC=10 SCAR=24.4
86 9 my ($c, $sample, %opts) = @_;
87 9 my $template = <<' INCLUDE';
88 <div class="mojo include-code">
89 <pre><%= t code => @$code %></pre>
90 % if (defined $annotation) {
91 <p class="mojo sample-annotation" style="float: right; text-color: white; font-size: small;"><%= $annotation %></p>
92 % }
93 </div>
94 INCLUDE
95  
96 9 my (@code, %data);
97 9 ▶ my $lang = $opts{language} // $opts{lang} // $c->stash('language');
Condition33%2/6
($opts{"language"} // $opts{"lang"}) // $c->stash("language")
l!l&&r!l&&!r
009
$opts{"language"} // $opts{"lang"}
l!l&&r!l&&!r
009
MC/DC0%0/3
($opts{"language"} // $opts{"lang"}) // $c->stash("language")
$opts{"language"} $opts{"lang"} $c->stash("language")
Truth table25%1/4
A // B // C
A= $opts{"language"}B= $opts{"lang"}C= $c->stash("language")
ABCresult
1XX1
01X1
0011
0000
98 9 ▶ if (defined $lang) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lang) 0 9 9
99 0 ▶ $lang = "lang-$lang" unless $lang =~ /^lang-/;
Branch: 1 branch
BranchTrueFalseTotal
unless $lang =~ /^lang-/u 0 0 0
100 0 push @code, class => $lang;
101 }
102  
103 9 $data{sample} = $sample;
104 9 ▶ $data{trim} = $opts{trim} if exists $opts{trim};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"trim"} 3 6 9
105 9 ▶ $data{noescape} = $opts{noescape} if exists $opts{noescape};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"noescape"} 3 6 9
106 9 ▶ $data{'sample-mark'} = $opts{mark} if exists $opts{mark};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"mark"} 3 6 9
107 9 ▶ $data{'sample-indent'} = $opts{indent} if exists $opts{indent};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"indent"} 3 6 9
108 9 push @code, data => \%data;
109  
110 9 my $anno_default = $sample;
111 9 $anno_default =~ s/\#.*$//;
112  
113 my $html = $c->render_to_string(
114 inline => $template,
115 code => \@code,
116 9 ▶ annotation => exists $opts{annotation} ? $opts{annotation} : $anno_default,
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'annotation'} ? : 6 3 9
117 );
118 9 return $html;
119 }
120  
121 0 ▶sub _export {
Subroutine _export: not called CC=3 SCAR=24.8
122 0 my ($c, $page, $to, $opts) = @_;
123 0 require Mojo::Util;
124 0 require File::Copy::Recursive;
125 0 File::Copy::Recursive->import('dircopy');
126 0 File::Copy::Recursive::pathmk($to);
127  
128 0 my $body = $c->ua->get($page)->res->body;
129  
130 # handle munging the base tag
131 0 ▶ if (my $base = $opts->{base}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $base = $opts->{"base"}) 0 0 0
132 0 require Mojo::DOM;
133 0 require Mojo::Util;
134 0 $base = Mojo::Util::xml_escape($base);
135 0 my $dom = Mojo::DOM->new($body);
136 0 $dom->at('base')->remove;
137 0 $dom->at('head')->child_nodes->first->prepend(qq[<base href="$base">]);
138 0 $body = $dom->to_string;
139 }
140  
141 0 Mojo::File->new($to)->child('index.html')->spurt($body);
142 0 for my $path( @{ $c->app->static->paths } ) {
143 0 dircopy($path, $to);
144 }
145 }
146  
147 1 1;
148  
149 =encoding utf8
150  
151 =head1 NAME
152  
153 Mojolicious::Plugin::RevealJS - Mojolicious ❤️ Reveal.js
154  
155 =head1 SYNOPSIS
156  
157 use Mojolicious::Lite;
158  
159 plugin 'RevealJS';
160  
161 any '/' => { template => 'mytalk', layout => 'revealjs' };
162  
163 app->start;
164  
165 =head1 DESCRIPTION
166  
167 L<Mojolicious::Plugin::RevealJS> is yet another attempt at making presentations with L<Mojolicious>.
168 While the author's previous attempts have tried do too much, this one simply makes it easier to use L<Reveal.js|http://lab.hakim.se/reveal-js>.
169 It provides a layout (C<revealjs>) which contains the boilerplate and loads the bundled libraries.
170 It also provides a few simple helpers.
171 Future versions of the plugin will allow setting of configuration like themes.
172  
173 The bundled version of Reveal.js is currently 3.7.0.
174 The bundled version of reveal-sampler is currently cd4a07d.
175  
176 Note that this module is in an alpha form!
177 The author makes no compatibilty promises.
178  
179 =head1 LAYOUTS
180  
181 # controller
182 $c->layout('revealjs'); # or
183 $c->stash(layout => 'revealjs');
184  
185 # or template
186 % layout 'revealjs';
187  
188 =head2 revealjs
189  
190 This layout is essentially the standard template distributed as part of the Reveal.js tarball.
191 It is modified for use in a Mojolicious template.
192  
193 =head3 stash paramters
194  
195 It accepts the stash parameters:
196  
197 =over
198  
199 =item *
200  
201 author - sets the metadata value
202  
203 =item *
204  
205 description - sets the metadata value
206  
207 =item *
208  
209 init - Reveal.js initialization options, a hashref for JSON conversion documented below
210  
211 =item *
212  
213 theme - a string representing a theme css to be included.
214 If the string ends in C<.css> it is included literally, otherwise it is assumed to be the name of a bundled Reveal.js theme.
215 Bundled themes are: black, white, league, beige, sky, night, serif, simple, solarized.
216 Defaults to black.
217 See more on the L<"Reveal.js page"|https://github.com/hakimel/reveal.js#theming>.
218  
219 =item *
220  
221 title - sets the window title, not used on the title slide
222  
223 =item *
224  
225 base - sets the C<< <base> >> tag for the document.
226 Useful for hosting static pages at a location other than C</>.
227 Defaults to C</>, if explicitly set to C<undef> the tag is not included.
228  
229 =item *
230  
231 hljs_theme_url - sets the url path for loading a css theme for highlight js.
232 Defaults to a bundled zenburn theme.
233  
234 =back
235  
236 =head3 initialization parameters
237  
238 As mentioned above, the stash key C<init> is a hashref that is merge into a set of defaults and used to initialize Reveal.js.
239 Some RevealJS initialization options, specifically those that have a default are:
240  
241 =over
242  
243 =item *
244  
245 center - enable slide centering (boolean, true by default)
246  
247 =item *
248  
249 controls - enable controls (boolean, true by default)
250  
251 =item *
252  
253 history - enable history (boolean, true by default)
254  
255 =item *
256  
257 progress - enable progress indicator (boolean, true by default)
258  
259 =item *
260  
261 transition - set the slide transition type (one of: none, fade, slide, convex, concave, zoom; default: slide)
262  
263 =back
264  
265 These defaults are set in the default stash value for C<revealjs.init>.
266 So they can be modified globally modifying that value (probably during setup).
267  
268 $app->defaults->{'revealjs.init'}{transition} = 'none';
269  
270 Note that booleans are references to scalar values, C<true == \1>, C<false == \0>.
271 See more availalbe options on the L<"Reveal.js page"|https://github.com/hakimel/reveal.js#configuration>.
272  
273 =head3 additional templates
274  
275 In order to further customize the template the following unimplemented templates are included into the layout
276  
277 =over
278  
279 =item *
280  
281 C<revealjs_head.html.ep> - included at the end of the C<< <head> >> tag.
282  
283 =item *
284  
285 C<revealjs_preinit.js.ep> - included just before initializing Reveal.js.
286 Especially useful to modify the javascript variable C<init>.
287  
288 =item *
289  
290 C<revealjs_body.html.ep> - included at the end of the C<< <body> >> tag.
291  
292 =back
293  
294 =head1 HELPERS
295  
296 =head2 include_code
297  
298 %= include_code 'path/to/file.pl'
299  
300 NOTE this helper is mildly-deprecated in favor of the reveal-sampler plugin and L</include_sample>.
301 It isn't going away yet, but if things work out with that functionality this method may eventually be implemented via it or removed entirely.
302  
303 This helper does several things:
304  
305 =over
306  
307 =item *
308  
309 localizes trailing arguments into the stash
310  
311 =item *
312  
313 slurps a file containing code
314  
315 =item *
316  
317 http escapes the content
318  
319 =item *
320  
321 applies some simple formatting
322  
323 =item *
324  
325 displays the relative path to the location of the file (for the benefit of repo cloners)
326  
327 =back
328  
329 The helper takes a file name and additional key-value pairs.
330 The following keys and their value are removed from the pairs, the remaining are localized into the stash:
331  
332 =over
333  
334 =item language
335  
336 sets the language for the highlighting, defaults to the value of C<< stash('language') // 'perl' >>
337  
338 =item section
339  
340 limits the section to a given section name
341  
342 =item include_filename
343  
344 if true (default) include the filename when the code is included
345  
346 =back
347  
348 NOTE: This feature is experimental!
349  
350 The section is definite by a line comment of the form C<#> or C<//> or C<--> or C<< <!-- >> followed by C<reveal begin $name> and ended with comment mark followed by C<reveal end $name>.
351  
352 %= include_code 'path/to/file', section => 'part1'
353  
354 Then in the file
355  
356 Excluded content
357  
358 # reveal begin part1
359 Included content
360 # reveal end part1
361  
362 Excluded content
363  
364 =head2 include_sample
365  
366 %= include_sample 'path/to/file.pl'
367  
368 The spiritual successor (and possbily actually the sucessor) to L</include_code>.
369 The heavy lifting is done in the client via the reveal-sampler plugin which is bundled.
370 It is much simpler than L</include_code>.
371  
372 It takes the url of the file to render, which must be in a publicly available via static render.
373 This file path may also contain a url fragment designating the section or line numbers to display.
374 Read more at L<https://github.com/ldionne/reveal-sampler>.
375  
376 After the file url, the following trailing key-value pair options are available.
377  
378 =over
379  
380 =item language
381  
382 Sets the language for the highlighting.
383 Note that the alias C<lang> is also allowed and defaults to the value of the C<language> stash value.
384 If this is not set, the client-side code will also attempt to set it based on the file extension.
385  
386 =item mark
387  
388 Sets lines to be marked by the client.
389 This follows the documentation at L<https://github.com/ldionne/reveal-sampler>.
390  
391 =item indent
392  
393 Instructs reveal-sampler to "keep" or "remove" any overall indentation in the sample.
394 This follows the documentation at L<https://github.com/ldionne/reveal-sampler>.
395  
396 Note that the default behavior (for when "indent" is not set) can be set in L</init>.
397 The default (from the plugin itself), is false.
398  
399 $c->stash( init => { sampler => { removeIndentation => \1 } } );
400  
401 =item trim
402  
403 Sets the C<data-trim> attribute for revealjs.
404  
405 =item noescape
406  
407 Sets the C<data-noescape> attribute for revealjs.
408 Note that if the L</mark> option is used, the front-end will automatically apply this attribute.
409  
410 =item annotation
411  
412 A text line to be rendered below the code section.
413 This is normally used to display the file name/path.
414 If not explicitly given it will default to the url of the file (without any fragment).
415 If explicitly undefined, the annotation will not be rendered.
416  
417 =back
418  
419 =head2 section
420  
421 %= section begin
422 ...
423 % end
424  
425 A shortcut for creating a section tag.
426  
427 %# longer form
428 %= tag section => ...
429  
430 =head2 markdown_section
431  
432 %= markdown_section begin
433 ...
434 % end
435  
436 Build a section tag and script/template tag to properly use the built-in markdown handling within this slide.
437  
438 =head2 revealjs->export
439  
440 $ ./myapp.pl eval 'app->revealjs->export("/" => "path/", \%options)'
441  
442 Exports the rendered page and all of the files in the static directories to the designated path.
443 This is very crude, but effective for usual cases.
444  
445 Allowed options are:
446  
447 =over
448  
449 =item base
450  
451 Override the base tag by removing the original and inserting a new one just inside the C<< <head> >> tag with the given value as the href target.
452 This feature is cludgy (as is this whole helper), consider it experimental, its behavior may change.
453  
454 =back
455  
456 =head1 SOURCE REPOSITORY
457  
458 L<http://github.com/jberger/Mojolicious-Plugin-RevealJS>
459  
460 =head1 AUTHOR
461  
462 Joel Berger, E<lt>joel.a.berger@gmail.comE<gt>
463  
464 =head1 COPYRIGHT AND LICENSE
465  
466 Copyright (C) 2015 by Joel Berger
467  
468 This library is free software; you can redistribute it and/or modify
469 it under the same terms as Perl itself.
470  
471 Reveal.js (bundled) is Copyright (C) 2015 Hakim El Hattab, http://hakim.se and released under the MIT license.
472 reveal-sampler (bundled) is Copyright (C) 2017 Louis Dionne and released under the MIT license.
↑ Summary