2020-10-24 21:45:32 +00:00
|
|
|
/**
|
2020-11-30 03:56:55 +00:00
|
|
|
* utils/converter.ts
|
2021-03-16 12:01:13 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2020,2021 CismonX <admin@cismon.net>
|
|
|
|
*
|
|
|
|
* This file is part of vscode-texinfo.
|
|
|
|
*
|
|
|
|
* vscode-texinfo is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* vscode-texinfo is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* vscode-texinfo. If not, see <https://www.gnu.org/licenses/>.
|
2020-10-24 21:45:32 +00:00
|
|
|
*/
|
|
|
|
|
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-04-19 12:43:20 +00:00
|
|
|
async convertToHtml(imgTransformer?: Operator<string>, insertScript?: string) {
|
|
|
|
const options = ['-o-', '--no-split', '--html', `--error-limit=${this.options.errorLimit}`];
|
|
|
|
this.options.noHeaders && options.push('--no-headers');
|
|
|
|
this.options.noValidation && options.push('--no-validate');
|
|
|
|
this.options.noWarnings && options.push('--no-warn');
|
2021-04-22 09:07:55 +00:00
|
|
|
this.includeCustomCSS(this.options.customCSS, options);
|
|
|
|
this.addVars(this.options.vars, options);
|
2021-04-19 12:43:20 +00:00
|
|
|
const result = await exec(this.options.makeinfo, options.concat(this.path), this.options.maxSize);
|
|
|
|
if (result.data !== undefined) {
|
|
|
|
// No worry about performance here, as the DOM is lazily initialized.
|
|
|
|
const dom = new DOM(result.data);
|
|
|
|
imgTransformer && dom.transformImageUri(imgTransformer);
|
|
|
|
insertScript && dom.insertScript(insertScript);
|
|
|
|
result.data = dom.outerHTML;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(private readonly path: string, private readonly options: Options, private readonly logger: Logger) {}
|
|
|
|
|
2021-04-22 09:07:55 +00:00
|
|
|
private addVars(vars: readonly string[], options: string[]) {
|
|
|
|
vars.forEach(varName => options.push('-D', varName));
|
|
|
|
}
|
|
|
|
|
2021-03-11 04:34:06 +00:00
|
|
|
private includeCustomCSS(cssFileURI: string, options: string[]) {
|
2021-04-22 09:07:55 +00:00
|
|
|
if (!cssFileURI) return;
|
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) {
|
2021-04-19 12:43:20 +00:00
|
|
|
this.logger.log(`Cannot load custom CSS. Invalid URI: '${cssFileURI}'`);
|
2021-02-22 07:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-24 21:45:32 +00:00
|
|
|
}
|