@tmke8/pikru 0.1.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.
- package/README.md +67 -0
- package/dist/package.json +4 -0
- package/dist/pikru_wasm.d.ts +20 -0
- package/dist/pikru_wasm.js +16 -0
- package/dist/pikru_wasm_bg.js +172 -0
- package/dist/pikru_wasm_bg.wasm +0 -0
- package/dist/pikru_wasm_bg.wasm.d.ts +13 -0
- package/package.json +32 -0
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# pikru-node
|
|
2
|
+
|
|
3
|
+
WASM bindings for [pikru](https://crates.io/crates/pikru), a Rust library that renders [Pikchr](https://pikchr.org/) diagrams to SVG.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @tmke8/pikru
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
import { Pikru } from '@tmke8/pikru';
|
|
15
|
+
|
|
16
|
+
const pikru = new Pikru();
|
|
17
|
+
const svg = pikru.render('box "Hello" arrow box "World"');
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Options
|
|
21
|
+
|
|
22
|
+
The `Pikru` constructor accepts an optional options object:
|
|
23
|
+
|
|
24
|
+
```javascript
|
|
25
|
+
const pikru = new Pikru({ cssVariables: true });
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
| Option | Type | Default | Description |
|
|
29
|
+
|--------|------|---------|-------------|
|
|
30
|
+
| `cssVariables` | `boolean` | `false` | Use CSS variables for colors, enabling light/dark mode support |
|
|
31
|
+
|
|
32
|
+
## Development
|
|
33
|
+
|
|
34
|
+
### Building
|
|
35
|
+
|
|
36
|
+
Build the Node.js package:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
cd node
|
|
40
|
+
npm install
|
|
41
|
+
npm run build
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Build for web (requires [wasm-pack](https://rustwasm.github.io/wasm-pack/installer/)):
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
cd pikru-wasm
|
|
48
|
+
wasm-pack build --target web --out-dir ../playground/pkg
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Testing
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
cd node
|
|
55
|
+
npm test
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Playground
|
|
59
|
+
|
|
60
|
+
A simple test page is included in the `playground/` directory:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
cd playground
|
|
64
|
+
python3 -m http.server 8000
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Then open http://localhost:8000 in your browser.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
interface RenderOptions {
|
|
5
|
+
cssVariables?: boolean;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
export class Pikru {
|
|
11
|
+
free(): void;
|
|
12
|
+
[Symbol.dispose](): void;
|
|
13
|
+
constructor(options?: RenderOptions | null);
|
|
14
|
+
/**
|
|
15
|
+
* Render pikchr markup to SVG.
|
|
16
|
+
*
|
|
17
|
+
* Returns the SVG string on success, or an error message on failure.
|
|
18
|
+
*/
|
|
19
|
+
render(source: string): string;
|
|
20
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
|
|
2
|
+
let imports = {};
|
|
3
|
+
import * as import0 from './pikru_wasm_bg.js';
|
|
4
|
+
imports['./pikru_wasm_bg.js'] = import0;
|
|
5
|
+
|
|
6
|
+
import { readFileSync } from 'node:fs';
|
|
7
|
+
|
|
8
|
+
const wasmUrl = new URL('pikru_wasm_bg.wasm', import.meta.url);
|
|
9
|
+
const wasmBytes = readFileSync(wasmUrl);
|
|
10
|
+
const wasmModule = new WebAssembly.Module(wasmBytes);
|
|
11
|
+
const wasm = new WebAssembly.Instance(wasmModule, imports).exports;
|
|
12
|
+
export { wasm as __wasm };
|
|
13
|
+
imports["./pikru_wasm_bg.js"].__wbg_set_wasm(wasm, wasmModule);
|
|
14
|
+
wasm.__wbindgen_start();
|
|
15
|
+
|
|
16
|
+
export * from "./pikru_wasm_bg.js";
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
let wasm;
|
|
2
|
+
let wasmModule;
|
|
3
|
+
export function __wbg_set_wasm(exports, module) {
|
|
4
|
+
wasm = exports;
|
|
5
|
+
wasmModule = module;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function addToExternrefTable0(obj) {
|
|
9
|
+
const idx = wasm.__externref_table_alloc();
|
|
10
|
+
wasm.__wbindgen_externrefs.set(idx, obj);
|
|
11
|
+
return idx;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function getStringFromWasm0(ptr, len) {
|
|
15
|
+
ptr = ptr >>> 0;
|
|
16
|
+
return decodeText(ptr, len);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
let cachedUint8ArrayMemory0 = null;
|
|
20
|
+
function getUint8ArrayMemory0() {
|
|
21
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
22
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
23
|
+
}
|
|
24
|
+
return cachedUint8ArrayMemory0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function isLikeNone(x) {
|
|
28
|
+
return x === undefined || x === null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
32
|
+
if (realloc === undefined) {
|
|
33
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
34
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
35
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
36
|
+
WASM_VECTOR_LEN = buf.length;
|
|
37
|
+
return ptr;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let len = arg.length;
|
|
41
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
42
|
+
|
|
43
|
+
const mem = getUint8ArrayMemory0();
|
|
44
|
+
|
|
45
|
+
let offset = 0;
|
|
46
|
+
|
|
47
|
+
for (; offset < len; offset++) {
|
|
48
|
+
const code = arg.charCodeAt(offset);
|
|
49
|
+
if (code > 0x7F) break;
|
|
50
|
+
mem[ptr + offset] = code;
|
|
51
|
+
}
|
|
52
|
+
if (offset !== len) {
|
|
53
|
+
if (offset !== 0) {
|
|
54
|
+
arg = arg.slice(offset);
|
|
55
|
+
}
|
|
56
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
57
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
58
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
59
|
+
|
|
60
|
+
offset += ret.written;
|
|
61
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
WASM_VECTOR_LEN = offset;
|
|
65
|
+
return ptr;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function takeFromExternrefTable0(idx) {
|
|
69
|
+
const value = wasm.__wbindgen_externrefs.get(idx);
|
|
70
|
+
wasm.__externref_table_dealloc(idx);
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
75
|
+
cachedTextDecoder.decode();
|
|
76
|
+
function decodeText(ptr, len) {
|
|
77
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const cachedTextEncoder = new TextEncoder();
|
|
81
|
+
|
|
82
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
83
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
84
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
85
|
+
view.set(buf);
|
|
86
|
+
return {
|
|
87
|
+
read: arg.length,
|
|
88
|
+
written: buf.length
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
let WASM_VECTOR_LEN = 0;
|
|
94
|
+
|
|
95
|
+
const PikruFinalization = (typeof FinalizationRegistry === 'undefined')
|
|
96
|
+
? { register: () => {}, unregister: () => {} }
|
|
97
|
+
: new FinalizationRegistry(ptr => wasm.__wbg_pikru_free(ptr >>> 0, 1));
|
|
98
|
+
|
|
99
|
+
export class Pikru {
|
|
100
|
+
__destroy_into_raw() {
|
|
101
|
+
const ptr = this.__wbg_ptr;
|
|
102
|
+
this.__wbg_ptr = 0;
|
|
103
|
+
PikruFinalization.unregister(this);
|
|
104
|
+
return ptr;
|
|
105
|
+
}
|
|
106
|
+
free() {
|
|
107
|
+
const ptr = this.__destroy_into_raw();
|
|
108
|
+
wasm.__wbg_pikru_free(ptr, 0);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* @param {RenderOptions | null} [options]
|
|
112
|
+
*/
|
|
113
|
+
constructor(options) {
|
|
114
|
+
const ret = wasm.pikru_new(isLikeNone(options) ? 0 : addToExternrefTable0(options));
|
|
115
|
+
this.__wbg_ptr = ret >>> 0;
|
|
116
|
+
PikruFinalization.register(this, this.__wbg_ptr, this);
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Render pikchr markup to SVG.
|
|
121
|
+
*
|
|
122
|
+
* Returns the SVG string on success, or an error message on failure.
|
|
123
|
+
* @param {string} source
|
|
124
|
+
* @returns {string}
|
|
125
|
+
*/
|
|
126
|
+
render(source) {
|
|
127
|
+
let deferred3_0;
|
|
128
|
+
let deferred3_1;
|
|
129
|
+
try {
|
|
130
|
+
const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
131
|
+
const len0 = WASM_VECTOR_LEN;
|
|
132
|
+
const ret = wasm.pikru_render(this.__wbg_ptr, ptr0, len0);
|
|
133
|
+
var ptr2 = ret[0];
|
|
134
|
+
var len2 = ret[1];
|
|
135
|
+
if (ret[3]) {
|
|
136
|
+
ptr2 = 0; len2 = 0;
|
|
137
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
138
|
+
}
|
|
139
|
+
deferred3_0 = ptr2;
|
|
140
|
+
deferred3_1 = len2;
|
|
141
|
+
return getStringFromWasm0(ptr2, len2);
|
|
142
|
+
} finally {
|
|
143
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (Symbol.dispose) Pikru.prototype[Symbol.dispose] = Pikru.prototype.free;
|
|
148
|
+
|
|
149
|
+
export function __wbg___wbindgen_throw_dd24417ed36fc46e(arg0, arg1) {
|
|
150
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
export function __wbg_cssVariables_ea828b89a9330cef(arg0) {
|
|
154
|
+
const ret = arg0.cssVariables;
|
|
155
|
+
return isLikeNone(ret) ? 0xFFFFFF : ret ? 1 : 0;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
export function __wbindgen_cast_2241b6af4c4b2941(arg0, arg1) {
|
|
159
|
+
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
160
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
161
|
+
return ret;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
export function __wbindgen_init_externref_table() {
|
|
165
|
+
const table = wasm.__wbindgen_externrefs;
|
|
166
|
+
const offset = table.grow(4);
|
|
167
|
+
table.set(0, undefined);
|
|
168
|
+
table.set(offset + 0, undefined);
|
|
169
|
+
table.set(offset + 1, null);
|
|
170
|
+
table.set(offset + 2, true);
|
|
171
|
+
table.set(offset + 3, false);
|
|
172
|
+
};
|
|
Binary file
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
export const memory: WebAssembly.Memory;
|
|
4
|
+
export const __wbg_pikru_free: (a: number, b: number) => void;
|
|
5
|
+
export const pikru_new: (a: number) => number;
|
|
6
|
+
export const pikru_render: (a: number, b: number, c: number) => [number, number, number, number];
|
|
7
|
+
export const __wbindgen_externrefs: WebAssembly.Table;
|
|
8
|
+
export const __externref_table_alloc: () => number;
|
|
9
|
+
export const __wbindgen_malloc: (a: number, b: number) => number;
|
|
10
|
+
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
11
|
+
export const __externref_table_dealloc: (a: number) => void;
|
|
12
|
+
export const __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
13
|
+
export const __wbindgen_start: () => void;
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tmke8/pikru",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "WASM bindings for pikru - renders Pikchr diagrams to SVG",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/pikru_wasm.js",
|
|
7
|
+
"types": "dist/pikru_wasm.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "npm run build:cargo && npm run build:wasm-bindgen && cp ../README.md .",
|
|
13
|
+
"build:cargo": "cargo build --release --target wasm32-unknown-unknown -p pikru-wasm",
|
|
14
|
+
"build:wasm-bindgen": "wasm-bindgen ../target/wasm32-unknown-unknown/release/pikru_wasm.wasm --out-dir ./dist --target experimental-nodejs-module",
|
|
15
|
+
"test": "mocha"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"chai": "^5.1.0",
|
|
19
|
+
"mocha": "^11.0.0"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"pikchr",
|
|
23
|
+
"svg",
|
|
24
|
+
"diagram",
|
|
25
|
+
"wasm"
|
|
26
|
+
],
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "https://github.com/tmke8/pikru-node.git"
|
|
31
|
+
}
|
|
32
|
+
}
|