zig-pug 0.3.4 → 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 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/yourusername/zig-pug
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/yourusername/zig-pug/blob/main/docs/TERMUX.md) for details.
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/yourusername/zig-pug/tree/main/examples) directory:
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/yourusername/zig-pug/blob/main/docs/GETTING-STARTED.md)**
342
- - **[Node.js Integration](https://github.com/yourusername/zig-pug/blob/main/docs/NODEJS-INTEGRATION.md)**
343
- - **[Pug Syntax Reference](https://github.com/yourusername/zig-pug/blob/main/docs/PUG-SYNTAX.md)**
344
- - **[API Reference](https://github.com/yourusername/zig-pug/blob/main/docs/API-REFERENCE.md)**
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/yourusername/zig-pug/issues) with:
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/yourusername/zig-pug/blob/main/LICENSE) for details.
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/yourusername/zig-pug
406
+ - **GitHub**: https://github.com/carlos-sweb/zig-pug
407
407
  - **npm**: https://www.npmjs.com/package/zig-pug
408
- - **Issues**: https://github.com/yourusername/zig-pug/issues
409
- - **Documentation**: https://github.com/yourusername/zig-pug#readme
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
@@ -115,14 +115,60 @@ class PugCompiler {
115
115
  return this;
116
116
  }
117
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
+
118
160
  /**
119
161
  * Set a variable (automatically detects type)
120
162
  * @param {string} key - Variable name
121
- * @param {string|number|boolean} value - Value of any supported type
163
+ * @param {string|number|boolean|Array|Object} value - Value of any supported type
122
164
  * @returns {PugCompiler} - Returns this for chaining
123
165
  */
124
166
  set(key, value) {
125
- if (typeof value === 'string') {
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') {
126
172
  return this.setString(key, value);
127
173
  } else if (typeof value === 'number') {
128
174
  return this.setNumber(key, value);
package/index.mjs CHANGED
@@ -121,14 +121,60 @@ export class PugCompiler {
121
121
  return this;
122
122
  }
123
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
+
124
166
  /**
125
167
  * Set a variable (automatically detects type)
126
168
  * @param {string} key - Variable name
127
- * @param {string|number|boolean} value - Value of any supported type
169
+ * @param {string|number|boolean|Array|Object} value - Value of any supported type
128
170
  * @returns {PugCompiler} - Returns this for chaining
129
171
  */
130
172
  set(key, value) {
131
- if (typeof value === 'string') {
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') {
132
178
  return this.setString(key, value);
133
179
  } else if (typeof value === 'number') {
134
180
  return this.setNumber(key, value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zig-pug",
3
- "version": "0.3.4",
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",
Binary file
Binary file
Binary file
Binary file
Binary file