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