Loading a file works

Thomas Kluyver 12 years ago
parent e6935d47c0
commit 25ee73a554

@ -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)

@ -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(
$('<p/>').text(dir_path + " is not a text file")
);
}
}
});
});

@ -2,13 +2,19 @@
{% block title %}{{page_title}}{% endblock %}
{% block stylesheet %}
<link rel="stylesheet" href="{{ static_url('components/codemirror/lib/codemirror.css') }}">
{{super()}}
{% endblock %}
{% block params %}
data-base-url="{{base_url}}"
data-file-path="{{file_path}}"
{% endblock %}
{% block site %}
<div id="texteditor-container"></div>

@ -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),

Loading…
Cancel
Save