diff --git a/packages/jael/angel_jael/CHANGELOG.md b/packages/jael/angel_jael/CHANGELOG.md index 8863b82c..21a32dbe 100644 --- a/packages/jael/angel_jael/CHANGELOG.md +++ b/packages/jael/angel_jael/CHANGELOG.md @@ -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` diff --git a/packages/jael/angel_jael/lib/angel3_jael.dart b/packages/jael/angel_jael/lib/angel3_jael.dart index 3b433037..be194ee3 100644 --- a/packages/jael/angel_jael/lib/angel3_jael.dart +++ b/packages/jael/angel_jael/lib/angel3_jael.dart @@ -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? patch, + Iterable patch = const [], bool asDSX = false, + bool minified = false, CodeBuffer Function()? createBuffer}) { var cache = {}; 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 { diff --git a/packages/jael/angel_jael/pubspec.yaml b/packages/jael/angel_jael/pubspec.yaml index f1aa6d0c..976e08b7 100644 --- a/packages/jael/angel_jael/pubspec.yaml +++ b/packages/jael/angel_jael/pubspec.yaml @@ -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 diff --git a/packages/jael/angel_jael/test/minified_test.dart b/packages/jael/angel_jael/test/minified_test.dart new file mode 100644 index 00000000..5f41fd32 --- /dev/null +++ b/packages/jael/angel_jael/test/minified_test.dart @@ -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(''' + + + + Hello + + + + Fallback content + + + + '''); + + viewsDirectory.childFile('github.jael').writeAsStringSync(''' + + {{username}} + + '''); + + 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( + '''Hellothosakwe''' + .trim()) + .outerHtml); + }); +}