@samuelbines/nunjucks 0.0.3
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/LICENSE +26 -0
- package/README.md +55 -0
- package/dist/scripts/smoke.d.ts +1 -0
- package/dist/scripts/smoke.js +95 -0
- package/dist/src/compiler.d.ts +12 -0
- package/dist/src/compiler.js +1050 -0
- package/dist/src/environment.d.ts +103 -0
- package/dist/src/environment.js +621 -0
- package/dist/src/express-app.d.ts +2 -0
- package/dist/src/express-app.js +33 -0
- package/dist/src/filters.d.ts +44 -0
- package/dist/src/filters.js +424 -0
- package/dist/src/globals.d.ts +14 -0
- package/dist/src/globals.js +342 -0
- package/dist/src/index.d.ts +28 -0
- package/dist/src/index.js +116 -0
- package/dist/src/interpreter.d.ts +16 -0
- package/dist/src/interpreter.js +489 -0
- package/dist/src/lexer.d.ts +72 -0
- package/dist/src/lexer.js +480 -0
- package/dist/src/lib.d.ts +74 -0
- package/dist/src/lib.js +237 -0
- package/dist/src/loader.d.ts +80 -0
- package/dist/src/loader.js +175 -0
- package/dist/src/nodes.d.ts +362 -0
- package/dist/src/nodes.js +894 -0
- package/dist/src/parser.d.ts +66 -0
- package/dist/src/parser.js +1068 -0
- package/dist/src/precompile.d.ts +15 -0
- package/dist/src/precompile.js +108 -0
- package/dist/src/runtime.d.ts +33 -0
- package/dist/src/runtime.js +314 -0
- package/dist/src/transformer.d.ts +3 -0
- package/dist/src/transformer.js +161 -0
- package/dist/src/types.d.ts +27 -0
- package/dist/src/types.js +2 -0
- package/dist/tests/compiler.test.d.ts +1 -0
- package/dist/tests/compiler.test.js +201 -0
- package/dist/tests/enviornment.test.d.ts +1 -0
- package/dist/tests/enviornment.test.js +279 -0
- package/dist/tests/express.test.d.ts +1 -0
- package/dist/tests/express.test.js +86 -0
- package/dist/tests/filters.test.d.ts +13 -0
- package/dist/tests/filters.test.js +286 -0
- package/dist/tests/globals.test.d.ts +1 -0
- package/dist/tests/globals.test.js +579 -0
- package/dist/tests/interpreter.test.d.ts +1 -0
- package/dist/tests/interpreter.test.js +208 -0
- package/dist/tests/lexer.test.d.ts +1 -0
- package/dist/tests/lexer.test.js +249 -0
- package/dist/tests/lib.test.d.ts +1 -0
- package/dist/tests/lib.test.js +236 -0
- package/dist/tests/loader.test.d.ts +1 -0
- package/dist/tests/loader.test.js +301 -0
- package/dist/tests/nodes.test.d.ts +1 -0
- package/dist/tests/nodes.test.js +137 -0
- package/dist/tests/parser.test.d.ts +1 -0
- package/dist/tests/parser.test.js +294 -0
- package/dist/tests/precompile.test.d.ts +1 -0
- package/dist/tests/precompile.test.js +224 -0
- package/dist/tests/runtime.test.d.ts +1 -0
- package/dist/tests/runtime.test.js +237 -0
- package/dist/tests/transformer.test.d.ts +1 -0
- package/dist/tests/transformer.test.js +125 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Copyright (c) 2025-2027, Samuel Bines
|
|
2
|
+
All rights reserved.
|
|
3
|
+
|
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
|
5
|
+
modification, are permitted provided that the following conditions are
|
|
6
|
+
met:
|
|
7
|
+
|
|
8
|
+
Redistributions of source code must retain the above copyright
|
|
9
|
+
notice, this list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
Redistributions in binary form must reproduce the above copyright
|
|
12
|
+
notice, this list of conditions and the following disclaimer in
|
|
13
|
+
the documentation and/or other materials provided with the
|
|
14
|
+
distribution.
|
|
15
|
+
|
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
17
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
18
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
19
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
20
|
+
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
21
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
22
|
+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
23
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
24
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
25
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
26
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Nunjucks
|
|
2
|
+
|
|
3
|
+
[![NPM Version][npm-image]][npm-url]
|
|
4
|
+
[![NPM Downloads][downloads-image]][downloads-url]
|
|
5
|
+
[![CI][github-actions-image]][github-actions-url]
|
|
6
|
+
[![Codecov][codecov-image]][codecov-url]
|
|
7
|
+
|
|
8
|
+
[Nunjucks](https://mozilla.github.io/nunjucks/) is a full featured
|
|
9
|
+
templating engine for javascript. It is heavily inspired by
|
|
10
|
+
[jinja2](https://jinja.palletsprojects.com/). View the docs
|
|
11
|
+
[here](https://mozilla.github.io/nunjucks/).
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
`npm install nunjucks`
|
|
16
|
+
|
|
17
|
+
To use the file watcher built-in to Nunjucks, Chokidar must be installed separately.
|
|
18
|
+
|
|
19
|
+
`npm install nunjucks chokidar`
|
|
20
|
+
|
|
21
|
+
(View the [CHANGELOG](https://github.com/mozilla/nunjucks/releases))
|
|
22
|
+
|
|
23
|
+
## Documentation
|
|
24
|
+
|
|
25
|
+
Nunjucks info and documentation source is in [`/docs`](/docs) and publishes to https://mozilla.github.io/nunjucks/
|
|
26
|
+
|
|
27
|
+
## Browser Support
|
|
28
|
+
|
|
29
|
+
Supported in all modern browsers. For IE8 support, use [es5-shim](https://github.com/es-shims/es5-shim).
|
|
30
|
+
|
|
31
|
+
## Tests
|
|
32
|
+
|
|
33
|
+
Run the tests with `npm test`.
|
|
34
|
+
|
|
35
|
+
## Get in touch
|
|
36
|
+
|
|
37
|
+
If you have ideas or questions, or need help, you can reach us in the [#nunjucks:mozilla.org](https://matrix.to/#/#nunjucks:mozilla.org) room on [Matrix](https://wiki.mozilla.org/Matrix).
|
|
38
|
+
|
|
39
|
+
## Want to help?
|
|
40
|
+
|
|
41
|
+
Contributions are always welcome! Before you submit an issue or pull request, please read our [contribution guidelines](CONTRIBUTING.md).
|
|
42
|
+
|
|
43
|
+
## Contributors
|
|
44
|
+
|
|
45
|
+
[](https://github.com/mozilla/nunjucks/graphs/contributors)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
[npm-image]: https://img.shields.io/npm/v/nunjucks.svg
|
|
49
|
+
[npm-url]: https://npmjs.org/package/nunjucks
|
|
50
|
+
[downloads-image]: https://img.shields.io/npm/dm/nunjucks.svg
|
|
51
|
+
[downloads-url]: https://npmjs.org/package/nunjucks
|
|
52
|
+
[github-actions-image]: https://github.com/mozilla/nunjucks/actions/workflows/tests.yml/badge.svg
|
|
53
|
+
[github-actions-url]: https://github.com/mozilla/nunjucks/actions
|
|
54
|
+
[codecov-image]: https://img.shields.io/codecov/c/gh/mozilla/nunjucks.svg
|
|
55
|
+
[codecov-url]: https://codecov.io/gh/mozilla/nunjucks/branch/master
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function renderView(name: string, ctx: any): Promise<string>;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.renderView = renderView;
|
|
40
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
41
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
42
|
+
const Runtime = __importStar(require("../src/runtime"));
|
|
43
|
+
const environment_1 = require("../src/environment");
|
|
44
|
+
const parser_1 = require("../src/parser");
|
|
45
|
+
const transformer_1 = require("../src/transformer");
|
|
46
|
+
const interpreter_1 = require("../src/interpreter");
|
|
47
|
+
const lib_1 = require("../src/lib");
|
|
48
|
+
const VIEWS = node_path_1.default.resolve(process.cwd(), 'scripts/views');
|
|
49
|
+
// nunjucks.configure('./views', {
|
|
50
|
+
// dev: true,
|
|
51
|
+
// watch: false,
|
|
52
|
+
// cache: false,
|
|
53
|
+
// });
|
|
54
|
+
const templates = {
|
|
55
|
+
"base.html": "<title>{% block title %}Base{% endblock %}</title>{% block body %}BaseBody{% endblock %}",
|
|
56
|
+
"index.html": "{% extends 'base.html' %}{% block title %}{{ title }}{% endblock %}{% block body %}Hello {{ name }}{% endblock %}",
|
|
57
|
+
};
|
|
58
|
+
const envSetup = {
|
|
59
|
+
autoescape: true,
|
|
60
|
+
throwOnUndefined: false,
|
|
61
|
+
path: VIEWS,
|
|
62
|
+
};
|
|
63
|
+
function makeEnv() {
|
|
64
|
+
const env = new environment_1.Environment(envSetup);
|
|
65
|
+
// add a couple baseline filters
|
|
66
|
+
env.addFilter?.('upper', (s) => String(s).toUpperCase());
|
|
67
|
+
env.addFilter?.('join', (arr, sep = ',') => (arr ?? []).join(sep));
|
|
68
|
+
return env;
|
|
69
|
+
}
|
|
70
|
+
async function renderView(name, ctx) {
|
|
71
|
+
const env = makeEnv();
|
|
72
|
+
const tpl = await env.getTemplate(name, undefined, { eagerCompile: true });
|
|
73
|
+
return await new Promise((resolve, reject) => {
|
|
74
|
+
tpl.render(ctx, (err, res) => (err ? reject(err) : resolve(res)));
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
async function renderFileDirect(name, ctx, env) {
|
|
78
|
+
const src = await node_fs_1.default.readFileSync(node_path_1.default.join(VIEWS, name), 'utf8');
|
|
79
|
+
const ast = (0, transformer_1.transform)((0, parser_1.parse)(src, [], env), []);
|
|
80
|
+
return (0, interpreter_1.renderAST)(env, ast, ctx, Runtime);
|
|
81
|
+
}
|
|
82
|
+
// async function renderString(src: string, ctx: Context, env = makeEnv()) {
|
|
83
|
+
// const ast = transform(parse(src, [], env), []); // adjust args to your parse/transform signatures
|
|
84
|
+
// return renderAST(env, ast, ctx, runtime);
|
|
85
|
+
// }
|
|
86
|
+
async function main() {
|
|
87
|
+
const env = new environment_1.Environment(envSetup);
|
|
88
|
+
const src = 'Hello {{ name }}';
|
|
89
|
+
const ast = (0, transformer_1.transform)((0, parser_1.parse)(src, [], {}), []);
|
|
90
|
+
const out = await (0, interpreter_1.renderAST)(env, ast, { name: 'Sam' }, Runtime);
|
|
91
|
+
lib_1.p.log(out);
|
|
92
|
+
const viewOut = await renderFileDirect('index.html', { name: 'Sam', title: 'Hello world' }, env);
|
|
93
|
+
lib_1.p.log(viewOut);
|
|
94
|
+
}
|
|
95
|
+
main();
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
interface ICompilerOpts {
|
|
2
|
+
codebuf?: any[];
|
|
3
|
+
lastId?: number;
|
|
4
|
+
buffer?: any;
|
|
5
|
+
bufferStack?: Buffer[];
|
|
6
|
+
_scopeClosers?: string;
|
|
7
|
+
inBlock?: boolean;
|
|
8
|
+
throwOnUndefined?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export declare const compile: (src: string, //TODO: check this is true
|
|
11
|
+
asyncFilters: readonly string[], extensions: any[], name: string, opts?: ICompilerOpts) => string;
|
|
12
|
+
export {};
|