import { createInterpretedTextGenerators } from '../../Generator/RstGenerator.ts' import { RstGeneratorError } from '../../Generator/RstGeneratorError.ts' import type { RstGeneratorState } from '../../Generator/RstGeneratorState.ts' import { createRstCompilerPlugins } from '../../RstCompilerPlugin.ts' import { RstInterpretedText } from '../../RstNode/Inline/InterpretedText.ts' const pepRe = /^(?\d+)$/ const rfcRe = /^(?\d+)(?#.+)?$/ // ---------------------------------------------------------------------------- // MARK: InterpretedText // ---------------------------------------------------------------------------- export const pepInterpretedTextGenerators = createInterpretedTextGenerators( [ 'pep-reference', 'PEP', ], (generatorState, node) => { const { label, url } = getPepInfo(generatorState, node) generatorState.writeTextWithLinePrefix(`${label}`) }, (generatorState, node) => { const { label, url } = getPepInfo(generatorState, node) generatorState.writeTextWithLinePrefix(`[${label}](${url})`) }, ) export const rfcInterpretedTextGenerators = createInterpretedTextGenerators( [ 'rfc-reference', 'RFC', ], (generatorState, node) => { const { label, url } = getRfcInfo(generatorState, node) generatorState.writeTextWithLinePrefix(`${label}`) }, (generatorState, node) => { const { label, url } = getRfcInfo(generatorState, node) generatorState.writeTextWithLinePrefix(`[${label}](${url})`) }, ) // ---------------------------------------------------------------------------- // MARK: Plugin // ---------------------------------------------------------------------------- export const pepRfcInterpretedTextPlugins = createRstCompilerPlugins({ interpretedTextGenerators: [ pepInterpretedTextGenerators, rfcInterpretedTextGenerators, ], isValidInterpretedText: (rawBodyText, role) => { if (pepInterpretedTextGenerators.roles.includes(role)) { return pepRe.test(rawBodyText) } if (rfcInterpretedTextGenerators.roles.includes(role)) { return rfcRe.test(rawBodyText) } return null }, }) // ---------------------------------------------------------------------------- // MARK: Helpers // ---------------------------------------------------------------------------- function getPepInfo(generatorState: RstGeneratorState, node: RstInterpretedText) { const pep = pepRe.exec(node.textContent)?.groups?.pep if (!pep) { throw new RstGeneratorError(generatorState, 'Invalid PEP') } const label = `PEP ${pep}` const url = `https://peps.python.org/pep-${pep.padStart(4, '0')}` return { label, url, } } function getRfcInfo(generatorState: RstGeneratorState, node: RstInterpretedText) { const match = rfcRe.exec(node.textContent) const rfc = match?.groups?.rfc if (!rfc) { throw new RstGeneratorError(generatorState, 'Invalid RFC') } const label = `RFC ${rfc}` const anchor = match?.groups?.anchor const url = anchor ? `https://tools.ietf.org/html/rfc${rfc}.html#${anchor}` : `https://tools.ietf.org/html/rfc${rfc}.html` return { label, url, } }