From 238828e36d72f5a87877a3dc41fcd0f19f618f5d Mon Sep 17 00:00:00 2001 From: Toon Baeyens Date: Thu, 28 May 2020 09:25:55 +0200 Subject: [PATCH] refactor code duplication of markdown renderers --- notebook/static/notebook/js/outputarea.js | 2 +- notebook/static/tree/js/directoryreadme.js | 185 --------------------- notebook/static/tree/js/main.js | 5 +- notebook/static/tree/js/notebooklist.js | 2 +- notebook/static/tree/less/tree.less | 19 --- notebook/templates/tree.html | 18 -- notebook/tree/handlers.py | 4 +- 7 files changed, 4 insertions(+), 231 deletions(-) delete mode 100644 notebook/static/tree/js/directoryreadme.js diff --git a/notebook/static/notebook/js/outputarea.js b/notebook/static/notebook/js/outputarea.js index 158c5268f..0355f2b11 100644 --- a/notebook/static/notebook/js/outputarea.js +++ b/notebook/static/notebook/js/outputarea.js @@ -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; diff --git a/notebook/static/tree/js/directoryreadme.js b/notebook/static/tree/js/directoryreadme.js deleted file mode 100644 index 2c17f5561..000000000 --- a/notebook/static/tree/js/directoryreadme.js +++ /dev/null @@ -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 = $(""); - $("
") - .addClass("list_header row readme_header") - .html([ - $('') - .addClass('item_icon file_icon'), - this.title - ]).appendTo(element); - - - this.page = $("
") - .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($("
").text(file.content.replace(/\r\n/g,'\n')));
-        }
-    };
-    
-    return {'DirectoryReadme': DirectoryReadme};
-});
diff --git a/notebook/static/tree/js/main.js b/notebook/static/tree/js/main.js
index 93b50c91e..d1952d60e 100644
--- a/notebook/static/tree/js/main.js
+++ b/notebook/static/tree/js/main.js
@@ -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);
diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js
index 386c18280..eefc9992e 100644
--- a/notebook/static/tree/js/notebooklist.js
+++ b/notebook/static/tree/js/notebooklist.js
@@ -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();
             }
         });
diff --git a/notebook/static/tree/less/tree.less b/notebook/static/tree/less/tree.less
index b1ec5e782..030aeaacb 100644
--- a/notebook/static/tree/less/tree.less
+++ b/notebook/static/tree/less/tree.less
@@ -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;
-    }
-}
diff --git a/notebook/templates/tree.html b/notebook/templates/tree.html
index 07995627b..d71527fcb 100644
--- a/notebook/templates/tree.html
+++ b/notebook/templates/tree.html
@@ -1,22 +1,5 @@
 {% extends "page.html" %}
 
-{% block stylesheet %}
-
-{% if mathjax_url %}
-
-{% endif %}
-
-
-
-
-{{super()}}
-
-{% endblock %}
-
 {% block title %}{{page_title}}{% endblock %}
 
 
@@ -169,7 +152,6 @@ data-server-root="{{server_root}}"
               
-
diff --git a/notebook/tree/handlers.py b/notebook/tree/handlers.py index ea5a4ee3e..bb4958d28 100644 --- a/notebook/tree/handlers.py +++ b/notebook/tree/handlers.py @@ -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