Commit 423fdc15 authored by Andrew Gibiansky's avatar Andrew Gibiansky

Update parsec widget to use a javascript display for its JS

parent 9036da3d
...@@ -6,19 +6,20 @@ import System.Random ...@@ -6,19 +6,20 @@ import System.Random
import Data.String.Here import Data.String.Here
import Data.HashMap.Strict as Map import Data.HashMap.Strict as Map
import Text.Parsec import Text.Parsec (parse, sourceLine, sourceColumn)
import Text.Parsec.Prim --import Text.Parsec.Prim (Parser)
import Text.Parsec.String import Text.Parsec.String (Parser)
import Text.Parsec.Error import Text.Parsec.Error (errorPos, ParseError)
import Data.Aeson import Data.Aeson
import IHaskell.Display import IHaskell.Display
instance Show a => IHaskellDisplay (Parser a) where instance Show a => IHaskellDisplay (Parser a) where
display renderable = return $ Display [html dom] display renderable = return $ many [Display [javascript js], Display [html dom]]
where where
dom = [hereFile|widget.html|] dom = [hereFile|widget.html|]
js = [hereFile|widget.js|]
-- | Text to parse. -- | Text to parse.
data ParseText = ParseText String data ParseText = ParseText String
......
...@@ -2,87 +2,6 @@ ...@@ -2,87 +2,6 @@
<link rel="stylesheet" href="/static/components/codemirror/addon/lint/lint.css"> <link rel="stylesheet" href="/static/components/codemirror/addon/lint/lint.css">
<script src="/static/components/codemirror/addon/lint/lint.js" charset="utf-8"></script> <script src="/static/components/codemirror/addon/lint/lint.js" charset="utf-8"></script>
<!-- Parsec widget -->
<script>
// Only load this script once.
var kernel = IPython.notebook.kernel;
var initialized = kernel !== undefined && kernel != null;
console.log("Initialized", initialized);
if (initialized && window.parsecWidgetRegistered === undefined) {
// Do not load this script again.
window.parsecWidgetRegistered = true;
// Register the comm target.
var ParsecWidget = function (comm) {
this.comm = comm;
this.comm.on_msg($.proxy(this.handler, this));
// Get the cell that was probably executed.
// The msg_id:cell mapping will make this possible without guessing.
this.cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()-1);
// Store this widget so we can use it from callbacks.
var widget = this;
// Editor options.
var options = {
lineNumbers: true,
// Show parsec errors as lint errors.
gutters: ["CodeMirror-lint-markers"],
lintWith: {
"getAnnotations": function(cm, update, opts) {
var errs = [];
if (widget.hasError) {
var col = widget.error["col"];
var line = widget.error["line"];
errs = [{
from: CodeMirror.Pos(line - 1, col - 1),
to: CodeMirror.Pos(line - 1, col),
message: widget.error["msg"],
severity: "error"
}];
}
update(cm, errs);
},
"async": true,
}
};
// Create the editor.
var out = this.cell.output_area.element;
this.textarea = out.find("#parsec-editor")[0];
this.output = out.find("#parsec-output")[0];
var editor = CodeMirror.fromTextArea(this.textarea, options);
var editor = editor;
// Update every key press.
editor.on("keyup", function() {
var text = editor.getDoc().getValue();
comm.send({"text": text});
});
};
ParsecWidget.prototype.handler = function(msg) {
var data = msg.content.data;
this.hasError = data["status"] == "error";
if (this.hasError) {
out = data["msg"];
this.error = data;
} else {
out = data["result"];
}
// Update viewed output.
this.output.innerHTML = out;
};
// Register this widget.
IPython.notebook.kernel.comm_manager.register_target('parsec', IPython.utils.always_new(ParsecWidget));
console.log("Registering Parsec widget.");
}
</script>
<!-- Parsec widget DOM --> <!-- Parsec widget DOM -->
<form><textarea id="parsec-editor">Insert parser text here...</textarea></form> <form><textarea id="parsec-editor">Insert parser text here...</textarea></form>
<pre id="parsec-output"></pre> <pre id="parsec-output"></pre>
// Only load this script once.
var kernel = IPython.notebook.kernel;
var initialized = kernel !== undefined && kernel != null;
console.log("Initialized", initialized);
if (initialized && window.parsecWidgetRegistered === undefined) {
// Do not load this script again.
window.parsecWidgetRegistered = true;
// Register the comm target.
var ParsecWidget = function (comm) {
this.comm = comm;
this.comm.on_msg($.proxy(this.handler, this));
// Get the cell that was probably executed.
// The msg_id:cell mapping will make this possible without guessing.
this.cell = IPython.notebook.get_cell(IPython.notebook.get_selected_index()-1);
// Store this widget so we can use it from callbacks.
var widget = this;
// Editor options.
var options = {
lineNumbers: true,
// Show parsec errors as lint errors.
gutters: ["CodeMirror-lint-markers"],
lintWith: {
"getAnnotations": function(cm, update, opts) {
var errs = [];
if (widget.hasError) {
var col = widget.error["col"];
var line = widget.error["line"];
errs = [{
from: CodeMirror.Pos(line - 1, col - 1),
to: CodeMirror.Pos(line - 1, col),
message: widget.error["msg"],
severity: "error"
}];
}
update(cm, errs);
},
"async": true,
}
};
// Create the editor.
var out = this.cell.output_area.element;
this.textarea = out.find("#parsec-editor")[0];
this.output = out.find("#parsec-output")[0];
var editor = CodeMirror.fromTextArea(this.textarea, options);
var editor = editor;
// Update every key press.
editor.on("keyup", function() {
var text = editor.getDoc().getValue();
comm.send({"text": text});
});
};
ParsecWidget.prototype.handler = function(msg) {
var data = msg.content.data;
this.hasError = data["status"] == "error";
if (this.hasError) {
out = data["msg"];
this.error = data;
} else {
out = data["result"];
}
// Update viewed output.
this.output.innerHTML = out;
};
// Register this widget.
IPython.notebook.kernel.comm_manager.register_target('parsec', IPython.utils.always_new(ParsecWidget));
console.log("Registering Parsec widget.");
}
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"metadata": { "metadata": {
"language": "haskell", "language": "haskell",
"name": "", "name": "",
"signature": "sha256:e714927a2d2b479853e19f3784692d8043ea0de9bcba6778363470d13d4f3f85" "signature": "sha256:82a2a991a98051acbda06fcf18983a1920f3295003c796f6aaba212cd0a71ae5"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
...@@ -84,20 +84,14 @@ ...@@ -84,20 +84,14 @@
" \n", " \n",
" -- Parse any whitespace\n", " -- Parse any whitespace\n",
" whitespace = many $ oneOf \" \\t\"\n", " whitespace = many $ oneOf \" \\t\"\n",
"\n", " \n",
"parser" "parser"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"html": [ "javascript": [
"<!-- CodeMirror component -->\n",
"<link rel=\"stylesheet\" href=\"/static/components/codemirror/addon/lint/lint.css\">\n",
"<script src=\"/static/components/codemirror/addon/lint/lint.js\" charset=\"utf-8\"></script>\n",
"\n",
"<!-- Parsec widget -->\n",
"<script>\n",
"// Only load this script once.\n", "// Only load this script once.\n",
"var kernel = IPython.notebook.kernel;\n", "var kernel = IPython.notebook.kernel;\n",
"var initialized = kernel !== undefined && kernel != null;\n", "var initialized = kernel !== undefined && kernel != null;\n",
...@@ -174,8 +168,16 @@ ...@@ -174,8 +168,16 @@
"// Register this widget.\n", "// Register this widget.\n",
"IPython.notebook.kernel.comm_manager.register_target('parsec', IPython.utils.always_new(ParsecWidget));\n", "IPython.notebook.kernel.comm_manager.register_target('parsec', IPython.utils.always_new(ParsecWidget));\n",
"console.log(\"Registering Parsec widget.\");\n", "console.log(\"Registering Parsec widget.\");\n",
"}\n", "}\n"
"</script>\n", ],
"metadata": {},
"output_type": "display_data"
},
{
"html": [
"<!-- CodeMirror component -->\n",
"<link rel=\"stylesheet\" href=\"/static/components/codemirror/addon/lint/lint.css\">\n",
"<script src=\"/static/components/codemirror/addon/lint/lint.js\" charset=\"utf-8\"></script>\n",
"\n", "\n",
"<!-- Parsec widget DOM -->\n", "<!-- Parsec widget DOM -->\n",
"<form><textarea id=\"parsec-editor\">Insert parser text here...</textarea></form>\n", "<form><textarea id=\"parsec-editor\">Insert parser text here...</textarea></form>\n",
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
module IHaskell.Display ( module IHaskell.Display (
IHaskellDisplay(..), IHaskellDisplay(..),
IHaskellWidget(..), IHaskellWidget(..),
plain, html, png, jpg, svg, latex, plain, html, png, jpg, svg, latex, javascript, many,
serializeDisplay, serializeDisplay,
Width, Height, Base64(..), Width, Height, Base64(..),
encode64, base64, encode64, base64,
...@@ -69,14 +69,18 @@ plain = DisplayData PlainText . pack . rstrip ...@@ -69,14 +69,18 @@ plain = DisplayData PlainText . pack . rstrip
html :: String -> DisplayData html :: String -> DisplayData
html = DisplayData MimeHtml . pack html = DisplayData MimeHtml . pack
-- | Genreate an SVG display. -- | Generate an SVG display.
svg :: String -> DisplayData svg :: String -> DisplayData
svg = DisplayData MimeSvg . pack svg = DisplayData MimeSvg . pack
-- | Genreate a LaTeX display. -- | Generate a LaTeX display.
latex :: String -> DisplayData latex :: String -> DisplayData
latex = DisplayData MimeLatex . pack latex = DisplayData MimeLatex . pack
-- | Generate a Javascript display.
javascript :: String -> DisplayData
javascript = DisplayData MimeJavascript . pack
-- | Generate a PNG display of the given width and height. Data must be -- | Generate a PNG display of the given width and height. Data must be
-- provided in a Base64 encoded manner, suitable for embedding into HTML. -- provided in a Base64 encoded manner, suitable for embedding into HTML.
-- The @base64@ function may be used to encode data into this format. -- The @base64@ function may be used to encode data into this format.
......
...@@ -394,6 +394,7 @@ data MimeType = PlainText ...@@ -394,6 +394,7 @@ data MimeType = PlainText
| MimeJpg Width Height | MimeJpg Width Height
| MimeSvg | MimeSvg
| MimeLatex | MimeLatex
| MimeJavascript
deriving (Eq, Typeable, Generic) deriving (Eq, Typeable, Generic)
-- Extract the plain text from a list of displays. -- Extract the plain text from a list of displays.
...@@ -412,3 +413,4 @@ instance Show MimeType where ...@@ -412,3 +413,4 @@ instance Show MimeType where
show (MimeJpg _ _) = "image/jpeg" show (MimeJpg _ _) = "image/jpeg"
show MimeSvg = "image/svg+xml" show MimeSvg = "image/svg+xml"
show MimeLatex = "text/latex" show MimeLatex = "text/latex"
show MimeJavascript = "application/javascript"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment