Updated jael

This commit is contained in:
thomashii 2021-11-25 08:33:56 +08:00
parent a8cb77a972
commit 3db5497480
4 changed files with 92 additions and 6 deletions

View file

@ -1,5 +1,9 @@
# Change Log
## 4.2.1
* Added `minified` parameter for generating minified HTML output
## 4.2.0
* Updated to use `package:belatuk_code_buffer`

View file

@ -7,20 +7,27 @@ import 'package:belatuk_symbol_table/belatuk_symbol_table.dart';
/// Configures an Angel server to use Jael to render templates.
///
/// To enable "minified" output, you need to override the [createBuffer] function,
/// to instantiate a [CodeBuffer] that emits no spaces or line breaks.
/// To enable "minified" output, set minified to true
///
/// For custom HTML formating, you need to override the [createBuffer] parameter
/// with a function that returns a new instance of [CodeBuffer].
///
/// To apply additional transforms to parsed documents, provide a set of [patch] functions.
AngelConfigurer jael(Directory viewsDirectory,
{String? fileExtension,
bool strictResolution = false,
bool cacheViews = false,
Iterable<Patcher>? patch,
Iterable<Patcher> patch = const [],
bool asDSX = false,
bool minified = false,
CodeBuffer Function()? createBuffer}) {
var cache = <String, Document?>{};
fileExtension ??= '.jael';
createBuffer ??= () => CodeBuffer();
if (createBuffer == null && minified) {
createBuffer = () => CodeBuffer(space: '', newline: '');
} else {
createBuffer ??= () => CodeBuffer();
}
return (Angel app) async {
app.viewGenerator = (String name, [Map? locals]) async {
@ -33,7 +40,7 @@ AngelConfigurer jael(Directory viewsDirectory,
var file = viewsDirectory.childFile(name + fileExtension!);
var contents = await file.readAsString();
var doc = parseDocument(contents,
sourceUrl: file.uri, asDSX: asDSX == true, onError: errors.add)!;
sourceUrl: file.uri, asDSX: asDSX, onError: errors.add)!;
processed = doc;
try {

View file

@ -1,5 +1,5 @@
name: angel3_jael
version: 4.2.0
version: 4.2.1
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

View file

@ -0,0 +1,75 @@
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_jael/angel3_jael.dart';
import 'package:angel3_test/angel3_test.dart';
import 'package:file/memory.dart';
import 'package:html/parser.dart' as html;
import 'package:logging/logging.dart';
import 'package:test/test.dart';
void main() {
// These tests need not actually test that the preprocessor or renderer works,
// because those packages are already tested.
//
// Instead, just test that we can render at all.
late TestClient client;
setUp(() async {
var app = Angel();
app.configuration['properties'] = app.configuration;
var fileSystem = MemoryFileSystem();
var viewsDirectory = fileSystem.directory('views')..createSync();
viewsDirectory.childFile('layout.jael').writeAsStringSync('''
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
</head>
<body>
<block name="content">
Fallback content
</block>
</body>
</html>
''');
viewsDirectory.childFile('github.jael').writeAsStringSync('''
<extend src="layout.jael">
<block name="content">{{username}}</block>
</extend>
''');
app.get('/github/:username', (req, res) {
var username = req.params['username'];
return res.render('github', {'username': username});
});
await app.configure(
jael(viewsDirectory, minified: true),
);
app.fallback((req, res) => throw AngelHttpException.notFound());
app.logger = Logger('angel')
..onRecord.listen((rec) {
print(rec);
if (rec.error != null) print(rec.error);
if (rec.stackTrace != null) print(rec.stackTrace);
});
client = await connectTo(app);
});
test('can render', () async {
var 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);
});
}