zig-pug 0.2.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 (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +346 -0
  3. package/binding.c +375 -0
  4. package/binding.gyp +28 -0
  5. package/common.gypi +5 -0
  6. package/include/zigpug.h +135 -0
  7. package/index.js +205 -0
  8. package/package.json +87 -0
  9. package/vendor/mujs/COPYING +16 -0
  10. package/vendor/mujs/README +50 -0
  11. package/vendor/mujs/astnames.h +92 -0
  12. package/vendor/mujs/jsarray.c +832 -0
  13. package/vendor/mujs/jsboolean.c +38 -0
  14. package/vendor/mujs/jsbuiltin.c +249 -0
  15. package/vendor/mujs/jscompile.c +1428 -0
  16. package/vendor/mujs/jsdate.c +861 -0
  17. package/vendor/mujs/jsdtoa.c +749 -0
  18. package/vendor/mujs/jserror.c +139 -0
  19. package/vendor/mujs/jsfunction.c +231 -0
  20. package/vendor/mujs/jsgc.c +284 -0
  21. package/vendor/mujs/jsi.h +870 -0
  22. package/vendor/mujs/jsintern.c +137 -0
  23. package/vendor/mujs/jslex.c +878 -0
  24. package/vendor/mujs/jsmath.c +194 -0
  25. package/vendor/mujs/jsnumber.c +198 -0
  26. package/vendor/mujs/jsobject.c +560 -0
  27. package/vendor/mujs/json.c +422 -0
  28. package/vendor/mujs/jsparse.c +1065 -0
  29. package/vendor/mujs/jsproperty.c +341 -0
  30. package/vendor/mujs/jsregexp.c +232 -0
  31. package/vendor/mujs/jsrepr.c +285 -0
  32. package/vendor/mujs/jsrun.c +2096 -0
  33. package/vendor/mujs/jsstate.c +334 -0
  34. package/vendor/mujs/jsstring.c +852 -0
  35. package/vendor/mujs/jsvalue.c +708 -0
  36. package/vendor/mujs/libmujs.a +0 -0
  37. package/vendor/mujs/main.c +396 -0
  38. package/vendor/mujs/mujs.h +253 -0
  39. package/vendor/mujs/one.c +25 -0
  40. package/vendor/mujs/opnames.h +85 -0
  41. package/vendor/mujs/pp.c +980 -0
  42. package/vendor/mujs/regexp.c +1277 -0
  43. package/vendor/mujs/regexp.h +46 -0
  44. package/vendor/mujs/utf.c +305 -0
  45. package/vendor/mujs/utf.h +52 -0
  46. package/vendor/mujs/utfdata.h +2209 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 zig-pug contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,346 @@
1
+ # zig-pug
2
+
3
+ High-performance Pug template engine powered by Zig and mujs - Native N-API addon with ES5.1 JavaScript support.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/zig-pug.svg)](https://www.npmjs.com/package/zig-pug)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+
8
+ ## Features
9
+
10
+ - **Blazing Fast**: Written in Zig with native N-API bindings
11
+ - **Lightweight**: ~500KB total (includes mujs JavaScript engine)
12
+ - **Modern**: Zig 0.15 best practices with StaticStringMap O(1) lookups
13
+ - **Secure**: Built-in XSS protection with automatic HTML escaping
14
+ - **Simple API**: Easy-to-use JavaScript interface
15
+ - **Cross-Platform**: Precompiled binaries for Linux, macOS, and Windows
16
+ - **Node.js Compatible**: Works with Node.js 14+ and Bun.js
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ npm install zig-pug
22
+ ```
23
+
24
+ Precompiled binaries are automatically downloaded for your platform. If no binary is available, it will build from source (requires Zig 0.15+).
25
+
26
+ ## Quick Start
27
+
28
+ ```javascript
29
+ const { compile } = require('zig-pug');
30
+
31
+ // Simple template
32
+ const html = compile('h1 Hello, World!');
33
+ console.log(html); // <h1>Hello, World!</h1>
34
+
35
+ // With variables
36
+ const html2 = compile('h1 Hello, #{name}!', { name: 'Alice' });
37
+ console.log(html2); // <h1>Hello, Alice!</h1>
38
+ ```
39
+
40
+ ## API Reference
41
+
42
+ ### compile(template, variables)
43
+
44
+ Compile a Pug template string to HTML.
45
+
46
+ ```javascript
47
+ const { compile } = require('zig-pug');
48
+
49
+ const html = compile('p Hello, #{name}!', { name: 'Bob' });
50
+ console.log(html); // <p>Hello, Bob!</p>
51
+ ```
52
+
53
+ **Parameters:**
54
+ - `template` (string): Pug template string
55
+ - `variables` (object, optional): Variables to interpolate
56
+
57
+ **Returns:** Compiled HTML string
58
+
59
+ ### compileFile(filename, variables)
60
+
61
+ Compile a Pug template file to HTML.
62
+
63
+ ```javascript
64
+ const { compileFile } = require('zig-pug');
65
+
66
+ const html = compileFile('./views/index.pug', {
67
+ title: 'My Page',
68
+ user: 'Alice'
69
+ });
70
+ ```
71
+
72
+ **Parameters:**
73
+ - `filename` (string): Path to Pug template file
74
+ - `variables` (object, optional): Variables to interpolate
75
+
76
+ **Returns:** Compiled HTML string
77
+
78
+ ### ZigPugCompiler Class
79
+
80
+ For multiple compilations, use the `ZigPugCompiler` class to reuse the context:
81
+
82
+ ```javascript
83
+ const { ZigPugCompiler } = require('zig-pug');
84
+
85
+ const compiler = new ZigPugCompiler();
86
+
87
+ // Set variables
88
+ compiler.setString('name', 'Alice');
89
+ compiler.setNumber('age', 30);
90
+ compiler.setBool('premium', true);
91
+
92
+ // Or set multiple at once
93
+ compiler.setVariables({
94
+ name: 'Bob',
95
+ age: 25,
96
+ premium: false
97
+ });
98
+
99
+ // Compile templates
100
+ const html1 = compiler.compile('p Hello, #{name}!');
101
+ const html2 = compiler.compile('p Age: #{age}');
102
+ ```
103
+
104
+ **Methods:**
105
+ - `setString(key, value)` - Set a string variable
106
+ - `setNumber(key, value)` - Set a number variable
107
+ - `setBool(key, value)` - Set a boolean variable
108
+ - `set(key, value)` - Auto-detect type and set variable
109
+ - `setVariables(obj)` - Set multiple variables from object
110
+ - `compile(template)` - Compile template with current variables
111
+ - `render(template, variables)` - Set variables and compile in one call
112
+
113
+ ### version()
114
+
115
+ Get the zig-pug version.
116
+
117
+ ```javascript
118
+ const { version } = require('zig-pug');
119
+ console.log(version()); // 0.2.0
120
+ ```
121
+
122
+ ## Pug Syntax Support
123
+
124
+ ### Tags
125
+
126
+ ```pug
127
+ div
128
+ p Hello, World!
129
+ span.class-name#id-name Text content
130
+ ```
131
+
132
+ ```html
133
+ <div>
134
+ <p>Hello, World!</p>
135
+ <span class="class-name" id="id-name">Text content</span>
136
+ </div>
137
+ ```
138
+
139
+ ### Attributes
140
+
141
+ ```pug
142
+ a(href="https://example.com" target="_blank") Link
143
+ input(type="text" name="username" required)
144
+ ```
145
+
146
+ ```html
147
+ <a href="https://example.com" target="_blank">Link</a>
148
+ <input type="text" name="username" required />
149
+ ```
150
+
151
+ ### Interpolation
152
+
153
+ ```pug
154
+ p Hello, #{name}!
155
+ p Age: #{age}
156
+ p Premium: #{premium}
157
+ ```
158
+
159
+ With variables: `{ name: 'Alice', age: 30, premium: true }`
160
+
161
+ ```html
162
+ <p>Hello, Alice!</p>
163
+ <p>Age: 30</p>
164
+ <p>Premium: true</p>
165
+ ```
166
+
167
+ ### HTML Escaping
168
+
169
+ Automatic XSS protection:
170
+
171
+ ```javascript
172
+ const html = compile('p #{userInput}', {
173
+ userInput: '<script>alert("XSS")</script>'
174
+ });
175
+ // <p>&lt;script&gt;alert(&quot;XSS&quot;)&lt;/script&gt;</p>
176
+ ```
177
+
178
+ ### Void Elements
179
+
180
+ Self-closing tags are handled automatically:
181
+
182
+ ```pug
183
+ br
184
+ hr
185
+ img(src="logo.png")
186
+ input(type="text")
187
+ ```
188
+
189
+ ```html
190
+ <br />
191
+ <hr />
192
+ <img src="logo.png" />
193
+ <input type="text" />
194
+ ```
195
+
196
+ ## Usage Examples
197
+
198
+ ### Express Integration
199
+
200
+ ```javascript
201
+ const express = require('express');
202
+ const { compileFile } = require('zig-pug');
203
+ const app = express();
204
+
205
+ app.get('/', (req, res) => {
206
+ const html = compileFile('./views/index.pug', {
207
+ title: 'Home Page',
208
+ user: req.user
209
+ });
210
+ res.send(html);
211
+ });
212
+
213
+ app.listen(3000);
214
+ ```
215
+
216
+ ### Koa Integration
217
+
218
+ ```javascript
219
+ const Koa = require('koa');
220
+ const { compileFile } = require('zig-pug');
221
+ const app = new Koa();
222
+
223
+ app.use(async ctx => {
224
+ const html = compileFile('./views/index.pug', {
225
+ title: 'Home Page',
226
+ path: ctx.path
227
+ });
228
+ ctx.body = html;
229
+ });
230
+
231
+ app.listen(3000);
232
+ ```
233
+
234
+ ### Bun.js Integration
235
+
236
+ ```javascript
237
+ import { compile } from 'zig-pug';
238
+
239
+ Bun.serve({
240
+ port: 3000,
241
+ fetch(req) {
242
+ const html = compile('h1 Hello from Bun!');
243
+ return new Response(html, {
244
+ headers: { 'Content-Type': 'text/html' }
245
+ });
246
+ }
247
+ });
248
+ ```
249
+
250
+ ## Performance
251
+
252
+ zig-pug is designed for performance:
253
+
254
+ - **O(1) lookups**: StaticStringMap for void element checks
255
+ - **Optimized I/O**: Writer pattern reduces allocations
256
+ - **Native code**: Zig compiles to machine code
257
+ - **Lightweight**: No heavy dependencies
258
+ - **Fast startup**: mujs has minimal overhead
259
+
260
+ ## Security
261
+
262
+ - **Automatic HTML escaping**: All interpolated values are escaped by default
263
+ - **XSS prevention**: Built-in protection against cross-site scripting
264
+ - **Safe parsing**: Zig's memory safety prevents buffer overflows
265
+ - **No eval()**: Templates are compiled, not evaluated
266
+
267
+ ## Building from Source
268
+
269
+ If precompiled binaries are not available for your platform:
270
+
271
+ ### Prerequisites
272
+
273
+ - Node.js 14+
274
+ - Zig 0.15+
275
+ - Python 3
276
+ - C compiler (gcc/clang)
277
+
278
+ ### Build Steps
279
+
280
+ From the repository root:
281
+
282
+ ```bash
283
+ # Build the addon (builds library + addon + copies library to build dir)
284
+ zig build node
285
+
286
+ # Or from nodejs directory
287
+ cd nodejs && npm run build
288
+ ```
289
+
290
+ ### Running Examples and Tests
291
+
292
+ ```bash
293
+ # From nodejs directory
294
+ npm test # Run test suite
295
+ npm run example # Run example
296
+
297
+ # Or directly with node (after building)
298
+ node test/test.js
299
+ node example.js
300
+ ```
301
+
302
+ **Note:** The build process automatically copies `libzigpug.so` to the `build/Release/` directory, so you don't need to set `LD_LIBRARY_PATH` manually.
303
+
304
+ See [BUILD_GUIDE.md](BUILD_GUIDE.md) for detailed build instructions.
305
+
306
+ ## Architecture
307
+
308
+ ```
309
+ ┌─────────────────────────────────────────────┐
310
+ │ Node.js Application │
311
+ │ (your code) │
312
+ └────────────┬────────────────────────────────┘
313
+ │ require('zig-pug')
314
+
315
+ ┌─────────────────────────────────────────────┐
316
+ │ N-API Addon (binding.c) │
317
+ │ - JavaScript API wrapper │
318
+ └────────────┬────────────────────────────────┘
319
+ │ FFI calls
320
+
321
+ ┌─────────────────────────────────────────────┐
322
+ │ libzigpug.so (Zig + mujs) │
323
+ │ - Tokenizer → Parser → Compiler → HTML │
324
+ └─────────────────────────────────────────────┘
325
+ ```
326
+
327
+ ## Contributing
328
+
329
+ Contributions are welcome! Please see the main repository for contributing guidelines.
330
+
331
+ ## License
332
+
333
+ MIT License - see [LICENSE](LICENSE) for details.
334
+
335
+ ## Links
336
+
337
+ - [GitHub Repository](https://github.com/carlos-sweb/zig-pug)
338
+ - [npm Package](https://www.npmjs.com/package/zig-pug)
339
+ - [Issue Tracker](https://github.com/carlos-sweb/zig-pug/issues)
340
+ - [Build Guide](BUILD_GUIDE.md)
341
+
342
+ ## Credits
343
+
344
+ - Built by [carlos-sweb](https://github.com/carlos-sweb)
345
+ - Powered by [Zig](https://ziglang.org/) and [mujs](https://mujs.com/)
346
+ - Inspired by the original [Pug](https://pugjs.org/) template engine