File size: 12,452 Bytes
fa0f006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
Three different highlighting schemes "mark"|"burk"|"girk"|"birk"|"pirk" are defined in the css highlighter.css
The following functions highlight the selected text, according to the scheme chosen by a menu button. More precisely, they replace selected text, both in edit or command mode, by 
a span tag with a given class and the selected text as data.
if no text is selected, then the whole cell is highlighted (using a div tag and a class corresponding to the chosen scheme). A function to remove all hihlightings is also provided. 
*/

function removeFullCellHighlight(cell_text) {
    cell_text = cell_text.replace(/<div class=(?:"mark"|"burk"|"girk"|"birk"|"pirk")>\n([\s\S]*?)<\/div><i class="fa fa-lightbulb-o "><\/i>/g, function (w, g) {
        return g
    })
    return cell_text
}

function fullCellHighlight(cell_text, scheme) {
    cell_text = removeFullCellHighlight(cell_text);
    return '<div class=' + '"' + scheme + '"' + '>\n' + cell_text + '</div><i class="fa fa-lightbulb-o "><\/i>'
}

function highlight(text, scheme) {
    var scheme = scheme;
    // replace by a span, wile preserving leading and trailing spaces
    var rep = text.replace(/(\S[\S\s]*\S)/, function (w, internal_text) {
        return '<span class=' + '"' + scheme + '"' + '>' + internal_text + '</span>'
    })
    return rep
    //return '<span class='+'"'+scheme+'"'+'>'+text+'</span>'
}


function add_div(text) {
    if (text.match(/^<div>([\S\s]*)<\/div>$/) == null) {
        return '<div>' + text + '</div>'
    } else {
        return text
    }
}

function rem_div(text) {
    return text.replace(/^<div>([\S\s]*)<\/div>$/, function (w, g) {
        return g
    })
}

function highlightInCmdMode(event, scheme) {
    var cell = IPython.notebook.get_selected_cell()
    var cm = IPython.notebook.get_selected_cell().code_mirror
    var selectedText = window.getSelection().toString();
    var cell_text = cell.get_text();
    if (selectedText.length == 0) {
        cell_text = fullCellHighlight(cell_text, scheme);
    } else {
        var identifiedText = align(selectedText, cell_text);
        cell_text = cell_text.replace(identifiedText, highlight(identifiedText, scheme));
    }
    cell.set_text(cell_text);
    cell.render();
    return false;
}

function highlightInEditMode(event, scheme) {
    var cell = IPython.notebook.get_selected_cell()
    var cm = cell.code_mirror
    var selectedText = cm.getSelection()
    if (selectedText.length == 0) {
        var cell_text = cell.get_text();
        cell_text = fullCellHighlight(cell_text, scheme);
        cell.set_text(cell_text);
    } else {
        cm.replaceSelection(highlight(selectedText, scheme))
    }
    return false;
}

function removeHighlights() {
    var cell = IPython.notebook.get_selected_cell();
    var cell_text = removeFullCellHighlight(cell.get_text());
    cell_text = cell_text.replace(/<span class=(?:"mark"|"burk"|"girk"|"birk"|"pirk")>([\s\S]*?)<\/span>/g,
        function (w, g) {
            return g
        }
    )
    cell.set_text(cell_text)
    cell.render();
}

//*****************************************************************************************
// Utilitary functions for finding a candidate corresponding text from an unformatted selection

/* In case of text selection in rendered cells, the returned text retains no formatting 
therefore, when looking for this text in the actual formatted text, we need to do a 
kind of "fuzzy" alignment. Though there exists specialized libraries for such task, 
we have developed here a simple heuristics that should work 90% of the time, 
but the problem cannot get a perfect solution. 
A first point is to replace special characters that could be interpreded with 
a special meaning in regular expressions. Then the idea is to find the exact matches 
on the longest substring from the beginning of text, then the longest substring 
from the end of the text. Finally, given the locations of the two substring, 
we extract the corresponding global match in the original text. 
*/
function escapeRegExp(str) {
    return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "#");
    // return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
    return str
}

// Extract the longest matching substring from the beginning of the text
function exsub_up(sub, text) {
    for (k = 0; k <= sub.length; k++) {
        if (text.match(sub.substr(0, k)) == null) {
            k = k - 2
            break
        }
    }
    return text.match(sub.substr(0, k + 1))
}

// Extract the longest matching substring from the end of the text
function exsub_down(sub, text) {
    var L = sub.length
    try {
        for (k = 0; k <= sub.length; k++) {
            tst = sub.substr(L - k - 1, L);
            if (text.match(tst) == null) {
                // console.log(tst)
                k = k - 1
                break
            }
        }
        return text.match(sub.substr(L - k - 1, L))
    } catch (e) {
        console.log('Error', e)
        return ""
    }

}

// Function that tries to find the best match of the unformatted 
// text in the formatted one. 

function align(tofind, text) {

    sub = escapeRegExp(tofind)
    textModified = escapeRegExp(text)
    //console.log(textModified.match(sub))
    if (textModified.match(sub) == null) {
        a = exsub_up(sub, textModified)
        b = exsub_down(sub, textModified)
        return text.substr(a.index, b.index + b[0].length - a.index)
    } else {
        var tmpMatch = textModified.match(sub)
        return text.substr(tmpMatch.index, tmpMatch[0].length)
    }
}



// ***************** Keyboard shortcuts ******************************

var add_cmd_shortcuts = {
    'Alt-g': {
        help: 'highlight selected text',
        help_index: 'ht',
        handler: function (event) {
            highlightInCmdMode("", mark);
            return false;
        }
    },
    'Alt-h': {
        help: 'highlight selected text',
        help_index: 'ht',
        handler: function (event) {
            highlightInCmdMode("", burk);
            return false;
        }
    },
};


var add_edit_shortcuts = {
    'Alt-g': {
        help: 'highlight selected text',
        help_index: 'ht',
        handler: function (event) {
            var highlight = mark;
            highlightInEditMode("", mark);
            return false;
        }
    },
    'Alt-h': {
        help: 'highlight selected text',
        help_index: 'ht',
        handler: function (event) {
            var highlight = burk;
            highlightInEditMode("", burk);
            return false;
        }
    },
};


//******Toolbar buttons *************************************************

function highlightText(scheme) {
    var cell = IPython.notebook.get_selected_cell();
    var rendered = cell.rendered;
    if (rendered) highlightInCmdMode("", scheme);
    else highlightInEditMode("", scheme);
}


function build_toolbar() {
    var test = ' <div id="hgl" class="toolbar btn-group" role="toolbar"> \
<button type="button" class="btn btn-default btn-group" id="higlighter_menu" href="#">\
 <i id="menu-hgl" class="fa fa-caret-right"></i></button>\
<div id="submenu" class="btn-group" style="font-weight:bold;margin-left:0" > \
    <button type="button" class="btn btn-default highlighter-btn burk" style="font-weight:bold;margin-left:0"  href="#" id="b1"></button>\
    <button type="button" class="btn btn-default highlighter-btn mark" style="font-weight:bold;margin-left:0"  href="#" id="b2"></button>\
    <button type="button" class="btn btn-default highlighter-btn girk" style="font-weight:bold;margin-left:0"  href="#" id="b3"></button>\
    <button type="button" class="btn btn-default highlighter-btn birk" style="font-weight:bold;margin-left:0"  href="#" id="b4"></button>\
    <button type="button" class="btn btn-default highlighter-btn pirk" style="font-weight:bold;margin-left:0"  href="#" id="b5"></button>\
<button type="button" class="btn btn-default" style="font-weight:bold;margin-left:0"\
 href="#" id="remove_highlights">\<i class="fa fa-times highlighter-close"></i> </button></div>\
                 </div>'


    $("#maintoolbar-container").append(test);
    $("#test").css({
        'padding': '5px'
    });

    $("#submenu").hide(); // initially hide the submenu

    //buttons initial css -- shall check if this is really necessary
    // $("#higlighter_menu").css({
    //     'padding': '2px 8px',
    //     'display': 'inline-block',
    //     'border': '1px solid',
    //     'border-color': '#cccccc',
    //     'font-weight': 'bold',
    //     'text-align': 'center',
    //     'vertical-align': 'middle',
    //     'margin-left': '0px',
    //     'margin-right': '0px'
    // })


    //Actions


    $("#higlighter_menu")
        .on('click', function () {
            $("#submenu").toggle();
            $("#menu-hgl").toggleClass("fa-caret-right")
            $("#menu-hgl").toggleClass("fa-caret-left")
        })
        .attr('title', 'Highlight Selected Text');


    $("#b1")
        .on('click', function () {
            highlightText("burk")
        })
        .on('mouseover', function () {
            $("#b1").removeClass("btn btn-default").addClass("btn burk")
            //.addClass("burk");
        }) //!!
        .on('mouseout', function () {
            $("#b1").addClass("btn btn-default")
        })


    $("#b2")
        .on('click', function () {
            highlightText("mark")
        })
        .on('mouseover', function () {
            $("#b2").removeClass("btn btn-default").addClass("btn mark")
        }) //!!
        .on('mouseout', function () {
            $("#b2").addClass("btn btn-default")
        })

    $("#b3")
        .on('click', function () {
            highlightText("girk")
        })
        .on('mouseover', function () {
            $(this).removeClass("btn btn-default").addClass("btn girk")
        }) //!!
        .on('mouseout', function () {
            $(this).addClass("btn btn-default")
        })

    $("#b4")
        .on('click', function () {
            highlightText("birk")
        })
        .on('mouseover', function () {
            $(this).removeClass("btn btn-default").addClass("btn birk")
        }) //!!
        .on('mouseout', function () {
            $(this).addClass("btn btn-default")
        })

    $("#b5")
        .on('click', function () {
            highlightText("pirk")
        })
        .on('mouseover', function () {
            $(this).removeClass("btn btn-default").addClass("btn pirk")
        }) //!!
        .on('mouseout', function () {
            $(this).addClass("btn btn-default")
        })


    $("#remove_highlights")
        .on('click', function () {
            removeHighlights()
        })
        .attr('title', 'Remove highlightings in selected cell');
} // end build_toolbar

//******************************* MAIN FUNCTION **************************

define(["require",
    'base/js/namespace'
], function (requirejs, Jupyter) {

    var security = requirejs("base/js/security")

    var load_css = function (name) {
        var link = document.createElement("link");
        link.type = "text/css";
        link.rel = "stylesheet";
        link.href = requirejs.toUrl(name);
        document.getElementsByTagName("head")[0].appendChild(link);

    };

    //Load_ipython_extension
    var load_ipython_extension = requirejs(['base/js/namespace'], function (Jupyter) {
        "use strict";
        if (Jupyter.version[0] < 3) {
            console.log("This extension requires Jupyter or IPython >= 3.x")
            return
        }

        console.log("[highlighter] Loading highlighter.css");
        load_css('./highlighter.css')

        IPython.keyboard_manager.edit_shortcuts.add_shortcuts(add_edit_shortcuts);
        IPython.keyboard_manager.command_shortcuts.add_shortcuts(add_cmd_shortcuts);

        build_toolbar();

        var _on_reload = true; /* make sure cells render on reload */

        //highlighter_init_cells(); /* initialize cells */


        /* on reload */
        $([Jupyter.events]).on('status_started.Kernel', function () {

            //highlighter_init_cells();
            console.log("[highlighter] reload...");
            _on_reload = false;
        })

    }); //end of load_ipython_extension function

    return {
        load_ipython_extension: load_ipython_extension,
    };
}); //End of main function

console.log("Loading ./highlighter.js");