-
Notifications
You must be signed in to change notification settings - Fork 207
/
nomnoml.ts
152 lines (130 loc) · 3.91 KB
/
nomnoml.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import type { Config, Measurer } from './domain'
import { Graphics } from './Graphics'
import { layout } from './layouter'
import { parse } from './parser'
import { render } from './renderer'
import { GraphicsCanvas } from './GraphicsCanvas'
import { GraphicsSvg } from './GraphicsSvg'
interface Rect {
width: number
height: number
}
function fitCanvasSize(canvas: HTMLCanvasElement, rect: Partial<Rect>, zoom: number) {
canvas.width = rect.width! * zoom
canvas.height = rect.height! * zoom
}
function createMeasurer(config: Config, graphics: Graphics): Measurer {
return {
setFont(
family: string,
size: number,
weight: 'bold' | 'normal',
style: 'italic' | 'normal'
): void {
graphics.setFont(family, size, weight, style)
},
textWidth(s: string): number {
return graphics.measureText(s).width
},
textHeight(): number {
return config.leading * config.fontSize
},
}
}
function parseAndRender(
code: string,
graphics: Graphics,
canvas: HTMLCanvasElement | null,
scale: number
) {
const parsedDiagram = parse(code)
const config = parsedDiagram.config
const measurer = createMeasurer(config, graphics)
const graphLayout = layout(measurer, config, parsedDiagram.root)
if (canvas) {
fitCanvasSize(canvas, graphLayout, config.zoom * scale)
}
config.zoom *= scale
render(graphics, config, graphLayout)
return { config: config, layout: graphLayout }
}
export function draw(canvas: HTMLCanvasElement, code: string, scale?: number): { config: Config } {
return parseAndRender(code, GraphicsCanvas(canvas), canvas, scale || 1)
}
export function renderSvg(code: string, document?: HTMLDocument): string {
const skCanvas = GraphicsSvg(document)
const { config, layout } = parseAndRender(code, skCanvas, null, 1)
return skCanvas.serialize(
{
width: layout.width!,
height: layout.height!,
},
code,
config.title
)
}
export class ImportDepthError extends Error {
constructor() {
super('max_import_depth exceeded')
}
}
type FileLoaderAsync = (filename: string) => Promise<string>
export async function processAsyncImports(
source: string,
loadFile: FileLoaderAsync,
maxImportDepth: number = 10
): Promise<string> {
if (maxImportDepth == -1) {
throw new ImportDepthError()
}
async function lenientLoadFile(key: string): Promise<string> {
try {
return (await loadFile(key)) || ''
} catch (e) {
return ''
}
}
const imports: { file: string; promise: Promise<string> }[] = []
source.replace(/#import: *(.*)/g, (a: unknown, file: string) => {
const promise = lenientLoadFile(file).then((contents) =>
processAsyncImports(contents, loadFile, maxImportDepth - 1)
)
imports.push({ file, promise })
return ''
})
const imported: Record<string, string> = {}
for (const imp of imports) {
imported[imp.file] = await imp.promise
}
return source.replace(/#import: *(.*)/g, (a: unknown, file: string) => imported[file])
}
type FileLoader = (filename: string) => string
export function processImports(
source: string,
loadFile: FileLoader,
maxImportDepth: number = 10
): string {
if (maxImportDepth == -1) {
throw new ImportDepthError()
}
function lenientLoadFile(key: string) {
try {
return loadFile(key) || ''
} catch (e) {
return ''
}
}
return source.replace(/#import: *(.*)/g, (a: unknown, file: string) =>
processImports(lenientLoadFile(file), loadFile, maxImportDepth - 1)
)
}
export function compileFile(filepath: string, maxImportDepth?: number): string {
const fs = require('fs')
const path = require('path')
const directory = path.dirname(filepath)
const rootFileName = filepath.substr(directory.length)
function loadFile(filename: string): string {
return fs.readFileSync(path.join(directory, filename), { encoding: 'utf8' })
}
return processImports(loadFile(rootFileName), loadFile, maxImportDepth)
}