blib/lib/Plack/Middleware/Debug/Notepad.pm

filter: Statementstmt 81.6% 49 / 60 Branchbran 50.0% 7 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 12 / 15 Podpod 50.0% 3 / 6 totaltotal 74.7% 71 / 95 CC 8 SCAR 9.6
CC
8
Cov
76%
prepare_app
6.0
get_markdown
4.1
call
4.0
CRAP 2.6
↑ Summary
1 package Plack::Middleware::Debug::Notepad;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.13';
6 1 $VERSION = eval $VERSION;
7  
8 1 ▶use Text::Markdown;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Text::MicroTemplate qw/ encoded_string /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Plack::Util::Accessor qw( notepad_file );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use parent 'Plack::Middleware::Debug::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶sub prepare_app {
Subroutine prepare_app: not called CC=2 SCAR=17.9
Pod documented
16 0 my $self = shift;
17  
18 0 ▶ $self->notepad_file( '/tmp/plack-middleware-debug-notepad.md' )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->notepad_file 0 0 0
19 unless $self->notepad_file;
20 }
21  
22 4 ▶sub call {
Subroutine call: called CC=4 SCAR=13.9
Pod documented
23 4 my ( $self, $env ) = @_;
24  
25 4 ▶ if ( $env->{ QUERY_STRING } =~ m/__plack_middleware_debug_notepad__/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{'QUERY_STRING'} =~ /__plack_middleware_debug_notepad__/) { } 2 2 4
26 2 ▶ if ( $env->{ REQUEST_METHOD } eq 'POST' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($env->{'REQUEST_METHOD'} eq 'POST') { } 1 1 2
elsif ($env->{'REQUEST_METHOD'} eq 'GET') { } 1 0 1
27 1 return $self->save_markdown( $env );
28 }
29 elsif ( $env->{ REQUEST_METHOD } eq 'GET' ) {
30 1 return [ 200, [ 'Content-Type', 'text/html' ], [ $self->get_markdown ] ];
31 }
32 }
33 else {
34 2 return $self->SUPER::call( $env );
35 }
36 }
37  
38 3 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod documented
39 3 my ( $self, $env, $panel ) = @_;
40  
41 1 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 1 $panel->title( 'Notepad' );
43 1 $panel->nav_title( 'Notepad' );
44 1 $panel->nav_subtitle( 'things to keep in mind' );
45 1 $panel->content( $self->get_notepad_content( $panel->dom_id ) );
46 }
47 3 }
48  
49 0 ▶sub get_notepad_content {
Subroutine get_notepad_content: not called CC=1 SCAR=6.9
50 0 my $self = shift;
51 0 my $id = shift;
52  
53 0 my $md = $self->get_markdown;
54 0 my $vars = {
55 markdown => $md,
56 id => $id,
57 rendered => encoded_string( Text::Markdown->new->markdown( $md ) ),
58 };
59  
60 0 return Text::MicroTemplate->new( template => $self->the_template )->build->( $vars );
61 }
62  
63 1 ▶sub get_markdown {
Subroutine get_markdown: called CC=3 SCAR=14.2
Pod undocumented
64 1 my $self = shift;
65  
66 1 ▶ if ( open my $fh, '<', $self->notepad_file ) {
Branch: 2 branches
BranchTrueFalseTotal
if (open my $fh, '<', $self->notepad_file) { } 1 0 1
elsif (not -e $self->notepad_file) { } 0 0 0
67 1 local $/;
68 1 return <$fh>;
69 }
70 elsif ( ! -e $self->notepad_file ) {
71 0 return 'Replace this with whatever you need to keep track of.';
72 }
73 else {
74 0 return "Error opening your notepad file: $!";
75 }
76 }
77  
78 0 ▶sub the_template {
Subroutine the_template: not called CC=1 SCAR=6.9
Pod undocumented
79 0 <<'EOTMPL' }
80 ? my $stash = $_[0];
81 <style type="text/css">
82 div#debug_<?= $stash->{ id } ?>_html { border: 1px solid black; background-color: white; padding: 4px; margin: 4px }
83 div#debug_<?= $stash->{ id } ?>_html h1 { font-size: 16px }
84 div#debug_<?= $stash->{ id } ?>_html h2 { font-size: 15px }
85 div#debug_<?= $stash->{ id } ?>_html h3 { font-size: 14px; font-weight: 700 }
86 div#debug_<?= $stash->{ id } ?>_html a:link { color: blue }
87 div#debug_<?= $stash->{ id } ?>_html a:visited { color: #800080 }
88 div#debug_<?= $stash->{ id } ?>_markdown textarea { width: 90%; height: 80%; padding: 4px; margin: 4px }
89 #debug_<?= $stash->{ id } ?>_html ul { list-style: disc outside; padding-left: 20px; }
90 #debug_<?= $stash->{ id } ?> input { color: white; background-color: black; }
91 </style>
92 <div id="debug_<?= $stash->{ id } ?>">
93 <script>
94 jQuery( function( $j ) {
95 function hide_editor() {
96 $j('#debug_<?= $stash->{ id } ?>_markdown').toggle();
97 $j('#debug_<?= $stash->{ id } ?>_html').toggle();
98 $j('#edit_button_<?= $stash->{ id } ?>').toggle();
99 }
100 $j( '#cancel_button_<?= $stash->{ id } ?>' ).click( function() {
101 hide_editor();
102 $j.get( "?__plack_middleware_debug_notepad__", '', function( response ) {
103 $j('#debug_<?= $stash->{ id } ?>_markdown_edited').val( response );
104 }, 'html' );
105 });
106 $j( '#edit_button_<?= $stash->{ id } ?>' ).click( function() {
107 $j('#debug_<?= $stash->{ id } ?>_markdown').toggle();
108 $j('#debug_<?= $stash->{ id } ?>_html').toggle();
109 $j('#edit_button_<?= $stash->{ id } ?>').toggle();
110 });
111 $j( '#save_button_<?= $stash->{ id } ?>' ).click( function() {
112 var data = { "markdown": $j( '#debug_<?= $stash->{ id } ?>_markdown_edited' ).val() };
113 $j.post( "?__plack_middleware_debug_notepad__", data, function( response ) {
114 $j('#debug_<?= $stash->{ id } ?>_html').html( response );
115 hide_editor();
116 }, 'html' );
117 });
118 })
119 </script>
120 <div id="debug_<?= $stash->{ id } ?>_markdown" style="display: none">
121 <textarea rows="20" name="markdown" id="debug_<?= $stash->{ id } ?>_markdown_edited"><?= $stash->{ markdown } ?></textarea>
122 <br>
123 <input type="button" value="save" id="save_button_<?= $stash->{ id } ?>">
124 <a target="_blank" href="http://daringfireball.net/projects/markdown/syntax">Syntax help</a>
125 <input type="button" value="cancel" id="cancel_button_<?= $stash->{ id } ?>">
126 </div>
127 <div id="debug_<?= $stash->{ id } ?>_html">
128 ?= $stash->{ rendered }
129 </div>
130 <input type="button" value="edit" id="edit_button_<?= $stash->{ id } ?>">
131 </div>
132 EOTMPL
133  
134 1 ▶sub save_markdown {
Subroutine save_markdown: called CC=2 SCAR=7.0
Pod undocumented
135 1 my $self = shift;
136 1 my $env = shift;
137  
138 1 my $md = Plack::Request->new( $env )->param( 'markdown' );
139  
140 1 my $response = eval {
141 1 ▶ if ( open my $fh, '>', $self->notepad_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '>', $self->notepad_file) { } 1 0 1
142 1 print $fh $md;
143 1 close $fh;
144 1 return Text::Markdown->new->markdown( $md );
145 }
146 else {
147 0 return "<h1>Error</h1><p>An error occured while trying to save your edited version: <pre>$!</pre></p>";
148 }
149 };
150  
151 1 return [ 200, [ 'Content-Type', 'text/html' ], [ $response ] ];
152 }
153  
154 1 1;
155  
156 __END__
↑ Summary