Skip to content

Commit b67c4cc

Browse files
committed
Do not use temporary files for adoc generation, closes #54
1 parent 1a2319b commit b67c4cc

File tree

4 files changed

+49
-40
lines changed

4 files changed

+49
-40
lines changed

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@
5959
"default": 200,
6060
"description": "Maximum size of output buffer from preview command in kB. Increase if you receive a stdout maxBuffer exceeded error"
6161
},
62-
"AsciiDoc.html_generator": {
62+
"AsciiDoc.asciidoctor_binary_path": {
6363
"type": "string",
64-
"default": "asciidoctor -o-",
65-
"description": "command to be used for the HTML generation"
64+
"default": "asciidoctor",
65+
"description": "Full path for the asciidoctor binary/executable"
6666
},
6767
"AsciiDoc.runInterval": {
6868
"type": "number",
@@ -127,7 +127,6 @@
127127
"dependencies": {
128128
"vscode": "^1.0.0",
129129
"asciidoctor.js": "^1.5.6-preview.5",
130-
"file-url": "^1.0.1",
131-
"tmp": "^0.0.29"
130+
"file-url": "^1.0.1"
132131
}
133132
}

src/AsciiDocProvider.ts

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@ import {
1616
} from 'vscode';
1717
import * as Asciidoctor from "asciidoctor.js";
1818

19-
import { exec } from "child_process";
19+
import { spawn } from "child_process";
2020
import * as fs from "fs";
2121
import * as path from "path";
2222
let fileUrl = require("file-url");
23-
let tmp = require("tmp");
23+
24+
const asciidoctor_config = {
25+
runtime: {
26+
platform: 'node',
27+
engine: 'v8'
28+
}
29+
}
2430

2531
export default class AsciiDocProvider implements TextDocumentContentProvider {
2632
static scheme = 'adoc-preview';
@@ -32,7 +38,9 @@ export default class AsciiDocProvider implements TextDocumentContentProvider {
3238
private needsRebuild : boolean = true;
3339
private editorDocument: TextDocument = null;
3440
private refreshInterval = 1000;
35-
private asciidoctor = Asciidoctor();
41+
42+
43+
private asciidoctor = Asciidoctor(asciidoctor_config);
3644

3745
private resolveDocument(uri: Uri): TextDocument {
3846
const matches = workspace.textDocuments.filter(d => {
@@ -125,46 +133,43 @@ export default class AsciiDocProvider implements TextDocumentContentProvider {
125133
let use_asciidoctor_js = workspace.getConfiguration('AsciiDoc').get('use_asciidoctor_js');
126134
let text = doc.getText();
127135
let documentPath = path.dirname(doc.fileName);
128-
let tmpobj = doc.isUntitled ? tmp.fileSync({ postfix: '.adoc' }) : tmp.fileSync({ postfix: '.adoc', dir: documentPath });
129-
fs.write(tmpobj.fd, text, 0);
136+
130137

131138
if(use_asciidoctor_js)
132139
{
133-
const options = {safe: 'unsafe', doctype: 'article', header_footer: true, attributes: ['copycss'], to_file: false};
140+
const options = {
141+
safe: 'unsafe',
142+
doctype: 'article',
143+
header_footer: true,
144+
attributes: ['copycss'],
145+
to_file: false,
146+
base_dir: path.dirname(doc.fileName),
147+
sourcemap: true
148+
};
134149

135150
return new Promise<string>((resolve, reject) => {
136-
let resultHTML = this.asciidoctor.convertFile(tmpobj.name, options);
137-
138-
tmpobj.removeCallback();
139-
let result = this.fixLinks(resultHTML, doc.fileName);
140-
resolve(this.buildPage(result));
151+
let resultHTML = this.asciidoctor.convert(text, options);
152+
//let result = this.fixLinks(resultHTML, doc.fileName);
153+
resolve(this.buildPage(resultHTML));
141154
})
142155
} else
143156
return new Promise<string>((resolve, reject) => {
144-
let html_generator = workspace.getConfiguration('AsciiDoc').get('html_generator')
145-
let cmd = `${html_generator} "${tmpobj.name}"`
146-
let maxBuff = parseInt(workspace.getConfiguration('AsciiDoc').get('buffer_size_kB'))
147-
exec(cmd, {maxBuffer: 1024 * maxBuff}, (error, stdout, stderr) => {
148-
tmpobj.removeCallback();
149-
if (error) {
150-
let errorMessage = [
151-
error.name,
152-
error.message,
153-
error.stack,
154-
"",
155-
stderr.toString()
156-
].join("\n");
157-
console.error(errorMessage);
158-
errorMessage = errorMessage.replace("\n", '<br><br>');
159-
errorMessage += "<br><br>"
160-
errorMessage += "<b>If the asciidoctor binary is not in your PATH, you can set the full path.<br>"
161-
errorMessage += "Go to `File -> Preferences -> User settings` and adjust the AsciiDoc.html_generator config option.</b>"
162-
errorMessage += "<br><br><b>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).</b>"
163-
resolve(this.errorSnippet(errorMessage));
164-
} else {
165-
let result = this.fixLinks(stdout.toString(), doc.fileName);
166-
resolve(this.buildPage(result));
167-
}
157+
let asciidoctor_binary_path = workspace.getConfiguration('AsciiDoc').get('asciidoctor_binary_path');
158+
const asciidoctor = spawn('asciidoctor', ['-o-', '-', '-B', path.dirname(doc.fileName)]);
159+
asciidoctor.stdin.write(text);
160+
asciidoctor.stdin.end();
161+
asciidoctor.stderr.on('data', (data) => {
162+
let errorMessage = data.toString();
163+
console.error(errorMessage);
164+
errorMessage += errorMessage.replace("\n", '<br><br>');
165+
errorMessage += "<br><br>"
166+
errorMessage += "<b>If the asciidoctor binary is not in your PATH, you can set the full path.<br>"
167+
errorMessage += "Go to `File -> Preferences -> User settings` and adjust the AsciiDoc.asciidoctor_binary_path/b>"
168+
resolve(this.errorSnippet(errorMessage));
169+
})
170+
asciidoctor.stdout.on('data', (data) => {
171+
let result = this.fixLinks(data.toString(), doc.fileName);
172+
resolve(this.buildPage(result));
168173
});
169174
});
170175
}

test/samples/footer.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Footer text

test/samples/script.pl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
$num_words = "eight";
2+
print "There are ";
3+
print $num_words;
4+
print " words altogether in this sentence.\n";

0 commit comments

Comments
 (0)