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