2021-05-15 10:45:39 +00:00
|
|
|
import 'package:angel3_framework/angel3_framework.dart';
|
2021-09-25 06:32:32 +00:00
|
|
|
import 'package:belatuk_code_buffer/belatuk_code_buffer.dart';
|
2017-09-30 23:01:30 +00:00
|
|
|
import 'package:file/file.dart';
|
2021-05-15 10:45:39 +00:00
|
|
|
import 'package:jael3/jael3.dart';
|
|
|
|
import 'package:jael3_preprocessor/jael3_preprocessor.dart';
|
2021-09-13 00:30:02 +00:00
|
|
|
import 'package:belatuk_symbol_table/belatuk_symbol_table.dart';
|
2017-09-30 23:01:30 +00:00
|
|
|
|
2017-10-01 03:14:44 +00:00
|
|
|
/// Configures an Angel server to use Jael to render templates.
|
|
|
|
///
|
2021-11-25 00:33:56 +00:00
|
|
|
/// 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].
|
2017-10-02 16:12:51 +00:00
|
|
|
///
|
|
|
|
/// To apply additional transforms to parsed documents, provide a set of [patch] functions.
|
2017-09-30 23:01:30 +00:00
|
|
|
AngelConfigurer jael(Directory viewsDirectory,
|
2021-12-19 17:39:16 +00:00
|
|
|
{String fileExtension = '.jael',
|
2021-04-30 07:19:26 +00:00
|
|
|
bool strictResolution = false,
|
2021-12-19 17:39:16 +00:00
|
|
|
bool cacheViews = true,
|
2021-11-25 00:33:56 +00:00
|
|
|
Iterable<Patcher> patch = const [],
|
2021-04-30 07:19:26 +00:00
|
|
|
bool asDSX = false,
|
2021-11-25 00:33:56 +00:00
|
|
|
bool minified = false,
|
2021-09-25 06:32:32 +00:00
|
|
|
CodeBuffer Function()? createBuffer}) {
|
2021-12-19 17:39:16 +00:00
|
|
|
var cache = <String, Document>{};
|
|
|
|
|
|
|
|
var bufferFunc = createBuffer ?? () => CodeBuffer();
|
|
|
|
|
|
|
|
if (minified) {
|
|
|
|
bufferFunc = () => CodeBuffer(space: '', newline: '');
|
2021-11-25 00:33:56 +00:00
|
|
|
}
|
2017-09-30 23:01:30 +00:00
|
|
|
|
|
|
|
return (Angel app) async {
|
2021-04-30 07:19:26 +00:00
|
|
|
app.viewGenerator = (String name, [Map? locals]) async {
|
2017-09-30 23:01:30 +00:00
|
|
|
var errors = <JaelError>[];
|
2021-04-30 07:19:26 +00:00
|
|
|
Document? processed;
|
2017-10-01 03:14:44 +00:00
|
|
|
|
2021-12-19 17:39:16 +00:00
|
|
|
if (cacheViews && cache.containsKey(name)) {
|
2017-10-01 03:14:44 +00:00
|
|
|
processed = cache[name];
|
|
|
|
} else {
|
2021-12-19 17:39:16 +00:00
|
|
|
var file = viewsDirectory.childFile(name + fileExtension);
|
2017-10-01 03:14:44 +00:00
|
|
|
var contents = await file.readAsString();
|
2018-06-26 15:31:34 +00:00
|
|
|
var doc = parseDocument(contents,
|
2021-12-19 17:39:16 +00:00
|
|
|
sourceUrl: file.uri, asDSX: asDSX, onError: errors.add);
|
|
|
|
if (doc == null) {
|
|
|
|
throw ArgumentError(name + fileExtension + " does not exists");
|
|
|
|
}
|
2017-10-01 03:14:44 +00:00
|
|
|
|
|
|
|
try {
|
2021-04-30 07:19:26 +00:00
|
|
|
processed = await (resolve(doc, viewsDirectory,
|
|
|
|
patch: patch, onError: errors.add));
|
2021-12-19 17:39:16 +00:00
|
|
|
} catch (e) {
|
2017-10-01 03:14:44 +00:00
|
|
|
// Ignore these errors, so that we can show syntax errors.
|
|
|
|
}
|
2021-12-19 17:39:16 +00:00
|
|
|
if (processed == null) {
|
|
|
|
throw ArgumentError(name + fileExtension + " does not exists");
|
|
|
|
}
|
2017-10-01 03:14:44 +00:00
|
|
|
|
2021-12-19 17:39:16 +00:00
|
|
|
if (cacheViews) {
|
2017-10-01 03:14:44 +00:00
|
|
|
cache[name] = processed;
|
|
|
|
}
|
2017-09-30 23:01:30 +00:00
|
|
|
}
|
|
|
|
|
2021-12-19 17:39:16 +00:00
|
|
|
var buf = bufferFunc();
|
2021-04-30 07:19:26 +00:00
|
|
|
var scope = SymbolTable(
|
|
|
|
values: locals?.keys.fold<Map<String, dynamic>>(<String, dynamic>{},
|
2018-07-15 20:41:43 +00:00
|
|
|
(out, k) => out..[k.toString()] = locals[k]) ??
|
|
|
|
<String, dynamic>{});
|
2017-09-30 23:01:30 +00:00
|
|
|
|
|
|
|
if (errors.isEmpty) {
|
|
|
|
try {
|
2021-04-30 07:19:26 +00:00
|
|
|
const Renderer().render(processed!, buf, scope,
|
2017-10-16 23:20:02 +00:00
|
|
|
strictResolution: strictResolution == true);
|
2017-09-30 23:01:30 +00:00
|
|
|
return buf.toString();
|
|
|
|
} on JaelError catch (e) {
|
|
|
|
errors.add(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 15:31:34 +00:00
|
|
|
Renderer.errorDocument(errors, buf..clear());
|
2017-09-30 23:01:30 +00:00
|
|
|
return buf.toString();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|