zig-pug 4.0.5 → 4.0.8
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 +99 -10
- package/binding.c +125 -0
- package/index.d.ts +48 -5
- package/index.js +58 -10
- package/package.json +1 -1
- package/prebuilt-binaries/darwin-arm64/zigpug.node +0 -0
- package/prebuilt-binaries/darwin-x64/zigpug.node +0 -0
- package/prebuilt-binaries/linux-arm64/zigpug.node +0 -0
- package/prebuilt-binaries/linux-x64/zigpug.node +0 -0
- package/prebuilts/linux-arm64/libzig-pug.a +0 -0
package/README.md
CHANGED
|
@@ -17,6 +17,7 @@ High-performance Pug template engine powered by Zig and mujs.
|
|
|
17
17
|
- ✅ **TypeScript support** - Full type definitions included
|
|
18
18
|
- ✅ **Bun.js compatible** - 2-5x faster than Node.js
|
|
19
19
|
- ✅ **Structured errors** - Detailed error messages with line numbers, hints, and context
|
|
20
|
+
- ✅ **Output formatting** - Minify, pretty-print, or format modes
|
|
20
21
|
- ⚡ **Native performance** - Written in Zig, compiled to native code
|
|
21
22
|
- 🔋 **Zero dependencies** - Only Zig and embedded mujs
|
|
22
23
|
- 🌍 **i18n ready** - Spanish, Portuguese, French, German, and more
|
|
@@ -88,6 +89,73 @@ console.log(html);
|
|
|
88
89
|
// <h1>My Page</h1>
|
|
89
90
|
```
|
|
90
91
|
|
|
92
|
+
## Output Formatting
|
|
93
|
+
|
|
94
|
+
zig-pug supports multiple output modes to suit different use cases:
|
|
95
|
+
|
|
96
|
+
### Default (Minified)
|
|
97
|
+
|
|
98
|
+
By default, HTML is minified for production use:
|
|
99
|
+
|
|
100
|
+
```javascript
|
|
101
|
+
const compiler = new PugCompiler();
|
|
102
|
+
const html = compiler.compile('div\n h1 Hello\n p World');
|
|
103
|
+
// Output: <div><h1>Hello</h1><p>World</p></div>
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Pretty Mode (Development)
|
|
107
|
+
|
|
108
|
+
Pretty-print with indentation and HTML comments for debugging:
|
|
109
|
+
|
|
110
|
+
```javascript
|
|
111
|
+
const compiler = new PugCompiler({ pretty: true });
|
|
112
|
+
const html = compiler.compile('div\n h1 Hello\n p World');
|
|
113
|
+
// Output:
|
|
114
|
+
// <div>
|
|
115
|
+
// <h1>Hello</h1>
|
|
116
|
+
// <p>World</p>
|
|
117
|
+
// </div>
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Format Mode (Readable)
|
|
121
|
+
|
|
122
|
+
Pretty-print without comments for readable production output:
|
|
123
|
+
|
|
124
|
+
```javascript
|
|
125
|
+
const compiler = new PugCompiler({ format: true });
|
|
126
|
+
const html = compiler.compile('div\n h1 Hello\n p World');
|
|
127
|
+
// Output: formatted HTML without comments
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Minify Mode (Explicit)
|
|
131
|
+
|
|
132
|
+
Explicitly minify to ensure smallest file size:
|
|
133
|
+
|
|
134
|
+
```javascript
|
|
135
|
+
const compiler = new PugCompiler({ minify: true });
|
|
136
|
+
const html = compiler.compile('div\n h1 Hello\n p World');
|
|
137
|
+
// Output: <div><h1>Hello</h1><p>World</p></div>
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Override Options at Compile Time
|
|
141
|
+
|
|
142
|
+
The hybrid approach allows you to set default options in the constructor and override them per compilation:
|
|
143
|
+
|
|
144
|
+
```javascript
|
|
145
|
+
// Create compiler with minify by default
|
|
146
|
+
const compiler = new PugCompiler({ minify: true });
|
|
147
|
+
compiler.set('title', 'Hello');
|
|
148
|
+
|
|
149
|
+
// Production build (uses default minify)
|
|
150
|
+
const prod = compiler.compile('h1= title');
|
|
151
|
+
|
|
152
|
+
// Debug build (overrides with pretty)
|
|
153
|
+
const debug = compiler.compile('h1= title', { pretty: true });
|
|
154
|
+
|
|
155
|
+
// Readable build (overrides with format)
|
|
156
|
+
const readable = compiler.compile('h1= title', { format: true });
|
|
157
|
+
```
|
|
158
|
+
|
|
91
159
|
### Express Integration
|
|
92
160
|
|
|
93
161
|
```javascript
|
|
@@ -136,8 +204,8 @@ import { compile, PugCompiler, ZigPugCompilationError } from 'zig-pug';
|
|
|
136
204
|
// Simple compilation with type inference
|
|
137
205
|
const html: string = compile('p Hello #{name}', { name: 'TypeScript' });
|
|
138
206
|
|
|
139
|
-
// Using PugCompiler class
|
|
140
|
-
const compiler: PugCompiler = new PugCompiler();
|
|
207
|
+
// Using PugCompiler class with formatting options
|
|
208
|
+
const compiler: PugCompiler = new PugCompiler({ pretty: true });
|
|
141
209
|
compiler
|
|
142
210
|
.setString('title', 'My Page')
|
|
143
211
|
.setNumber('count', 42)
|
|
@@ -170,6 +238,7 @@ try {
|
|
|
170
238
|
|
|
171
239
|
- `PugCompiler` - Main compiler class
|
|
172
240
|
- `PugVariables` - Type for template variables
|
|
241
|
+
- `CompilationOptions` - Formatting options (pretty, format, minify)
|
|
173
242
|
- `CompilationErrorInfo` - Individual error information
|
|
174
243
|
- `CompilationErrors` - Collection of compilation errors
|
|
175
244
|
- `ZigPugCompilationError` - Extended Error with compilation details
|
|
@@ -288,48 +357,66 @@ html
|
|
|
288
357
|
|
|
289
358
|
## API Reference
|
|
290
359
|
|
|
291
|
-
### `compile(template, data)`
|
|
360
|
+
### `compile(template, data, options)`
|
|
292
361
|
|
|
293
|
-
Compile a template with data.
|
|
362
|
+
Compile a template with data and formatting options.
|
|
294
363
|
|
|
295
364
|
**Parameters:**
|
|
296
365
|
- `template` (string) - Pug template source
|
|
297
366
|
- `data` (object) - Variables to interpolate
|
|
367
|
+
- `options` (object) - Formatting options (optional)
|
|
368
|
+
- `pretty` (boolean) - Enable pretty-print with comments
|
|
369
|
+
- `format` (boolean) - Enable pretty-print without comments
|
|
370
|
+
- `minify` (boolean) - Enable HTML minification
|
|
371
|
+
- `includeComments` (boolean) - Include HTML comments
|
|
298
372
|
|
|
299
373
|
**Returns:** (string) Compiled HTML
|
|
300
374
|
|
|
301
375
|
```javascript
|
|
302
376
|
const html = zigpug.compile(
|
|
303
377
|
'p Hello #{name}!',
|
|
304
|
-
{ name: 'Alice' }
|
|
378
|
+
{ name: 'Alice' },
|
|
379
|
+
{ pretty: true }
|
|
305
380
|
);
|
|
306
381
|
```
|
|
307
382
|
|
|
308
383
|
### `PugCompiler`
|
|
309
384
|
|
|
310
|
-
Reusable compiler with state.
|
|
385
|
+
Reusable compiler with state and formatting options.
|
|
311
386
|
|
|
312
387
|
```javascript
|
|
313
388
|
const { PugCompiler } = require('zig-pug');
|
|
314
389
|
|
|
315
|
-
const compiler = new PugCompiler();
|
|
390
|
+
const compiler = new PugCompiler({ pretty: true });
|
|
316
391
|
compiler.set('key', 'value'); // String/Number
|
|
317
392
|
compiler.setBool('flag', true); // Boolean
|
|
318
393
|
|
|
319
394
|
const html = compiler.compile(template);
|
|
320
395
|
```
|
|
321
396
|
|
|
397
|
+
**Constructor Options:**
|
|
398
|
+
- `pretty` (boolean) - Enable pretty-print with indentation and comments
|
|
399
|
+
- `format` (boolean) - Enable pretty-print without comments
|
|
400
|
+
- `minify` (boolean) - Enable HTML minification
|
|
401
|
+
- `includeComments` (boolean) - Include HTML comments
|
|
402
|
+
|
|
322
403
|
**Methods:**
|
|
323
|
-
- `set(key, value)` - Set string or
|
|
404
|
+
- `set(key, value)` - Set string, number, array, or object variable (auto-detects type)
|
|
405
|
+
- `setString(key, value)` - Set string variable
|
|
406
|
+
- `setNumber(key, value)` - Set number variable
|
|
324
407
|
- `setBool(key, value)` - Set boolean variable
|
|
325
|
-
- `
|
|
408
|
+
- `setArray(key, value)` - Set array variable
|
|
409
|
+
- `setObject(key, value)` - Set object variable
|
|
410
|
+
- `setVariables(object)` - Set multiple variables from an object
|
|
411
|
+
- `compile(template, options)` - Compile template with current variables
|
|
412
|
+
- `render(template, variables, options)` - Compile template with variables in one call
|
|
326
413
|
|
|
327
414
|
### `version()`
|
|
328
415
|
|
|
329
416
|
Get zig-pug version.
|
|
330
417
|
|
|
331
418
|
```javascript
|
|
332
|
-
console.log(zigpug.version()); // "0.
|
|
419
|
+
console.log(zigpug.version()); // "0.4.0"
|
|
333
420
|
```
|
|
334
421
|
|
|
335
422
|
## Platform Support
|
|
@@ -382,6 +469,7 @@ console.log(`${iterations} in ${elapsed}ms`);
|
|
|
382
469
|
1. **Reuse PugCompiler** - Faster than creating new context each time
|
|
383
470
|
2. **Pre-load templates** - Read files once at startup
|
|
384
471
|
3. **Use Bun.js** - 2-5x faster than Node.js
|
|
472
|
+
4. **Use minify in production** - Smaller output, faster rendering
|
|
385
473
|
|
|
386
474
|
## Examples
|
|
387
475
|
|
|
@@ -390,6 +478,7 @@ See the [examples](https://github.com/carlos-sweb/zig-pug/tree/main/examples) di
|
|
|
390
478
|
- **Node.js**: `examples/nodejs/`
|
|
391
479
|
- **Bun.js**: `examples/bun/`
|
|
392
480
|
- **Express**: `examples/nodejs/05-express-integration.js`
|
|
481
|
+
- **TypeScript**: `examples/nodejs/08-typescript-example.ts`
|
|
393
482
|
|
|
394
483
|
## Documentation
|
|
395
484
|
|
package/binding.c
CHANGED
|
@@ -23,6 +23,8 @@ extern void zigpug_free_string(char* str);
|
|
|
23
23
|
extern const char* zigpug_version(void);
|
|
24
24
|
extern size_t zigpug_get_error_count(ZigPugContext* ctx);
|
|
25
25
|
extern int zigpug_get_error(ZigPugContext* ctx, size_t index, size_t* line_out, const char** message_out, const char** detail_out, const char** hint_out);
|
|
26
|
+
extern char* zigpug_pretty_print(const char* html, int include_comments);
|
|
27
|
+
extern char* zigpug_minify(const char* html);
|
|
26
28
|
|
|
27
29
|
// Wrapper for ZigPugContext to store in JavaScript
|
|
28
30
|
typedef struct {
|
|
@@ -601,6 +603,117 @@ static napi_value Version(napi_env env, napi_callback_info info) {
|
|
|
601
603
|
return result;
|
|
602
604
|
}
|
|
603
605
|
|
|
606
|
+
// Pretty-print HTML with optional comments
|
|
607
|
+
// JavaScript: const formatted = zigpug.prettyPrint(html, includeComments)
|
|
608
|
+
static napi_value PrettyPrint(napi_env env, napi_callback_info info) {
|
|
609
|
+
napi_status status;
|
|
610
|
+
size_t argc = 2;
|
|
611
|
+
napi_value args[2];
|
|
612
|
+
|
|
613
|
+
status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
|
|
614
|
+
if (status != napi_ok || argc < 2) {
|
|
615
|
+
napi_throw_error(env, NULL, "Expected 2 arguments: html, includeComments");
|
|
616
|
+
return NULL;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// Get HTML string
|
|
620
|
+
size_t html_len;
|
|
621
|
+
status = napi_get_value_string_utf8(env, args[0], NULL, 0, &html_len);
|
|
622
|
+
if (status != napi_ok) {
|
|
623
|
+
napi_throw_error(env, NULL, "Invalid HTML string");
|
|
624
|
+
return NULL;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
char* html = malloc(html_len + 1);
|
|
628
|
+
status = napi_get_value_string_utf8(env, args[0], html, html_len + 1, &html_len);
|
|
629
|
+
if (status != napi_ok) {
|
|
630
|
+
free(html);
|
|
631
|
+
napi_throw_error(env, NULL, "Failed to get HTML string");
|
|
632
|
+
return NULL;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
// Get includeComments boolean
|
|
636
|
+
bool include_comments;
|
|
637
|
+
status = napi_get_value_bool(env, args[1], &include_comments);
|
|
638
|
+
if (status != napi_ok) {
|
|
639
|
+
free(html);
|
|
640
|
+
napi_throw_error(env, NULL, "Invalid includeComments boolean");
|
|
641
|
+
return NULL;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// Call zig-pug pretty print
|
|
645
|
+
char* formatted = zigpug_pretty_print(html, include_comments ? 1 : 0);
|
|
646
|
+
free(html);
|
|
647
|
+
|
|
648
|
+
if (!formatted) {
|
|
649
|
+
napi_throw_error(env, NULL, "Failed to format HTML");
|
|
650
|
+
return NULL;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// Create JavaScript string
|
|
654
|
+
napi_value result;
|
|
655
|
+
status = napi_create_string_utf8(env, formatted, NAPI_AUTO_LENGTH, &result);
|
|
656
|
+
zigpug_free_string(formatted);
|
|
657
|
+
|
|
658
|
+
if (status != napi_ok) {
|
|
659
|
+
napi_throw_error(env, NULL, "Failed to create result string");
|
|
660
|
+
return NULL;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
return result;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
// Minify HTML
|
|
667
|
+
// JavaScript: const minified = zigpug.minify(html)
|
|
668
|
+
static napi_value Minify(napi_env env, napi_callback_info info) {
|
|
669
|
+
napi_status status;
|
|
670
|
+
size_t argc = 1;
|
|
671
|
+
napi_value args[1];
|
|
672
|
+
|
|
673
|
+
status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
|
|
674
|
+
if (status != napi_ok || argc < 1) {
|
|
675
|
+
napi_throw_error(env, NULL, "Expected 1 argument: html");
|
|
676
|
+
return NULL;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
// Get HTML string
|
|
680
|
+
size_t html_len;
|
|
681
|
+
status = napi_get_value_string_utf8(env, args[0], NULL, 0, &html_len);
|
|
682
|
+
if (status != napi_ok) {
|
|
683
|
+
napi_throw_error(env, NULL, "Invalid HTML string");
|
|
684
|
+
return NULL;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
char* html = malloc(html_len + 1);
|
|
688
|
+
status = napi_get_value_string_utf8(env, args[0], html, html_len + 1, &html_len);
|
|
689
|
+
if (status != napi_ok) {
|
|
690
|
+
free(html);
|
|
691
|
+
napi_throw_error(env, NULL, "Failed to get HTML string");
|
|
692
|
+
return NULL;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
// Call zig-pug minify
|
|
696
|
+
char* minified = zigpug_minify(html);
|
|
697
|
+
free(html);
|
|
698
|
+
|
|
699
|
+
if (!minified) {
|
|
700
|
+
napi_throw_error(env, NULL, "Failed to minify HTML");
|
|
701
|
+
return NULL;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
// Create JavaScript string
|
|
705
|
+
napi_value result;
|
|
706
|
+
status = napi_create_string_utf8(env, minified, NAPI_AUTO_LENGTH, &result);
|
|
707
|
+
zigpug_free_string(minified);
|
|
708
|
+
|
|
709
|
+
if (status != napi_ok) {
|
|
710
|
+
napi_throw_error(env, NULL, "Failed to create result string");
|
|
711
|
+
return NULL;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
return result;
|
|
715
|
+
}
|
|
716
|
+
|
|
604
717
|
// Initialize the N-API module
|
|
605
718
|
static napi_value Init(napi_env env, napi_value exports) {
|
|
606
719
|
napi_status status;
|
|
@@ -654,6 +767,18 @@ static napi_value Init(napi_env env, napi_value exports) {
|
|
|
654
767
|
status = napi_set_named_property(env, exports, "version", fn);
|
|
655
768
|
if (status != napi_ok) return NULL;
|
|
656
769
|
|
|
770
|
+
// prettyPrint
|
|
771
|
+
status = napi_create_function(env, NULL, 0, PrettyPrint, NULL, &fn);
|
|
772
|
+
if (status != napi_ok) return NULL;
|
|
773
|
+
status = napi_set_named_property(env, exports, "prettyPrint", fn);
|
|
774
|
+
if (status != napi_ok) return NULL;
|
|
775
|
+
|
|
776
|
+
// minify
|
|
777
|
+
status = napi_create_function(env, NULL, 0, Minify, NULL, &fn);
|
|
778
|
+
if (status != napi_ok) return NULL;
|
|
779
|
+
status = napi_set_named_property(env, exports, "minify", fn);
|
|
780
|
+
if (status != napi_ok) return NULL;
|
|
781
|
+
|
|
657
782
|
return exports;
|
|
658
783
|
}
|
|
659
784
|
|
package/index.d.ts
CHANGED
|
@@ -65,6 +65,20 @@ export interface PugVariables {
|
|
|
65
65
|
[key: string]: PugVariable;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Compilation options for formatting output
|
|
70
|
+
*/
|
|
71
|
+
export interface CompilationOptions {
|
|
72
|
+
/** Enable pretty-print with indentation and comments (development mode) */
|
|
73
|
+
pretty?: boolean;
|
|
74
|
+
/** Enable pretty-print without comments (readable mode) */
|
|
75
|
+
format?: boolean;
|
|
76
|
+
/** Enable HTML minification (production mode) */
|
|
77
|
+
minify?: boolean;
|
|
78
|
+
/** Include HTML comments in output (only with pretty/format) */
|
|
79
|
+
includeComments?: boolean;
|
|
80
|
+
}
|
|
81
|
+
|
|
68
82
|
/**
|
|
69
83
|
* PugCompiler class for advanced usage with reusable context
|
|
70
84
|
*
|
|
@@ -79,9 +93,22 @@ export interface PugVariables {
|
|
|
79
93
|
export class PugCompiler {
|
|
80
94
|
/**
|
|
81
95
|
* Create a new PugCompiler instance
|
|
96
|
+
* @param options - Default compilation options
|
|
82
97
|
* @throws {Error} If context creation fails
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* // Development mode with pretty-printing
|
|
102
|
+
* const devCompiler = new PugCompiler({ pretty: true });
|
|
103
|
+
*
|
|
104
|
+
* // Production mode with minification
|
|
105
|
+
* const prodCompiler = new PugCompiler({ minify: true });
|
|
106
|
+
*
|
|
107
|
+
* // Readable mode without comments
|
|
108
|
+
* const readableCompiler = new PugCompiler({ format: true });
|
|
109
|
+
* ```
|
|
83
110
|
*/
|
|
84
|
-
constructor();
|
|
111
|
+
constructor(options?: CompilationOptions);
|
|
85
112
|
|
|
86
113
|
/**
|
|
87
114
|
* Set a variable (auto-detects type)
|
|
@@ -203,6 +230,7 @@ export class PugCompiler {
|
|
|
203
230
|
/**
|
|
204
231
|
* Compile a Pug template to HTML
|
|
205
232
|
* @param template - Pug template string
|
|
233
|
+
* @param options - Compilation options (overrides constructor options)
|
|
206
234
|
* @returns Compiled HTML string
|
|
207
235
|
* @throws {TypeError} If template is not a string
|
|
208
236
|
* @throws {ZigPugCompilationError} If compilation fails with structured error info
|
|
@@ -211,7 +239,12 @@ export class PugCompiler {
|
|
|
211
239
|
* @example
|
|
212
240
|
* ```typescript
|
|
213
241
|
* try {
|
|
242
|
+
* // Use default options from constructor
|
|
214
243
|
* const html = compiler.compile('h1= title');
|
|
244
|
+
*
|
|
245
|
+
* // Override options for this compilation
|
|
246
|
+
* const prettyHtml = compiler.compile('h1= title', { pretty: true });
|
|
247
|
+
*
|
|
215
248
|
* console.log(html);
|
|
216
249
|
* } catch (error) {
|
|
217
250
|
* if ((error as ZigPugCompilationError).compilationErrors) {
|
|
@@ -223,12 +256,13 @@ export class PugCompiler {
|
|
|
223
256
|
* }
|
|
224
257
|
* ```
|
|
225
258
|
*/
|
|
226
|
-
compile(template: string): string;
|
|
259
|
+
compile(template: string, options?: CompilationOptions): string;
|
|
227
260
|
|
|
228
261
|
/**
|
|
229
262
|
* Compile a template with variables in one call
|
|
230
263
|
* @param template - Pug template string
|
|
231
264
|
* @param variables - Variables to set before compiling
|
|
265
|
+
* @param options - Compilation options (overrides constructor options)
|
|
232
266
|
* @returns Compiled HTML string
|
|
233
267
|
* @throws {TypeError} If template is not a string
|
|
234
268
|
* @throws {ZigPugCompilationError} If compilation fails
|
|
@@ -236,15 +270,17 @@ export class PugCompiler {
|
|
|
236
270
|
* @example
|
|
237
271
|
* ```typescript
|
|
238
272
|
* const html = compiler.render('h1= title', { title: 'Hello' });
|
|
273
|
+
* const prettyHtml = compiler.render('h1= title', { title: 'Hello' }, { pretty: true });
|
|
239
274
|
* ```
|
|
240
275
|
*/
|
|
241
|
-
render(template: string, variables?: PugVariables): string;
|
|
276
|
+
render(template: string, variables?: PugVariables, options?: CompilationOptions): string;
|
|
242
277
|
}
|
|
243
278
|
|
|
244
279
|
/**
|
|
245
280
|
* Compile a Pug template string to HTML (simple API)
|
|
246
281
|
* @param template - Pug template string
|
|
247
282
|
* @param variables - Optional variables for interpolation
|
|
283
|
+
* @param options - Optional compilation options
|
|
248
284
|
* @returns Compiled HTML string
|
|
249
285
|
* @throws {ZigPugCompilationError} If compilation fails
|
|
250
286
|
*
|
|
@@ -252,14 +288,18 @@ export class PugCompiler {
|
|
|
252
288
|
* ```typescript
|
|
253
289
|
* const html = compile('p Hello #{name}!', { name: 'World' });
|
|
254
290
|
* console.log(html); // <p>Hello World!</p>
|
|
291
|
+
*
|
|
292
|
+
* // With pretty-print
|
|
293
|
+
* const prettyHtml = compile('div\n p Hello', {}, { pretty: true });
|
|
255
294
|
* ```
|
|
256
295
|
*/
|
|
257
|
-
export function compile(template: string, variables?: PugVariables): string;
|
|
296
|
+
export function compile(template: string, variables?: PugVariables, options?: CompilationOptions): string;
|
|
258
297
|
|
|
259
298
|
/**
|
|
260
299
|
* Compile a Pug template from a file
|
|
261
300
|
* @param filename - Path to .pug file
|
|
262
301
|
* @param variables - Optional variables for interpolation
|
|
302
|
+
* @param options - Optional compilation options
|
|
263
303
|
* @returns Compiled HTML string
|
|
264
304
|
* @throws {Error} If file cannot be read
|
|
265
305
|
* @throws {ZigPugCompilationError} If compilation fails
|
|
@@ -270,9 +310,12 @@ export function compile(template: string, variables?: PugVariables): string;
|
|
|
270
310
|
* title: 'Home',
|
|
271
311
|
* user: { name: 'John' }
|
|
272
312
|
* });
|
|
313
|
+
*
|
|
314
|
+
* // Development mode with pretty-print
|
|
315
|
+
* const devHtml = compileFile('./views/index.pug', { title: 'Home' }, { pretty: true });
|
|
273
316
|
* ```
|
|
274
317
|
*/
|
|
275
|
-
export function compileFile(filename: string, variables?: PugVariables): string;
|
|
318
|
+
export function compileFile(filename: string, variables?: PugVariables, options?: CompilationOptions): string;
|
|
276
319
|
|
|
277
320
|
/**
|
|
278
321
|
* Get zig-pug version string
|
package/index.js
CHANGED
|
@@ -8,8 +8,14 @@ const path = require('path');
|
|
|
8
8
|
const os = require('os');
|
|
9
9
|
|
|
10
10
|
// Detect platform and architecture
|
|
11
|
-
|
|
11
|
+
let platform = os.platform(); // 'linux', 'darwin', 'win32', 'android'
|
|
12
12
|
const arch = os.arch(); // 'x64', 'arm64'
|
|
13
|
+
|
|
14
|
+
// Termux/Android reports 'android' but uses Linux binaries
|
|
15
|
+
if (platform === 'android') {
|
|
16
|
+
platform = 'linux';
|
|
17
|
+
}
|
|
18
|
+
|
|
13
19
|
const platformKey = `${platform}-${arch}`;
|
|
14
20
|
|
|
15
21
|
// Try to load prebuilt binary first
|
|
@@ -43,13 +49,28 @@ if (fs.existsSync(prebuiltPath)) {
|
|
|
43
49
|
|
|
44
50
|
/**
|
|
45
51
|
* PugCompiler class - High-level API for compiling Pug templates
|
|
52
|
+
* @param {Object} options - Compiler options
|
|
53
|
+
* @param {boolean} options.pretty - Enable pretty-print with indentation and comments (development mode)
|
|
54
|
+
* @param {boolean} options.format - Enable pretty-print without comments (readable mode)
|
|
55
|
+
* @param {boolean} options.minify - Enable HTML minification (production mode)
|
|
56
|
+
* @param {boolean} options.includeComments - Include HTML comments (only with pretty/format)
|
|
46
57
|
*/
|
|
47
58
|
class PugCompiler {
|
|
48
|
-
constructor() {
|
|
59
|
+
constructor(options = {}) {
|
|
49
60
|
this.context = binding.createContext();
|
|
50
61
|
if (!this.context) {
|
|
51
62
|
throw new Error('Failed to create zig-pug context');
|
|
52
63
|
}
|
|
64
|
+
|
|
65
|
+
// Default options
|
|
66
|
+
this.defaultOptions = {
|
|
67
|
+
pretty: options.pretty || false,
|
|
68
|
+
format: options.format || false,
|
|
69
|
+
minify: options.minify || false,
|
|
70
|
+
includeComments: options.includeComments !== undefined
|
|
71
|
+
? options.includeComments
|
|
72
|
+
: (options.pretty || false)
|
|
73
|
+
};
|
|
53
74
|
}
|
|
54
75
|
|
|
55
76
|
/**
|
|
@@ -199,18 +220,42 @@ class PugCompiler {
|
|
|
199
220
|
/**
|
|
200
221
|
* Compile a Pug template to HTML
|
|
201
222
|
* @param {string} template - Pug template string
|
|
223
|
+
* @param {Object} options - Compilation options (overrides constructor options)
|
|
224
|
+
* @param {boolean} options.pretty - Enable pretty-print with indentation and comments
|
|
225
|
+
* @param {boolean} options.format - Enable pretty-print without comments
|
|
226
|
+
* @param {boolean} options.minify - Enable HTML minification
|
|
227
|
+
* @param {boolean} options.includeComments - Include HTML comments
|
|
202
228
|
* @returns {string} - Compiled HTML
|
|
203
229
|
*/
|
|
204
|
-
compile(template) {
|
|
230
|
+
compile(template, options = {}) {
|
|
205
231
|
if (typeof template !== 'string') {
|
|
206
232
|
throw new TypeError('Template must be a string');
|
|
207
233
|
}
|
|
208
234
|
|
|
209
|
-
|
|
235
|
+
let html = binding.compile(this.context, template);
|
|
210
236
|
if (!html) {
|
|
211
237
|
throw new Error('Failed to compile template');
|
|
212
238
|
}
|
|
213
239
|
|
|
240
|
+
// Merge default options with compile-time options
|
|
241
|
+
const finalOptions = { ...this.defaultOptions, ...options };
|
|
242
|
+
|
|
243
|
+
// Apply formatting based on options
|
|
244
|
+
if (finalOptions.minify) {
|
|
245
|
+
const minified = binding.minify(html);
|
|
246
|
+
if (minified) {
|
|
247
|
+
html = minified;
|
|
248
|
+
}
|
|
249
|
+
} else if (finalOptions.pretty || finalOptions.format) {
|
|
250
|
+
const includeComments = finalOptions.includeComments !== undefined
|
|
251
|
+
? finalOptions.includeComments
|
|
252
|
+
: finalOptions.pretty;
|
|
253
|
+
const formatted = binding.prettyPrint(html, includeComments);
|
|
254
|
+
if (formatted) {
|
|
255
|
+
html = formatted;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
214
259
|
return html;
|
|
215
260
|
}
|
|
216
261
|
|
|
@@ -218,11 +263,12 @@ class PugCompiler {
|
|
|
218
263
|
* Compile a template with variables in one call
|
|
219
264
|
* @param {string} template - Pug template string
|
|
220
265
|
* @param {Object} variables - Variables to set before compiling
|
|
266
|
+
* @param {Object} options - Compilation options (overrides constructor options)
|
|
221
267
|
* @returns {string} - Compiled HTML
|
|
222
268
|
*/
|
|
223
|
-
render(template, variables = {}) {
|
|
269
|
+
render(template, variables = {}, options = {}) {
|
|
224
270
|
this.setVariables(variables);
|
|
225
|
-
return this.compile(template);
|
|
271
|
+
return this.compile(template, options);
|
|
226
272
|
}
|
|
227
273
|
}
|
|
228
274
|
|
|
@@ -230,10 +276,11 @@ class PugCompiler {
|
|
|
230
276
|
* Convenience function to compile a template with variables
|
|
231
277
|
* @param {string} template - Pug template string
|
|
232
278
|
* @param {Object} variables - Variables for the template
|
|
279
|
+
* @param {Object} options - Compilation options
|
|
233
280
|
* @returns {string} - Compiled HTML
|
|
234
281
|
*/
|
|
235
|
-
function compile(template, variables = {}) {
|
|
236
|
-
const compiler = new PugCompiler();
|
|
282
|
+
function compile(template, variables = {}, options = {}) {
|
|
283
|
+
const compiler = new PugCompiler(options);
|
|
237
284
|
return compiler.render(template, variables);
|
|
238
285
|
}
|
|
239
286
|
|
|
@@ -241,12 +288,13 @@ function compile(template, variables = {}) {
|
|
|
241
288
|
* Convenience function to compile a template from a file
|
|
242
289
|
* @param {string} filename - Path to the Pug template file
|
|
243
290
|
* @param {Object} variables - Variables for the template
|
|
291
|
+
* @param {Object} options - Compilation options
|
|
244
292
|
* @returns {string} - Compiled HTML
|
|
245
293
|
*/
|
|
246
|
-
function compileFile(filename, variables = {}) {
|
|
294
|
+
function compileFile(filename, variables = {}, options = {}) {
|
|
247
295
|
const fs = require('fs');
|
|
248
296
|
const template = fs.readFileSync(filename, 'utf8');
|
|
249
|
-
return compile(template, variables);
|
|
297
|
+
return compile(template, variables, options);
|
|
250
298
|
}
|
|
251
299
|
|
|
252
300
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zig-pug",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.8",
|
|
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), Builder API for dynamic data construction, comprehensive C/C++ API, 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
|