Parcourir la source

Implement ipython modes

Steven Silvester il y a 9 ans
Parent
commit
51c25a8a89

+ 1 - 1
src/notebook/cells/widget.ts

@@ -127,7 +127,7 @@ class MarkdownCellWidget extends CellWidget {
 
     this._model = model;
     // Insist on the Github-flavored markdown mode
-    model.input.textEditor.mimetype = 'text/x-gfm';
+    model.input.textEditor.mimetype = 'text/x-ipythongfm';
     this.input = new InputAreaWidget(model.input);
     this.rendered = new Widget();
     if (model.rendered) {

+ 29 - 0
src/notebook/notebook/codemirror-ipython.ts

@@ -0,0 +1,29 @@
+// IPython mode is just a slightly altered Python Mode with `?` beeing a extra
+// single operator. Here we define `ipython` mode in the require `python`
+// callback to auto-load python mode, which is more likely not the best things
+// to do, but at least the simple one for now.
+"use strict";
+
+import * as CodeMirror
+  from 'codemirror';
+
+import 'codemirror/mode/python/python';
+
+CodeMirror.defineMode("ipython", (config: CodeMirror.EditorConfiguration, modeOptions?: any) => {
+    let pythonConf: any = {};
+    for (var prop in modeOptions) {
+      if (modeOptions.hasOwnProperty(prop)) {
+        pythonConf[prop] = modeOptions[prop];
+      }
+    }
+    pythonConf.name = 'python';
+    pythonConf.singleOperators = new RegExp("^[\\+\\-\\*/%&|\\^~<>!\\?]");
+    if (pythonConf.version === 3) {
+        pythonConf.identifiers = new RegExp("^[_A-Za-z\u00A1-\uFFFF][_A-Za-z0-9\u00A1-\uFFFF]*");
+    } else if (pythonConf.version === 2) {
+      pythonConf.identifiers = new RegExp("^[_A-Za-z][_A-Za-z0-9]*");
+    }
+    return CodeMirror.getMode(config, pythonConf);
+}, 'python');
+
+CodeMirror.defineMIME("text/x-ipython", "ipython");

+ 49 - 0
src/notebook/notebook/codemirror-ipythongfm.ts

@@ -0,0 +1,49 @@
+// IPython GFM (GitHub Flavored Markdown) mode is just a slightly altered GFM
+// Mode with support for latex.
+//
+// Latex support was supported by Codemirror GFM as of
+//   https://github.com/codemirror/CodeMirror/pull/567
+// But was later removed in
+//   https://github.com/codemirror/CodeMirror/commit/d9c9f1b1ffe984aee41307f3e927f80d1f23590c
+"use strict";
+
+import * as CodeMirror
+  from 'codemirror';
+
+import 'codemirror/mode/stex/stex';
+import 'codemirror/mode/gfm/gfm';
+import 'codemirror/addon/mode/multiplex';
+
+
+CodeMirror.defineMode("ipythongfm", (config: CodeMirror.EditorConfiguration, modeOptions?: any) => {
+  let gfm_mode = CodeMirror.getMode(config, "gfm");
+  let tex_mode = CodeMirror.getMode(config, "stex");
+
+  return CodeMirror.multiplexingMode(
+    gfm_mode,
+    {
+      open: "$", close: "$",
+      mode: tex_mode,
+      delimStyle: "delimit"
+    },
+    {
+      // not sure this works as $$ is interpreted at (opening $, closing $, as defined just above)
+      open: "$$", close: "$$",
+      mode: tex_mode,
+      delimStyle: "delimit"
+    },
+    {
+      open: "\\(", close: "\\)",
+      mode: tex_mode,
+      delimStyle: "delimit"
+    },
+    {
+      open: "\\[", close: "\\]",
+      mode: tex_mode,
+      delimStyle: "delimit"
+    }
+    // .. more multiplexed styles can follow here
+  );
+}, 'gfm');
+
+CodeMirror.defineMIME("text/x-ipythongfm", "ipythongfm");

+ 1 - 0
src/notebook/notebook/model.ts

@@ -456,6 +456,7 @@ namespace NotebookModelPrivate {
   export
   const defaultMimetype = new Property<NotebookModel, string>({
     name: 'defaultMimetype',
+    value: "text/x-ipython",
     notify: stateChangedSignal,
   });
 

+ 0 - 1
src/notebook/notebook/serialize.ts

@@ -47,7 +47,6 @@ import {
 export
 function populateNotebookModel(nb: INotebookModel, data: NotebookContent): void {
   nb.cells.clear();
-  nb.defaultMimetype = 'text/x-python';
 
   // Iterate through the cell data, creating cell models.
   data.cells.forEach((c) => {

+ 3 - 0
src/notebook/notebook/widget.ts

@@ -36,6 +36,9 @@ import {
   DisposableDelegate, IDisposable
 } from 'phosphor-disposable';
 
+import './codemirror-ipython';
+import './codemirror-ipythongfm';
+
 
 /**
  * The class name added to notebook widgets.

+ 11 - 0
src/notebook/typings/codemirror/codemirror.d.ts

@@ -1057,6 +1057,17 @@ declare module CodeMirror {
      * (modes should not leak anything into the global scope!), i.e. write your whole mode inside this function.
      */
     function defineMode(id: string, modefactory: ModeFactory<any>): void;
+    function defineMode(id: string, modefactory: ModeFactory<any>, base: any): void;
+
+    /**
+     * Define a mimetype.
+     */
+    function defineMIME(mimetype: string, mode: any): void;
+
+    /**
+     * A mode that encompasses many mode types.
+     */
+    function multiplexingMode<T>(...modes: any[]): Mode<T>;
 
     /**
      * The first argument is a configuration object as passed to the mode constructor function, and the second argument