353ec2b60797d6054925f61892a3be88f80fe029
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / Nopaste.pm
1 use strict;
2 use warnings;
3 package Devel::REPL::Plugin::Nopaste;
4
5 our $VERSION = '1.003027';
6
7 use Devel::REPL::Plugin;
8 use Moose::Util::TypeConstraints 'enum';
9 use namespace::autoclean;
10 use Scalar::Util qw(blessed);
11
12 sub BEFORE_PLUGIN {
13     my $self = shift;
14     $self->load_plugin('Turtles');
15 }
16
17 has complete_session => (
18     traits => ['String'],
19     is        => 'rw',
20     isa       => 'Str',
21     lazy      => 1,
22     default   => '',
23     handles  => {
24         add_to_session => 'append',
25     },
26 );
27
28 has paste_title => (
29     is        => 'rw',
30     isa       => 'Str',
31     lazy      => 1,
32     default   => 'Devel::REPL session',
33 );
34
35 has 'nopaste_format' => (
36     is      => 'rw',
37     isa     => enum( [qw[ comment_code comment_output ]] ),
38     lazy    => 1,
39     default => 'comment_output',
40 );
41
42 before eval => sub {
43     my $self = shift;
44     my $line = shift;
45
46     if ( $self->nopaste_format() eq 'comment_code' ) {
47         # prepend each line with #
48         $line =~ s/^/# /mg;
49     }
50
51     $self->add_to_session($line . "\n");
52 };
53
54 around eval => sub {
55     my $orig = shift;
56     my $self = shift;
57     my $line = shift;
58
59     my @ret = $orig->($self, $line, @_);
60     my @ret_as_str = map {
61         if (!defined($_)) {
62             '';
63         } elsif (blessed($_) && $_->can('stringify')) {
64             $_->stringify();
65         } else {
66             $_;
67         }
68     } @ret;
69
70     if ( $self->nopaste_format() eq 'comment_output' ) {
71         # prepend each line with #
72         map { $_ =~ s/^/# /mg } @ret_as_str;
73     }
74
75     $self->add_to_session(join("\n", @ret_as_str) . "\n\n");
76
77     return @ret;
78 };
79
80 sub command_nopaste {
81     my $self = shift;
82
83     require App::Nopaste;
84     return App::Nopaste->nopaste(
85         text => $self->complete_session,
86         desc => $self->paste_title(),
87         lang => "perl",
88     );
89 }
90
91 sub command_pastetitle {
92     my ( $self, undef, $title ) = @_;
93
94     $self->paste_title( $title );
95 }
96
97 1;
98
99 __END__
100
101 =head1 NAME
102
103 Devel::REPL::Plugin::Nopaste - #nopaste to upload session's input and output
104
105 =head1 COMMANDS
106
107 This module provides these commands to your Devel::REPL shell:
108
109 =head2 #nopaste
110
111 The C<#nopaste> sends a transcript of your session to a nopaste
112 site.
113
114 =head2 #pastetitle
115
116 The C<#pastetitle> command allows you to set the title of the paste on
117 the nopaste site. For example:
118
119 C<#pastetitle example of some code>
120
121 defaults to C<'Devel::REPL session'>.
122
123 =head1 CONFIGURATION
124
125 =head2 nopaste_format
126
127 The format sent to the nopaste server can be adjusted with the
128 C<nopaste_format> option. By default, the output of each perl
129 statement is commented out, and the perl statements themselves are
130 not. This can be reversed by setting the C<nopaste_format> attribute
131 to C<comment_code> like this in your re.pl file:
132
133 C<< $_REPL->nopaste_format( 'comment_code' ); >>
134
135 The default of commenting out the output would be set like this:
136
137 C<< $_REPL->nopaste_format( 'comment_output' ); >>
138
139 These options can be set during a L<Devel::REPL> session, but only affect
140 the future parts of the session, not the past parts.
141
142 =head1 AUTHOR
143
144 Shawn M Moore, C<< <sartak at gmail dot com> >>
145
146 =head1 CONTRIBUTORS
147
148 =over 4
149
150 =item Andrew Moore - C<< <amoore@cpan.org> >>
151
152 =back
153
154 =cut
155