From 5d05e378b2f19a6f9f67c69d9f3dd90736c50092 Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Tue, 26 Jun 2018 12:26:30 -0400 Subject: [PATCH 1/2] Work in progress for multifile --- static/editor.css | 29 ++++++ static/editor.js | 174 ++++++++++++++++++++++++++++++++++++ static/test.html | 162 +++++++++++++++++++++++++++++++++ static/test2.html | 9 ++ templates/neweditor.html.tt | 101 +++++++++++++++++++++ 5 files changed, 475 insertions(+) create mode 100644 static/editor.css create mode 100644 static/editor.js create mode 100644 static/test.html create mode 100644 static/test2.html create mode 100644 templates/neweditor.html.tt diff --git a/static/editor.css b/static/editor.css new file mode 100644 index 0000000..844e79f --- /dev/null +++ b/static/editor.css @@ -0,0 +1,29 @@ +#editor { + margin: auto; + position: relative !important; + width: 100%; + height: 500px; + display: none; + font-variant-ligatures: none; +} + +#paste { + font-family: 'monospace' +} + +html, body, #content { + width: 100%; +} + +@font-face { + font-family: "AnonymousPro"; + src: url("/static/fonts/AnonymousPro-Regular.woff2") format('woff2'); +} + +.options label { + padding-right: 0.5em; +} + +.options label:not(:first-child) { + padding-left: 2em; +} diff --git a/static/editor.js b/static/editor.js new file mode 100644 index 0000000..a4a26c7 --- /dev/null +++ b/static/editor.js @@ -0,0 +1,174 @@ +$(function() { + var hostname = $(location).attr('hostname'); + var channel = $(location).attr('hash'); + + // console.log("host: " + hostname + " channel: " + channel); + + // fuck parsing the hostname + var servers = { + "f.perlbot.pl": "localhost:perlbot:", + "freenode.perlbot.pl": "localhost:perlbot:", + "m.perlbot.pl": "localhost:perlbot-magnet:", + "magnet.perlbot.pl": "localhost:perlbot-magnet:", + "o.perlbot.pl": "localhost:perlbot-oftc:", + "oftc.perlbot.pl": "localhost:perlbot-oftc:", + "f.perl.bot": "localhost:perlbot:", + "freenode.perl.bot": "localhost:perlbot:", + "m.perl.bot": "localhost:perlbot-magnet:", + "magnet.perl.bot": "localhost:perlbot-magnet:", + "o.perl.bot": "localhost:perlbot-oftc:", + "oftc.perl.bot": "localhost:perlbot-oftc:", + }; + + if (channel && servers[hostname]) { // only do this if we have a channel and a valid server + // console.log("found default channel to post: "+servers[hostname]+channel); + var option = $("option[value='"+servers[hostname]+channel+"']"); + // console.log(option); + option.prop('selected', true); + } + + if (servers[hostname]) { + var badoptions = $("select#channel option:not([value^='"+servers[hostname]+"'])"); + console.log(badoptions); + badoptions.remove(); + } +}); + +$(function() { + var showingmodules = 0; + var showingeval = 0; + + var editor; + + var use_editor = function() { + if ($("#raw_editor").is(":checked")) { + if (editor) { + $("#paste").val(editor.getValue()); + } + $("#editor").hide(); + $("#paste").show(); + } else { + $("#paste").hide(); + $("#editor").show(); + if (!editor) { + $("#editor").text($("#paste").val()); + } else { + editor.setValue($("#paste").val(),0); + editor.clearSelection(); + } + } + }; + + use_editor(); + $("#raw_editor").on("change", use_editor); + editor = ace.edit("editor"); + //editor.setTheme("ace/theme/twilight"); + + //safely delete all bindings + var save_keys={}; + Object.keys(editor.keyBinding.$defaultHandler.commandKeyBinding) + .filter((value) => value.match(/(?:(?:backspac|hom)e|d(?:elete|own)|(?:righ|lef)t|end|up|tab)/)) + .forEach((key) => save_keys[key] = editor.keyBinding.$defaultHandler.commandKeyBinding[key]); + + editor.keyBinding.$defaultHandler.commandKeyBinding = save_keys; + + + $("#language").on('change', function () { + var language = $('#language option').filter(':selected').attr('data-lang'); + console.log("language: ", language); + editor.session.setMode("ace/mode/" + language); + }); + + var channel = $(location).attr('hash'); + if (channel == "#perl6") { + $("#language").val("perl6").change(); + } else if (channel == "#cobol") { + $("#language").val("cobol").change(); + } + + editor.session.setMode("ace/mode/perl"); + +// editor.setOptions({fontFamily: ['AnonymousPro', 'monospace', 'mono']}); + + + function setup_columns() { + if (showingeval && showingmodules) { + $("#editors").removeClass().addClass('col-md-6'); + $("#evalcol").removeClass().addClass('col-md-4'); + $("#modules").removeClass().addClass('col-md-2'); + } else if (showingeval) { + $("#editors").removeClass().addClass('col-md-10'); + $("#evalcol").removeClass().addClass('col-md-2'); + $("#modules").removeClass().addClass('hidden'); + } else if (showingmodules) { + $("#editors").removeClass().addClass('col-md-10'); + $("#evalcol").removeClass().addClass('hidden'); + $("#modules").removeClass().addClass('col-md-2'); + } else { + $("#editors").removeClass().addClass('col-md-12'); + $("#evalcol").removeClass().addClass('hidden'); + $("#modules").removeClass().addClass('hidden'); + } + }; + + function resizeAce() { + var h = window.innerHeight; + if (h > 360) { + $('#editor').css('height', (h - 175).toString() + 'px'); + } + }; + $(window).on('resize', function () { + resizeAce(); + }); + resizeAce(); + + $("#submit").on('click', function () { + var code = $("#raw_editor").is(":checked") ? + $("#paste").val() : + editor.getValue(); + $("#paste").text(code); // copy to the textarea + }); + + $('#evalme').on('click', function () { + showingeval = 1; + $('#eval').text("Evaluating..."); + + setup_columns(); + + var code = $("#raw_editor").is(":checked") ? + $("#paste").val() : + editor.getValue(); + + var language = $('#language option').filter(':selected').val(); + + $.ajax('/eval', { + method: 'post', + data: {code: code, language: language}, + dataType: "json", + success: function(data, status) { + console.log("data out", data); + var keys = Object.keys(data.evalout); + var outputarr = []; + + if (keys.length > 1) { + outputarr = $.map(data.evalout, function(output, lang) { + return "[[ "+lang+" ]]\n"+output+"\n\n"; + }); + } else { + outputarr = [data.evalout[keys[0]]]; + } + console.log("outputarr", outputarr); + console.log(outputarr.join("\n")); + + $('#eval').text(outputarr.join("\n")); + } + }); + }); + + $("#showmodules").on('click', function() { + showingmodules = 1 - showingmodules; + + setup_columns(); + }); + +}); diff --git a/static/test.html b/static/test.html new file mode 100644 index 0000000..10ae45e --- /dev/null +++ b/static/test.html @@ -0,0 +1,162 @@ + + + + + Amazon Lex for JavaScript - Sample Application (BookTrip) + + + + + +

Amazon Lex - BookTrip

+

+ This little chatbot shows how easy it is to incorporate + Amazon Lex into your web pages. Try it out. +

+
+
+ +
+ + + + diff --git a/static/test2.html b/static/test2.html new file mode 100644 index 0000000..5c4af1e --- /dev/null +++ b/static/test2.html @@ -0,0 +1,9 @@ + +perlbot v2 api test + +
+ + + +
+ diff --git a/templates/neweditor.html.tt b/templates/neweditor.html.tt new file mode 100644 index 0000000..e123a49 --- /dev/null +++ b/templates/neweditor.html.tt @@ -0,0 +1,101 @@ +[% BLOCK body_style %] + + + +[% END %] + +[% BLOCK page_header %] +
+
+ + +
+
+ + +
+
+ + + +
+ +
+[% END %] + +[% BLOCK body %] +
+ +
+
+
+ [% PROCESS page_header %] +
+
+
+ +
+
+
+ + + +
+ + +
+
+ + + + + + + + + +
+
+ +

+              
+
+ +
+ + +
+ + +
+ +[% END %] From b21463287b47a4794de5ecb26ee38b360270226d Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Sun, 8 Sep 2019 01:49:48 -0400 Subject: [PATCH 2/2] Waiting works properly --- lib/App/Controller/Apiv1.pm | 7 +++++-- lib/App/Controller/Apiv2.pm | 10 +++++----- lib/App/Controller/Eval.pm | 10 ++++++---- lib/App/Controller/Paste.pm | 7 +++++-- lib/App/Model/Eval.pm | 24 +++++++++++++++++++----- templates/viewer.html | 3 +++ 6 files changed, 43 insertions(+), 18 deletions(-) diff --git a/lib/App/Controller/Apiv1.pm b/lib/App/Controller/Apiv1.pm index b560315..7bbdf22 100644 --- a/lib/App/Controller/Apiv1.pm +++ b/lib/App/Controller/Apiv1.pm @@ -30,10 +30,12 @@ sub api_get_paste { if ($row) { $c->delay(sub { my ($delay) = @_; - $c->eval->get_eval($pasteid, $row->{paste}, [$row->{language}], $delay->begin(0, 1)) + $c->eval->get_eval($pasteid, $row->{paste}, [$row->{language}], 0, $delay->begin(0, 1)) }, sub { - my ($delay, $output_hr) = @_; + my ($delay, $evalres) = @_; + + my ($status, $output_hr) = $evalres->@{qw/status output/}; my ($output_lang) = keys %$output_hr; # grab a random output value, should be the first one since multilang support isn't working yet my ($output) = $output_hr->{$output_lang}; @@ -44,6 +46,7 @@ sub api_get_paste { description => $row->{desc}, language => $output_lang, output => $output, + status => $status, warning => "If this was multi-language paste, you just got a random language", }; diff --git a/lib/App/Controller/Apiv2.pm b/lib/App/Controller/Apiv2.pm index 388f88f..f2b1c12 100644 --- a/lib/App/Controller/Apiv2.pm +++ b/lib/App/Controller/Apiv2.pm @@ -50,21 +50,21 @@ sub api_get_paste { if ($row) { $c->delay(sub { my ($delay) = @_; - $c->eval->get_eval($pasteid, $row->{paste}, [$row->{language}], $delay->begin(0, 1)) + $c->eval->get_eval($pasteid, $row->{paste}, [$row->{language}], 0, $delay->begin(0, 1)) }, sub { - my ($delay, $output_hr) = @_; + my ($delay, $evalres) = @_; + + my ($status, $output_hr) = $evalres->@{qw/status output/}; my ($output_lang) = keys %$output_hr; # grab a random output value, should be the first one since multilang support isn't working yet - my ($output) = $output_hr->{$output_lang}; my $data = { paste => $row->{paste}, when => $row->{when}, username => $row->{who}, description => $row->{desc}, language => $output_lang, - output => $output, - warning => "If this was multi-language paste, you just got a random language", + output => $output_hr, }; $c->render(json => $data); diff --git a/lib/App/Controller/Eval.pm b/lib/App/Controller/Eval.pm index 41505f4..605129e 100644 --- a/lib/App/Controller/Eval.pm +++ b/lib/App/Controller/Eval.pm @@ -20,8 +20,6 @@ sub run_eval { my ($self) = @_; my $data = $self->req->body_params; - - $self = $self->inactivity_timeout(3600); my $code = $data->param('code') // ''; @@ -29,13 +27,17 @@ sub run_eval { $self->delay(sub { my $delay = shift; - $self->eval->get_eval(undef, $code, [$language], $delay->begin(0,1)); + $self->eval->get_eval(undef, $code, [$language], 1, $delay->begin(0,1)); return 1; }, sub { my $delay = shift; - my ($output) = @_; + my ($evalres) = @_; + + use Data::Dumper; + print Dumper("wutwut", $evalres); + my $output = $evalres->{output}; $self->render(json => {evalout => $output}); }) diff --git a/lib/App/Controller/Paste.pm b/lib/App/Controller/Paste.pm index 5b43a90..4160c26 100644 --- a/lib/App/Controller/Paste.pm +++ b/lib/App/Controller/Paste.pm @@ -85,15 +85,18 @@ sub get_paste { $c->delay(sub { my $delay = shift; - $c->eval->get_eval($pasteid, $row->{paste}, [$row->{language}], $delay->begin(0,1)); + $c->eval->get_eval($pasteid, $row->{paste}, [$row->{language}], 0, $delay->begin(0,1)); }, sub { - my ($delay, $evalout) = @_; + my ($delay, $evalres) = @_; + + my ($status, $evalout) = $evalres->@{qw/status output/}; $c->stash($row); $c->stash({language => $c->languages->get_language_hash->{$row->{language}}}); $c->stash({all_langs => $c->languages->get_language_hash}); $c->stash({page_tmpl => 'viewer.html'}); $c->stash({paste_id => $pasteid}); $c->stash({eval => $evalout}); + $c->stash({eval_status => $status}); $c->stash({perl_sort_versions => \&App::Model::Languages::perl_sort_versions}); $c->render('page'); diff --git a/lib/App/Model/Eval.pm b/lib/App/Model/Eval.pm index 30f9fa2..4654cdb 100644 --- a/lib/App/Model/Eval.pm +++ b/lib/App/Model/Eval.pm @@ -29,9 +29,14 @@ sub _adopt_future { }) } +sub _get_cache { + my ($key) = @_; + return unless defined $key; + return $memd->get($key); +} sub get_eval { - my ($self, $paste_id, $code, $langs, $callback) = @_; + my ($self, $paste_id, $code, $langs, $wait, $callback) = @_; print "Entering\n"; if (@$langs == 1 && $langs->[0] eq "evalall") { @@ -40,7 +45,7 @@ sub get_eval { use Data::Dumper; print "Languages! ", Dumper($langs); - if ($paste_id && (my $cached = $memd->get($paste_id))) { + if ($paste_id && (my $cached = _get_cache($paste_id))) { $callback->($cached); } else { # connect to server @@ -52,9 +57,16 @@ sub get_eval { my $reader = $self->get_eval_reader($stream); my %output; + # TODO make running status messages per langs? + $memd->set($paste_id, {status => 'running', output => {}}); + + if (!$wait && @$langs != 1) { + $callback->({status => 'running', output => {}}); + } + for my $lang (@$langs) { if ($lang eq 'text') { - $callback->(""); + $callback->({status => "ready", output => {}}); return; } else { my $future = $self->async_eval($stream, $reader, $lang, $code); @@ -72,11 +84,13 @@ sub get_eval { if (!keys %futures) { # I'm the last one print "Calling memset\n"; - $memd->set($paste_id, \%output) if ($paste_id); + $memd->set($paste_id, {status => 'ready', output => \%output}) if ($paste_id); print "Returning output to delay\n"; use Data::Dumper; print Dumper(\%output); - $callback->(\%output); + if ($wait || @$langs == 1) { + $callback->({status => "ready", output => \%output}); + } } }); } diff --git a/templates/viewer.html b/templates/viewer.html index c66754c..f2906a0 100755 --- a/templates/viewer.html +++ b/templates/viewer.html @@ -74,6 +74,9 @@
+ [% IF eval_status == "running" %] +
Eval still running, please refresh page after waiting
+ [% END %] [% IF eval.keys.size == 0 || !eval.keys.size.defined %] [% ELSIF eval.keys.size == 1 %]