Skip to content

Commit 5897787

Browse files
committed
view readme in directory
1 parent 9d4852d commit 5897787

File tree

4 files changed

+120
-1
lines changed

4 files changed

+120
-1
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (c) Jupyter Development Team.
2+
// Distributed under the terms of the Modified BSD License.
3+
4+
define([
5+
'jquery',
6+
'base/js/utils',
7+
'base/js/events',
8+
'components/marked/lib/marked'
9+
], function ($, utils, events, marked) {
10+
"use strict";
11+
12+
var DirectoryReadme = function (selector, notebook_list, options) {
13+
this.selector = selector;
14+
this.element = $(selector);
15+
this.notebook_list = notebook_list;
16+
this.base_url = options.base_url || utils.get_body_data("baseUrl");
17+
this.contents = options.contents;
18+
19+
this.init_readme();
20+
this.bind_events();
21+
};
22+
23+
DirectoryReadme.prototype.fetch_readme = function() {
24+
var that = this;
25+
this.contents.get(utils.url_path_join(this.notebook_list.notebook_path, 'readme.md'), {type: 'file'}).then(
26+
function(file) {
27+
that.draw_readme(file);
28+
},
29+
function() {
30+
that.clear_readme();
31+
}
32+
);
33+
}
34+
35+
DirectoryReadme.prototype.bind_events = function () {
36+
events.on("draw_notebook_list.NotebookList", $.proxy(this.fetch_readme, this));
37+
}
38+
39+
DirectoryReadme.prototype.init_readme = function() {
40+
var element = this.element;
41+
element.hide().addClass("list_container");
42+
43+
this.title = $("<a />");
44+
$("<div/>")
45+
.addClass("list_header row")
46+
.html([
47+
$('<i/>')
48+
.addClass('item_icon file_icon'),
49+
this.title
50+
]).appendTo(element);
51+
52+
53+
var frame = $("<iframe/>")
54+
.attr("src", "about:blank")
55+
.attr("sandbox", "allow-same-origin")
56+
.appendTo(element);
57+
58+
frame.on("load", function() {
59+
var contents = frame.contents();
60+
contents.find("head")
61+
.append($("<link/>")
62+
.attr("rel", "stylesheet")
63+
.attr("type","text/css")
64+
.attr("href", utils.url_path_join(
65+
element.data("static-url"),
66+
"style",
67+
"style.min.css"
68+
)));
69+
});
70+
this.frame = frame;
71+
}
72+
73+
DirectoryReadme.prototype.clear_readme = function () {
74+
this.element.hide();
75+
}
76+
77+
DirectoryReadme.prototype.draw_readme = function (file) {
78+
this.element.show();
79+
this.title
80+
.attr("href",
81+
utils.url_path_join(
82+
this.base_url,
83+
"edit",
84+
utils.encode_uri_components(file.path)
85+
))
86+
.text(file.name);
87+
this.frame.height(0); // reset height
88+
var contents = this.frame.contents();
89+
contents.find("body").html(marked(file.content));
90+
this.frame.height(contents.height());
91+
};
92+
93+
return {'DirectoryReadme': DirectoryReadme};
94+
});

notebook/static/tree/js/main.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ requirejs([
3636
'tree/js/terminallist',
3737
'tree/js/newnotebook',
3838
'tree/js/shutdownbutton',
39+
'tree/js/directoryreadme',
3940
'auth/js/loginwidget',
4041
'bidi/bidi',
4142
], function(
@@ -54,6 +55,7 @@ requirejs([
5455
terminallist,
5556
newnotebook,
5657
shutdownbutton,
58+
directoryreadme,
5759
loginwidget,
5860
bidi){
5961
"use strict";
@@ -100,7 +102,11 @@ requirejs([
100102
var kernel_list = new kernellist.KernelList('#running_list', $.extend({
101103
session_list: session_list},
102104
common_options));
103-
105+
var directory_readme = new directoryreadme.DirectoryReadme('#directory_readme', notebook_list, $.extend({
106+
contents: contents,
107+
base_url: common_options.base_url,},
108+
common_options));
109+
104110
var terminal_list;
105111
if (utils.get_body_data("terminalsAvailable") === "True") {
106112
terminal_list = new terminallist.TerminalList('#terminal_list', common_options);

notebook/static/tree/less/tree.less

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
// The left padding of the selector button's contents.
1616
@dashboard-selectorbtn-lpad: 7px;
1717

18+
// The horizontal padding of the readme iframe.
19+
@dashboard_readme_lr_pad: 21px;
20+
1821
ul#tabs {
1922
margin-bottom: @dashboard_tb_pad;
2023
}
@@ -457,3 +460,18 @@ outline-width:1px;
457460
margin: 3px;
458461
font-size:10px;
459462
}
463+
464+
#directory_readme {
465+
&>div {
466+
padding-top: @dashboard_tb_pad;
467+
padding-bottom: @dashboard_tb_pad;
468+
padding-left: @dashboard_lr_pad;
469+
padding-right: @dashboard_lr_pad;
470+
}
471+
472+
iframe {
473+
padding: @dashboard_tb_pad @dashboard_readme_lr_pad;
474+
border: 0;
475+
width: 100%;
476+
}
477+
}

notebook/templates/tree.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
</div>
153153
</div>
154154
</div>
155+
<div id="directory_readme" data-static-url="{{static_url("")}}"></div>
155156
</div>
156157
<div id="running" class="tab-pane">
157158
<div id="running_toolbar" class="row">

0 commit comments

Comments
 (0)