vscode-texinfo/src/utils/converter.ts

93 lines
3.4 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
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
*/
import * as path from 'path';
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 { 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 {
async toHTML(imgTransformer: Operator<vscode.Uri>, insertScript?: string) {
const pathUri = vscode.Uri.file(path.dirname(this._path));
const newPath = imgTransformer(pathUri).toString() + '/';
const options = ['-o-', '--no-split', '--html',
`--error-limit=${this._options.errorLimit}`,
`--init-file=${this._initFile}`,
'-D', `__vscode_texinfo_image_uri_base ${newPath}`,
];
2021-05-25 04:16:56 +00:00
this._options.noHeaders && options.push('--no-headers');
this._options.noNumberSections && options.push('--no-number-sections');
this._options.noValidation && options.push('--no-validate');
this._options.noWarnings && options.push('--no-warn');
if (insertScript !== undefined) {
options.push('-c', `EXTRA_HEAD <script>${insertScript}</script>`);
}
2021-05-25 04:16:56 +00:00
this._addIncludePaths(this._options.includePaths, options);
this._defineVariables(this._options.variables, options);
this._includeCustomCSS(this._options.customCSS, options);
return await exec(this._options.makeinfo, options.concat(this._path),
this._options.maxSize);
2021-04-19 12:43:20 +00:00
}
constructor(
2021-05-25 04:16:56 +00:00
private readonly _path: string,
private readonly _initFile: string,
private readonly _options: Options,
private readonly _logger: Logger,
) {}
2021-04-19 12:43:20 +00:00
2021-05-25 04:16:56 +00:00
private _addIncludePaths(paths: readonly string[], options: string[]) {
const separator = process.platform === 'win32' ? ';' : ':';
options.push('-I', paths.join(separator));
}
2021-05-25 04:16:56 +00:00
private _defineVariables(variables: readonly string[], options: string[]) {
variables.forEach(varName => options.push('-D', varName));
}
2021-05-25 04:16:56 +00:00
private _includeCustomCSS(cssFileURI: string, options: string[]) {
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) {
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
}