Skip to content

Commit 0e2f74d

Browse files
committed
[code_prettify] added support for IPython magics, cf #1018
1 parent e80e0c4 commit 0e2f74d

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/jupyter_contrib_nbextensions/nbextensions/code_prettify/code_prettify.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,20 @@ define(function(require, exports, module) {
2525

2626
cfg.kernel_config_map = { // map of parameters for supported kernels
2727
"python": {
28-
"library": "import json\nimport yapf.yapflib.yapf_api",
29-
"prefix": "print(json.dumps(yapf.yapflib.yapf_api.FormatCode(u",
30-
"postfix": ")[0]))"
28+
"library": ["import json",
29+
"def yapf_reformat(cell_text):",
30+
" import yapf.yapflib.yapf_api",
31+
" import re",
32+
" cell_text = re.sub('^%', '#%#', cell_text, flags=re.M)",
33+
" reformated_text = yapf.yapflib.yapf_api.FormatCode(cell_text)[0]",
34+
" return re.sub('^#%#', '%', reformated_text, flags=re.M)"].join("\n"),
35+
"prefix": "print(json.dumps(yapf_reformat(u",
36+
"postfix": ")))"
3137
},
3238
"r": {
3339
"library": "library(formatR)\nlibrary(jsonlite)",
34-
"prefix": "cat(paste(tidy_source(text=",
35-
"postfix": ")[['text.tidy']], collapse='\n'))"
40+
"prefix": "cat(toJSON(paste(tidy_source(text=",
41+
"postfix": ", output=FALSE)[['text.tidy']], collapse='\n')))"
3642
},
3743
"javascript": {
3844
"library": "jsbeautify = require(" + "'js-beautify')",

src/jupyter_contrib_nbextensions/nbextensions/code_prettify/code_prettify.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ Parameters:
5555
default: |
5656
{
5757
"python": {
58-
"library": "import json\nimport yapf.yapflib.yapf_api",
58+
"library": "import json\ndef yapf_reformat(cell_text):\n import yapf.yapflib.yapf_api\n import re\n cell_text = re.sub('^%', '#%#', cell_text, flags=re.M)\n reformated_text = yapf.yapflib.yapf_api.FormatCode(cell_text)[0]\n return re.sub('^#%#', '%', reformated_text, flags=re.M)",
5959
"prefix": "print(json.dumps(yapf.yapflib.yapf_api.FormatCode(u",
6060
"postfix": ")[0]))"
6161
},
6262
"r": {
6363
"library": "library(formatR)\nlibrary(jsonlite)",
64-
"prefix": "cat(paste(tidy_source(text=",
65-
"postfix": ")[['text.tidy']], collapse='\n'))"
64+
"prefix": "cat(toJSON(paste(tidy_source(text=",
65+
"postfix": ", output=FALSE)[['text.tidy']], collapse='\n')))"
6666
},
6767
"javascript": {
6868
"library": "jsbeautify = require('js-beautify')",

0 commit comments

Comments
 (0)