blib/lib/App/Greple/md/Mermaid.pm

filter: Statementstmt 87.4% 202 / 231 Branchbran 62.7% 69 / 110 Conditioncond 63.2% 55 / 87 MC/DCmcdc 41.6% 20 / 48 Subroutinesub 83.3% 25 / 30 Podpod 9.0% 1 / 11 totaltotal 71.9% 372 / 517 CC 96 SCAR 27.7
CC
96
Cov
76%
__ANON__
30.0
diagram_files
30.0
draw
21.7
CRAP 16.0
« blib/lib/App/Greple/md/Blocks.pm ↑ Summary blib/lib/App/Greple/md.pm »
1 package App::Greple::md::Mermaid;
2  
3 # The images of the Mermaid diagrams of a Markdown file. They are
4 # drawn by a command (mmdr of mermaid-rs-renderer, or mmdc of
5 # mermaid-cli) into PNG files in a directory of the temporary
6 # directory, and are shown by Text::ANSI::Image as the other images
7 # are.
8  
9 342 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 342 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 342 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 342 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 342 our @EXPORT_OK = qw(diagram_files);
15  
16 342 ▶use Encode qw(encode_utf8 decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 342 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 342 ▶use Digest::SHA qw(sha1_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 342 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 342 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 342 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 342 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 342 ▶use Text::ParseWords qw(shellwords);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # The commands which draw, in the order they are looked for in PATH
26 # when none is given; the first when none is found. $COMMAND, when
27 # set, is used in place of them.
28 342 our @COMMANDS = qw(mmdc mmdr);
29 342 our $COMMAND;
30  
31 4 ▶sub default_command {
Subroutine default_command: called CC=5 SCAR=16.1
Pod undocumented
32 4 for my $name (@COMMANDS) {
33 6 for my $dir (File::Spec->path) {
34 10 my $file = File::Spec->catfile($dir, $name);
35 10 ▶ return $name if -f $file and -x _;
Branch: 1 branch
BranchTrueFalseTotal
if -f $file and -x _ 3 7 10
Condition66%2/3
-f $file and -x _
!ll&&!rl&&r
703
MC/DC50%1/2
-f $file and -x _
-f $file -x _
Truth table66%2/3
A and B
A= -f $fileB= -x _
ABresult
0X0
100
111
36 }
37 }
38 1 $COMMANDS[0];
39 }
40  
41 # Whether the command is mmdr, by its name: mmdr has other options
42 # than mmdc (no -b, -s, -q or -p) and draws without a browser
43 0 ▶sub is_mmdr { kind($_[0]) eq 'mmdr' }
Subroutine is_mmdr: not called CC=1 SCAR=6.9
Pod undocumented
44  
45 # The kind of the command, by its name: mmdr, mermaidx (of PyPI, which
46 # runs mermaid.js in Python, without a browser), or mmdc for any other
47 96 ▶sub kind {
Subroutine kind: called CC=4 SCAR=13.9
Pod undocumented
48 96 ▶ my $name = (File::Spec->splitpath($_[0] // ''))[2];
Condition50%1/2
$_[0] // ""
l!l
960
MC/DC0%0/1
$_[0] // ""
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
49 96 ▶ $name =~ /^mmdr/i ? 'mmdr'
Branch: 2 branches
BranchTrueFalseTotal
$name =~ /^mmdr/iu ? : 70 26 96
$name =~ /^mermaidx/iu ? : 5 21 26
50 : $name =~ /^mermaidx/i ? 'mermaidx'
51 : 'mmdc';
52 }
53  
54 # The options of puppeteer given to mmdc: the browser goes to the
55 # network only through a proxy that is not there, so that a diagram
56 # cannot fetch anything (a remote image in a node) when it is drawn.
57 # Nothing is fetched from the network, as for the other images.
58 # The config of Mermaid given to mmdc: a diagram is drawn in its own
59 # size, not made as narrow as the page of the browser (about 800
60 # pixels), which made the text of a wide diagram small; it is made
61 # narrower to the width when it is shown.
62 342 our %MERMAID = map { $_ => { useMaxWidth => JSON::PP::false() } } qw(
63 flowchart sequence gantt journey timeline class state er pie
64 quadrantChart xyChart requirement architecture mindmap kanban
65 gitGraph c4 sankey packet block radar
66 );
67  
68 342 our %PUPPETEER = (
69 args => [ '--proxy-server=http://127.0.0.1:9', '--proxy-bypass-list=<-loopback>' ],
70 );
71  
72 # The browsers that mmdc is given, the first found, when
73 # PUPPETEER_EXECUTABLE_PATH is not set: puppeteer looks only for its
74 # own, which the mermaid-cli of Homebrew does not have. A name
75 # without a slash is looked for in PATH.
76 342 our @BROWSERS = (
77 '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
78 '/Applications/Chromium.app/Contents/MacOS/Chromium',
79 qw(google-chrome google-chrome-stable chromium chromium-browser),
80 );
81  
82 99 ▶sub browser {
Subroutine browser: called CC=7 SCAR=19.5
Pod undocumented
83 99 ▶ return if $ENV{PUPPETEER_EXECUTABLE_PATH};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"PUPPETEER_EXECUTABLE_PATH"} 1 98 99
84 98 for my $name (@BROWSERS) {
85 579 ▶ for my $file ($name =~ m{/} ? $name
Branch: 1 branch
BranchTrueFalseTotal
$name =~ m[/]u ? : 194 385 579
86 3842 : map { File::Spec->catfile($_, $name) } File::Spec->path) {
87 4036 ▶ return $file if -f $file and -x _;
Branch: 1 branch
BranchTrueFalseTotal
if -f $file and -x _ 2 4034 4036
Condition66%2/3
-f $file and -x _
!ll&&!rl&&r
403402
MC/DC50%1/2
-f $file and -x _
-f $file -x _
Truth table66%2/3
A and B
A= -f $fileB= -x _
ABresult
0X0
100
111
88 }
89 }
90 96 return;
91 }
92  
93 # The directory of the drawn diagrams, mdee-mermaid-UID in the
94 # temporary directory, or undef when there is no place for it: a real
95 # directory of the user, not a symbolic link, which nobody else can
96 # read, for /tmp is shared. A diagram is kept by the digest of the
97 # command, its options and the code, so that the same diagram is the
98 # same file, and is drawn once. The files stay after mdee ends, which
99 # they must on paper, where ansi2pdf reads them after greple, and are
100 # left for the system to clean up; one that is gone is drawn again.
101 97 ▶sub cache_dir {
Subroutine cache_dir: called CC=7 SCAR=21.2
Pod undocumented
102 97 my $dir = File::Spec->catdir(File::Spec->tmpdir, "mdee-mermaid-$>");
103 97 ▶ -e $dir or mkdir $dir, 0700;
Branch: 1 branch
BranchTrueFalseTotal
-e $dir or mkdir $dir, 448 87 10 97
104 97 ▶ my @st = lstat $dir or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@st) = lstat $dir or return 97 0 97
105 97 ▶ -d _ && $st[4] == $> or return;
Branch: 1 branch
BranchTrueFalseTotal
-d _ and $st[4] == $> or return 96 1 97
Condition66%2/3
-d _ and $st[4] == $>
!ll&&!rl&&r
1096
MC/DC50%1/2
-d _ and $st[4] == $>
-d _ $st[4] == $>
Truth table66%2/3
A and B
A= -d _B= $st[4] == $>
ABresult
0X0
100
111
106 96 ▶ if ($st[2] & 077) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st[2] & 63) 0 96 96
107 0 ▶ chmod $st[2] & 0700, $dir or return;
Branch: 1 branch
BranchTrueFalseTotal
chmod $st[2] & 448, $dir or return 0 0 0
108 }
109 96 $dir;
110 }
111  
112 # The PNG files of the diagrams, in the order of the codes; undef for
113 # one that could not be drawn.
114 #
115 # codes => [ CODE, ... ] the text of each diagram
116 # command => 'mmdc ...' the command, split as a shell does; one
117 # whose name starts with mmdr is run as mmdr
118 # theme => 'default' the theme of Mermaid (dark in the dark mode)
119 # background => 'transparent' the background, a color of CSS
120 # scale => 1.5 the scale of the image: the larger, the
121 # more cells it takes, up to the width
122 #
123 # Each diagram is drawn in a run of its own, but mmdc, which starts a
124 # browser for each run, draws all that are to be drawn in one run
125 # first, and those it could not one by one. mmdc is given the look
126 # by its options, and the options of puppeteer (%PUPPETEER) in a file
127 # of -p. mmdr has no options for the background and the scale: the
128 # background goes in a file of -c, and the size is asked for first
129 # and given multiplied (see draw). The file is not given when the
130 # command has its own. A diagram that cannot be drawn is not shown,
131 # and is tried again the next time.
132 97 ▶sub diagram_files {
Subroutine diagram_files: called CC=29 SCAR=34.0
Pod documented
133 97 my %opt = @_;
134 97 ▶ my @codes = @{$opt{codes} // []} or return ();
Branch: 1 branch
BranchTrueFalseTotal
my(@codes) = @{$opt{"codes"} // [];} or return () 97 0 97
Condition50%1/2
$opt{"codes"} // []
l!l
970
MC/DC0%0/1
$opt{"codes"} // []
$opt{"codes"}
Truth table50%1/2
A
A= $opt{"codes"}
Aresult
11
00
135 97 ▶ my $dir = cache_dir() // return (undef) x @codes;
Condition100%2/2
cache_dir() // (return (undef) x @codes)
l!l
961
MC/DC100%1/1
cache_dir() // (return (undef) x @codes)
cache_dir()
Truth table100%2/2
A
A= cache_dir()
Aresult
11
00
136 96 ▶ my @cmd = shellwords($opt{command} || $COMMAND || default_command());
Condition33%2/6
$opt{'command'} || $App::Greple::md::Mermaid::COMMAND || default_command()
l!l&&r!l&&!r
9600
$opt{"command"} or $App::Greple::md::Mermaid::COMMAND
l!l&&r!l&&!r
9600
MC/DC0%0/3
$opt{'command'} || $App::Greple::md::Mermaid::COMMAND || default_command()
$opt{"command"} $App::Greple::md::Mermaid::COMMAND default_command()
Truth table25%1/4
A or B || C
A= $opt{"command"}B= $App::Greple::md::Mermaid::COMMANDC= default_command()
ABCresult
1XX1
01X1
0011
0000
137 96 ▶ @cmd or return (undef) x @codes;
Branch: 1 branch
BranchTrueFalseTotal
@cmd or return (undef) x @codes 96 0 96
138 96 my $kind = kind($cmd[0]);
139 96 my $mmdr = $kind eq 'mmdr';
140 96 ▶ my $background = $opt{background} || 'transparent';
Condition50%1/2
$opt{'background'} || 'transparent'
l!l
960
MC/DC0%0/1
$opt{'background'} || 'transparent'
$opt{'background'}
Truth table50%1/2
A
A= $opt{'background'}
Aresult
11
00
141 96 ▶ my $scale = $opt{scale} || 1.5;
Condition100%2/2
$opt{'scale'} || 1.5
l!l
6729
MC/DC100%1/1
$opt{'scale'} || 1.5
$opt{'scale'}
Truth table100%2/2
A
A= $opt{'scale'}
Aresult
11
00
142 96 ▶ my $theme = $opt{theme} || 'default';
Condition50%1/2
$opt{'theme'} || 'default'
l!l
960
MC/DC0%0/1
$opt{'theme'} || 'default'
$opt{'theme'}
Truth table50%1/2
A
A= $opt{'theme'}
Aresult
11
00
143 # what the digest is made of, which are also the options of mmdc
144 96 my @look = ('-t', $theme, '-b', $background, '-s', $scale);
145 my $r = { dir => $dir, cmd => \@cmd,
146 opt => {
147 mmdr => [ '-e', 'png', '-t', $theme ],
148 mmdc => [ '-e', 'png', '-q', @look ],
149 # PNG by the name of the output file
150 mermaidx => [ '-q', '-t', $theme, '-b', $background, '--scale', $scale ],
151 96 ▶ }->{$kind},
Branch: 1 branch
BranchTrueFalseTotal
$mmdr ? : 70 26 96
152 $mmdr ? (scale => $scale) : () };
153 # files of options, each as [ FLAG, OWN, DATA ], not given when the
154 # command has its own: the background for mmdr; for mmdc, the
155 # browser, and the diagrams in their own size (%MERMAID)
156 96 my $c = qr/^(?:-c|--configFile(?:=|$))/;
157 96 my @conf = @{ {
158 mmdr => [ [ '-c', $c, { themeVariables => { background => "$background" } } ] ],
159 mmdc => [ [ '-p', qr/^(?:-p|--puppeteerConfigFile(?:=|$))/,
160 0 { %PUPPETEER, map { (executablePath => $_) } browser() } ],
161 [ '-c', $c, \%MERMAID ] ],
162 96 ▶ }->{$kind} // [] };
Condition100%2/2
{"mmdr", [["-c", $c, {"themeVariables", {"background", "$background"}}]], "mmdc", [["-p", qr/^(?:-p|--puppeteerConfigFile(?:=|$))/u, {%App::Greple::md::Mermaid::PUPPETEER, map({"executablePath", $_;} browser())}], ["-c", $c, \%App::Greple::md::Mermaid::MERMAID]]}->{$kind} // []
l!l
915
MC/DC100%1/1
{"mmdr", [["-c", $c, {"themeVariables", {"background", "$background"}}]], "mmdc", [["-p", qr/^(?:-p|--puppeteerConfigFile(?:=|$))/u, {%App::Greple::md::Mermaid::PUPPETEER, map({"executablePath", $_;} browser())}], ["-c", $c, \%App::Greple::md::Mermaid::MERMAID]]}->{$kind} // []
{"mmdr", [["-c", $c, {"themeVariables", {"background", "$background"}}]], "mmdc", [["-p", qr/^(?:-p|--puppeteerConfigFile(?:=|$))/u, {%App::Greple::md::Mermaid::PUPPETEER, map({"executablePath", $_;} browser())}], ["-c", $c, \%App::Greple::md::Mermaid::MERMAID]]}->{$kind}
Truth table100%2/2
A
A= {"mmdr", [["-c", $c, {"themeVariables", {"background", "$background"}}]], "mmdc", [["-p", qr/^(?:-p|--puppeteerConfigFile(?:=|$))/u, {%App::Greple::md::Mermaid::PUPPETEER, map({"executablePath", $_;} browser())}], ["-c", $c, \%App::Greple::md::Mermaid::MERMAID]]}->{$kind}
Aresult
11
00
163 # what the digest is made of besides: the config of Mermaid, which
164 # changes the diagram, unless the command has its own
165 19 my @more = map { JSON::PP->new->canonical->encode($_->[2]) }
166 96 ▶ grep { $_->[2] == \%MERMAID and not grep { /$c/ } @cmd } @conf;
Condition100%3/3
$_->[2] == \%App::Greple::md::Mermaid::MERMAID && !grep({/$c/u;} @cmd)
!ll&&!rl&&r
91219
MC/DC100%2/2
$_->[2] == \%App::Greple::md::Mermaid::MERMAID && !grep({/$c/u;} @cmd)
$_->[2] == \%App::Greple::md::Mermaid::MERMAID !grep({/$c/u;} @cmd)
Truth table100%3/3
A && not(B)
A= $_->[2] == \%App::Greple::md::Mermaid::MERMAIDB= !grep({/$c/u;} @cmd)
ABresult
0X0
100
111
167 # the file of each diagram, by the digest
168 my @path = map {
169 96 File::Spec->catfile($dir,
170 sha1_hex(encode_utf8(join "\0", @cmd, @look, @more, s/\n?\z/\n/r)) . '.png');
171 } @codes;
172 # those to be drawn, once each; without a place to write, the
173 # diagrams drawn before are shown
174 96 my(%seen, @todo);
175 96 ▶ if (-w $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-w $dir) 95 1 96
176 95 for my $i (0 .. $#codes) {
177 push @todo, [ $path[$i], $codes[$i] =~ s/\n?\z/\n/r ]
178 188 ▶ if !-e $path[$i] and !$seen{$path[$i]}++;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path[$i] or $seen{$path[$i]}++ 145 43 188
Condition100%3/3
-e $path[$i] or $seen{$path[$i]}++
l!l&&r!l&&!r
1528145
MC/DC100%2/2
-e $path[$i] or $seen{$path[$i]}++
-e $path[$i] $seen{$path[$i]}++
Truth table100%3/3
A or B
A= -e $path[$i]B= $seen{$path[$i]}++
ABresult
1X1
011
000
179 }
180 }
181 96 my(%error, @file);
182 96 ▶ if (@todo) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo) 90 6 96
183 # the files of options, after the digest, which must not depend
184 # on their names
185 90 for (@conf) {
186 106 my($flag, $own, $data) = @$_;
187 106 ▶ next if grep { /$own/ } @cmd;
Branch: 1 branch
BranchTrueFalseTotal
if grep {/$own/u;} @cmd 7 99 106
188 99 ▶ my $file = config_file($dir, $data) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $file = config_file($dir, $data) or next 99 0 99
189 99 push @file, $file;
190 99 push @{$r->{opt}}, $flag, "$file";
191 }
192 90 my $progress = progress(basename($cmd[0]));
193 90 ▶ my $diagrams = sub { "$_[0] Mermaid diagram" . ($_[0] > 1 ? 's' : '') };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] > 1 ? : 50 40 90
194 90 $progress->('drawing ' . $diagrams->(scalar @todo) . '...');
195 # mmdc starts a browser for each run: all in one run first
196 90 ▶ draw_all($r, $progress, @todo) if $kind eq 'mmdc' and @todo > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "mmdc" and @todo > 1 9 81 90
Condition100%3/3
$kind eq "mmdc" and @todo > 1
!ll&&!rl&&r
69129
MC/DC100%2/2
$kind eq "mmdc" and @todo > 1
$kind eq "mmdc" @todo > 1
Truth table100%3/3
A and B
A= $kind eq "mmdc"B= @todo > 1
ABresult
0X0
100
111
197 87 my @left = grep { !-s $_->[0] } @todo;
198 87 for my $n (1 .. @left) {
199 115 my($path, $code) = @{$left[$n - 1]};
200 115 ▶ $progress->("drawing Mermaid diagram $n of " . @left . '...') if @left > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @left > 1 75 40 115
201 115 $error{$path} = draw($r, $code, $path);
202 }
203 28 $progress->();
204 }
205 map {
206 34 my $path = $path[$_];
207 # what the command said when it failed, for the author to see
208 59 ▶ if ($opt{errors} and defined $error{$path} and $error{$path} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"errors"} and defined $error{$path} and $error{$path} ne "") 7 52 59
Condition83%5/6
$opt{"errors"} and defined $error{$path} and $error{$path} ne ""
!ll&&!rl&&r
5027
$opt{"errors"} and defined $error{$path}
!ll&&!rl&&r
0509
MC/DC66%2/3
$opt{"errors"} and defined $error{$path} and $error{$path} ne ""
$opt{"errors"} defined $error{$path} $error{$path} ne ""
Truth table75%3/4
A and B and C
A= $opt{"errors"}B= defined $error{$path}C= $error{$path} ne ""
ABCresult
0XX0
10X0
1100
1111
209 7 $opt{errors}[$_] = [ basename($cmd[0]), split /\n/, $error{$path} ];
210 }
211 59 ▶ -s $path ? $path : undef;
Branch: 1 branch
BranchTrueFalseTotal
-s $path ? : 49 10 59
212 } 0 .. $#codes;
213 }
214  
215 # Lines on the terminal while the diagrams are drawn, which may take
216 # seconds with mmdc, so that one knows that something is going on:
217 # shown on the standard error when it is a terminal, a line for each
218 # step, with the seconds gone by on the line of the last. Returns a
219 # function to call with the text that follows the name of the command,
220 # and with nothing at the end. run() calls $TICK every tenth of a
221 # second while it waits, which puts the seconds again when they have
222 # changed.
223 342 our $TICK;
224 90 ▶sub progress {
Subroutine progress: called CC=2 SCAR=16.3
Pod undocumented
225 90 my $name = shift;
226 90 ▶ -t STDERR or return sub {};
Subroutine __ANON__: called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
-t STDERR or return sub { } 0 90 90
227 0 my $start = time;
228 0 my($text, $sec) = ('', 0);
229 0 ▶ my $line = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
230 0 ▶ print STDERR "\r\e[K$name: $text", $sec ? " (${sec}s)" : '';
Branch: 1 branch
BranchTrueFalseTotal
$sec ? : 0 0 0
231 0 };
232 0 ▶ $TICK = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
233 0 my $s = time - $start;
234 0 ▶ $line->() if length $text and $s != $sec and ($sec = $s);
Branch: 1 branch
BranchTrueFalseTotal
if length $text and $s != $sec and $sec = $s 0 0 0
Condition0%0/6
length $text and $s != $sec and $sec = $s
!ll&&!rl&&r
000
length $text and $s != $sec
!ll&&!rl&&r
000
MC/DC0%0/3
length $text and $s != $sec and $sec = $s
length $text $s != $sec $sec = $s
Truth table0%0/4
A and B and C
A= length $textB= $s != $secC= $sec = $s
ABCresult
0XX0
10X0
1100
1111
235 0 };
236 0 ▶ sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
237 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
238 0 ▶ return if $_[0] eq $text;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq $text 0 0 0
239 0 ▶ print STDERR "\n" if length $text;
Branch: 1 branch
BranchTrueFalseTotal
if length $text 0 0 0
240 0 $text = shift;
241 0 $line->();
242 } else {
243 0 undef $TICK;
244 0 ▶ print STDERR "\n" if length $text;
Branch: 1 branch
BranchTrueFalseTotal
if length $text 0 0 0
245 }
246 0 };
247 }
248  
249 # Draw the diagrams in one run of mmdc, from a Markdown file of their
250 # code blocks, which it draws into OUT-1.png, OUT-2.png and so on. It
251 # draws none when one has an error, and the diagrams are then drawn
252 # one by one, which tells the error of each. A code with a fence of
253 # its own is not put in the file.
254 #
255 # Without -q, it writes a line with OUT-N.png when it has drawn one,
256 # which the line on the terminal counts.
257 9 ▶sub draw_all {
Subroutine draw_all: called CC=8 SCAR=24.7
Pod undocumented
258 9 my($r, $progress, @todo) = @_;
259 9 ▶ return if grep { $_->[1] =~ /^\h*(?:```|~~~)/m } @todo;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->[1] =~ /^\h*(?:```|~~~)/mu;} @todo 0 9 9
260 9 my($cmd, $opt) = @{$r}{qw(cmd opt)};
261 9 ▶ my $tmp = eval { File::Temp->newdir(DIR => $r->{dir}) } or return;
Branch: 1 branch
BranchTrueFalseTotal
my $tmp = eval { do { "File::Temp"->newdir("DIR", $r->{"dir"}) } } or return 9 0 9
262 9 my $in = File::Spec->catfile($tmp, 'in.md');
263 9 my $out = File::Spec->catfile($tmp, 'out.md');
264 9 my $log = File::Spec->catfile($tmp, 'log.txt');
265 9 ▶ open my $fh, '>:encoding(utf8)', $in or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(utf8)", $in or return 9 0 9
266 9 print $fh map { "```mermaid\n$_->[1]```\n\n" } @todo;
267 9 close $fh;
268 9 my $count = @todo;
269 9 ▶ local $TICK = $TICK && do {
Condition33%1/3
$App::Greple::md::Mermaid::TICK && do { my $tick = $App::Greple::md::Mermaid::TICK; sub { my $done = 0; if (open my $l, '<:raw', $log) { $done = grep({/out-\d+\.png\s*$/u;} readline $l); } ; &$progress("$done of $count drawn") if $done; &$tick(); } }
!ll&&!rl&&r
900
MC/DC0%0/2
$App::Greple::md::Mermaid::TICK && do { my $tick = $App::Greple::md::Mermaid::TICK; sub { my $done = 0; if (open my $l, '<:raw', $log) { $done = grep({/out-\d+\.png\s*$/u;} readline $l); } ; &$progress("$done of $count drawn") if $done; &$tick(); } }
$App::Greple::md::Mermaid::TICK do { my $tick = $App::Greple::md::Mermaid::TICK; sub { my $done = 0; if (open my $l, '<:raw', $log) { $done = grep({/out-\d+\.png\s*$/u;} readline $l); } ; &$progress("$done of $count drawn") if $done; &$tick(); } }
Truth table33%1/3
A && B
A= $App::Greple::md::Mermaid::TICKB= do { my $tick = $App::Greple::md::Mermaid::TICK; sub { my $done = 0; if (open my $l, '<:raw', $log) { $done = grep({/out-\d+\.png\s*$/u;} readline $l); } ; &$progress("$done of $count drawn") if $done; &$tick(); } }
ABresult
0X0
100
111
270 0 my $tick = $TICK;
271 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
272 0 my $done = 0;
273 0 ▶ if (open my $l, '<:raw', $log) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $l, "<:raw", $log) 0 0 0
274 0 $done = grep { /out-\d+\.png\s*$/ } <$l>;
275 }
276 0 ▶ $progress->("$done of $count drawn") if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 0 0
277 0 $tick->();
278 0 };
279 };
280 9 ▶ run({ out => $log }, @$cmd, grep({ $_ ne '-q' } @$opt), '-i', $in, '-o', $out)
Branch: 1 branch
BranchTrueFalseTotal
run({"out", $log}, @$cmd, grep({$_ ne "-q";} @$opt), "-i", $in, "-o", $out) or return 2 4 6
281 or return;
282 2 for my $n (1 .. @todo) {
283 4 my $png = File::Spec->catfile($tmp, "out-$n.png");
284 4 ▶ rename $png, $todo[$n - 1][0] if -s $png;
Branch: 1 branch
BranchTrueFalseTotal
if -s $png 4 0 4
285 }
286 }
287  
288 # A file of options in JSON, removed with the object
289 99 ▶sub config_file {
Subroutine config_file: called CC=2 SCAR=7.0
Pod undocumented
290 99 my($dir, $data) = @_;
291 99 ▶ my $fh = eval { File::Temp->new(DIR => $dir, SUFFIX => '.json') } or return;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = eval { do { "File::Temp"->new("DIR", $dir, "SUFFIX", ".json") } } or return 99 0 99
292 99 print $fh JSON::PP->new->utf8->canonical->encode($data), "\n";
293 99 close $fh;
294 99 $fh;
295 }
296  
297 # Draw one diagram from a file of its own. With a scale (mmdr), the
298 # command is first asked for the size of the diagram (--size prints it in
299 # JSON), and is given that size multiplied: it takes a width and a
300 # height and draws the diagram as large; with only one of them it
301 # takes 1200 or 800 for the other. A diagram whose size is not told
302 # is drawn in its own size, and so is any diagram when the command has
303 # a width or a height of its own. Returns undef when it is drawn, or
304 # else what the command wrote to the standard error, without control
305 # characters.
306 115 ▶sub draw {
Subroutine draw: called CC=20 SCAR=30.8
Pod undocumented
307 115 my($r, $code, $path) = @_;
308 115 my($cmd, $opt) = @{$r}{qw(cmd opt)};
309 115 ▶ my $tmp = eval { File::Temp->newdir(DIR => $r->{dir}) } or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $tmp = eval { do { "File::Temp"->newdir("DIR", $r->{"dir"}) } } or return "" 115 0 115
310 115 my $in = File::Spec->catfile($tmp, 'in.mmd');
311 115 my $out = File::Spec->catfile($tmp, 'out.png');
312 115 my $err = File::Spec->catfile($tmp, 'err.txt');
313 115 ▶ open my $fh, '>:encoding(utf8)', $in or return '';
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(utf8)", $in or return "" 115 0 115
314 115 print $fh $code;
315 115 close $fh;
316 115 my @size;
317 115 ▶ if ($r->{scale} and $r->{scale} != 1
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"scale"} and $r->{"scale"} != 1 and not grep({/^(?:-w|-H|--(?:width|height)(?:=|$))/u;} @$cmd)) 80 35 115
Condition100%6/6
$r->{"scale"} and $r->{"scale"} != 1 and not grep({/^(?:-w|-H|--(?:width|height)(?:=|$))/u;} @$cmd)
!ll&&!rl&&r
33280
$r->{"scale"} and $r->{"scale"} != 1
!ll&&!rl&&r
28582
MC/DC100%3/3
$r->{"scale"} and $r->{"scale"} != 1 and not grep({/^(?:-w|-H|--(?:width|height)(?:=|$))/u;} @$cmd)
$r->{"scale"} $r->{"scale"} != 1 not grep({/^(?:-w|-H|--(?:width|height)(?:=|$))/u;} @$cmd)
Truth table100%4/4
A and B and not(C)
A= $r->{"scale"}B= $r->{"scale"} != 1C= not grep({/^(?:-w|-H|--(?:width|height)(?:=|$))/u;} @$cmd)
ABCresult
0XX0
10X0
1100
1111
318 92 and not grep { /^(?:-w|-H|--(?:width|height)(?:=|$))/ } @$cmd) {
319 80 my $json = File::Spec->catfile($tmp, 'size.json');
320 80 ▶ my $size = run({ out => $json }, @$cmd, @$opt, '-i', $in, '--size') && eval {
Condition100%3/3
run({'out', $json}, @$cmd, @$opt, '-i', $in, '--size') && eval { do { open my $fh, '<:raw', $json or die; 'JSON::PP'->new->utf8->decode(do { local $/; readline $fh }) } }
!ll&&!rl&&r
12244
MC/DC100%2/2
run({'out', $json}, @$cmd, @$opt, '-i', $in, '--size') && eval { do { open my $fh, '<:raw', $json or die; 'JSON::PP'->new->utf8->decode(do { local $/; readline $fh }) } }
run({'out', $json}, @$cmd, @$opt, '-i', $in, '--size') eval { do { open my $fh, '<:raw', $json or die; 'JSON::PP'->new->utf8->decode(do { local $/; readline $fh }) } }
Truth table100%3/3
A && B
A= run({'out', $json}, @$cmd, @$opt, '-i', $in, '--size')B= eval { do { open my $fh, '<:raw', $json or die; 'JSON::PP'->new->utf8->decode(do { local $/; readline $fh }) } }
ABresult
0X0
100
111
321 46 ▶ open my $fh, '<:raw', $json or die;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $json or die 46 0 46
322 46 JSON::PP->new->utf8->decode(do { local $/; <$fh> });
323 };
324 88 my($w, $h) = map { $_ * $r->{scale} }
325 88 ▶ grep { defined and !ref and /^[\d.]+$/ and $_ > 0 }
Branch: 1 branch
BranchTrueFalseTotal
ref $size eq 'HASH' ? : 44 14 58
Condition33%3/9
defined $_ && !ref($_) && /^[\d.]+$/u && $_ > 0
!ll&&!rl&&r
0088
defined $_ and not ref $_ and /^[\d.]+$/u
!ll&&!rl&&r
0088
defined $_ and not ref $_
!ll&&!rl&&r
0088
MC/DC0%0/4
defined $_ && !ref($_) && /^[\d.]+$/u && $_ > 0
defined $_ not ref $_ /^[\d.]+$/u $_ > 0
Truth table20%1/5
A and not(B) and C && D
A= defined $_B= not ref $_C= /^[\d.]+$/uD= $_ > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
326 58 ref $size eq 'HASH' ? @{$size}{qw(width height)} : ();
327 58 ▶ @size = ('-w', POSIX::ceil($w), '-H', POSIX::ceil($h)) if defined $h;
Branch: 1 branch
BranchTrueFalseTotal
if defined $h 44 14 58
328 }
329 93 ▶ if (run({ err => $err }, @$cmd, @$opt, @size, '-i', $in, '-o', $out)
Branch: 1 branch
BranchTrueFalseTotal
if (run({"err", $err}, @$cmd, @$opt, @size, "-i", $in, "-o", $out) and -s $out and rename $out, $path) 44 12 56
Condition66%4/6
run({"err", $err}, @$cmd, @$opt, @size, "-i", $in, "-o", $out) and -s $out and rename $out, $path
!ll&&!rl&&r
12044
run({"err", $err}, @$cmd, @$opt, @size, "-i", $in, "-o", $out) and -s $out
!ll&&!rl&&r
12044
MC/DC33%1/3
run({"err", $err}, @$cmd, @$opt, @size, "-i", $in, "-o", $out) and -s $out and rename $out, $path
run({"err", $err}, @$cmd, @$opt, @size, "-i", $in, "-o", $out) -s $out rename $out, $path
Truth table50%2/4
A and B and C
A= run({"err", $err}, @$cmd, @$opt, @size, "-i", $in, "-o", $out)B= -s $outC= rename $out, $path
ABCresult
0XX0
10X0
1100
1111
330 and -s $out and rename($out, $path)) {
331 44 return undef;
332 }
333 12 ▶ open my $e, '<:raw', $err or return '';
Branch: 1 branch
BranchTrueFalseTotal
open my $e, "<:raw", $err or return "" 12 0 12
334 12 ▶ my $text = decode('UTF-8', do { local $/; <$e> } // '');
Condition50%1/2
do { local $/; readline $e } // ""
l!l
120
MC/DC0%0/1
do { local $/; readline $e } // ""
do { local $/; readline $e }
Truth table50%1/2
A
A= do { local $/; readline $e }
Aresult
11
00
335 12 $text =~ s/\e(?:\[[0-9;?]*[ -\/]*[\@-~]|\][^\a\e]*(?:\a|\e\\)?|.)//gs;
336 12 $text =~ s/\r?\n/\n/g;
337 12 $text =~ s/\t/ /g;
338 12 $text =~ s/[\x00-\x09\x0b-\x1f\x7f\x{80}-\x{9f}]//g;
339 # the stack of JavaScript (mmdc) is not for the author: it starts
340 # with a line of "at FUNCTION (FILE)", or of "FUNCTION (URL)"
341 12 $text =~ s/^(?:\h+at\h(?:.*\()?|\S+\h\()(?:file|https?|node):.*//ms;
342 # nor that of Python (mermaidx): the error at its end, without the
343 # name of the exception which carries it
344 12 ▶ if ($text =~ /^Traceback \(most recent call last\):/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^Traceback \(most recent call last\):/mu) 1 11 12
345 1 $text =~ s/\A.*^[\w.]+(?:Error|Exception): (?:Mermaid rendering failed: )?//ms;
346 }
347 12 $text =~ s/\A\n+|\s+\z//g;
348 # a long one, as a panic with its backtrace, is cut to ten lines
349 12 my @line = split /\n/, $text;
350 12 ▶ join "\n", @line > 10 ? (@line[0..9], '...') : @line;
Branch: 1 branch
BranchTrueFalseTotal
@line > 10 ? : 0 12 12
351 }
352  
353 # Run a command, without a shell, its output thrown away, or its
354 # standard output and standard error written to the files of
355 # { out => FILE, err => FILE } given first.
356 # True when it ends with success.
357 182 ▶sub run {
Subroutine run: called CC=10 SCAR=26.4
Pod undocumented
358 182 ▶ my $to = ref $_[0] eq 'HASH' ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 182 0 182
359 182 my @cmd = @_;
360 182 ▶ my $pid = fork // return;
Condition50%1/2
fork // (return)
l!l
1820
MC/DC0%0/1
fork // (return)
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
361 182 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 62 120 182
362 62 open STDIN, '<', File::Spec->devnull;
363 62 ▶ open STDOUT, '>', $to->{out} // File::Spec->devnull or POSIX::_exit(127);
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", $to->{"out"} // "File::Spec"->devnull or POSIX::_exit(127) 62 0 62
Condition66%2/3
$to->{"out"} // "File::Spec"->devnull
l!l&&r!l&&!r
25370
MC/DC0%0/2
$to->{"out"} // "File::Spec"->devnull
$to->{"out"} "File::Spec"->devnull
Truth table66%2/3
A // B
A= $to->{"out"}B= "File::Spec"->devnull
ABresult
1X1
011
000
364 62 ▶ open STDERR, '>', $to->{err} // File::Spec->devnull;
Condition66%2/3
$to->{"err"} // "File::Spec"->devnull
l!l&&r!l&&!r
37250
MC/DC0%0/2
$to->{"err"} // "File::Spec"->devnull
$to->{"err"} "File::Spec"->devnull
Truth table66%2/3
A // B
A= $to->{"err"}B= "File::Spec"->devnull
ABresult
1X1
011
000
365 # a command which is not there says nothing: it is not a fault
366 # of the text
367 342 ▶ no warnings 'exec';
Subroutine BEGIN: called CC=1 SCAR=0.0
368 62 ▶ exec { $cmd[0] } @cmd or POSIX::_exit(127);
Branch: 1 branch
BranchTrueFalseTotal
exec {$cmd[0];} @cmd or POSIX::_exit(127) 0 0 0
369 }
370 120 ▶ if ($TICK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Greple::md::Mermaid::TICK) { } 0 120 120
371 0 ▶ until (waitpid($pid, POSIX::WNOHANG()) == $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (waitpid($pid, 1) == $pid) 0 0 0
372 0 select undef, undef, undef, 0.1;
373 0 $TICK->();
374 }
375 } else {
376 120 waitpid $pid, 0;
377 }
378 120 $? == 0;
379 }
380  
381 342 1;
382  
383 __END__
« blib/lib/App/Greple/md/Blocks.pm ↑ Summary blib/lib/App/Greple/md.pm »