Commit | Line | Data |
24cc824f |
1 | package Devel::REPL::Plugin::Nopaste; |
2 | |
6a5409bc |
3 | use Devel::REPL::Plugin; |
e2d0b019 |
4 | use namespace::sweep; |
a478012d |
5 | use Scalar::Util qw(blessed); |
e2d0b019 |
6 | use MooX::Types::MooseLike::Base qw(Str); |
24cc824f |
7 | |
3a400715 |
8 | sub BEFORE_PLUGIN { |
9 | my $self = shift; |
10 | $self->load_plugin('Turtles'); |
11 | } |
24cc824f |
12 | |
13 | has complete_session => ( |
24cc824f |
14 | is => 'rw', |
e2d0b019 |
15 | isa => Str, |
8995c74b |
16 | lazy => 1, |
e2d0b019 |
17 | default => sub { '' }, |
24cc824f |
18 | ); |
19 | |
e2d0b019 |
20 | sub add_to_session { |
21 | my $self = shift; |
22 | my $session = $self->complete_session; |
23 | $session .= $_ for @_; |
24 | $self->complete_session($session); |
25 | } |
26 | |
da4881b1 |
27 | has paste_title => ( |
da4881b1 |
28 | is => 'rw', |
e2d0b019 |
29 | isa => Str, |
da4881b1 |
30 | lazy => 1, |
e2d0b019 |
31 | default => sub { 'Devel::REPL session' }, |
da4881b1 |
32 | ); |
33 | |
76cd9acd |
34 | has 'nopaste_format' => ( |
35 | is => 'rw', |
e2d0b019 |
36 | isa => sub { $_[0] =~ qr[^(?:comment_code|comment_output)$] }, |
76cd9acd |
37 | lazy => 1, |
e2d0b019 |
38 | default => sub { 'comment_output' }, |
76cd9acd |
39 | ); |
40 | |
a4c582b6 |
41 | before eval => sub { |
24cc824f |
42 | my $self = shift; |
43 | my $line = shift; |
44 | |
76cd9acd |
45 | if ( $self->nopaste_format() eq 'comment_code' ) { |
46 | # prepend each line with # |
47 | $line =~ s/^/# /mg; |
48 | } |
24cc824f |
49 | |
a4c582b6 |
50 | $self->add_to_session($line . "\n"); |
51 | }; |
52 | |
53 | around eval => sub { |
54 | my $orig = shift; |
55 | my $self = shift; |
56 | my $line = shift; |
57 | |
58 | my @ret = $orig->($self, $line, @_); |
a478012d |
59 | my @ret_as_str = map { |
60 | if (!defined($_)) { |
61 | ''; |
62 | } elsif (blessed($_) && $_->can('stringify')) { |
63 | $_->stringify(); |
64 | } else { |
65 | $_; |
66 | } |
67 | } @ret; |
68 | |
76cd9acd |
69 | if ( $self->nopaste_format() eq 'comment_output' ) { |
70 | # prepend each line with # |
71 | map { $_ =~ s/^/# /mg } @ret_as_str; |
72 | } |
73 | |
a478012d |
74 | $self->add_to_session(join("\n", @ret_as_str) . "\n\n"); |
24cc824f |
75 | |
76 | return @ret; |
77 | }; |
78 | |
79 | sub command_nopaste { |
80 | my $self = shift; |
81 | |
82 | require App::Nopaste; |
83 | return App::Nopaste->nopaste( |
84 | text => $self->complete_session, |
da4881b1 |
85 | desc => $self->paste_title(), |
24cc824f |
86 | lang => "perl", |
87 | ); |
88 | } |
89 | |
da4881b1 |
90 | sub command_pastetitle { |
91 | my ( $self, undef, $title ) = @_; |
92 | |
93 | $self->paste_title( $title ); |
94 | } |
95 | |
24cc824f |
96 | 1; |
97 | |
cfd1094b |
98 | __END__ |
99 | |
100 | =head1 NAME |
101 | |
102 | Devel::REPL::Plugin::Nopaste - #nopaste to upload session's input and output |
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 | |
120 | defaults to 'Devel::REPL session' |
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 | |
138 | These options can be set during a Devel::REPL session, but only affect |
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 |
154 | |