Commit | Line | Data |
1716b200 |
1 | use strict; |
2 | use warnings; |
24cc824f |
3 | package Devel::REPL::Plugin::Nopaste; |
4 | |
6a5409bc |
5 | use Devel::REPL::Plugin; |
76cd9acd |
6 | use Moose::Util::TypeConstraints; |
aa8b7647 |
7 | use namespace::autoclean; |
a478012d |
8 | use Scalar::Util qw(blessed); |
24cc824f |
9 | |
3a400715 |
10 | sub BEFORE_PLUGIN { |
11 | my $self = shift; |
12 | $self->load_plugin('Turtles'); |
13 | } |
24cc824f |
14 | |
15 | has complete_session => ( |
78bc5721 |
16 | traits => ['String'], |
24cc824f |
17 | is => 'rw', |
18 | isa => 'Str', |
8995c74b |
19 | lazy => 1, |
24cc824f |
20 | default => '', |
8bf72450 |
21 | handles => { |
22 | add_to_session => 'append', |
24cc824f |
23 | }, |
24 | ); |
25 | |
da4881b1 |
26 | has paste_title => ( |
da4881b1 |
27 | is => 'rw', |
28 | isa => 'Str', |
29 | lazy => 1, |
30 | default => 'Devel::REPL session', |
31 | ); |
32 | |
76cd9acd |
33 | has 'nopaste_format' => ( |
34 | is => 'rw', |
35 | isa => enum( [qw[ comment_code comment_output ]] ), |
36 | lazy => 1, |
37 | default => 'comment_output', |
38 | ); |
39 | |
a4c582b6 |
40 | before 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 | |
52 | around 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 | |
78 | sub 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 |
89 | sub command_pastetitle { |
90 | my ( $self, undef, $title ) = @_; |
91 | |
92 | $self->paste_title( $title ); |
93 | } |
94 | |
24cc824f |
95 | 1; |
96 | |
cfd1094b |
97 | __END__ |
98 | |
99 | =head1 NAME |
100 | |
101 | Devel::REPL::Plugin::Nopaste - #nopaste to upload session's input and output |
102 | |
da4881b1 |
103 | =head1 COMMANDS |
104 | |
105 | This module provides these commands to your Devel::REPL shell: |
106 | |
107 | =head2 #nopaste |
108 | |
109 | The C<#nopaste> sends a transcript of your session to a nopaste |
110 | site. |
111 | |
112 | =head2 #pastetitle |
113 | |
114 | The C<#pastetitle> command allows you to set the title of the paste on |
115 | the nopaste site. For example: |
116 | |
117 | C<#pastetitle example of some code> |
118 | |
8d5343b5 |
119 | defaults to C<'Devel::REPL session'>. |
da4881b1 |
120 | |
76cd9acd |
121 | =head1 CONFIGURATION |
122 | |
123 | =head2 nopaste_format |
124 | |
125 | The format sent to the nopaste server can be adjusted with the |
126 | C<nopaste_format> option. By default, the output of each perl |
127 | statement is commented out, and the perl statements themselves are |
128 | not. This can be reversed by setting the C<nopaste_format> attribute |
129 | to C<comment_code> like this in your re.pl file: |
130 | |
131 | C<< $_REPL->nopaste_format( 'comment_code' ); >> |
132 | |
133 | The default of commenting out the output would be set like this: |
134 | |
135 | C<< $_REPL->nopaste_format( 'comment_output' ); >> |
136 | |
8d5343b5 |
137 | These options can be set during a L<Devel::REPL> session, but only affect |
76cd9acd |
138 | the future parts of the session, not the past parts. |
139 | |
30b459d4 |
140 | =head1 AUTHOR |
141 | |
142 | Shawn 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 | |