From 82765a7db97b614b5c78e6edeb2a2b9b61f2f8e9 2014-11-20 19:52:35 From: Thomas Kluyver Date: 2014-11-20 19:52:35 Subject: [PATCH] Loading a file works --- diff --git a/IPython/html/services/contents/filemanager.py b/IPython/html/services/contents/filemanager.py index 92465e1..2a313ba 100644 --- a/IPython/html/services/contents/filemanager.py +++ b/IPython/html/services/contents/filemanager.py @@ -9,6 +9,7 @@ import io import os import shutil from contextlib import contextmanager +import mimetypes from tornado import web @@ -204,6 +205,7 @@ class FileContentsManager(ContentsManager): model['created'] = created model['content'] = None model['format'] = None + model['mimetype'] = None try: model['writable'] = os.access(os_path, os.W_OK) except OSError: @@ -264,8 +266,11 @@ class FileContentsManager(ContentsManager): """ model = self._base_model(path) model['type'] = 'file' + + os_path = self._get_os_path(path) + model['mimetype'] = mimetypes.guess_type(os_path)[0] or 'text/plain' + if content: - os_path = self._get_os_path(path) if not os.path.isfile(os_path): # could be FIFO raise web.HTTPError(400, "Cannot get content of non-file %s" % os_path) diff --git a/IPython/html/static/texteditor/js/main.js b/IPython/html/static/texteditor/js/main.js index b630eeb..2ed7713 100644 --- a/IPython/html/static/texteditor/js/main.js +++ b/IPython/html/static/texteditor/js/main.js @@ -5,19 +5,56 @@ require([ 'jquery', 'base/js/utils', 'base/js/page', + 'contents', 'codemirror/lib/codemirror', + 'codemirror/mode/meta', 'custom/custom', ], function( $, utils, page, + contents, CodeMirror ){ page = new page.Page(); var base_url = utils.get_body_data('baseUrl'); - var cm_instance = CodeMirror($('#texteditor-container')[0]); - - page.show(); + contents = new contents.Contents({base_url: base_url}); + var file_path = utils.get_body_data('filePath'); + var ix = file_path.lastIndexOf("/"); + var dir_path, basename; + if (ix == -1) { + dir_path = ''; + basename = file_path; + } else { + dir_path = file_path.substring(0, ix); + basename = file_path.substring(ix); + } + contents.load(dir_path, basename, { + success: function(model) { + page.show(); + if (model.type === "file" && model.format === "text") { + console.log(modeinfo); + var cm = CodeMirror($('#texteditor-container')[0], { + value: model.content, + }); + + // Find and load the highlighting mode + var modeinfo = CodeMirror.findModeByMIME(model.mimetype); + if (modeinfo) { + utils.requireCodeMirrorMode(modeinfo.mode, function() { + cm.setOption('mode', modeinfo.mode); + }); + } + + // Attach to document for debugging + document.cm_instance = cm; + } else { + $('#texteditor-container').append( + $('

').text(dir_path + " is not a text file") + ); + } + } + }); }); diff --git a/IPython/html/templates/texteditor.html b/IPython/html/templates/texteditor.html index 6112181..e1ffb6f 100644 --- a/IPython/html/templates/texteditor.html +++ b/IPython/html/templates/texteditor.html @@ -2,13 +2,19 @@ {% block title %}{{page_title}}{% endblock %} +{% block stylesheet %} + + +{{super()}} +{% endblock %} + {% block params %} data-base-url="{{base_url}}" +data-file-path="{{file_path}}" {% endblock %} - {% block site %}

diff --git a/IPython/html/texteditor/handlers.py b/IPython/html/texteditor/handlers.py index 26b22bc..199c545 100644 --- a/IPython/html/texteditor/handlers.py +++ b/IPython/html/texteditor/handlers.py @@ -6,12 +6,21 @@ from tornado import web from ..base.handlers import IPythonHandler, file_path_regex +from ..utils import url_escape class EditorHandler(IPythonHandler): """Render the terminal interface.""" @web.authenticated def get(self, path, name): - self.write(self.render_template('texteditor.html')) + path = path.strip('/') + if not self.contents_manager.file_exists(name, path): + raise web.HTTPError(404, u'File does not exist: %s/%s' % (path, name)) + + file_path = url_escape(path) + "/" + url_escape(name) + self.write(self.render_template('texteditor.html', + file_path=file_path, + ) + ) default_handlers = [ (r"/texteditor%s" % file_path_regex, EditorHandler),