@@ -16,11 +16,17 @@ import {
16
16
} from 'vscode' ;
17
17
import * as Asciidoctor from "asciidoctor.js" ;
18
18
19
- import { exec } from "child_process" ;
19
+ import { spawn } from "child_process" ;
20
20
import * as fs from "fs" ;
21
21
import * as path from "path" ;
22
22
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
+ }
24
30
25
31
export default class AsciiDocProvider implements TextDocumentContentProvider {
26
32
static scheme = 'adoc-preview' ;
@@ -32,7 +38,9 @@ export default class AsciiDocProvider implements TextDocumentContentProvider {
32
38
private needsRebuild : boolean = true ;
33
39
private editorDocument : TextDocument = null ;
34
40
private refreshInterval = 1000 ;
35
- private asciidoctor = Asciidoctor ( ) ;
41
+
42
+
43
+ private asciidoctor = Asciidoctor ( asciidoctor_config ) ;
36
44
37
45
private resolveDocument ( uri : Uri ) : TextDocument {
38
46
const matches = workspace . textDocuments . filter ( d => {
@@ -125,46 +133,43 @@ export default class AsciiDocProvider implements TextDocumentContentProvider {
125
133
let use_asciidoctor_js = workspace . getConfiguration ( 'AsciiDoc' ) . get ( 'use_asciidoctor_js' ) ;
126
134
let text = doc . getText ( ) ;
127
135
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
+
130
137
131
138
if ( use_asciidoctor_js )
132
139
{
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
+ } ;
134
149
135
150
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 ) ) ;
141
154
} )
142
155
} else
143
156
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 ) ) ;
168
173
} ) ;
169
174
} ) ;
170
175
}
0 commit comments