File Coverage

blib/lib/Plack/Middleware/Debug/Notepad.pm
Criterion Covered Total %
statement 46 57 80.7
branch 7 14 50.0
condition n/a
subroutine 12 15 80.0
pod 3 4 75.0
total 68 90 75.5


line stmt bran cond sub pod time code
1             package Plack::Middleware::Debug::Notepad;
2 1     1   154993 use strict;
  1         1  
  1         24  
3 1     1   3 use warnings;
  1         1  
  1         45  
4              
5             our $VERSION = '0.10';
6             $VERSION = eval $VERSION;
7              
8 1     1   555 use Text::Markdown;
  1         21885  
  1         58  
9 1     1   628 use Text::MicroTemplate qw/ encoded_string /;
  1         2268  
  1         57  
10 1     1   628 use Plack::Request;
  1         40336  
  1         39  
11 1     1   7 use Plack::Util::Accessor qw( notepad_file );
  1         2  
  1         8  
12              
13 1     1   66 use parent 'Plack::Middleware::Debug::Base';
  1         2  
  1         10  
14              
15             sub prepare_app {
16 0     0 1 0 my $self = shift;
17              
18 0 0       0 $self->notepad_file( '/tmp/plack-middleware-debug-notepad.md' )
19             unless $self->notepad_file;
20             }
21              
22             sub call {
23 4     4 1 1666 my ( $self, $env ) = @_;
24              
25 4 100       24 if ( $env->{ QUERY_STRING } =~ m/__plack_middleware_debug_notepad__/ ) {
26 2 100       12 if ( $env->{ REQUEST_METHOD } eq 'POST' ) {
    50          
27 1         3 return $self->save_markdown( $env );
28             }
29             elsif ( $env->{ REQUEST_METHOD } eq 'GET' ) {
30 1         4 return [ 200, [ 'Content-Type', 'text/html' ], [ $self->get_markdown ] ];
31             }
32             }
33             else {
34 2         11 return $self->SUPER::call( $env );
35             }
36             }
37              
38             sub run {
39 3     3 1 906 my ( $self, $env, $panel ) = @_;
40              
41             return sub {
42 1     1   799 $panel->title( 'Notepad' );
43 1         6 $panel->nav_title( 'Notepad' );
44 1         4 $panel->nav_subtitle( 'things to keep in mind' );
45 1         4 $panel->content( $self->get_notepad_content( $panel->dom_id ) );
46             }
47 3         10 }
48              
49             sub get_notepad_content {
50 0     0   0 my $self = shift;
51 0         0 my $id = shift;
52              
53 0         0 my $md = $self->get_markdown;
54 0         0 my $vars = {
55             markdown => $md,
56             id => $id,
57             rendered => encoded_string( Text::Markdown->new->markdown( $md ) ),
58             };
59              
60 0         0 return Text::MicroTemplate->new( template => $self->the_template )->build->( $vars );
61             }
62              
63             sub get_markdown {
64 1     1   2993 my $self = shift;
65              
66 1 50       7 if ( open my $fh, '<', $self->notepad_file ) {
    0          
67 1         34 local $/;
68 1         30 return <$fh>;
69             }
70             elsif ( ! -e $self->notepad_file ) {
71 0         0 return 'Replace this with whatever you need to keep track of.';
72             }
73             else {
74 0         0 return "Error opening your notepad file: $!";
75             }
76             }
77              
78             sub the_template {
79 0     0 0 0 <<'EOTMPL' }
80             ? my $stash = $_[0];
81            
92            
93            
120            
121            
122            
123            
124             Syntax help
125            
126            
127            
128             ?= $stash->{ rendered }
129            
130            
131            
132             EOTMPL
133              
134             sub save_markdown {
135 1     1   2019 my $self = shift;
136 1         1 my $env = shift;
137              
138 1         8 my $md = Plack::Request->new( $env )->param( 'markdown' );
139              
140 1         128 my $response = eval {
141 1 50       6 if ( open my $fh, '>', $self->notepad_file ) {
142 1         92 print $fh $md;
143 1         8 close $fh;
144 1         10 return Text::Markdown->new->markdown( $md );
145             }
146             else {
147 0         0 return "

Error

An error occured while trying to save your edited version:

$!

";
148             }
149             };
150              
151 1         942 return [ 200, [ 'Content-Type', 'text/html' ], [ $response ] ];
152             }
153              
154             1;
155              
156             __END__