refactor code duplication of markdown renderers

This commit is contained in:
Toon Baeyens 2020-05-28 09:25:55 +02:00
parent 3f50688d51
commit 238828e36d
7 changed files with 4 additions and 231 deletions

View File

@ -735,7 +735,7 @@ define([
clean_tables: true
}, function (err, html) {
toinsert.append(html);
})
});
dblclick_to_reset_size(toinsert.find('img'));
element.append(toinsert);
return toinsert;

View File

@ -1,185 +0,0 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
define([
'jquery',
'base/js/utils',
'base/js/events',
'base/js/markdown',
], function ($, utils, events, markdown) {
"use strict";
function endsWith(haystack, needle) {
if(haystack.endsWith) return haystack.endsWith(needle);
return haystack.substring(
haystack.length - needle.length, haystack.length) === needle;
}
var DirectoryReadme = function (selector, notebook_list) {
/**
* Constructor
*
* Parameters:
* selector: string
* notebook_list: NotebookList
* Used to obtain a file listing of the active directory.
*/
this.selector = selector;
this.element = $(selector);
this.notebook_list = notebook_list;
this.drawn_readme = null;
this.readme_order = [
/^readme\.(md|markdown)$/i,
/^about\.(md|markdown)$/i,
/^readme(\.[^\.]*)?$/i,
/^about(\.[^\.]*)?$/i,
]
this.init_readme();
this.bind_events();
};
DirectoryReadme.prototype.find_readme = function() {
/**
* Find a readme in the current directory. Look for files with
* a name matching a pattern in this.readme_order.
*
*
* @return null or { name, path, last_modified... }
*/
var files_in_directory = this.notebook_list.model_list.content;
for(var j = 0; j < this.readme_order.length; ++j) {
var readme_name = this.readme_order[j];
for (var i = 0; i < files_in_directory.length; ++i) {
var file = files_in_directory[i];
if(file.type === "file"
&& file.name.match(readme_name)
){
return file;
}
}
}
return null;
}
DirectoryReadme.prototype.needs_update = function(readme) {
/**
* Checks if readme is newer or different from the current drawn readme.
*
* @private
* @return if a redraw should happen
*/
if(this.drawn_readme === readme) return false;
if(this.drawn_readme === null || readme === null) return true;
if(this.drawn_readme.path !== readme.path) return true;
if(this.draw_readme.last_modified < readme.last_modified) return true;
return false;
}
DirectoryReadme.prototype.fetch_readme = function() {
/**
* Find and fetch a readme file, and if necessary trigger a redraw.
*/
var readme = this.find_readme();
if(this.needs_update(readme)) {
if(readme === null) {
this.clear_readme();
} else {
var that = this;
this.notebook_list.contents.get(readme.path, {type: 'file'}).then(
function(file) {
if(file.format !== "text") {
that.clear_readme(file);
} else {
that.draw_readme(file);
}
},
function() {
that.clear_readme();
}
);
}
}
}
DirectoryReadme.prototype.bind_events = function () {
/**
* When the notebook_list fires a draw_notebook event, fetch the readme.
*/
events.on("draw_notebook_list.NotebookList", $.proxy(this.fetch_readme, this));
var that = this;
events.on("notebook_deleted.NotebookList", function(event, path) {
if(that.drawn_readme.path === path) {
that.clear_readme();
}
});
}
DirectoryReadme.prototype.init_readme = function() {
/**
* Build the DOM.
*/
var element = this.element;
element.hide().addClass("list_container");
this.title = $("<a />");
$("<div/>")
.addClass("list_header row readme_header")
.html([
$('<i/>')
.addClass('item_icon file_icon'),
this.title
]).appendTo(element);
this.page = $("<div/>")
.addClass("readme_content")
.appendTo(element);
}
DirectoryReadme.prototype.clear_readme = function (drawn_readme) {
/**
* If no readme is found, hide.
*/
this.drawn_readme = drawn_readme || null;
this.element.hide();
}
DirectoryReadme.prototype.draw_readme = function (file) {
/**
* Draw the given readme file. This function is used by fetch_readme.
*
* @param file: {name, path, content}
*/
this.drawn_readme = file;
this.element.show();
this.title
.attr("href",
utils.url_path_join(
this.notebook_list.base_url,
"edit",
utils.encode_uri_components(file.path)
))
.text(file.name);
var page = this.page;
if(endsWith(file.name.toLowerCase(), ".md") || endsWith(file.name.toLowerCase(), ".markdown")){
markdown.render(file.content, {
with_math: true,
sanitize: true
}, function(err, html) {
page.html(html);
utils.typeset(page);
});
} else {
page.html($("<pre>").text(file.content.replace(/\r\n/g,'\n')));
}
};
return {'DirectoryReadme': DirectoryReadme};
});

View File

@ -36,7 +36,6 @@ requirejs([
'tree/js/terminallist',
'tree/js/newnotebook',
'tree/js/shutdownbutton',
'tree/js/directoryreadme',
'auth/js/loginwidget',
'bidi/bidi',
], function(
@ -55,7 +54,6 @@ requirejs([
terminallist,
newnotebook,
shutdownbutton,
directoryreadme,
loginwidget,
bidi){
"use strict";
@ -102,8 +100,7 @@ requirejs([
var kernel_list = new kernellist.KernelList('#running_list', $.extend({
session_list: session_list},
common_options));
var directory_readme = new directoryreadme.DirectoryReadme('#directory_readme', notebook_list);
var terminal_list;
if (utils.get_body_data("terminalsAvailable") === "True") {
terminal_list = new terminallist.TerminalList('#terminal_list', common_options);

View File

@ -1311,7 +1311,7 @@ define([
var element = $(this);
if (element.data("path") === path) {
element.remove();
events.trigger('notebook_deleted.NotebookList', [path]);
events.trigger('notebook_deleted.NotebookList');
that._selection_changed();
}
});

View File

@ -15,12 +15,6 @@
// The left padding of the selector button's contents.
@dashboard-selectorbtn-lpad: 7px;
// The horizontal padding of the readme.
@dashboard_readme_lr_pad: 21px;
// The vertical padding of the readme.
@dashboard_readme_top_pad: 7px;
@dashboard_readme_bottom_pad: 14px;
ul#tabs {
margin-bottom: @dashboard_tb_pad;
}
@ -463,16 +457,3 @@ outline-width:1px;
margin: 3px;
font-size:10px;
}
#directory_readme {
.readme_header {
padding-top: @dashboard_tb_pad;
padding-bottom: @dashboard_tb_pad;
padding-left: @dashboard_lr_pad;
padding-right: @dashboard_lr_pad;
}
.readme_content {
padding: @dashboard_readme_top_pad @dashboard_readme_lr_pad @dashboard_readme_bottom_pad;
}
}

View File

@ -1,22 +1,5 @@
{% extends "page.html" %}
{% block stylesheet %}
{% if mathjax_url %}
<script type="text/javascript" src="{{mathjax_url}}?config={{mathjax_config}}&delayStartupUntil=configured" charset="utf-8"></script>
{% endif %}
<script type="text/javascript">
// MathJax disabled, set as null to distinguish from *missing* MathJax,
// where it will be undefined, and should prompt a dialog later.
window.mathjax_url = "{{mathjax_url}}";
</script>
<link rel="stylesheet" href="{{ static_url("components/codemirror/lib/codemirror.css") }}">
{{super()}}
{% endblock %}
{% block title %}{{page_title}}{% endblock %}
@ -169,7 +152,6 @@ data-server-root="{{server_root}}"
</div>
</div>
</div>
<div id="directory_readme"></div>
</div>
<div id="running" class="tab-pane">
<div id="running_toolbar" class="row">

View File

@ -51,9 +51,7 @@ class TreeHandler(IPythonHandler):
breadcrumbs=breadcrumbs,
terminals_available=self.settings['terminals_available'],
server_root=self.settings['server_root_dir'],
shutdown_button=self.settings.get('shutdown_button', False),
mathjax_url=self.mathjax_url,
mathjax_config=self.mathjax_config
shutdown_button=self.settings.get('shutdown_button', False)
))
elif cm.file_exists(path):
# it's not a directory, we have redirecting to do