import { tokenizeInput } from './Lexer/tokenizeInput.ts' import { type RstParserOutput, RstParserState } from './Parser/RstParserState.ts' import type { RstDirectiveGenerator, RstInterpretedTextGenerator, RstNodeGenerator } from './Generator/RstGenerator.ts' import { RstGeneratorState, type RstGeneratorInput, type RstGeneratorOutput } from './Generator/RstGeneratorState.ts' import { type RstGeneratorOptions, createDefaultGeneratorOptions } from './Generator/RstGeneratorOptions.ts' import { type RstParserOptions, createDefaultParserOptions } from './Parser/RstParserOptions.ts' import { htmlNodeGenerators, htmlPlugins, mdNodeGenerators, mdPlugins, nodeParsers } from './Defaults.ts' import type { RstNodeType } from './RstNode/RstNodeType.ts' import type { RstNodeParser } from './Parser/RstParser.ts' import type { RstCompilerPlugin } from './RstCompilerPlugin.ts' import type { RstNodeJson } from './RstNode/RstNode.ts' import { RstNodeRegistrar } from './Parser/RstNodeRegistrar.ts' import { RstDocument } from './RstNode/Block/Document.ts' import { HtmlAttrResolver } from './Parser/Resolver/HtmlAttrResolver.ts' import { SimpleNameResolver } from './Parser/Resolver/SimpleNameResolver.ts' import { SubstitutionResolver } from './Parser/Resolver/SubstitutionResolver.ts' // ---------------------------------------------------------------------------- // MARK: Base // ---------------------------------------------------------------------------- export class RstCompiler { readonly nodeParsers = [...nodeParsers] readonly nodeGenerators = new Map() readonly directiveGenerators = new Map() readonly interpretedTextGenerators = new Map() readonly plugins = new Array() private readonly _outputWarnings = new Array() private readonly _outputErrors = new Array() useNodeParser(parser: RstNodeParser) { this.nodeParsers.unshift(parser) } useNodeGenerator(generator: RstNodeGenerator) { this.nodeGenerators.set(generator.nodeType, generator) } useDirectiveGenerator(generator: RstDirectiveGenerator) { for (const directive of generator.directives) { this.directiveGenerators.set(directive.toLowerCase(), generator) } } useInterpretedTextGenerator(generator: RstInterpretedTextGenerator) { for (const role of generator.roles) { this.interpretedTextGenerators.set(role.toLowerCase(), generator) } } usePlugin(plugin: RstCompilerPlugin) { this.plugins.push(plugin) plugin.onInstall?.(this) } notifyWarning(text: string) { this._outputWarnings.push(text) } notifyError(text: string) { this._outputErrors.push(text) } clearNotifications() { this._outputWarnings.length = 0 this._outputErrors.length = 0 } get outputWarnings(): ReadonlyArray { return this._outputWarnings } get outputErrors(): ReadonlyArray { return this._outputErrors } consoleLogProblems(opts?: { disableWarnings?: boolean; disableErrors?: boolean }): void { if (!opts?.disableWarnings) { for (const warning of this.outputWarnings) { console.warn(warning) } } if (!opts?.disableErrors) { for (const error of this.outputErrors) { console.error(error) } } } parse(input: string, parserOptions?: Partial): RstParserOutput { this.clearNotifications() const opts = createDefaultParserOptions(parserOptions) for (const plugin of this.plugins) { plugin.onBeforeParse?.(opts) } if (opts.epilog) { input += `\n\n${opts.epilog}` } const tokens = tokenizeInput(input) const parserState = new RstParserState(opts, tokens, this) const parserOutput = parserState.parserOutput for (const plugin of this.plugins) { plugin.onParse?.(parserOutput) } this.consoleLogProblems(opts) return parserOutput } parseJson(rootJson: RstNodeJson, parserOptions?: Partial): RstParserOutput { this.clearNotifications() const opts = createDefaultParserOptions(parserOptions) for (const plugin of this.plugins) { plugin.onBeforeParse?.(opts) } const registrar = new RstNodeRegistrar() const root = registrar.reviveRstNodeFromJson(rootJson) if (!(root instanceof RstDocument)) { throw new Error('Failed to parseJson') } const parserOutput = this.createParserOutput(root) for (const plugin of this.plugins) { plugin.onParse?.(parserOutput) } this.consoleLogProblems(opts) return parserOutput } generate(generatorInput: RstGeneratorInput, generatorOptions?: Partial): RstGeneratorOutput { this.clearNotifications() const opts = createDefaultGeneratorOptions(generatorOptions) for (const plugin of this.plugins) { plugin.onBeforeGenerate?.(opts) } const generatorState = new RstGeneratorState(opts, generatorInput, this) const generatorOutput = generatorState.generatorOutput for (const plugin of this.plugins) { plugin.onGenerate?.(generatorOutput) } this.consoleLogProblems(generatorOptions) return generatorOutput } createParserOutput(root: RstDocument): RstParserOutput { // Need to create a bundle around root (with resolves) after parse() instead of before generate() // because plugins' onParse hook need access to resolvers const htmlAttrResolver = new HtmlAttrResolver() const substitutionResolver = new SubstitutionResolver(root.findAllChildren('SubstitutionDef')) const simpleNameResolver = new SimpleNameResolver(this, htmlAttrResolver, root) return { root, htmlAttrResolver, substitutionResolver, simpleNameResolver, } } compile(input: string | RstParserOutput, parserOptions?: Partial, generatorOptions?: Partial): RstGeneratorOutput { const parserOutput = typeof input === 'string' ? this.parse(input, parserOptions) : input const basePath = '/' const docPath = 'index' const generatorInput: RstGeneratorInput = { basePath, currentDocPath: docPath, docs: [ { docPath, parserOutput, }, ], } return this.generate(generatorInput, generatorOptions) } } // ---------------------------------------------------------------------------- // MARK: Html // ---------------------------------------------------------------------------- export class RstToHtmlCompiler extends RstCompiler { constructor() { super() for (const generator of htmlNodeGenerators) { this.useNodeGenerator(generator) } for (const plugin of htmlPlugins) { this.usePlugin(plugin) } } } // ---------------------------------------------------------------------------- // MARK: Markdown // ---------------------------------------------------------------------------- export class RstToMdCompiler extends RstCompiler { constructor() { super() for (const generator of mdNodeGenerators) { this.useNodeGenerator(generator) } for (const plugin of mdPlugins) { this.usePlugin(plugin) } } }