@thi.ng/tangle
Version:
Literate programming code block tangling / codegen utility, inspired by org-mode & noweb
87 lines • 2.69 kB
TypeScript
import type { Fn, Fn2, FnAnyT, IObjectOf, Predicate } from "@thi.ng/api";
import { type ILogger } from "@thi.ng/logger";
export interface Block {
id: string;
tangle?: string;
publish?: string;
noweb: string;
lang: string;
matchStart: number;
matchEnd: number;
start: number;
end: number;
body: string;
edited?: boolean;
resolved?: boolean;
}
export interface TangleRef {
path: string;
src: string;
blocks: Record<string, Block>;
}
export type Blocks = Record<string, Block>;
export interface TangleCtx {
/**
* Code block marker definitions for current source file format. If not
* pre-defined, then {@link tangleFile} will auto-detect the format based on
* given file extension. See {@link BLOCK_FORMATS} for supported source
* formats.
*/
format: CodeBlockFormat;
/**
* Stores all referenced source files and their extracted code blocks.
*/
files: Record<string, TangleRef>;
/**
* Stores all generated outputs (absolute file paths are used as keys)
*/
outputs: Record<string, string>;
/**
* Logger for debug outputs
*/
logger: ILogger;
/**
* "Filesystem" implementation. When using {@link tangleString}, in-memory
* stubs will be used to obtain file contents from a given object.
*/
fs: FileSystem;
/**
* Tangle & codegen options
*/
opts: Partial<TangleOpts>;
}
export interface FileSystem {
isAbsolute: Predicate<string>;
resolve: FnAnyT<string, string>;
read: Fn2<string, ILogger, string>;
}
export interface TangleOpts {
comments: boolean;
}
export interface CodeBlockFormat {
/**
* String or regexp matching the beginning of a code block. If given a
* regexp, it will become part of a larger one and any regexp flags given
* here will be ignored. Also the given regexp pattern will always be
* prefixed with the line-start symbol (`^`), so it must not be given
* here...
*/
prefix: string | RegExp;
/**
* String or regexp matching the end of a code block. If given a
* regexp, it MUST have the multiline flag enabled!
*/
suffix: string | RegExp;
/**
* Optional function to transform the codeblock's body (e.g. to remove any
* prefix characters or remove trailing whitespace).
*/
xform?: Fn<string, string>;
}
export declare const BLOCK_FORMATS: IObjectOf<CodeBlockFormat>;
export declare const COMMENT_FORMATS: IObjectOf<string | [string, string]>;
/**
* See [thi.ng/logger](https://docs.thi.ng/umbrella/logger/) for usage.
*/
export declare const LOGGER: ILogger;
//# sourceMappingURL=api.d.ts.map