zig-pug 0.3.3 → 0.3.5
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 +12 -12
- package/binding.c +216 -0
- package/binding.gyp +11 -1
- package/index.js +70 -8
- package/index.mjs +70 -8
- package/install.js +14 -2
- package/package.json +5 -3
- package/prebuilt-binaries/linux-x64/zigpug.node +0 -0
- package/prebuilts/darwin-arm64/libzig-pug.a +0 -0
- package/prebuilts/darwin-x64/libzig-pug.a +0 -0
- package/prebuilts/linux-arm64/libzig-pug.a +0 -0
- package/prebuilts/linux-x64/libzig-pug.a +0 -0
- package/prebuilts/win32-x64/zig-pug.lib +0 -0
package/README.md
CHANGED
|
@@ -295,7 +295,7 @@ The addon compiles on Termux but cannot be loaded due to Android namespace restr
|
|
|
295
295
|
pkg install zig
|
|
296
296
|
|
|
297
297
|
# Clone and build
|
|
298
|
-
git clone https://github.com/
|
|
298
|
+
git clone https://github.com/carlos-sweb/zig-pug
|
|
299
299
|
cd zig-pug
|
|
300
300
|
zig build
|
|
301
301
|
|
|
@@ -303,7 +303,7 @@ zig build
|
|
|
303
303
|
./zig-out/bin/zig-pug template.pug
|
|
304
304
|
```
|
|
305
305
|
|
|
306
|
-
See [docs/TERMUX.md](https://github.com/
|
|
306
|
+
See [docs/TERMUX.md](https://github.com/carlos-sweb/zig-pug/blob/main/docs/TERMUX.md) for details.
|
|
307
307
|
|
|
308
308
|
## Performance
|
|
309
309
|
|
|
@@ -330,7 +330,7 @@ console.log(`${iterations} in ${elapsed}ms`);
|
|
|
330
330
|
|
|
331
331
|
## Examples
|
|
332
332
|
|
|
333
|
-
See the [examples](https://github.com/
|
|
333
|
+
See the [examples](https://github.com/carlos-sweb/zig-pug/tree/main/examples) directory:
|
|
334
334
|
|
|
335
335
|
- **Node.js**: `examples/nodejs/`
|
|
336
336
|
- **Bun.js**: `examples/bun/`
|
|
@@ -338,10 +338,10 @@ See the [examples](https://github.com/yourusername/zig-pug/tree/main/examples) d
|
|
|
338
338
|
|
|
339
339
|
## Documentation
|
|
340
340
|
|
|
341
|
-
- **[Getting Started](https://github.com/
|
|
342
|
-
- **[Node.js Integration](https://github.com/
|
|
343
|
-
- **[Pug Syntax Reference](https://github.com/
|
|
344
|
-
- **[API Reference](https://github.com/
|
|
341
|
+
- **[Getting Started](https://github.com/carlos-sweb/zig-pug/blob/main/docs/GETTING-STARTED.md)**
|
|
342
|
+
- **[Node.js Integration](https://github.com/carlos-sweb/zig-pug/blob/main/docs/NODEJS-INTEGRATION.md)**
|
|
343
|
+
- **[Pug Syntax Reference](https://github.com/carlos-sweb/zig-pug/blob/main/docs/PUG-SYNTAX.md)**
|
|
344
|
+
- **[API Reference](https://github.com/carlos-sweb/zig-pug/blob/main/docs/API-REFERENCE.md)**
|
|
345
345
|
|
|
346
346
|
## Troubleshooting
|
|
347
347
|
|
|
@@ -375,7 +375,7 @@ npm run build
|
|
|
375
375
|
|
|
376
376
|
### Compilation errors
|
|
377
377
|
|
|
378
|
-
If you encounter compilation errors, please [open an issue](https://github.com/
|
|
378
|
+
If you encounter compilation errors, please [open an issue](https://github.com/carlos-sweb/zig-pug/issues) with:
|
|
379
379
|
- Your OS and version
|
|
380
380
|
- Node.js version (`node --version`)
|
|
381
381
|
- Complete error output
|
|
@@ -392,7 +392,7 @@ Contributions are welcome! Please:
|
|
|
392
392
|
|
|
393
393
|
## License
|
|
394
394
|
|
|
395
|
-
MIT License - see [LICENSE](https://github.com/
|
|
395
|
+
MIT License - see [LICENSE](https://github.com/carlos-sweb/zig-pug/blob/main/LICENSE) for details.
|
|
396
396
|
|
|
397
397
|
## Credits
|
|
398
398
|
|
|
@@ -403,10 +403,10 @@ MIT License - see [LICENSE](https://github.com/yourusername/zig-pug/blob/main/LI
|
|
|
403
403
|
|
|
404
404
|
## Links
|
|
405
405
|
|
|
406
|
-
- **GitHub**: https://github.com/
|
|
406
|
+
- **GitHub**: https://github.com/carlos-sweb/zig-pug
|
|
407
407
|
- **npm**: https://www.npmjs.com/package/zig-pug
|
|
408
|
-
- **Issues**: https://github.com/
|
|
409
|
-
- **Documentation**: https://github.com/
|
|
408
|
+
- **Issues**: https://github.com/carlos-sweb/zig-pug/issues
|
|
409
|
+
- **Documentation**: https://github.com/carlos-sweb/zig-pug#readme
|
|
410
410
|
|
|
411
411
|
---
|
|
412
412
|
|
package/binding.c
CHANGED
|
@@ -17,6 +17,8 @@ extern char* zigpug_compile(ZigPugContext* ctx, const char* pug_source);
|
|
|
17
17
|
extern int zigpug_set_string(ZigPugContext* ctx, const char* key, const char* value);
|
|
18
18
|
extern int zigpug_set_int(ZigPugContext* ctx, const char* key, long long value);
|
|
19
19
|
extern int zigpug_set_bool(ZigPugContext* ctx, const char* key, int value);
|
|
20
|
+
extern int zigpug_set_array_json(ZigPugContext* ctx, const char* key, const char* json_str);
|
|
21
|
+
extern int zigpug_set_object_json(ZigPugContext* ctx, const char* key, const char* json_str);
|
|
20
22
|
extern void zigpug_free_string(char* str);
|
|
21
23
|
extern const char* zigpug_version(void);
|
|
22
24
|
|
|
@@ -310,6 +312,208 @@ static napi_value Compile(napi_env env, napi_callback_info info) {
|
|
|
310
312
|
return result;
|
|
311
313
|
}
|
|
312
314
|
|
|
315
|
+
// Set an array variable from JavaScript array
|
|
316
|
+
// JavaScript: zigpug.setArray(ctx, 'items', ['a', 'b', 'c'])
|
|
317
|
+
static napi_value SetArray(napi_env env, napi_callback_info info) {
|
|
318
|
+
napi_status status;
|
|
319
|
+
size_t argc = 3;
|
|
320
|
+
napi_value args[3];
|
|
321
|
+
|
|
322
|
+
status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
|
|
323
|
+
if (status != napi_ok || argc < 3) {
|
|
324
|
+
napi_throw_error(env, NULL, "Expected 3 arguments: context, key, array");
|
|
325
|
+
return NULL;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// Get context
|
|
329
|
+
PugContextWrapper* wrapper;
|
|
330
|
+
status = napi_get_value_external(env, args[0], (void**)&wrapper);
|
|
331
|
+
if (status != napi_ok || !wrapper || !wrapper->ctx) {
|
|
332
|
+
napi_throw_error(env, NULL, "Invalid context");
|
|
333
|
+
return NULL;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// Get key string
|
|
337
|
+
size_t key_len;
|
|
338
|
+
status = napi_get_value_string_utf8(env, args[1], NULL, 0, &key_len);
|
|
339
|
+
if (status != napi_ok) {
|
|
340
|
+
napi_throw_error(env, NULL, "Invalid key");
|
|
341
|
+
return NULL;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
char* key = malloc(key_len + 1);
|
|
345
|
+
status = napi_get_value_string_utf8(env, args[1], key, key_len + 1, &key_len);
|
|
346
|
+
if (status != napi_ok) {
|
|
347
|
+
free(key);
|
|
348
|
+
napi_throw_error(env, NULL, "Failed to get key string");
|
|
349
|
+
return NULL;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// Convert JavaScript array to JSON string
|
|
353
|
+
napi_value json_value;
|
|
354
|
+
napi_value global;
|
|
355
|
+
napi_value json_obj;
|
|
356
|
+
napi_value stringify_fn;
|
|
357
|
+
|
|
358
|
+
status = napi_get_global(env, &global);
|
|
359
|
+
if (status != napi_ok) {
|
|
360
|
+
free(key);
|
|
361
|
+
napi_throw_error(env, NULL, "Failed to get global object");
|
|
362
|
+
return NULL;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
status = napi_get_named_property(env, global, "JSON", &json_obj);
|
|
366
|
+
if (status != napi_ok) {
|
|
367
|
+
free(key);
|
|
368
|
+
napi_throw_error(env, NULL, "Failed to get JSON object");
|
|
369
|
+
return NULL;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
status = napi_get_named_property(env, json_obj, "stringify", &stringify_fn);
|
|
373
|
+
if (status != napi_ok) {
|
|
374
|
+
free(key);
|
|
375
|
+
napi_throw_error(env, NULL, "Failed to get JSON.stringify");
|
|
376
|
+
return NULL;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
napi_value argv[1] = { args[2] };
|
|
380
|
+
status = napi_call_function(env, json_obj, stringify_fn, 1, argv, &json_value);
|
|
381
|
+
if (status != napi_ok) {
|
|
382
|
+
free(key);
|
|
383
|
+
napi_throw_error(env, NULL, "Failed to stringify array");
|
|
384
|
+
return NULL;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// Get JSON string
|
|
388
|
+
size_t json_len;
|
|
389
|
+
status = napi_get_value_string_utf8(env, json_value, NULL, 0, &json_len);
|
|
390
|
+
if (status != napi_ok) {
|
|
391
|
+
free(key);
|
|
392
|
+
napi_throw_error(env, NULL, "Failed to get JSON string length");
|
|
393
|
+
return NULL;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
char* json_str = malloc(json_len + 1);
|
|
397
|
+
status = napi_get_value_string_utf8(env, json_value, json_str, json_len + 1, &json_len);
|
|
398
|
+
if (status != napi_ok) {
|
|
399
|
+
free(key);
|
|
400
|
+
free(json_str);
|
|
401
|
+
napi_throw_error(env, NULL, "Failed to get JSON string");
|
|
402
|
+
return NULL;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// Set in zig-pug
|
|
406
|
+
int result = zigpug_set_array_json(wrapper->ctx, key, json_str);
|
|
407
|
+
|
|
408
|
+
free(key);
|
|
409
|
+
free(json_str);
|
|
410
|
+
|
|
411
|
+
napi_value js_result;
|
|
412
|
+
status = napi_get_boolean(env, result != 0, &js_result);
|
|
413
|
+
return js_result;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// Set an object variable from JavaScript object
|
|
417
|
+
// JavaScript: zigpug.setObject(ctx, 'user', {name: 'Alice', age: 30})
|
|
418
|
+
static napi_value SetObject(napi_env env, napi_callback_info info) {
|
|
419
|
+
napi_status status;
|
|
420
|
+
size_t argc = 3;
|
|
421
|
+
napi_value args[3];
|
|
422
|
+
|
|
423
|
+
status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
|
|
424
|
+
if (status != napi_ok || argc < 3) {
|
|
425
|
+
napi_throw_error(env, NULL, "Expected 3 arguments: context, key, object");
|
|
426
|
+
return NULL;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// Get context
|
|
430
|
+
PugContextWrapper* wrapper;
|
|
431
|
+
status = napi_get_value_external(env, args[0], (void**)&wrapper);
|
|
432
|
+
if (status != napi_ok || !wrapper || !wrapper->ctx) {
|
|
433
|
+
napi_throw_error(env, NULL, "Invalid context");
|
|
434
|
+
return NULL;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// Get key string
|
|
438
|
+
size_t key_len;
|
|
439
|
+
status = napi_get_value_string_utf8(env, args[1], NULL, 0, &key_len);
|
|
440
|
+
if (status != napi_ok) {
|
|
441
|
+
napi_throw_error(env, NULL, "Invalid key");
|
|
442
|
+
return NULL;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
char* key = malloc(key_len + 1);
|
|
446
|
+
status = napi_get_value_string_utf8(env, args[1], key, key_len + 1, &key_len);
|
|
447
|
+
if (status != napi_ok) {
|
|
448
|
+
free(key);
|
|
449
|
+
napi_throw_error(env, NULL, "Failed to get key string");
|
|
450
|
+
return NULL;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
// Convert JavaScript object to JSON string
|
|
454
|
+
napi_value json_value;
|
|
455
|
+
napi_value global;
|
|
456
|
+
napi_value json_obj;
|
|
457
|
+
napi_value stringify_fn;
|
|
458
|
+
|
|
459
|
+
status = napi_get_global(env, &global);
|
|
460
|
+
if (status != napi_ok) {
|
|
461
|
+
free(key);
|
|
462
|
+
napi_throw_error(env, NULL, "Failed to get global object");
|
|
463
|
+
return NULL;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
status = napi_get_named_property(env, global, "JSON", &json_obj);
|
|
467
|
+
if (status != napi_ok) {
|
|
468
|
+
free(key);
|
|
469
|
+
napi_throw_error(env, NULL, "Failed to get JSON object");
|
|
470
|
+
return NULL;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
status = napi_get_named_property(env, json_obj, "stringify", &stringify_fn);
|
|
474
|
+
if (status != napi_ok) {
|
|
475
|
+
free(key);
|
|
476
|
+
napi_throw_error(env, NULL, "Failed to get JSON.stringify");
|
|
477
|
+
return NULL;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
napi_value argv[1] = { args[2] };
|
|
481
|
+
status = napi_call_function(env, json_obj, stringify_fn, 1, argv, &json_value);
|
|
482
|
+
if (status != napi_ok) {
|
|
483
|
+
free(key);
|
|
484
|
+
napi_throw_error(env, NULL, "Failed to stringify object");
|
|
485
|
+
return NULL;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// Get JSON string
|
|
489
|
+
size_t json_len;
|
|
490
|
+
status = napi_get_value_string_utf8(env, json_value, NULL, 0, &json_len);
|
|
491
|
+
if (status != napi_ok) {
|
|
492
|
+
free(key);
|
|
493
|
+
napi_throw_error(env, NULL, "Failed to get JSON string length");
|
|
494
|
+
return NULL;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
char* json_str = malloc(json_len + 1);
|
|
498
|
+
status = napi_get_value_string_utf8(env, json_value, json_str, json_len + 1, &json_len);
|
|
499
|
+
if (status != napi_ok) {
|
|
500
|
+
free(key);
|
|
501
|
+
free(json_str);
|
|
502
|
+
napi_throw_error(env, NULL, "Failed to get JSON string");
|
|
503
|
+
return NULL;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// Set in zig-pug
|
|
507
|
+
int result = zigpug_set_object_json(wrapper->ctx, key, json_str);
|
|
508
|
+
|
|
509
|
+
free(key);
|
|
510
|
+
free(json_str);
|
|
511
|
+
|
|
512
|
+
napi_value js_result;
|
|
513
|
+
status = napi_get_boolean(env, result != 0, &js_result);
|
|
514
|
+
return js_result;
|
|
515
|
+
}
|
|
516
|
+
|
|
313
517
|
// Get zig-pug version
|
|
314
518
|
// JavaScript: const version = zigpug.version()
|
|
315
519
|
static napi_value Version(napi_env env, napi_callback_info info) {
|
|
@@ -357,6 +561,18 @@ static napi_value Init(napi_env env, napi_value exports) {
|
|
|
357
561
|
status = napi_set_named_property(env, exports, "setBool", fn);
|
|
358
562
|
if (status != napi_ok) return NULL;
|
|
359
563
|
|
|
564
|
+
// setArray
|
|
565
|
+
status = napi_create_function(env, NULL, 0, SetArray, NULL, &fn);
|
|
566
|
+
if (status != napi_ok) return NULL;
|
|
567
|
+
status = napi_set_named_property(env, exports, "setArray", fn);
|
|
568
|
+
if (status != napi_ok) return NULL;
|
|
569
|
+
|
|
570
|
+
// setObject
|
|
571
|
+
status = napi_create_function(env, NULL, 0, SetObject, NULL, &fn);
|
|
572
|
+
if (status != napi_ok) return NULL;
|
|
573
|
+
status = napi_set_named_property(env, exports, "setObject", fn);
|
|
574
|
+
if (status != napi_ok) return NULL;
|
|
575
|
+
|
|
360
576
|
// compile
|
|
361
577
|
status = napi_create_function(env, NULL, 0, Compile, NULL, &fn);
|
|
362
578
|
if (status != napi_ok) return NULL;
|
package/binding.gyp
CHANGED
|
@@ -13,7 +13,17 @@
|
|
|
13
13
|
"libraries": [
|
|
14
14
|
"<(module_root_dir)/prebuilts/win32-<(target_arch)/zig-pug.lib"
|
|
15
15
|
]
|
|
16
|
-
},
|
|
16
|
+
}],
|
|
17
|
+
["OS=='android'", {
|
|
18
|
+
"libraries": [
|
|
19
|
+
"-lm",
|
|
20
|
+
"<(module_root_dir)/prebuilts/linux-<(target_arch)/libzig-pug.a"
|
|
21
|
+
],
|
|
22
|
+
"cflags": [
|
|
23
|
+
"-std=c99"
|
|
24
|
+
]
|
|
25
|
+
}],
|
|
26
|
+
["OS!='win' and OS!='android'", {
|
|
17
27
|
"libraries": [
|
|
18
28
|
"-lm",
|
|
19
29
|
"<(module_root_dir)/prebuilts/<(OS)-<(target_arch)/libzig-pug.a"
|
package/index.js
CHANGED
|
@@ -5,13 +5,31 @@
|
|
|
5
5
|
|
|
6
6
|
const fs = require('fs');
|
|
7
7
|
const path = require('path');
|
|
8
|
+
const os = require('os');
|
|
8
9
|
|
|
9
|
-
//
|
|
10
|
-
const
|
|
11
|
-
|
|
10
|
+
// Detect platform and architecture
|
|
11
|
+
const platform = os.platform(); // 'linux', 'darwin', 'win32'
|
|
12
|
+
const arch = os.arch(); // 'x64', 'arm64'
|
|
13
|
+
const platformKey = `${platform}-${arch}`;
|
|
14
|
+
|
|
15
|
+
// Try to load prebuilt binary first
|
|
16
|
+
const prebuiltPath = path.join(__dirname, 'prebuilt-binaries', platformKey, 'zigpug.node');
|
|
17
|
+
const builtPath = path.join(__dirname, 'build', 'Release', 'zigpug.node');
|
|
18
|
+
|
|
19
|
+
let binding;
|
|
20
|
+
|
|
21
|
+
if (fs.existsSync(prebuiltPath)) {
|
|
22
|
+
// Use prebuilt binary
|
|
23
|
+
binding = require(prebuiltPath);
|
|
24
|
+
} else if (fs.existsSync(builtPath)) {
|
|
25
|
+
// Use locally built binary
|
|
26
|
+
binding = require(builtPath);
|
|
27
|
+
} else {
|
|
12
28
|
console.error('');
|
|
13
29
|
console.error('zig-pug native addon not found!');
|
|
14
30
|
console.error('');
|
|
31
|
+
console.error(`Platform: ${platformKey}`);
|
|
32
|
+
console.error('');
|
|
15
33
|
console.error('The native addon needs to be built. Please run:');
|
|
16
34
|
console.error('');
|
|
17
35
|
console.error(' cd node_modules/zig-pug && npm run install');
|
|
@@ -20,11 +38,9 @@ if (!fs.existsSync(addonPath)) {
|
|
|
20
38
|
console.error('');
|
|
21
39
|
console.error(' cd node_modules/zig-pug && bun run install');
|
|
22
40
|
console.error('');
|
|
23
|
-
throw new Error('zig-pug native addon not
|
|
41
|
+
throw new Error('zig-pug native addon not found. See instructions above.');
|
|
24
42
|
}
|
|
25
43
|
|
|
26
|
-
const binding = require('./build/Release/zigpug.node');
|
|
27
|
-
|
|
28
44
|
/**
|
|
29
45
|
* PugCompiler class - High-level API for compiling Pug templates
|
|
30
46
|
*/
|
|
@@ -99,14 +115,60 @@ class PugCompiler {
|
|
|
99
115
|
return this;
|
|
100
116
|
}
|
|
101
117
|
|
|
118
|
+
/**
|
|
119
|
+
* Set an array variable in the template context
|
|
120
|
+
* @param {string} key - Variable name
|
|
121
|
+
* @param {Array} value - Array value
|
|
122
|
+
* @returns {PugCompiler} - Returns this for chaining
|
|
123
|
+
*/
|
|
124
|
+
setArray(key, value) {
|
|
125
|
+
if (typeof key !== 'string') {
|
|
126
|
+
throw new TypeError('Key must be a string');
|
|
127
|
+
}
|
|
128
|
+
if (!Array.isArray(value)) {
|
|
129
|
+
throw new TypeError('Value must be an array');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const success = binding.setArray(this.context, key, value);
|
|
133
|
+
if (!success) {
|
|
134
|
+
throw new Error(`Failed to set array variable: ${key}`);
|
|
135
|
+
}
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Set an object variable in the template context
|
|
141
|
+
* @param {string} key - Variable name
|
|
142
|
+
* @param {Object} value - Object value (plain object, not array)
|
|
143
|
+
* @returns {PugCompiler} - Returns this for chaining
|
|
144
|
+
*/
|
|
145
|
+
setObject(key, value) {
|
|
146
|
+
if (typeof key !== 'string') {
|
|
147
|
+
throw new TypeError('Key must be a string');
|
|
148
|
+
}
|
|
149
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
150
|
+
throw new TypeError('Value must be a plain object');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const success = binding.setObject(this.context, key, value);
|
|
154
|
+
if (!success) {
|
|
155
|
+
throw new Error(`Failed to set object variable: ${key}`);
|
|
156
|
+
}
|
|
157
|
+
return this;
|
|
158
|
+
}
|
|
159
|
+
|
|
102
160
|
/**
|
|
103
161
|
* Set a variable (automatically detects type)
|
|
104
162
|
* @param {string} key - Variable name
|
|
105
|
-
* @param {string|number|boolean} value - Value of any supported type
|
|
163
|
+
* @param {string|number|boolean|Array|Object} value - Value of any supported type
|
|
106
164
|
* @returns {PugCompiler} - Returns this for chaining
|
|
107
165
|
*/
|
|
108
166
|
set(key, value) {
|
|
109
|
-
if (
|
|
167
|
+
if (Array.isArray(value)) {
|
|
168
|
+
return this.setArray(key, value);
|
|
169
|
+
} else if (typeof value === 'object' && value !== null) {
|
|
170
|
+
return this.setObject(key, value);
|
|
171
|
+
} else if (typeof value === 'string') {
|
|
110
172
|
return this.setString(key, value);
|
|
111
173
|
} else if (typeof value === 'number') {
|
|
112
174
|
return this.setNumber(key, value);
|
package/index.mjs
CHANGED
|
@@ -7,17 +7,35 @@ import { createRequire } from 'module';
|
|
|
7
7
|
import { fileURLToPath } from 'url';
|
|
8
8
|
import { dirname, join } from 'path';
|
|
9
9
|
import { readFileSync, existsSync } from 'fs';
|
|
10
|
+
import os from 'os';
|
|
10
11
|
|
|
11
12
|
const __filename = fileURLToPath(import.meta.url);
|
|
12
13
|
const __dirname = dirname(__filename);
|
|
13
14
|
const require = createRequire(import.meta.url);
|
|
14
15
|
|
|
15
|
-
//
|
|
16
|
-
const
|
|
17
|
-
|
|
16
|
+
// Detect platform and architecture
|
|
17
|
+
const platform = os.platform(); // 'linux', 'darwin', 'win32'
|
|
18
|
+
const arch = os.arch(); // 'x64', 'arm64'
|
|
19
|
+
const platformKey = `${platform}-${arch}`;
|
|
20
|
+
|
|
21
|
+
// Try to load prebuilt binary first
|
|
22
|
+
const prebuiltPath = join(__dirname, 'prebuilt-binaries', platformKey, 'zigpug.node');
|
|
23
|
+
const builtPath = join(__dirname, 'build', 'Release', 'zigpug.node');
|
|
24
|
+
|
|
25
|
+
let binding;
|
|
26
|
+
|
|
27
|
+
if (existsSync(prebuiltPath)) {
|
|
28
|
+
// Use prebuilt binary
|
|
29
|
+
binding = require(prebuiltPath);
|
|
30
|
+
} else if (existsSync(builtPath)) {
|
|
31
|
+
// Use locally built binary
|
|
32
|
+
binding = require(builtPath);
|
|
33
|
+
} else {
|
|
18
34
|
console.error('');
|
|
19
35
|
console.error('zig-pug native addon not found!');
|
|
20
36
|
console.error('');
|
|
37
|
+
console.error(`Platform: ${platformKey}`);
|
|
38
|
+
console.error('');
|
|
21
39
|
console.error('The native addon needs to be built. Please run:');
|
|
22
40
|
console.error('');
|
|
23
41
|
console.error(' cd node_modules/zig-pug && npm run install');
|
|
@@ -26,11 +44,9 @@ if (!existsSync(addonPath)) {
|
|
|
26
44
|
console.error('');
|
|
27
45
|
console.error(' cd node_modules/zig-pug && bun run install');
|
|
28
46
|
console.error('');
|
|
29
|
-
throw new Error('zig-pug native addon not
|
|
47
|
+
throw new Error('zig-pug native addon not found. See instructions above.');
|
|
30
48
|
}
|
|
31
49
|
|
|
32
|
-
const binding = require('./build/Release/zigpug.node');
|
|
33
|
-
|
|
34
50
|
/**
|
|
35
51
|
* PugCompiler class - High-level API for compiling Pug templates
|
|
36
52
|
*/
|
|
@@ -105,14 +121,60 @@ export class PugCompiler {
|
|
|
105
121
|
return this;
|
|
106
122
|
}
|
|
107
123
|
|
|
124
|
+
/**
|
|
125
|
+
* Set an array variable in the template context
|
|
126
|
+
* @param {string} key - Variable name
|
|
127
|
+
* @param {Array} value - Array value
|
|
128
|
+
* @returns {PugCompiler} - Returns this for chaining
|
|
129
|
+
*/
|
|
130
|
+
setArray(key, value) {
|
|
131
|
+
if (typeof key !== 'string') {
|
|
132
|
+
throw new TypeError('Key must be a string');
|
|
133
|
+
}
|
|
134
|
+
if (!Array.isArray(value)) {
|
|
135
|
+
throw new TypeError('Value must be an array');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const success = binding.setArray(this.context, key, value);
|
|
139
|
+
if (!success) {
|
|
140
|
+
throw new Error(`Failed to set array variable: ${key}`);
|
|
141
|
+
}
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Set an object variable in the template context
|
|
147
|
+
* @param {string} key - Variable name
|
|
148
|
+
* @param {Object} value - Object value (plain object, not array)
|
|
149
|
+
* @returns {PugCompiler} - Returns this for chaining
|
|
150
|
+
*/
|
|
151
|
+
setObject(key, value) {
|
|
152
|
+
if (typeof key !== 'string') {
|
|
153
|
+
throw new TypeError('Key must be a string');
|
|
154
|
+
}
|
|
155
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
156
|
+
throw new TypeError('Value must be a plain object');
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const success = binding.setObject(this.context, key, value);
|
|
160
|
+
if (!success) {
|
|
161
|
+
throw new Error(`Failed to set object variable: ${key}`);
|
|
162
|
+
}
|
|
163
|
+
return this;
|
|
164
|
+
}
|
|
165
|
+
|
|
108
166
|
/**
|
|
109
167
|
* Set a variable (automatically detects type)
|
|
110
168
|
* @param {string} key - Variable name
|
|
111
|
-
* @param {string|number|boolean} value - Value of any supported type
|
|
169
|
+
* @param {string|number|boolean|Array|Object} value - Value of any supported type
|
|
112
170
|
* @returns {PugCompiler} - Returns this for chaining
|
|
113
171
|
*/
|
|
114
172
|
set(key, value) {
|
|
115
|
-
if (
|
|
173
|
+
if (Array.isArray(value)) {
|
|
174
|
+
return this.setArray(key, value);
|
|
175
|
+
} else if (typeof value === 'object' && value !== null) {
|
|
176
|
+
return this.setObject(key, value);
|
|
177
|
+
} else if (typeof value === 'string') {
|
|
116
178
|
return this.setString(key, value);
|
|
117
179
|
} else if (typeof value === 'number') {
|
|
118
180
|
return this.setNumber(key, value);
|
package/install.js
CHANGED
|
@@ -7,10 +7,22 @@
|
|
|
7
7
|
const { execSync, spawnSync } = require('child_process');
|
|
8
8
|
const fs = require('fs');
|
|
9
9
|
const path = require('path');
|
|
10
|
+
const os = require('os');
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
// Detect platform
|
|
13
|
+
const platform = os.platform();
|
|
14
|
+
const arch = os.arch();
|
|
15
|
+
const platformKey = `${platform}-${arch}`;
|
|
12
16
|
|
|
13
|
-
// Check if
|
|
17
|
+
// Check if prebuilt binary exists
|
|
18
|
+
const prebuiltPath = path.join(__dirname, 'prebuilt-binaries', platformKey, 'zigpug.node');
|
|
19
|
+
if (fs.existsSync(prebuiltPath)) {
|
|
20
|
+
console.log(`✓ zig-pug: Using prebuilt binary for ${platformKey}`);
|
|
21
|
+
process.exit(0);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Check if addon already built
|
|
25
|
+
const addonPath = path.join(__dirname, 'build', 'Release', 'zigpug.node');
|
|
14
26
|
if (fs.existsSync(addonPath)) {
|
|
15
27
|
console.log('✓ zig-pug native addon already built');
|
|
16
28
|
process.exit(0);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zig-pug",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.5",
|
|
4
4
|
"description": "High-performance Pug template engine powered by Zig and mujs. Native N-API addon with ES5.1 JavaScript support, full UTF-8 (emoji, accents), documentation comments (//!), and fast compilation. Compatible with Node.js and Bun.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "index.js",
|
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
"clean": "node-gyp clean",
|
|
19
19
|
"test": "node test/test.js",
|
|
20
20
|
"build-prebuilts": "./build-prebuilts.sh",
|
|
21
|
-
"
|
|
21
|
+
"build-binaries": "./build-node-binaries.sh",
|
|
22
|
+
"prepublishOnly": "npm run build-prebuilts && npm run build-binaries"
|
|
22
23
|
},
|
|
23
24
|
"keywords": [
|
|
24
25
|
"pug",
|
|
@@ -93,6 +94,7 @@
|
|
|
93
94
|
"README.md",
|
|
94
95
|
"LICENSE",
|
|
95
96
|
"include/",
|
|
96
|
-
"prebuilts/"
|
|
97
|
+
"prebuilts/",
|
|
98
|
+
"prebuilt-binaries/"
|
|
97
99
|
]
|
|
98
100
|
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|