UNPKG

@eagleoutice/flowr

Version:

Static Dataflow Analyzer and Program Slicer for the R Programming Language

101 lines (97 loc) 3.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.printReplHelpAsMarkdownTable = printReplHelpAsMarkdownTable; exports.documentReplSession = documentReplSession; const repl_commands_1 = require("../../cli/repl/commands/repl-commands"); const doc_cli_option_1 = require("./doc-cli-option"); const html_hover_over_1 = require("../../util/html-hover-over"); const core_1 = require("../../cli/repl/core"); const ansi_1 = require("../../util/text/ansi"); const doc_docker_1 = require("./doc-docker"); const prompt_1 = require("../../cli/repl/prompt"); const doc_code_1 = require("./doc-code"); const print_version_1 = require("../../cli/repl/print-version"); const flowr_analyzer_builder_1 = require("../../project/flowr-analyzer-builder"); function printHelpForScript(script, starredVersion) { let base = `| **${(0, doc_cli_option_1.getReplCommand)(script[0], false, starredVersion !== undefined)}** | ${script[1].description}`; if (starredVersion) { base += ` (star: ${starredVersion.description})`; } if (script[1].aliases.length === 0) { return base; } const aliases = script[1].aliases; return `${base} (alias${aliases.length > 1 ? 'es' : ''}: ${aliases.map(a => '**:' + (0, html_hover_over_1.textWithTooltip)(a, `Alias of ':${script[0]}'. ${script[1].description}`) + '**').join(', ')}) |`; } /** * */ function printReplHelpAsMarkdownTable() { const scriptHelp = []; const cmds = (0, repl_commands_1.getReplCommands)(); for (const c of Object.entries(cmds)) { if (c[1].script || c[0].endsWith('*')) { continue; } const starred = cmds[c[0] + '*']; scriptHelp.push(printHelpForScript(c, starred)); } return ` | Command | Description | | ------- | ----------- | ${scriptHelp.sort().join('\n')} `; } function dropAnsiEscapeCodesAndCodeTags(input) { // eslint-disable-next-line no-control-regex return input.replace(/\x1B\[[0-9;]*[mK]/g, '') .replace(/<\/?code>/g, '') .replace(/\**([^*]+)\**/g, '$1') .replace(/_([^_]+)_/g, '$1'); } /** * Creates a documented REPL session for the given commands. * This is intended for documentation purposes. */ async function documentReplSession(parser, commands, options) { const collect = []; for (const command of commands) { const entry = { command, lines: [] }; const collectingOutput = { formatter: ansi_1.voidFormatter, stdout(msg) { entry.lines.push(dropAnsiEscapeCodesAndCodeTags(msg)); }, stderr(msg) { entry.lines.push(dropAnsiEscapeCodesAndCodeTags(msg)); } }; const analyzer = await new flowr_analyzer_builder_1.FlowrAnalyzerBuilder() .setParser(parser) .build(); await (0, core_1.replProcessAnswer)(analyzer, collectingOutput, command.command, options?.allowRSessionAccess ?? false); collect.push(entry); } let result = ''; let cache = options?.hideEntry ? '' : `$ docker run -it --rm ${doc_docker_1.DockerName} ${options?.args ? options?.args + ' ' : ''}# or npm run flowr ${options?.args ? '-- ' + options?.args : ''}\n`; if (!options?.hideEntry) { cache += await (0, print_version_1.versionReplString)(parser) + '\n'; } for (const { command, lines } of collect) { if (lines.length === 0) { cache += prompt_1.rawPrompt + ' ' + command.command + '\n'; continue; } result += ` ${(0, doc_code_1.codeBlock)('shell', cache + prompt_1.rawPrompt + ' ' + command.command)} <details${options?.openOutput ? ' open' : ''}> <summary style='color:gray'>Output</summary> ${(0, doc_code_1.codeBlock)('text', lines.join('\n'))} ${command.description} </details> `; cache = ''; } return result; } //# sourceMappingURL=doc-repl.js.map