-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcode_prettify.js
More file actions
55 lines (48 loc) · 1.98 KB
/
code_prettify.js
File metadata and controls
55 lines (48 loc) · 1.98 KB
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
// Copyright (c) Jupyter-Contrib Team.
// Distributed under the terms of the Modified BSD License.
// Authors: @jfbercher and @jcb91
define(function(require, exports, module) {
'use strict';
var kernel_exec_on_cell = require('./kernel_exec_on_cell');
var mod_name = 'code_prettify';
// gives default settings
var cfg = {
add_toolbar_button: true,
hotkeys: {
process_selected: 'Ctrl-L',
process_all: 'Ctrl-Shift-L',
},
register_hotkey: true,
show_alerts_for_errors: true,
button_label: 'Code prettify',
button_icon: 'fa-legal',
kbd_shortcut_text: 'Code prettify',
};
cfg.kernel_config_map = { // map of parameters for supported kernels
"python": {
"library": ["import json",
"def yapf_reformat(cell_text):",
" import yapf.yapflib.yapf_api",
" import re",
" cell_text = re.sub('^%', '#%#', cell_text, flags=re.M)",
" reformated_text = yapf.yapflib.yapf_api.FormatCode(cell_text)[0]",
" return re.sub('^#%#', '%', reformated_text, flags=re.M)"].join("\n"),
"prefix": "print(json.dumps(yapf_reformat(u",
"postfix": ")))"
},
"r": {
"library": "library(formatR)\nlibrary(jsonlite)",
"prefix": "cat(toJSON(paste(tidy_source(text=",
"postfix": ", output=FALSE)[['text.tidy']], collapse='\n')))"
},
"javascript": {
"library": "jsbeautify = require(" + "'js-beautify')",
// we do this + trick to prevent require.js attempting to load js-beautify when processing the AMI-style load for this module
"prefix": "console.log(JSON.stringify(jsbeautify.js_beautify(",
"postfix": ")));"
}
};
var prettifier = new kernel_exec_on_cell.define_plugin(mod_name, cfg);
prettifier.load_ipython_extension = prettifier.initialize_plugin;
return prettifier;
});