@tinyvolt/pie 0.9.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/README.md +18 -1
  2. package/dist/components/SplitPane.d.ts +21 -0
  3. package/dist/components/SplitPane.jsx +113 -0
  4. package/dist/components/icons.d.ts +12 -0
  5. package/dist/components/icons.jsx +45 -0
  6. package/dist/lib/autodiff/index.d.ts +28 -0
  7. package/dist/lib/autodiff/index.js +39 -0
  8. package/dist/lib/customModules.d.ts +19 -0
  9. package/dist/lib/customModules.js +52 -0
  10. package/dist/lib/markdown/compile.d.ts +27 -0
  11. package/dist/lib/markdown/compile.js +26 -0
  12. package/dist/lib/markdown/components/CanvasPage.d.ts +9 -0
  13. package/dist/lib/markdown/components/CanvasPage.jsx +16 -0
  14. package/dist/lib/markdown/components/MarkdownBody.d.ts +11 -0
  15. package/dist/lib/markdown/components/MarkdownBody.jsx +37 -0
  16. package/dist/lib/markdown/components/PiePage.d.ts +14 -0
  17. package/dist/lib/markdown/components/PiePage.jsx +42 -0
  18. package/dist/lib/markdown/components/PythonEditor.d.ts +10 -0
  19. package/dist/lib/markdown/components/PythonEditor.jsx +209 -0
  20. package/dist/lib/markdown/features/build.d.ts +60 -0
  21. package/dist/lib/markdown/features/build.js +36 -0
  22. package/dist/lib/markdown/features/glossary/View.d.ts +9 -0
  23. package/dist/lib/markdown/features/glossary/View.jsx +62 -0
  24. package/dist/lib/markdown/features/glossary/build.d.ts +6 -0
  25. package/dist/lib/markdown/features/glossary/build.js +81 -0
  26. package/dist/lib/markdown/features/sidebar/View.d.ts +11 -0
  27. package/dist/lib/markdown/features/sidebar/View.jsx +45 -0
  28. package/dist/lib/markdown/features/sidebar/build.d.ts +3 -0
  29. package/dist/lib/markdown/features/sidebar/build.js +7 -0
  30. package/dist/lib/markdown/features/tags/build.d.ts +7 -0
  31. package/dist/lib/markdown/features/tags/build.js +22 -0
  32. package/dist/lib/markdown/features/tags/url.d.ts +6 -0
  33. package/dist/lib/markdown/features/tags/url.js +10 -0
  34. package/dist/lib/markdown/features/toc/build.d.ts +7 -0
  35. package/dist/lib/markdown/features/toc/build.js +62 -0
  36. package/dist/lib/markdown/features/views.d.ts +4 -0
  37. package/dist/lib/markdown/features/views.jsx +11 -0
  38. package/dist/lib/markdown/frontmatter.d.ts +44 -0
  39. package/dist/lib/markdown/frontmatter.js +109 -0
  40. package/dist/lib/markdown/render.d.ts +6 -0
  41. package/dist/lib/markdown/render.js +95 -0
  42. package/dist/lib/nodes/animation.d.ts +14 -0
  43. package/dist/lib/nodes/animation.js +34 -0
  44. package/dist/lib/nodes/components/CenteredGrid.d.ts +20 -0
  45. package/dist/lib/nodes/components/CenteredGrid.jsx +44 -0
  46. package/dist/lib/nodes/components/NodeCanvas.d.ts +7 -0
  47. package/dist/lib/nodes/components/NodeCanvas.jsx +50 -0
  48. package/dist/lib/nodes/execute.d.ts +27 -0
  49. package/dist/lib/nodes/execute.js +159 -0
  50. package/dist/lib/nodes/functions/Node2NodeFunc.d.ts +70 -0
  51. package/dist/lib/nodes/functions/Node2NodeFunc.js +223 -0
  52. package/dist/lib/nodes/functions/array.d.ts +12 -0
  53. package/dist/lib/nodes/functions/array.js +31 -0
  54. package/dist/lib/nodes/functions/autodiff.d.ts +12 -0
  55. package/dist/lib/nodes/functions/autodiff.js +60 -0
  56. package/dist/lib/nodes/functions/basic.d.ts +111 -0
  57. package/dist/lib/nodes/functions/basic.js +275 -0
  58. package/dist/lib/nodes/functions/registry.d.ts +9 -0
  59. package/dist/lib/nodes/functions/registry.js +45 -0
  60. package/dist/lib/nodes/functions/sideEffects.d.ts +78 -0
  61. package/dist/lib/nodes/functions/sideEffects.js +201 -0
  62. package/dist/lib/nodes/hooks/useCanvasGestures.d.ts +9 -0
  63. package/dist/lib/nodes/hooks/useCanvasGestures.js +120 -0
  64. package/dist/lib/nodes/hooks/useCanvasResize.d.ts +2 -0
  65. package/dist/lib/nodes/hooks/useCanvasResize.js +33 -0
  66. package/dist/lib/nodes/jax.d.ts +46 -0
  67. package/dist/lib/nodes/jax.js +120 -0
  68. package/dist/lib/nodes/render/renderLogic.d.ts +13 -0
  69. package/dist/lib/nodes/render/renderLogic.jsx +234 -0
  70. package/dist/lib/nodes/state/NameGenerator.d.ts +10 -0
  71. package/dist/lib/nodes/state/NameGenerator.js +29 -0
  72. package/dist/lib/nodes/state/Node.d.ts +134 -0
  73. package/dist/lib/nodes/state/Node.js +57 -0
  74. package/dist/lib/nodes/state/NodeStore.d.ts +49 -0
  75. package/dist/lib/nodes/state/NodeStore.js +138 -0
  76. package/dist/lib/nodes/tensors/broadcast.d.ts +18 -0
  77. package/dist/lib/nodes/tensors/broadcast.js +92 -0
  78. package/dist/lib/nodes/tensors/tensor.d.ts +13 -0
  79. package/dist/lib/nodes/tensors/tensor.js +34 -0
  80. package/dist/lib/python/lift.d.ts +51 -0
  81. package/dist/lib/python/lift.js +155 -0
  82. package/dist/lib/python/py/pie.py +303 -0
  83. package/dist/lib/python/py/pie_mpl.py +15 -0
  84. package/dist/lib/python/py/show_figures.py +5 -0
  85. package/dist/lib/python/runPage.d.ts +21 -0
  86. package/dist/lib/python/runPage.js +60 -0
  87. package/dist/lib/python/runner.d.ts +68 -0
  88. package/dist/lib/python/runner.js +243 -0
  89. package/dist/lib/python/worker.d.ts +1 -0
  90. package/dist/lib/python/worker.js +111 -0
  91. package/dist/lib/ui/UiLayer.d.ts +2 -0
  92. package/dist/lib/ui/UiLayer.jsx +15 -0
  93. package/dist/lib/ui/UiView.d.ts +23 -0
  94. package/dist/lib/ui/UiView.jsx +56 -0
  95. package/dist/lib/ui/components/checkboxes.d.ts +10 -0
  96. package/dist/lib/ui/components/checkboxes.jsx +30 -0
  97. package/dist/lib/ui/components/latex.d.ts +8 -0
  98. package/dist/lib/ui/components/latex.jsx +41 -0
  99. package/dist/lib/ui/components/radio.d.ts +6 -0
  100. package/dist/lib/ui/components/radio.jsx +19 -0
  101. package/dist/lib/ui/components/slider.d.ts +8 -0
  102. package/dist/lib/ui/components/slider.jsx +7 -0
  103. package/dist/lib/ui/components/textbox.d.ts +5 -0
  104. package/dist/lib/ui/components/textbox.jsx +21 -0
  105. package/dist/lib/ui/dragUi.d.ts +10 -0
  106. package/dist/lib/ui/dragUi.js +129 -0
  107. package/dist/lib/ui/nodeHandle.d.ts +15 -0
  108. package/dist/lib/ui/nodeHandle.js +37 -0
  109. package/dist/lib/ui/registry.d.ts +10 -0
  110. package/dist/lib/ui/registry.js +42 -0
  111. package/dist/runtime.css +696 -0
  112. package/dist/runtime.d.ts +10 -0
  113. package/dist/runtime.jsx +7 -0
  114. package/init/index.js +2 -2
  115. package/init/site-agents.md +2 -1
  116. package/package.json +15 -6
  117. package/runtime-agents.md +168 -0
  118. package/src/app.css +7 -665
  119. package/src/lib/customModules.ts +67 -0
  120. package/src/lib/markdown/compile.ts +49 -0
  121. package/src/lib/markdown/components/PiePage.tsx +49 -0
  122. package/src/lib/markdown/features/build.ts +15 -6
  123. package/src/lib/markdown/features/sidebar/View.tsx +4 -2
  124. package/src/lib/markdown/features/tags/build.ts +6 -3
  125. package/src/lib/markdown/pages.ts +1 -9
  126. package/src/lib/markdown/vitePlugin.ts +4 -8
  127. package/src/lib/nodes/functions/registry.ts +30 -21
  128. package/src/lib/nodes/functions/sideEffects.ts +14 -8
  129. package/src/lib/nodes/functions/vitePlugin.ts +8 -10
  130. package/src/lib/nodes/state/Node.ts +27 -19
  131. package/src/lib/python/runPage.ts +78 -0
  132. package/src/lib/python/runner.ts +189 -164
  133. package/src/lib/siteModules.ts +13 -0
  134. package/src/lib/ui/dragUi.ts +4 -2
  135. package/src/lib/ui/registry.ts +35 -14
  136. package/src/page.css +664 -0
  137. package/src/routes/[...slug].tsx +1 -0
  138. package/src/runtime.css +29 -0
  139. package/src/runtime.tsx +16 -0
package/README.md CHANGED
@@ -58,6 +58,23 @@ Updating doesn't change the site's `AGENTS.md` or `vite.config.ts`. To get the n
58
58
 
59
59
  Requires Node 24. Package: [@tinyvolt/pie](https://www.npmjs.com/package/@tinyvolt/pie).
60
60
 
61
+ ## Use pie inside another app
62
+
63
+ A Solid app built with Vite can show pie pages too, e.g. as a Markdown preview:
64
+
65
+ ```tsx
66
+ import { compilePie, PiePage } from '@tinyvolt/pie/runtime';
67
+ import '@tinyvolt/pie/runtime.css';
68
+
69
+ <PiePage page={compilePie(markdown)} />
70
+ ```
71
+
72
+ - `compilePie(markdown, { site, readFile })` turns pie markdown into a page, as a site build would. It throws with the build's error messages.
73
+ - `<PiePage page modules css />` shows it, in a box that fills its parent. `modules` holds the app's own functions, node types and UIs; `css` is CSS limited to the page.
74
+ - The app's own functions, node types and UIs import from `@tinyvolt/pie/runtime` instead of `~/lib/...`.
75
+
76
+ The full guide, for developers and AI agents, is [runtime-agents.md](runtime-agents.md), also at `node_modules/@tinyvolt/pie/runtime-agents.md`.
77
+
61
78
  ## Developing pie-ssg
62
79
 
63
80
  Requires Node 24 (see `.nvmrc`) and pnpm.
@@ -79,7 +96,7 @@ pnpm preview # preview the build with vite
79
96
 
80
97
  ## Publishing
81
98
 
82
- Bump `version` in `package.json`, then at the repo root:
99
+ Bump `version` in `package.json`, then at the repo root (packing runs `pnpm build:runtime` first, which writes `dist/`):
83
100
 
84
101
  ```sh
85
102
  npm whoami
@@ -0,0 +1,21 @@
1
+ import { type JSX } from 'solid-js';
2
+ /** The edge the first pane sits against. */
3
+ export type SplitSide = 'left' | 'right' | 'top' | 'bottom';
4
+ /** The collapsed pane, if any. */
5
+ export type Collapsed = 'none' | 'first' | 'second';
6
+ export interface SplitPaneProps {
7
+ side: SplitSide;
8
+ first: JSX.Element;
9
+ second: JSX.Element;
10
+ /** Names used on the collapse buttons, e.g. "Hide canvas". */
11
+ firstLabel: string;
12
+ secondLabel: string;
13
+ collapsed: Collapsed;
14
+ setCollapsed: (c: Collapsed) => void;
15
+ class?: string;
16
+ }
17
+ /**
18
+ * Two panes split by a draggable divider: `first` against `side`, `second` in the rest.
19
+ * Each pane can be collapsed with a button on the divider, or by dragging it nearly shut.
20
+ */
21
+ export declare function SplitPane(props: SplitPaneProps): JSX.Element;
@@ -0,0 +1,113 @@
1
+ import { createSignal, Show } from 'solid-js';
2
+ import { Chevron } from './icons';
3
+ // Limits on the first pane's share, in %, while dragging.
4
+ const MIN_PERCENT = 20;
5
+ const MAX_PERCENT = 85;
6
+ /** Dragging a pane below this share, in %, collapses it. */
7
+ const SNAP_PERCENT = 8;
8
+ const SIDE_DIRECTION = { left: 'left', right: 'right', top: 'up', bottom: 'down' };
9
+ const OPPOSITE = { left: 'right', right: 'left', up: 'down', down: 'up' };
10
+ const BUTTONS_TRANSFORM = {
11
+ center: 'translate(-50%, -50%)',
12
+ left: 'translate(-100%, -50%)',
13
+ right: 'translate(0, -50%)',
14
+ up: 'translate(-50%, -100%)',
15
+ down: 'translate(-50%, 0)',
16
+ };
17
+ const HIDDEN_PANE = { flex: '0 0 0%', overflow: 'hidden', visibility: 'hidden' };
18
+ /** The first pane's share of `rect`, in %, with the pointer at (`x`, `y`). */
19
+ function firstPercentAt(side, rect, x, y) {
20
+ const fraction = {
21
+ left: (x - rect.left) / rect.width,
22
+ right: (rect.right - x) / rect.width,
23
+ top: (y - rect.top) / rect.height,
24
+ bottom: (rect.bottom - y) / rect.height,
25
+ }[side];
26
+ return fraction * 100;
27
+ }
28
+ /**
29
+ * Calls `onMove` on every pointer move until the pointer is released.
30
+ * Listens on the window, so the drag goes on after the pointer leaves the divider.
31
+ * Text selection is off meanwhile, so dragging doesn't highlight page text.
32
+ */
33
+ function trackDrag(onMove) {
34
+ document.body.style.userSelect = 'none';
35
+ const stop = () => {
36
+ document.body.style.userSelect = '';
37
+ window.removeEventListener('pointermove', onMove);
38
+ window.removeEventListener('pointerup', stop);
39
+ };
40
+ window.addEventListener('pointermove', onMove);
41
+ window.addEventListener('pointerup', stop);
42
+ }
43
+ /** The draggable bar between the panes, with a button to collapse each one. */
44
+ function SplitDivider(props) {
45
+ const isStacked = () => props.side === 'top' || props.side === 'bottom';
46
+ const firstDirection = () => SIDE_DIRECTION[props.side];
47
+ const secondDirection = () => OPPOSITE[firstDirection()];
48
+ // A collapsed pane's buttons sit beside the bar, on the open pane's side.
49
+ const buttonsTransform = () => {
50
+ const c = props.collapsed;
51
+ return BUTTONS_TRANSFORM[c === 'first' ? secondDirection() : c === 'second' ? firstDirection() : 'center'];
52
+ };
53
+ const firstButtonLabel = () => (props.collapsed === 'second' ? `Show ${props.secondLabel}` : `Hide ${props.firstLabel}`);
54
+ const secondButtonLabel = () => (props.collapsed === 'first' ? `Show ${props.firstLabel}` : `Hide ${props.secondLabel}`);
55
+ return (<div class="split-pane-divider" classList={{ 'split-pane-divider--horizontal': isStacked() }} onPointerDown={props.onPointerDown} role="separator" aria-orientation={isStacked() ? 'horizontal' : 'vertical'}>
56
+ <div class="split-pane-buttons" style={{ transform: buttonsTransform() }}>
57
+ <Show when={props.collapsed !== 'first'}>
58
+ <button class="split-pane-button" onPointerDown={(e) => e.stopPropagation()} onClick={() => props.setCollapsed(props.collapsed === 'second' ? 'none' : 'first')} aria-label={firstButtonLabel()} title={firstButtonLabel()}>
59
+ <Chevron direction={firstDirection()}/>
60
+ </button>
61
+ </Show>
62
+ <Show when={props.collapsed !== 'second'}>
63
+ <button class="split-pane-button" onPointerDown={(e) => e.stopPropagation()} onClick={() => props.setCollapsed(props.collapsed === 'first' ? 'none' : 'second')} aria-label={secondButtonLabel()} title={secondButtonLabel()}>
64
+ <Chevron direction={secondDirection()}/>
65
+ </button>
66
+ </Show>
67
+ </div>
68
+ </div>);
69
+ }
70
+ /**
71
+ * Two panes split by a draggable divider: `first` against `side`, `second` in the rest.
72
+ * Each pane can be collapsed with a button on the divider, or by dragging it nearly shut.
73
+ */
74
+ export function SplitPane(props) {
75
+ let ref;
76
+ const [firstPercent, setFirstPercent] = createSignal(50);
77
+ const startResize = (e) => {
78
+ e.preventDefault();
79
+ trackDrag((ev) => {
80
+ if (!ref)
81
+ return;
82
+ const pct = firstPercentAt(props.side, ref.getBoundingClientRect(), ev.clientX, ev.clientY);
83
+ if (pct < SNAP_PERCENT) {
84
+ props.setCollapsed('first');
85
+ }
86
+ else if (pct > 100 - SNAP_PERCENT) {
87
+ props.setCollapsed('second');
88
+ }
89
+ else {
90
+ props.setCollapsed('none');
91
+ setFirstPercent(Math.min(MAX_PERCENT, Math.max(MIN_PERCENT, pct)));
92
+ }
93
+ });
94
+ };
95
+ const firstStyle = () => {
96
+ const c = props.collapsed;
97
+ if (c === 'first')
98
+ return HIDDEN_PANE;
99
+ if (c === 'second')
100
+ return { flex: '1 1 0%' };
101
+ return { flex: `0 0 ${firstPercent()}%` };
102
+ };
103
+ const secondStyle = () => (props.collapsed === 'second' ? HIDDEN_PANE : { flex: '1 1 0%' });
104
+ return (<div class={`split-pane split-pane--${props.side} ${props.class ?? ''}`} ref={ref}>
105
+ <div class="split-pane-pane" style={firstStyle()}>
106
+ {props.first}
107
+ </div>
108
+ <SplitDivider side={props.side} collapsed={props.collapsed} setCollapsed={props.setCollapsed} firstLabel={props.firstLabel} secondLabel={props.secondLabel} onPointerDown={startResize}/>
109
+ <div class="split-pane-pane" style={secondStyle()}>
110
+ {props.second}
111
+ </div>
112
+ </div>);
113
+ }
@@ -0,0 +1,12 @@
1
+ import type { JSX } from 'solid-js';
2
+ export type Direction = 'left' | 'right' | 'up' | 'down';
3
+ /** A small arrow pointing `direction`. */
4
+ export declare function Chevron(props: {
5
+ direction: Direction;
6
+ }): JSX.Element;
7
+ export declare function PlayIcon(): JSX.Element;
8
+ export declare function StopIcon(): JSX.Element;
9
+ export declare function ResetIcon(): JSX.Element;
10
+ export declare function CopyIcon(): JSX.Element;
11
+ export declare function CheckIcon(): JSX.Element;
12
+ export declare function CrossIcon(): JSX.Element;
@@ -0,0 +1,45 @@
1
+ const CHEVRON_TURN = { left: 0, up: 90, right: 180, down: 270 };
2
+ /** A small arrow pointing `direction`. */
3
+ export function Chevron(props) {
4
+ return (<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style={{ transform: `rotate(${CHEVRON_TURN[props.direction]}deg)` }}>
5
+ <path d="M15 18l-6-6 6-6"/>
6
+ </svg>);
7
+ }
8
+ /** The shared frame for the icons below: 24×24 drawing, lines in the text color. */
9
+ function Icon(props) {
10
+ return (<svg class="icon" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
11
+ {props.children}
12
+ </svg>);
13
+ }
14
+ export function PlayIcon() {
15
+ return (<Icon>
16
+ <path d="M6 4l14 8-14 8z" fill="currentColor"/>
17
+ </Icon>);
18
+ }
19
+ export function StopIcon() {
20
+ return (<Icon>
21
+ <rect x="5" y="5" width="14" height="14" rx="1" fill="currentColor"/>
22
+ </Icon>);
23
+ }
24
+ export function ResetIcon() {
25
+ return (<Icon>
26
+ <path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/>
27
+ <path d="M3 3v5h5"/>
28
+ </Icon>);
29
+ }
30
+ export function CopyIcon() {
31
+ return (<Icon>
32
+ <rect x="8" y="8" width="14" height="14" rx="2"/>
33
+ <path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/>
34
+ </Icon>);
35
+ }
36
+ export function CheckIcon() {
37
+ return (<Icon>
38
+ <path d="M20 6L9 17l-5-5"/>
39
+ </Icon>);
40
+ }
41
+ export function CrossIcon() {
42
+ return (<Icon>
43
+ <path d="M18 6L6 18M6 6l12 12"/>
44
+ </Icon>);
45
+ }
@@ -0,0 +1,28 @@
1
+ import { numpy as np } from '@jax-js/jax';
2
+ /**
3
+ * How to walk a graph whose items carry their own inputs.
4
+ * `inputs` gives undefined for an item that wasn't computed from others.
5
+ */
6
+ export interface Graph<T> {
7
+ id: (item: T) => string;
8
+ inputs: (item: T) => T[] | undefined;
9
+ /** Computes `item` again from new inputs. */
10
+ recompute: (item: T, inputs: T[]) => T;
11
+ }
12
+ /**
13
+ * `target` computed again, with the item whose id is `varId` swapped for
14
+ * `replacement`. Items that don't read that item, directly or through others,
15
+ * are returned as they are and not recomputed.
16
+ */
17
+ export declare function replay<T>(graph: Graph<T>, target: T, varId: string, replacement: T): T;
18
+ /**
19
+ * df/dxᵢ for each xᵢ in `xs`.
20
+ *
21
+ * jax-js frees an array once it is used. This uses up `xs` and whatever `f`
22
+ * returns, so pass `.ref` copies of values you keep.
23
+ *
24
+ * Nesting works: `f` may itself call `gradient`. Any value the inner `f` reads
25
+ * from an outer one must be captured by `f`, not passed in as an extra argument —
26
+ * jax-js drops the gradient through arguments it is not differentiating.
27
+ */
28
+ export declare function gradient(f: (...xs: np.Array[]) => np.Array, xs: np.Array[]): np.Array[];
@@ -0,0 +1,39 @@
1
+ import { grad } from '@jax-js/jax';
2
+ /**
3
+ * `target` computed again, with the item whose id is `varId` swapped for
4
+ * `replacement`. Items that don't read that item, directly or through others,
5
+ * are returned as they are and not recomputed.
6
+ */
7
+ export function replay(graph, target, varId, replacement) {
8
+ const done = new Map();
9
+ const visit = (item) => {
10
+ const id = graph.id(item);
11
+ if (id === varId)
12
+ return replacement;
13
+ if (done.has(id))
14
+ return done.get(id);
15
+ const inputs = graph.inputs(item);
16
+ let result = item;
17
+ if (inputs) {
18
+ const newInputs = inputs.map(visit);
19
+ if (newInputs.some((input, i) => input !== inputs[i]))
20
+ result = graph.recompute(item, newInputs);
21
+ }
22
+ done.set(id, result);
23
+ return result;
24
+ };
25
+ return visit(target);
26
+ }
27
+ /**
28
+ * df/dxᵢ for each xᵢ in `xs`.
29
+ *
30
+ * jax-js frees an array once it is used. This uses up `xs` and whatever `f`
31
+ * returns, so pass `.ref` copies of values you keep.
32
+ *
33
+ * Nesting works: `f` may itself call `gradient`. Any value the inner `f` reads
34
+ * from an outer one must be captured by `f`, not passed in as an extra argument —
35
+ * jax-js drops the gradient through arguments it is not differentiating.
36
+ */
37
+ export function gradient(f, xs) {
38
+ return grad(f, { argnums: xs.map((_, i) => i) })(...xs);
39
+ }
@@ -0,0 +1,19 @@
1
+ /** A file of a site's or a host's own code. `exports`: what `import * as m from '...'` gives. `file` is for error messages. */
2
+ export interface CustomModule {
3
+ file: string;
4
+ exports: Record<string, unknown>;
5
+ }
6
+ /** Own code, by kind: `Node2NodeFunc`s and `SideEffectFunc`s, `NodeDefinition`s, and UIs (PascalCase components). */
7
+ export interface CustomModules {
8
+ functions?: CustomModule[];
9
+ nodes?: CustomModule[];
10
+ ui?: CustomModule[];
11
+ }
12
+ /** Replaces the custom functions, node types and UIs. Built-ins always stay. Throws on a name used twice, leaving none. */
13
+ export declare function setCustomModules(modules: CustomModules): void;
14
+ /**
15
+ * Sets `modules` until the returned function is called. Other holders can use the same modules meanwhile;
16
+ * different ones throw `conflict`. The last holder to let go clears them.
17
+ * Throws on a name used twice, as `setCustomModules` does.
18
+ */
19
+ export declare function holdCustomModules(modules: CustomModules, conflict: string): () => void;
@@ -0,0 +1,52 @@
1
+ import { setCustomFunctions } from './nodes/functions/registry';
2
+ import { setCustomNodes } from './nodes/state/Node';
3
+ import { setCustomUis } from './ui/registry';
4
+ /** Replaces the custom functions, node types and UIs. Built-ins always stay. Throws on a name used twice, leaving none. */
5
+ export function setCustomModules(modules) {
6
+ try {
7
+ setCustomFunctions(modules.functions ?? []);
8
+ setCustomNodes(modules.nodes ?? []);
9
+ setCustomUis(modules.ui ?? []);
10
+ }
11
+ catch (e) {
12
+ setCustomFunctions([]);
13
+ setCustomNodes([]);
14
+ setCustomUis([]);
15
+ throw e;
16
+ }
17
+ }
18
+ const KINDS = ['functions', 'nodes', 'ui'];
19
+ /** Same files with the same exports. A host may build a new `modules` object each render, so the objects can differ. */
20
+ const sameModules = (a, b) => KINDS.every((kind) => {
21
+ const x = a[kind] ?? [];
22
+ const y = b[kind] ?? [];
23
+ return x.length === y.length && x.every((m, i) => m.file === y[i].file && m.exports === y[i].exports);
24
+ });
25
+ // The modules set by `holdCustomModules`, and how many holders use them.
26
+ let held;
27
+ /**
28
+ * Sets `modules` until the returned function is called. Other holders can use the same modules meanwhile;
29
+ * different ones throw `conflict`. The last holder to let go clears them.
30
+ * Throws on a name used twice, as `setCustomModules` does.
31
+ */
32
+ export function holdCustomModules(modules, conflict) {
33
+ if (held) {
34
+ if (!sameModules(held.modules, modules))
35
+ throw new Error(conflict);
36
+ held.count++;
37
+ }
38
+ else {
39
+ setCustomModules(modules);
40
+ held = { modules, count: 1 };
41
+ }
42
+ let released = false;
43
+ return () => {
44
+ if (released)
45
+ return;
46
+ released = true;
47
+ if (--held.count === 0) {
48
+ held = undefined;
49
+ setCustomModules({});
50
+ }
51
+ };
52
+ }
@@ -0,0 +1,27 @@
1
+ import { type FeatureData, type FeatureValues } from './features/build';
2
+ import { type Frontmatter } from './frontmatter';
3
+ /** One page as HTML, with its frontmatter and its features' data. */
4
+ export interface Page {
5
+ frontmatter: Frontmatter;
6
+ html: string;
7
+ features: FeatureData;
8
+ }
9
+ /**
10
+ * Compiles the markdown of the page `file`. Throws, naming the file, on bad frontmatter or a feature's bad value.
11
+ * `site`: each feature's value for a page that leaves its key out.
12
+ * `readFile(from, path)`: the markdown, without its frontmatter, of the file at `path`, set in `from`.
13
+ */
14
+ export declare function compilePage(file: string, markdown: string, site: Partial<FeatureValues>, readFile: (from: string, path: string) => string, tagLinks: boolean): Page;
15
+ export interface CompileOptions {
16
+ /** The page's name in error messages. Default: `page.md`. */
17
+ file?: string;
18
+ /** What a site's `site.yml` gives: each feature's value for a page that leaves its key out. */
19
+ site?: Partial<FeatureValues>;
20
+ /** The text of another `.md` file, e.g. `_topics.md` for `sidebar: _topics.md`. `path` is as the page wrote it. */
21
+ readFile?(path: string): string;
22
+ }
23
+ /**
24
+ * Compiles pie markdown into a `Page` for `PiePage`, as a site build would, in the browser or on a server.
25
+ * Throws with the messages a site build gives. Tags are plain text: there are no tag pages to link to.
26
+ */
27
+ export declare function compilePie(markdown: string, options?: CompileOptions): Page;
@@ -0,0 +1,26 @@
1
+ import { buildFeatures } from './features/build';
2
+ import { parseFrontmatter } from './frontmatter';
3
+ import { renderMarkdown } from './render';
4
+ /**
5
+ * Compiles the markdown of the page `file`. Throws, naming the file, on bad frontmatter or a feature's bad value.
6
+ * `site`: each feature's value for a page that leaves its key out.
7
+ * `readFile(from, path)`: the markdown, without its frontmatter, of the file at `path`, set in `from`.
8
+ */
9
+ export function compilePage(file, markdown, site, readFile, tagLinks) {
10
+ const { frontmatter, raw, body } = parseFrontmatter(file, markdown);
11
+ const { data, extensions } = buildFeatures(file, raw, site, readFile, tagLinks);
12
+ return { frontmatter, html: renderMarkdown(body, extensions), features: data };
13
+ }
14
+ /**
15
+ * Compiles pie markdown into a `Page` for `PiePage`, as a site build would, in the browser or on a server.
16
+ * Throws with the messages a site build gives. Tags are plain text: there are no tag pages to link to.
17
+ */
18
+ export function compilePie(markdown, options = {}) {
19
+ const { file = 'page.md', site = {}, readFile } = options;
20
+ const read = (_from, path) => {
21
+ if (!readFile)
22
+ throw new Error(`file ${path} does not exist`);
23
+ return parseFrontmatter(path, readFile(path)).body;
24
+ };
25
+ return compilePage(file, markdown, site, read, false);
26
+ }
@@ -0,0 +1,9 @@
1
+ import type { CanvasSide } from '../frontmatter';
2
+ /**
3
+ * A canvas on `side` of the page's text. `python-editor` blocks in the text draw on it.
4
+ * `none`: the canvas starts collapsed, on the right.
5
+ */
6
+ export declare function CanvasPage(props: {
7
+ side: CanvasSide;
8
+ html: string;
9
+ }): import("solid-js").JSX.Element;
@@ -0,0 +1,16 @@
1
+ import { SplitPane } from '../../../components/SplitPane';
2
+ import { NodeCanvas } from '../../nodes/components/NodeCanvas';
3
+ import { NodeStore } from '../../nodes/state/NodeStore';
4
+ import { UiLayer } from '../../ui/UiLayer';
5
+ import { MarkdownBody } from './MarkdownBody';
6
+ /**
7
+ * A canvas on `side` of the page's text. `python-editor` blocks in the text draw on it.
8
+ * `none`: the canvas starts collapsed, on the right.
9
+ */
10
+ export function CanvasPage(props) {
11
+ const store = props.side === 'none' ? new NodeStore('right', 'first') : new NodeStore(props.side, 'none');
12
+ return (<SplitPane class="canvas-page" side={store.canvasSide()} collapsed={store.collapsedPane()} setCollapsed={store.setCollapsedPane} first={<div class="canvas-page-canvas">
13
+ <NodeCanvas store={store}/>
14
+ <UiLayer />
15
+ </div>} second={<MarkdownBody class="canvas-page-text" html={props.html} store={store}/>} firstLabel="canvas" secondLabel="text"/>);
16
+ }
@@ -0,0 +1,11 @@
1
+ import type { NodeStore } from '../../nodes/state/NodeStore';
2
+ /**
3
+ * Rendered markdown. On the client, each ```python-editor block becomes a runnable editor;
4
+ * until then it shows as a plain code block. All editors on the page share Python variables.
5
+ * `store`: the canvas their `pie.*` calls draw on.
6
+ */
7
+ export declare function MarkdownBody(props: {
8
+ html: string;
9
+ store?: NodeStore;
10
+ class?: string;
11
+ }): import("solid-js").JSX.Element;
@@ -0,0 +1,37 @@
1
+ import { onCleanup, onMount } from 'solid-js';
2
+ import { render } from 'solid-js/web';
3
+ import { PythonScope } from '../../python/runner';
4
+ const PYTHON_EDITOR_LANG = 'python-editor';
5
+ /**
6
+ * Rendered markdown. On the client, each ```python-editor block becomes a runnable editor;
7
+ * until then it shows as a plain code block. All editors on the page share Python variables.
8
+ * `store`: the canvas their `pie.*` calls draw on.
9
+ */
10
+ export function MarkdownBody(props) {
11
+ let ref;
12
+ onMount(() => {
13
+ const blocks = ref.querySelectorAll(`pre > code.language-${PYTHON_EDITOR_LANG}`);
14
+ if (blocks.length === 0)
15
+ return;
16
+ const scope = new PythonScope(props.store);
17
+ const disposers = [];
18
+ onCleanup(() => {
19
+ disposers.forEach((dispose) => dispose());
20
+ scope.dispose();
21
+ });
22
+ // Loaded only on pages that have an editor.
23
+ import('./PythonEditor').then(({ PythonEditor }) => {
24
+ blocks.forEach((codeEl) => {
25
+ const pre = codeEl.parentElement;
26
+ if (!pre.isConnected)
27
+ return;
28
+ const code = codeEl.textContent ?? '';
29
+ const collapsed = codeEl.hasAttribute('data-collapsed');
30
+ const host = document.createElement('div');
31
+ pre.replaceWith(host);
32
+ disposers.push(render(() => <PythonEditor code={code} collapsed={collapsed} scope={scope}/>, host));
33
+ });
34
+ });
35
+ });
36
+ return <main ref={ref} class={props.class} innerHTML={props.html}/>;
37
+ }
@@ -0,0 +1,14 @@
1
+ import 'katex/dist/katex.min.css';
2
+ import { type CustomModules } from '../../customModules';
3
+ import type { Page } from '../compile';
4
+ /**
5
+ * A page from `compilePie`, shown as a pie site shows it, in a `.pie-page` box that fills its parent.
6
+ * `modules`: the host's own functions, node types and UIs. `css`: author CSS, limited to the page.
7
+ * A new `page`, `modules` or `css` starts a fresh canvas and fresh Python variables.
8
+ * Throws on a name used twice in `modules`, when another PiePage is mounted, or when a running `runPage` has other `modules`.
9
+ */
10
+ export declare function PiePage(props: {
11
+ page: Page;
12
+ modules?: CustomModules;
13
+ css?: string;
14
+ }): import("solid-js").JSX.Element;
@@ -0,0 +1,42 @@
1
+ import { createMemo, onCleanup, Show } from 'solid-js';
2
+ import { isServer } from 'solid-js/web';
3
+ import 'katex/dist/katex.min.css';
4
+ import { holdCustomModules } from '../../customModules';
5
+ import { wrapPage } from '../features/views';
6
+ import { CanvasPage } from './CanvasPage';
7
+ // Custom modules are global, so only one PiePage can use them at a time.
8
+ let mounted = false;
9
+ /** Sets `modules` until the calling component is cleaned up. */
10
+ function useModules(modules) {
11
+ if (mounted)
12
+ throw new Error('Only one PiePage can be mounted at a time');
13
+ // A `runPage` running at the same time may hold the same modules.
14
+ const release = holdCustomModules(modules, "PiePage: modules differ from a running runPage's");
15
+ mounted = true;
16
+ onCleanup(() => {
17
+ release();
18
+ mounted = false;
19
+ });
20
+ }
21
+ /**
22
+ * A page from `compilePie`, shown as a pie site shows it, in a `.pie-page` box that fills its parent.
23
+ * `modules`: the host's own functions, node types and UIs. `css`: author CSS, limited to the page.
24
+ * A new `page`, `modules` or `css` starts a fresh canvas and fresh Python variables.
25
+ * Throws on a name used twice in `modules`, when another PiePage is mounted, or when a running `runPage` has other `modules`.
26
+ */
27
+ export function PiePage(props) {
28
+ const current = createMemo(() => ({ page: props.page, modules: props.modules, css: props.css }));
29
+ return (<div class="pie-page">
30
+ <Show when={current()} keyed>
31
+ {({ page, modules, css }) => {
32
+ // Only in the browser: the server draws no nodes and runs no Python, and every request would share its modules.
33
+ if (!isServer)
34
+ useModules(modules ?? {});
35
+ return (<>
36
+ {css && <style>{`.pie-page {\n${css}\n}`}</style>}
37
+ {wrapPage(page.features, <CanvasPage side={page.frontmatter.canvas} html={page.html}/>)}
38
+ </>);
39
+ }}
40
+ </Show>
41
+ </div>);
42
+ }
@@ -0,0 +1,10 @@
1
+ import type { PythonScope } from '../../python/runner';
2
+ /**
3
+ * An editable block of Python. Run draws its `pie.*` calls on the scope's canvas.
4
+ * `collapsed`: start with only the toolbar showing.
5
+ */
6
+ export declare function PythonEditor(props: {
7
+ code: string;
8
+ collapsed?: boolean;
9
+ scope: PythonScope;
10
+ }): import("solid-js").JSX.Element;