1
0
Fork 0
mirror of https://github.com/perlbot/perlbuut-pastebin synced 2025-06-07 14:17:26 -04:00

Allow ajax calls for eval.

This commit is contained in:
Ryan Voots 2016-06-26 12:30:47 -04:00
parent cc881ff4f1
commit 75e1de74a1
4 changed files with 44 additions and 12 deletions

9
app.pl
View file

@ -106,14 +106,13 @@ get '/pastebin/:pasteid' => sub {
};
get '/eval/:pasteid' => sub {
post '/eval' => sub {
my ($c) = @_;
my $pasteid = $c->param('pasteid');
my $data = $c->req->body_params;
my $row = $dbh->selectrow_hashref("SELECT * FROM posts WHERE id = ? LIMIT 1", {}, $pasteid);
my $code = $row->{paste} // '';
my $code = $data->param('code') // '';
my $output = get_eval($pasteid, $code);
my $output = get_eval(undef, $code);
$c->render(json => {evalout => $output});
};

View file

@ -16,7 +16,7 @@ use App::Memcached;
sub get_eval {
my ($paste_id, $code) = @_;
if (my $cached = $memd->get($paste_id)) {
if ($paste_id && (my $cached = $memd->get($paste_id))) {
return $cached;
} else {
my $filter = POE::Filter::Reference->new();
@ -31,7 +31,7 @@ sub get_eval {
my $result = $filter->get( [ $output ] );
my $str = eval {decode("utf8", $result->[0]->[0])} // $result->[0]->[0];
$str = eval {decode("utf8", $str)} // $str; # I don't know why i need to decode this twice. shurg.
$memd->set($paste_id, $str);
$memd->set($paste_id, $str) if ($paste_id);
return $str;
}

BIN
pastes.db

Binary file not shown.

View file

@ -49,15 +49,29 @@
</div>
</div>
<div class="panel-body">
<div class="editors">
<textarea name="paste" id="paste" cols="80" rows="25">[% pastedata | html %]</textarea>
<pre id="editor">
</pre>
<div class="row">
<div id="editors" class="col-md-12">
<textarea name="paste" id="paste" cols="80" rows="25">[% pastedata | html %]</textarea>
<pre id="editor">
</pre>
</div>
<div id="evalcol" class="hidden">
<h3>Program Output:</h3>
<pre id="eval">[% eval | html %]</pre>
</div>
<div id="modules" class="hidden">
<h3>Program Output:</h3>
<ul>
<li>Moose</li>
</ul>
</div>
</div>
<div class="panel-footer">
<input value="Submit" type="submit" id="submit" />
<input value="Check Eval" type="button" id="evalme" />
</div>
</div>
</div>
</form>
@ -85,5 +99,24 @@
$("#paste").text(editor.getValue()); // copy to the textarea
});
$('#evalme').on('click', function () {
$('#eval').text("Evaluating...");
$('#evalcol').removeClass();
if (1) { // Eval, no docs
$('#evalcol').addClass('col-md-6');
$('#editors').removeClass().addClass('col-md-6');
$.ajax('/eval', {
method: 'post',
data: {code: editor.getValue()},
dataType: "json",
success: function(data, status) {
$('#eval').text(data.evalout);
}
});
} else if (0) { // Eval with docs
}
});
</script>
[% END %]