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

View file

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

View file

@ -72,4 +72,21 @@ void main() {
.trim()) .trim())
.outerHtml); .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);
});
} }