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