vscode-texinfo/src/utils/converter.ts

59 lines
2.0 KiB
TypeScript
Raw Normal View History

2020-10-24 21:45:32 +00:00
/**
2020-11-30 03:56:55 +00:00
* utils/converter.ts
2020-10-24 21:45:32 +00:00
*
* @author CismonX <admin@cismon.net>
* @license MIT
*/
2021-02-22 07:06:05 +00:00
import * as vscode from 'vscode';
import Logger from '../logger';
2020-10-24 21:45:32 +00:00
import Options from '../options';
import DOM from './dom';
import { exec } from './misc';
import { Operator } from './types';
/**
2021-03-11 04:34:06 +00:00
* Converter which converts file from Texinfo to other formats.
2020-10-24 21:45:32 +00:00
*/
export default class Converter {
2021-03-11 04:34:06 +00:00
private includeCustomCSS(cssFileURI: string, options: string[]) {
2021-02-22 07:06:05 +00:00
try {
const uri = vscode.Uri.parse(cssFileURI, true);
switch (uri.scheme) {
case 'file':
2021-03-11 04:34:06 +00:00
options.push(`--css-include=${uri.path}`);
2021-02-22 07:06:05 +00:00
break;
case 'http':
case 'https':
2021-03-11 04:34:06 +00:00
options.push(`--css-ref=${uri.toString()}`);
2021-02-22 07:06:05 +00:00
break;
default:
throw URIError;
}
} catch (e) {
Logger.log(`Cannot load custom CSS. Invalid URI: '${cssFileURI}'`);
}
}
2021-03-11 04:34:06 +00:00
constructor(private readonly path: string) {}
2020-10-24 21:45:32 +00:00
2021-03-15 12:43:38 +00:00
async convertToHtml(imgTransformer?: Operator<string>, insertScript?: string) {
2021-03-11 04:34:06 +00:00
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);
2020-10-24 21:45:32 +00:00
if (result.data !== undefined) {
// No worry about performance here, as the DOM is lazily initialized.
const dom = new DOM(result.data);
2021-03-11 04:34:06 +00:00
imgTransformer && dom.transformImageUri(imgTransformer);
insertScript && dom.insertScript(insertScript);
2020-10-24 21:45:32 +00:00
result.data = dom.outerHTML;
}
return result;
}
}