Updated jael

This commit is contained in:
thomashii 2021-12-20 01:39:16 +08:00
parent 6ffbdcf442
commit 9dc5f60379
4 changed files with 43 additions and 22 deletions

View file

@ -1,5 +1,9 @@
# Change Log
## 4.2.2
* Set default `cacheViews` to true
## 4.2.1
* Added `minified` parameter for generating minified HTML output

View file

@ -14,19 +14,19 @@ import 'package:belatuk_symbol_table/belatuk_symbol_table.dart';
///
/// To apply additional transforms to parsed documents, provide a set of [patch] functions.
AngelConfigurer jael(Directory viewsDirectory,
{String? fileExtension,
{String fileExtension = '.jael',
bool strictResolution = false,
bool cacheViews = false,
bool cacheViews = true,
Iterable<Patcher> patch = const [],
bool asDSX = false,
bool minified = false,
CodeBuffer Function()? createBuffer}) {
var cache = <String, Document?>{};
fileExtension ??= '.jael';
if (createBuffer == null && minified) {
createBuffer = () => CodeBuffer(space: '', newline: '');
} else {
createBuffer ??= () => CodeBuffer();
var cache = <String, Document>{};
var bufferFunc = createBuffer ?? () => CodeBuffer();
if (minified) {
bufferFunc = () => CodeBuffer(space: '', newline: '');
}
return (Angel app) async {
@ -34,28 +34,33 @@ AngelConfigurer jael(Directory viewsDirectory,
var errors = <JaelError>[];
Document? processed;
if (cacheViews == true && cache.containsKey(name)) {
if (cacheViews && cache.containsKey(name)) {
processed = cache[name];
} else {
var file = viewsDirectory.childFile(name + fileExtension!);
var file = viewsDirectory.childFile(name + fileExtension);
var contents = await file.readAsString();
var doc = parseDocument(contents,
sourceUrl: file.uri, asDSX: asDSX, onError: errors.add)!;
processed = doc;
sourceUrl: file.uri, asDSX: asDSX, onError: errors.add);
if (doc == null) {
throw ArgumentError(name + fileExtension + " does not exists");
}
try {
processed = await (resolve(doc, viewsDirectory,
patch: patch, onError: errors.add));
} catch (_) {
} catch (e) {
// Ignore these errors, so that we can show syntax errors.
}
if (processed == null) {
throw ArgumentError(name + fileExtension + " does not exists");
}
if (cacheViews == true) {
if (cacheViews) {
cache[name] = processed;
}
}
var buf = createBuffer!();
var buf = bufferFunc();
var scope = SymbolTable(
values: locals?.keys.fold<Map<String, dynamic>>(<String, dynamic>{},
(out, k) => out..[k.toString()] = locals[k]) ??

View file

@ -1,5 +1,5 @@
name: angel3_jael
version: 4.2.1
version: 4.2.2
description: Angel support for the Jael templating engine, similar to Blade or Liquid.
homepage: https://angel3-framework.web.app/
repository: https://github.com/dukefirehawk/angel/tree/master/packages/jael/angel_jael
@ -17,9 +17,4 @@ dev_dependencies:
angel3_test: ^4.0.0
html: ^0.15.0
test: ^1.17.3
lints: ^1.0.0
#dependency_overrides:
# jael3:
# path: ../jael
# jael3_preprocessor:
# path: ../jael_preprocessor
lints: ^1.0.0

View file

@ -72,4 +72,21 @@ void main() {
.trim())
.outerHtml);
});
test('can render multiples', () async {
var response = await client.get(Uri.parse('/github/thosakwe'));
response = await client.get(Uri.parse('/github/thosakwe'));
response = await client.get(Uri.parse('/github/thosakwe'));
response = await client.get(Uri.parse('/github/thosakwe'));
response = await client.get(Uri.parse('/github/thosakwe'));
print('Body:\n${response.body}');
expect(
html.parse(response.body).outerHtml,
html
.parse(
'''<html><head><title>Hello</title></head><body>thosakwe</body></html>'''
.trim())
.outerHtml);
});
}