From de1ad62023152ea6250d581fd1ae91ffc482afda Mon Sep 17 00:00:00 2001 From: Josh Barnes Date: Sat, 30 Sep 2017 02:42:11 +0100 Subject: [PATCH] [ruler] css patch for notebook > 4.2.3 see https://github.com/jupyter/notebook/issues/2869 for details --- .../nbextensions/ruler/main.js | 21 ++++++++++++++++++- .../nbextensions/ruler/readme.md | 16 +++++++++++++- .../nbextensions/ruler/ruler.yaml | 8 +++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/jupyter_contrib_nbextensions/nbextensions/ruler/main.js b/src/jupyter_contrib_nbextensions/nbextensions/ruler/main.js index d3b2a9c35..7c3ceee09 100644 --- a/src/jupyter_contrib_nbextensions/nbextensions/ruler/main.js +++ b/src/jupyter_contrib_nbextensions/nbextensions/ruler/main.js @@ -13,6 +13,7 @@ define([ var ruler_column = [78]; var ruler_color = ["#ff0000"]; var ruler_linestyle = ["dashed"]; + var ruler_do_css_patch; var rulers = []; @@ -51,6 +52,8 @@ define([ }); } console.debug(log_prefix, 'ruler specs:', rulers); + + ruler_do_css_patch = config.data.ruler_do_css_patch; // undefined is ok } var load_ipython_extension = function() { @@ -59,13 +62,29 @@ define([ console.warn(log_prefix, 'error loading config:', reason); }) .then(function () { + // Add css patch fix for notebook > 4.2.3 - see + // https://github.com/jupyter/notebook/issues/2869 + // for details + if (ruler_do_css_patch !== false) { + var sheet = document.createElement('style'); + sheet.innerHTML = [ + '.CodeMirror-lines {', + ' padding: 0.4em 0; /* Vertical padding around content */', + '}', + '.CodeMirror pre {', + ' padding: 0 0.4em; /* Horizonal padding around content */', + '}', + ].join('\n'); + document.head.appendChild(sheet); + } + // Change default for new cells codecell.CodeCell.options_default.cm_config.rulers = rulers; // Apply to any already-existing cells var cells = Jupyter.notebook.get_cells().forEach(function (cell) { if (cell instanceof codecell.CodeCell) { cell.code_mirror.setOption('rulers', rulers); - } + } }); }) .catch(function on_error (reason) { diff --git a/src/jupyter_contrib_nbextensions/nbextensions/ruler/readme.md b/src/jupyter_contrib_nbextensions/nbextensions/ruler/readme.md index 1caf69609..d815e1853 100644 --- a/src/jupyter_contrib_nbextensions/nbextensions/ruler/readme.md +++ b/src/jupyter_contrib_nbextensions/nbextensions/ruler/readme.md @@ -1,5 +1,6 @@ Ruler ===== + This extension enables the Ruler CodeMirror feature @@ -15,8 +16,21 @@ cm = ConfigManager(parent=ip) cm.update('notebook', {"ruler_column": [80]}) ``` + +#### CSS patch #### + +Notebook versions from 4.3.0 through 5.1.0dev show up a bug in their CodeMirror +CSS padding which causes the ruler to be misplaced (see +[jupyter/notebook#2869](https://github.com/jupyter/notebook/issues/2869) +for details). +This nbextension introduces a css patch to attempt to correct this, but if it +causes problems for you, you can disable it by setting the `ruler_do_css_patch` +config key to `false`. + + #### Multiple Rulers #### -To specify multiple rulers, set the `ruler_column` to an list of values, for example + +To specify multiple rulers, set the `ruler_column` to a list of values, for example ```Python cm.update('notebook', {"ruler_column": [10, 20, 30, 40, 50, 60, 70, 80]}) diff --git a/src/jupyter_contrib_nbextensions/nbextensions/ruler/ruler.yaml b/src/jupyter_contrib_nbextensions/nbextensions/ruler/ruler.yaml index 818eed51b..dee55c832 100644 --- a/src/jupyter_contrib_nbextensions/nbextensions/ruler/ruler.yaml +++ b/src/jupyter_contrib_nbextensions/nbextensions/ruler/ruler.yaml @@ -6,19 +6,27 @@ Icon: icon.png Main: main.js Compatibility: 4.x, 5.x Parameters: + - name: ruler_column input_type: list list_element: input_type: number description: Column where ruler is displayed default: [78] + - name: ruler_color input_type: list list_element: input_type: color description: Ruler color default: ["#ff0000"] + - name: ruler_linestyle description: 'Ruler style, e.g. solid, dashed' input_type: list default: ['dashed'] + +- name: ruler_do_css_patch + description: apply css patch for ruler padding bug in notebook >= 4.3 + input_type: checkbox + default: true