##// END OF EJS Templates
Loading a file works
Thomas Kluyver -
Show More
@@ -9,6 +9,7 b' import io'
9 9 import os
10 10 import shutil
11 11 from contextlib import contextmanager
12 import mimetypes
12 13
13 14 from tornado import web
14 15
@@ -204,6 +205,7 b' class FileContentsManager(ContentsManager):'
204 205 model['created'] = created
205 206 model['content'] = None
206 207 model['format'] = None
208 model['mimetype'] = None
207 209 try:
208 210 model['writable'] = os.access(os_path, os.W_OK)
209 211 except OSError:
@@ -264,8 +266,11 b' class FileContentsManager(ContentsManager):'
264 266 """
265 267 model = self._base_model(path)
266 268 model['type'] = 'file'
269
270 os_path = self._get_os_path(path)
271 model['mimetype'] = mimetypes.guess_type(os_path)[0] or 'text/plain'
272
267 273 if content:
268 os_path = self._get_os_path(path)
269 274 if not os.path.isfile(os_path):
270 275 # could be FIFO
271 276 raise web.HTTPError(400, "Cannot get content of non-file %s" % os_path)
@@ -5,19 +5,56 b' require(['
5 5 'jquery',
6 6 'base/js/utils',
7 7 'base/js/page',
8 'contents',
8 9 'codemirror/lib/codemirror',
10 'codemirror/mode/meta',
9 11 'custom/custom',
10 12 ], function(
11 13 $,
12 14 utils,
13 15 page,
16 contents,
14 17 CodeMirror
15 18 ){
16 19 page = new page.Page();
17 20
18 21 var base_url = utils.get_body_data('baseUrl');
19 var cm_instance = CodeMirror($('#texteditor-container')[0]);
20
21 page.show();
22 contents = new contents.Contents({base_url: base_url});
22 23
24 var file_path = utils.get_body_data('filePath');
25 var ix = file_path.lastIndexOf("/");
26 var dir_path, basename;
27 if (ix == -1) {
28 dir_path = '';
29 basename = file_path;
30 } else {
31 dir_path = file_path.substring(0, ix);
32 basename = file_path.substring(ix);
33 }
34 contents.load(dir_path, basename, {
35 success: function(model) {
36 page.show();
37 if (model.type === "file" && model.format === "text") {
38 console.log(modeinfo);
39 var cm = CodeMirror($('#texteditor-container')[0], {
40 value: model.content,
41 });
42
43 // Find and load the highlighting mode
44 var modeinfo = CodeMirror.findModeByMIME(model.mimetype);
45 if (modeinfo) {
46 utils.requireCodeMirrorMode(modeinfo.mode, function() {
47 cm.setOption('mode', modeinfo.mode);
48 });
49 }
50
51 // Attach to document for debugging
52 document.cm_instance = cm;
53 } else {
54 $('#texteditor-container').append(
55 $('<p/>').text(dir_path + " is not a text file")
56 );
57 }
58 }
59 });
23 60 });
@@ -2,13 +2,19 b''
2 2
3 3 {% block title %}{{page_title}}{% endblock %}
4 4
5 {% block stylesheet %}
6 <link rel="stylesheet" href="{{ static_url('components/codemirror/lib/codemirror.css') }}">
7
8 {{super()}}
9 {% endblock %}
10
5 11 {% block params %}
6 12
7 13 data-base-url="{{base_url}}"
14 data-file-path="{{file_path}}"
8 15
9 16 {% endblock %}
10 17
11
12 18 {% block site %}
13 19
14 20 <div id="texteditor-container"></div>
@@ -6,12 +6,21 b''
6 6
7 7 from tornado import web
8 8 from ..base.handlers import IPythonHandler, file_path_regex
9 from ..utils import url_escape
9 10
10 11 class EditorHandler(IPythonHandler):
11 12 """Render the terminal interface."""
12 13 @web.authenticated
13 14 def get(self, path, name):
14 self.write(self.render_template('texteditor.html'))
15 path = path.strip('/')
16 if not self.contents_manager.file_exists(name, path):
17 raise web.HTTPError(404, u'File does not exist: %s/%s' % (path, name))
18
19 file_path = url_escape(path) + "/" + url_escape(name)
20 self.write(self.render_template('texteditor.html',
21 file_path=file_path,
22 )
23 )
15 24
16 25 default_handlers = [
17 26 (r"/texteditor%s" % file_path_regex, EditorHandler),
General Comments 0
You need to be logged in to leave comments. Login now