From b67c4ccbe3c4c5a7faabcaa2446f075986fc6497 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pinto?= Date: Mon, 26 Mar 2018 14:48:40 +0100 Subject: [PATCH] Do not use temporary files for adoc generation, closes #54 --- package.json | 9 +++-- src/AsciiDocProvider.ts | 75 ++++++++++++++++++++++------------------- test/samples/footer.txt | 1 + test/samples/script.pl | 4 +++ 4 files changed, 49 insertions(+), 40 deletions(-) create mode 100644 test/samples/footer.txt create mode 100644 test/samples/script.pl diff --git a/package.json b/package.json index 1b6f6b25..237ee294 100644 --- a/package.json +++ b/package.json @@ -59,10 +59,10 @@ "default": 200, "description": "Maximum size of output buffer from preview command in kB. Increase if you receive a stdout maxBuffer exceeded error" }, - "AsciiDoc.html_generator": { + "AsciiDoc.asciidoctor_binary_path": { "type": "string", - "default": "asciidoctor -o-", - "description": "command to be used for the HTML generation" + "default": "asciidoctor", + "description": "Full path for the asciidoctor binary/executable" }, "AsciiDoc.runInterval": { "type": "number", @@ -127,7 +127,6 @@ "dependencies": { "vscode": "^1.0.0", "asciidoctor.js": "^1.5.6-preview.5", - "file-url": "^1.0.1", - "tmp": "^0.0.29" + "file-url": "^1.0.1" } } diff --git a/src/AsciiDocProvider.ts b/src/AsciiDocProvider.ts index 72353bda..609cf67c 100644 --- a/src/AsciiDocProvider.ts +++ b/src/AsciiDocProvider.ts @@ -16,11 +16,17 @@ import { } from 'vscode'; import * as Asciidoctor from "asciidoctor.js"; -import { exec } from "child_process"; +import { spawn } from "child_process"; import * as fs from "fs"; import * as path from "path"; let fileUrl = require("file-url"); -let tmp = require("tmp"); + +const asciidoctor_config = { + runtime: { + platform: 'node', + engine: 'v8' + } +} export default class AsciiDocProvider implements TextDocumentContentProvider { static scheme = 'adoc-preview'; @@ -32,7 +38,9 @@ export default class AsciiDocProvider implements TextDocumentContentProvider { private needsRebuild : boolean = true; private editorDocument: TextDocument = null; private refreshInterval = 1000; - private asciidoctor = Asciidoctor(); + + + private asciidoctor = Asciidoctor(asciidoctor_config); private resolveDocument(uri: Uri): TextDocument { const matches = workspace.textDocuments.filter(d => { @@ -125,46 +133,43 @@ export default class AsciiDocProvider implements TextDocumentContentProvider { let use_asciidoctor_js = workspace.getConfiguration('AsciiDoc').get('use_asciidoctor_js'); let text = doc.getText(); let documentPath = path.dirname(doc.fileName); - let tmpobj = doc.isUntitled ? tmp.fileSync({ postfix: '.adoc' }) : tmp.fileSync({ postfix: '.adoc', dir: documentPath }); - fs.write(tmpobj.fd, text, 0); + if(use_asciidoctor_js) { - const options = {safe: 'unsafe', doctype: 'article', header_footer: true, attributes: ['copycss'], to_file: false}; + const options = { + safe: 'unsafe', + doctype: 'article', + header_footer: true, + attributes: ['copycss'], + to_file: false, + base_dir: path.dirname(doc.fileName), + sourcemap: true + }; return new Promise((resolve, reject) => { - let resultHTML = this.asciidoctor.convertFile(tmpobj.name, options); - - tmpobj.removeCallback(); - let result = this.fixLinks(resultHTML, doc.fileName); - resolve(this.buildPage(result)); + let resultHTML = this.asciidoctor.convert(text, options); + //let result = this.fixLinks(resultHTML, doc.fileName); + resolve(this.buildPage(resultHTML)); }) } else return new Promise((resolve, reject) => { - let html_generator = workspace.getConfiguration('AsciiDoc').get('html_generator') - let cmd = `${html_generator} "${tmpobj.name}"` - let maxBuff = parseInt(workspace.getConfiguration('AsciiDoc').get('buffer_size_kB')) - exec(cmd, {maxBuffer: 1024 * maxBuff}, (error, stdout, stderr) => { - tmpobj.removeCallback(); - if (error) { - let errorMessage = [ - error.name, - error.message, - error.stack, - "", - stderr.toString() - ].join("\n"); - console.error(errorMessage); - errorMessage = errorMessage.replace("\n", '

'); - errorMessage += "

" - errorMessage += "If the asciidoctor binary is not in your PATH, you can set the full path.
" - errorMessage += "Go to `File -> Preferences -> User settings` and adjust the AsciiDoc.html_generator config option.
" - errorMessage += "

Alternatively if you get a stdout maxBuffer exceeded error, Go to `File -> Preferences -> User settings and adjust the AsciiDoc.buffer_size_kB to a larger number (default is 200 kB)." - resolve(this.errorSnippet(errorMessage)); - } else { - let result = this.fixLinks(stdout.toString(), doc.fileName); - resolve(this.buildPage(result)); - } + let asciidoctor_binary_path = workspace.getConfiguration('AsciiDoc').get('asciidoctor_binary_path'); + const asciidoctor = spawn('asciidoctor', ['-o-', '-', '-B', path.dirname(doc.fileName)]); + asciidoctor.stdin.write(text); + asciidoctor.stdin.end(); + asciidoctor.stderr.on('data', (data) => { + let errorMessage = data.toString(); + console.error(errorMessage); + errorMessage += errorMessage.replace("\n", '

'); + errorMessage += "

" + errorMessage += "If the asciidoctor binary is not in your PATH, you can set the full path.
" + errorMessage += "Go to `File -> Preferences -> User settings` and adjust the AsciiDoc.asciidoctor_binary_path/b>" + resolve(this.errorSnippet(errorMessage)); + }) + asciidoctor.stdout.on('data', (data) => { + let result = this.fixLinks(data.toString(), doc.fileName); + resolve(this.buildPage(result)); }); }); } diff --git a/test/samples/footer.txt b/test/samples/footer.txt new file mode 100644 index 00000000..dbd37017 --- /dev/null +++ b/test/samples/footer.txt @@ -0,0 +1 @@ +Footer text \ No newline at end of file diff --git a/test/samples/script.pl b/test/samples/script.pl new file mode 100644 index 00000000..5013bd8c --- /dev/null +++ b/test/samples/script.pl @@ -0,0 +1,4 @@ +$num_words = "eight"; +print "There are "; +print $num_words; +print " words altogether in this sentence.\n"; \ No newline at end of file