From 80674a20c3b9a84d3dae1d358426a8b31ee5ae2e Mon Sep 17 00:00:00 2001 From: CismonX Date: Thu, 11 Mar 2021 12:34:06 +0800 Subject: [PATCH] Make converter reentrant. --- src/contexts/preview.ts | 4 ++-- src/utils/converter.ts | 43 +++++++++++++++++------------------------ 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/contexts/preview.ts b/src/contexts/preview.ts index 7c5404a..9294731 100644 --- a/src/contexts/preview.ts +++ b/src/contexts/preview.ts @@ -81,8 +81,8 @@ export default class PreviewContext { this.pendingUpdate = false; // Inform the user that the preview is updating if `makeinfo` takes too long. setTimeout(() => this.updating && this.updateTitle(), 500); - const { data, error } = await new Converter(this.document.fileName, this.imageTransformer, this.script) - .convert(); + const { data, error } = await new Converter(this.document.fileName) + .convertToHtml(this.imageTransformer, this.script); if (error) { Logger.log(error); Diagnosis.update(this.document, error); diff --git a/src/utils/converter.ts b/src/utils/converter.ts index 6bb3144..184bf2f 100644 --- a/src/utils/converter.ts +++ b/src/utils/converter.ts @@ -13,25 +13,20 @@ import { exec } from './misc'; import { Operator } from './types'; /** - * Texinfo to HTML converter. + * Converter which converts file from Texinfo to other formats. */ export default class Converter { - /** - * The options to be passed to the `makeinfo` command. - */ - private readonly options = ['-o', '-', '--no-split', '--html']; - - private includeCustomCSS(cssFileURI: string) { + private includeCustomCSS(cssFileURI: string, options: string[]) { try { const uri = vscode.Uri.parse(cssFileURI, true); switch (uri.scheme) { case 'file': - this.options.push(`--css-include=${uri.path}`); + options.push(`--css-include=${uri.path}`); break; case 'http': case 'https': - this.options.push(`--css-ref=${uri.toString()}`); + options.push(`--css-ref=${uri.toString()}`); break; default: throw URIError; @@ -41,26 +36,24 @@ export default class Converter { } } - constructor( - private readonly path: string, - private readonly imgTransformer?: Operator, - private readonly insertScript?: string, - ) { - Options.noHeaders && this.options.push('--no-headers'); - Options.force && this.options.push('--force'); - Options.noValidation && this.options.push('--no-validate'); - Options.noWarnings && this.options.push('--no-warn'); - Options.customCSS && this.includeCustomCSS(Options.customCSS); - this.options.push(`--error-limit=${Options.errorLimit}`); - } + constructor(private readonly path: string) {} - async convert() { - const result = await exec(Options.makeinfo, this.options.concat(this.path), Options.maxSize); + async convertToHtml( + imgTransformer?: Operator, + insertScript?: string, + ) { + const options = ['-o', '-', '--no-split', '--html', `--error-limit=${Options.errorLimit}`]; + Options.noHeaders && options.push('--no-headers'); + Options.force && options.push('--force'); + Options.noValidation && options.push('--no-validate'); + Options.noWarnings && options.push('--no-warn'); + Options.customCSS && this.includeCustomCSS(Options.customCSS, options); + const result = await exec(Options.makeinfo, options.concat(this.path), Options.maxSize); if (result.data !== undefined) { // No worry about performance here, as the DOM is lazily initialized. const dom = new DOM(result.data); - this.imgTransformer && dom.transformImageUri(this.imgTransformer); - this.insertScript && dom.insertScript(this.insertScript); + imgTransformer && dom.transformImageUri(imgTransformer); + insertScript && dom.insertScript(insertScript); result.data = dom.outerHTML; } return result;