Skip to content

Commit

Permalink
feat: Add export to ECL option.
Browse files Browse the repository at this point in the history
Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
  • Loading branch information
GordonSmith committed Jul 20, 2021
1 parent e09d0d4 commit 96f66ef
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"configurations": [
{
"name": "test.html",
"type": "chrome",
"type": "pwa-msedge",
"request": "launch",
"url": "file:///${workspaceRoot}/test.html",
"runtimeArgs": [
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ _All commands are prefixed with "OJS" and are available via the command palette
|OJS: Preview Web Page |Ctrl+K V | Preview notebook in an embedded Web Page |
|OJS: Import Notebook | | Import published or shared notebook cells into current document |
|OJS: Export to HTML | | Export as a self contained HTML file |
|OJS: Export to ECL | | Export as a self contained ECL attribute |

## Settings
_All settings are prefixed with "ojs." and are available via `file -> preferneces -> settings` menu_
Expand Down
24 changes: 20 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"build": "run-s compile-es6 bundle",
"watch": "npm-run-all compile-es6 --parallel compile-es6-watch bundle-watch",
"lint": "tslint --project . src/**/*.ts",
"lint-fix": "tslint --fix --project . src/**/*.ts",
"rm-hpcc": "rimraf ./node_modules/@hpcc-js",
"test": "sh ./scripts/e2e.sh",
"standard-version": "standard-version",
Expand Down Expand Up @@ -193,6 +194,16 @@
"dark": "./images/export-dark.svg",
"light": "./images/export.svg"
}
},
{
"category": "OJS",
"command": "ojs.exportECL",
"title": "Export to ECL",
"description": "Export as a self contained ECL file.",
"icon": {
"dark": "./images/export-dark.svg",
"light": "./images/export.svg"
}
}
],
"menus": {
Expand All @@ -207,17 +218,22 @@
{
"when": "resourceLangId == ojs || resourceLangId == omd",
"command": "ojs.preview",
"group": "navigation@997"
"group": "navigation@960"
},
{
"when": "resourceLangId == ojs || resourceLangId == omd",
"command": "ojs.import",
"group": "navigation@998"
"group": "navigation@970"
},
{
"when": "resourceLangId == ojs || resourceLangId == omd",
"command": "ojs.export",
"group": "navigation@999"
"group": "navigation@980"
},
{
"when": "resourceLangId == ojs || resourceLangId == omd",
"command": "ojs.exportECL",
"group": "navigation@990"
}
],
"editor/title": [
Expand Down Expand Up @@ -265,4 +281,4 @@
}
}
}
}
}
2 changes: 1 addition & 1 deletion samples/HelloWorld.omd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Hello and ${w}

```
w = "Welcome!";
w = 'Welcome!';
```
23 changes: 23 additions & 0 deletions src/ojs/command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from "fs";
import fetch from "node-fetch";
import * as path from "path";
import * as vscode from "vscode";
import { Diagnostic } from "./diagnostic";
import { Meta } from "./meta";
Expand All @@ -26,6 +27,7 @@ export class Commands {
ctx.subscriptions.push(vscode.commands.registerCommand("ojs.checkSyntax", this.activeCheckSyntax, this));
ctx.subscriptions.push(vscode.commands.registerCommand("ojs.import", this.import, this));
ctx.subscriptions.push(vscode.commands.registerCommand("ojs.export", this.export, this));
ctx.subscriptions.push(vscode.commands.registerCommand("ojs.exportECL", this.exportECL, this));
}

static attach(ctx: vscode.ExtensionContext): Commands {
Expand Down Expand Up @@ -242,6 +244,27 @@ ${encode(node.value)}
});
}
}

private exportECLTpl(attrID: string, text: string): string {
const escapedTextParts = text.split("'").join("\\'").split("\r\n").join("\n").split("\n");
return `\
EXPORT ${attrID} := ${escapedTextParts.map(line => `'${line}`).join("\\n' + \n")}';
`;
}

async exportECL() {
if (vscode.window.activeTextEditor) {
const textDocument = vscode.window.activeTextEditor.document;
const eclPath = textDocument.languageId === "omd" ? textDocument.uri.path.replace(".omd", ".ecl") : textDocument.uri.path.replace(".ojs", ".ecl");
vscode.window.showSaveDialog({ defaultUri: vscode.Uri.file(eclPath), saveLabel: "Export to ECL" }).then(resource => {
if (resource) {
const text = textDocument.getText();
const ecl = this.exportECLTpl(path.basename(textDocument.uri.path, path.extname(textDocument.uri.path)), text);
fs.writeFile(resource.fsPath, ecl, "utf8", () => { });
}
});
}
}
}

function InsertText(activeEditor, getText: (i: number) => string, i: number = 0, wasEmpty: boolean = false) {
Expand Down

0 comments on commit 96f66ef

Please sign in to comment.