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