blib/lib/Statocles/Plugin/Diagram/Mermaid.pm

filter: Statementstmt 96.5% 28 / 29 Branchbran 83.3% 5 / 6 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 7 / 7 Podpod 50.0% 1 / 2 totaltotal 89.8% 44 / 49 CC 5 SCAR 10.5
CC
5
Cov
92%
mermaid
5.1
BEGIN
1.0
BEGIN
1.0
CRAP 2.8
« blib/lib/Statocles/Page/File.pm ↑ Summary blib/lib/Statocles/Command.pm »
1 package Statocles::Plugin::Diagram::Mermaid;
2 2 our $VERSION = '0.099';
3 # ABSTRACT: Render diagrams using mermaid https://mermaidjs.github.io
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod # --- Configuration
8 #pod # site.yml
9 #pod ---
10 #pod site:
11 #pod class: Statocles::Site
12 #pod args:
13 #pod plugins:
14 #pod diagram:
15 #pod $class: Statocles::Plugin::Diagram::Mermaid
16 #pod
17 #pod # --- Usage
18 #pod <%= diagram mermaid => begin %>
19 #pod sequenceDiagram
20 #pod loop every day
21 #pod Alice->>John: Hello John, how are you?
22 #pod John-->>Alice: Great!
23 #pod end
24 #pod <% end %>
25 #pod
26 #pod =head1 DESCRIPTION
27 #pod
28 #pod This plugin adds the C<diagram> helper function to all templates and
29 #pod content documents, allowing for creation of L<mermaid|https://mermaidjs.github.io>
30 #pod diagrams.
31 #pod
32 #pod =cut
33  
34 2 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use Statocles::Base 'Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 with 'Statocles::Plugin';
37  
38  
39 #pod =attr mermaid_url
40 #pod
41 #pod Set the url to use as a, possibly local, alternative to the default script
42 #pod L<mermaid.min.js|https://unpkg.com/mermaid/dist/mermaid.min.js> for including in
43 #pod a script tag.
44 #pod
45 #pod =cut
46  
47 has mermaid_url => (
48 is => 'ro',
49 isa => InstanceOf['Mojo::URL'],
50 2 ▶ default => sub { Mojo::URL->new('https://unpkg.com/mermaid/dist/mermaid.min.js') },
Subroutine __ANON__: called CC=1 SCAR=0.0
51 2 ▶ coerce => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
52 2 my ( $args ) = @_;
53 2 return Mojo::URL->new( $args );
54 },
55 2 );
56  
57  
58 #pod =method diagram
59 #pod
60 #pod %= diagram $type => $content
61 #pod
62 #pod Wrap the given C<$content> with the html for displaying the diagram with
63 #pod C<mermaid.js>.
64 #pod
65 #pod In most cases displaying a diagram will require the use of C<begin>/C<end>:
66 #pod
67 #pod %= diagram mermaid => begin
68 #pod graph TD
69 #pod A[Christmas] -->|Get money| B(Go shopping)
70 #pod B --> C{Let me think}
71 #pod C -->|One| D[Laptop]
72 #pod C -->|Two| E[iPhone]
73 #pod C -->|Three| F[Car]
74 #pod % end
75 #pod
76 #pod =cut
77  
78 # https://unpkg.com/mermaid@7.1.0/dist/mermaid.min.js
79 # <script src="./mermaid.min.js"></script>
80 # <script>
81 # mermaid.initialize({startOnLoad: true, theme: 'forest'});
82 # </script>
83 2 ▶sub mermaid {
Subroutine mermaid: called CC=5 SCAR=16.2
Pod undocumented
84 2 my ($self, $args, @args) = @_;
85 2 my ( $text, $type ) = ( pop @args, pop @args );
86  
87 # Handle Mojolicious begin/end
88 2 ▶ if ( ref $text eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $text eq "CODE") 1 1 2
89 1 $text = $text->();
90 # begin/end starts with a newline, so remove it to prevent too
91 # much top space
92 1 $text =~ s/\n$//;
93 }
94  
95 2 ▶ my $page = $args->{page} || $args->{self};
Condition66%2/3
$args->{'page'} || $args->{'self'}
l!l&&r!l&&!r
101
MC/DC50%1/2
$args->{'page'} || $args->{'self'}
$args->{'page'} $args->{'self'}
Truth table66%2/3
A || B
A= $args->{'page'}B= $args->{'self'}
ABresult
1X1
011
000
96 2 ▶ if ( $page ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page) 1 1 2
97 # Add the appropriate stylesheet to the page
98 1 my $mermaid_url = $self->mermaid_url->to_string;
99 0 ▶ if ( !grep { $_->href eq $mermaid_url } $page->links( 'script' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_->href eq $mermaid_url;} $page->links("script")) 1 0 1
100 1 $page->links( script => {href => $mermaid_url} );
101 1 $page->links( script => {
102 href => '',
103 text => q|mermaid.initialize({startOnLoad: true, theme: 'forest'});|
104 } );
105 }
106 }
107 2 return qq{<div class="mermaid">$text</div>};
108 }
109  
110 #pod =method register
111 #pod
112 #pod Register this plugin with the site. Called automatically.
113 #pod
114 #pod =cut
115  
116 2 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
117 2 my ( $self, $site ) = @_;
118 2 ▶ $site->theme->helper( diagram => sub { $self->mermaid( @_ ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
119 }
120  
121 2 1;
122  
123 __END__
« blib/lib/Statocles/Page/File.pm ↑ Summary blib/lib/Statocles/Command.pm »