-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
94 lines (72 loc) · 2.5 KB
/
extension.js
File metadata and controls
94 lines (72 loc) · 2.5 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
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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function add_nb(output) {
output["nbformat"] = 4;
output["nbformat_minor"] = 2;
}
function add_metadata(output) {
let metadata = { }
output["metadata"] = metadata
}
function add_cells(output, cells) {
let clls = [];
let execution_count = 0;
cells.forEach(function (cell) {
let cellTrimed = cell.trim();
let splitedCell = cell.split('\n')
if (cellTrimed === "") {
} else if (splitedCell[0].trim() == "[markdown]") {
// removing the [markdown]
delete splitedCell[0]
clls.push({
"cell_type": "markdown",
"metadata": {},
"source": splitedCell.join('\n').split('\n# ').join('\n').trim()
});
} else if (splitedCell[0].trim() == "") {
clls.push({
"cell_type": "code",
"metadata": {},
"source": cell.trimLeft().trimRight(),
"outputs":[],
"execution_count": ++execution_count
});
}
})
output['cells'] = clls;
}
/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
let disposable = vscode.commands.registerCommand('extension.helloWorld', function () {
let output = {}
add_nb(output)
add_metadata(output)
// vscode.workspace.getConfiguration().python // getting the python configurration for the metadata
let editor = vscode.window.activeTextEditor;
let document = editor.document
if (editor) {
let text = document.getText();
let cells = text.split('\n# %%');
add_cells(output, cells)
}
let filePath = document.fileName.replace('.py', '.ipynb');
let fs = require('fs');
let content = JSON.stringify(output)
fs.writeFileSync(filePath, content, 'utf8')
// Display a message box to the user
vscode.window.showInformationMessage("Save to " + filePath);
});
context.subscriptions.push(disposable);
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() { }
module.exports = {
activate,
deactivate
}