Commit | Line | Data |
1716b200 |
1 | use strict; |
2 | use warnings; |
24cc824f |
3 | package Devel::REPL::Plugin::Nopaste; |
4 | |
54beb05d |
5 | our $VERSION = '1.003027'; |
6 | |
6a5409bc |
7 | use Devel::REPL::Plugin; |
e73ba6da |
8 | use Moose::Util::TypeConstraints 'enum'; |
aa8b7647 |
9 | use namespace::autoclean; |
a478012d |
10 | use Scalar::Util qw(blessed); |
24cc824f |
11 | |
3a400715 |
12 | sub BEFORE_PLUGIN { |
13 | my $self = shift; |
14 | $self->load_plugin('Turtles'); |
15 | } |
24cc824f |
16 | |
17 | has complete_session => ( |
78bc5721 |
18 | traits => ['String'], |
24cc824f |
19 | is => 'rw', |
20 | isa => 'Str', |
8995c74b |
21 | lazy => 1, |
24cc824f |
22 | default => '', |
8bf72450 |
23 | handles => { |
24 | add_to_session => 'append', |
24cc824f |
25 | }, |
26 | ); |
27 | |
da4881b1 |
28 | has paste_title => ( |
da4881b1 |
29 | is => 'rw', |
30 | isa => 'Str', |
31 | lazy => 1, |
32 | default => 'Devel::REPL session', |
33 | ); |
34 | |
76cd9acd |
35 | has 'nopaste_format' => ( |
36 | is => 'rw', |
37 | isa => enum( [qw[ comment_code comment_output ]] ), |
38 | lazy => 1, |
39 | default => 'comment_output', |
40 | ); |
41 | |
a4c582b6 |
42 | before eval => sub { |
24cc824f |
43 | my $self = shift; |
44 | my $line = shift; |
45 | |
76cd9acd |
46 | if ( $self->nopaste_format() eq 'comment_code' ) { |
47 | # prepend each line with # |
48 | $line =~ s/^/# /mg; |
49 | } |
24cc824f |
50 | |
a4c582b6 |
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, @_); |
a478012d |
60 | my @ret_as_str = map { |
61 | if (!defined($_)) { |
62 | ''; |
63 | } elsif (blessed($_) && $_->can('stringify')) { |
64 | $_->stringify(); |
65 | } else { |
66 | $_; |
67 | } |
68 | } @ret; |
69 | |
76cd9acd |
70 | if ( $self->nopaste_format() eq 'comment_output' ) { |
71 | # prepend each line with # |
72 | map { $_ =~ s/^/# /mg } @ret_as_str; |
73 | } |
74 | |
a478012d |
75 | $self->add_to_session(join("\n", @ret_as_str) . "\n\n"); |
24cc824f |
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, |
da4881b1 |
86 | desc => $self->paste_title(), |
24cc824f |
87 | lang => "perl", |
88 | ); |
89 | } |
90 | |
da4881b1 |
91 | sub command_pastetitle { |
92 | my ( $self, undef, $title ) = @_; |
93 | |
94 | $self->paste_title( $title ); |
95 | } |
96 | |
24cc824f |
97 | 1; |
98 | |
cfd1094b |
99 | __END__ |
100 | |
101 | =head1 NAME |
102 | |
103 | Devel::REPL::Plugin::Nopaste - #nopaste to upload session's input and output |
104 | |
da4881b1 |
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 | |
8d5343b5 |
121 | defaults to C<'Devel::REPL session'>. |
da4881b1 |
122 | |
76cd9acd |
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 | |
8d5343b5 |
139 | These options can be set during a L<Devel::REPL> session, but only affect |
76cd9acd |
140 | the future parts of the session, not the past parts. |
141 | |
30b459d4 |
142 | =head1 AUTHOR |
143 | |
144 | Shawn M Moore, C<< <sartak at gmail dot com> >> |
145 | |
da4881b1 |
146 | =head1 CONTRIBUTORS |
147 | |
148 | =over 4 |
149 | |
150 | =item Andrew Moore - C<< <amoore@cpan.org> >> |
151 | |
152 | =back |
153 | |
cfd1094b |
154 | =cut |
155 | |