From d4eec510b21544c1591c5bda7bcc006d866d2823 Mon Sep 17 00:00:00 2001 From: Juergen Hasch Date: Sat, 5 Sep 2015 15:20:31 +0200 Subject: [PATCH] Update ruler extension --- nbextensions/usability/ruler/main.js | 44 ++++++++++++++++++------- nbextensions/usability/ruler/readme.md | 12 ++++++- nbextensions/usability/ruler/ruler.yaml | 7 ++++ 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/nbextensions/usability/ruler/main.js b/nbextensions/usability/ruler/main.js index 1e57e5e..42eed67 100644 --- a/nbextensions/usability/ruler/main.js +++ b/nbextensions/usability/ruler/main.js @@ -7,38 +7,58 @@ define([ 'require', 'base/js/events', 'services/config', - 'base/js/utils', + 'base/js/utils', 'codemirror/lib/codemirror', 'codemirror/addon/display/rulers' ], function(IPython, $, require, events, configmod, utils, codemirror) { "use strict"; - var RULERCOLUMN = 78; - var base_url = utils.get_body_data("baseUrl") + var ruler_column = 78; + var ruler_color = "#ff0000"; + var ruler_linestyle = "dashed"; + + var base_url = utils.get_body_data("baseUrl"); var config = new configmod.ConfigSection('notebook', {base_url: base_url}); var rulers = []; - function isNumber(n) { + var isNumber = function(n) { return !isNaN(parseFloat(n)) && isFinite(n); - } + }; config.loaded.then(function() { - if (config.data.hasOwnProperty('ruler_column') ){ - if (isNumber(config.data.ruler_column)) RULERCOLUMN = config.data.ruler_column; + if (config.data.hasOwnProperty('ruler_color')) { + ruler_color = config.data.ruler_color; } - console.log("RULER:", RULERCOLUMN) - rulers.push({color: "#f00", column: RULERCOLUMN, lineStyle: "dashed"}); + console.log("ruler_color:", ruler_color); + + if (config.data.hasOwnProperty('ruler_column')) { + if (isNumber(config.data.ruler_column)) { + ruler_column = config.data.ruler_column; + } + } + console.log("ruler_column:", ruler_column); + + if (config.data.hasOwnProperty('ruler_linestyle')) { + ruler_linestyle = config.data.ruler_linestyle; + } + console.log("ruler_linestyle:", ruler_linestyle); + + rulers.push({ + color: ruler_color, + column: ruler_column, + lineStyle: ruler_linestyle + }); var cells = IPython.notebook.get_cells(); - for(var i in cells){ + for(var i in cells) { var cell = cells[i]; if ((cell instanceof IPython.CodeCell)) { cell.code_mirror.setOption('rulers', rulers); } } - events.on('create.Cell',createCell); + events.on('create.Cell', createCell); }); - var createCell = function (event,nbcell) { + var createCell = function (event, nbcell) { var cell = nbcell.cell; if ((cell instanceof IPython.CodeCell)) { cell.code_mirror.setOption('rulers', rulers); diff --git a/nbextensions/usability/ruler/readme.md b/nbextensions/usability/ruler/readme.md index 52e1b50..ef7843e 100644 --- a/nbextensions/usability/ruler/readme.md +++ b/nbextensions/usability/ruler/readme.md @@ -1 +1,11 @@ -Display a rulter +This extension enables the Ruler CodeMirror feature + +## Configuration + +You can set the number of characters in the notebook extensions configration page or use the ConfigManager: +```Python +from IPython.html.services.config import ConfigManager +ip = get_ipython() +cm = ConfigManager(parent=ip) +cm.update('notebook', {"ruler_column": 80}) +``` diff --git a/nbextensions/usability/ruler/ruler.yaml b/nbextensions/usability/ruler/ruler.yaml index ad31919..cc00f1e 100644 --- a/nbextensions/usability/ruler/ruler.yaml +++ b/nbextensions/usability/ruler/ruler.yaml @@ -10,3 +10,10 @@ Parameters: input_type: number description: Column where ruler is displayed default: 78 + ruler_color: + input_type: color + description: Color of the ruler + default: "#ff0000" + ruler_linestyle: + description: 'Ruler style, e.g. solid, dashed' + default: dashed