@@ -3,7 +3,7 @@ part of dsg;
3
3
/// Takes a template string (such as a Mustache template) and renders it out to an HTML string
4
4
/// using the given input values/options.
5
5
///
6
- typedef TemplateRenderer = String Function (String template, Map options, PartialsResolver resolver);
6
+ typedef TemplateRenderer = String Function (String template, Map < dynamic , dynamic > options, PartialsResolver resolver);
7
7
8
8
/// Resolved partial-names into mustache.Templates
9
9
typedef PartialsResolver = mustache.Template Function (String name);
@@ -15,12 +15,13 @@ String _outputFormat = 'MMMM dd yyyy';
15
15
///
16
16
/// Uses [Mustache templates] (https://pub.dartlang.org/packages/mustache) by default.
17
17
///
18
- TemplateRenderer renderTemplate = (final String ? source, final Map options, final PartialsResolver resolver) {
18
+ TemplateRenderer renderTemplate =
19
+ (final String ? source, final Map <dynamic , dynamic > options, final PartialsResolver resolver) {
19
20
final template = mustache.Template (source ?? '' , htmlEscapeValues: false , partialResolver: resolver, lenient: true );
20
21
21
22
final inFormat = 'yyyy-MM-dd' ;
22
23
23
- final formatDate = (mustache.LambdaContext ctx) => '${ _parseDate (ctx .renderString (), inFormat , _outputFormat )}' ;
24
+ formatDate (mustache.LambdaContext ctx) => _parseDate (ctx.renderString (), inFormat, _outputFormat);
24
25
25
26
options['formatDate' ] = formatDate;
26
27
@@ -31,6 +32,7 @@ TemplateRenderer renderTemplate = (final String? source, final Map options, fina
31
32
32
33
class Generator {
33
34
/// Mustache-Renderer strips out newlines
35
+ // ignore: constant_identifier_names
34
36
static const String _NEWLINE_PROTECTOR = '@@@#@@@' ;
35
37
36
38
/// Render and output your static site (WARNING: overwrites existing HTML files in output directory).
@@ -69,11 +71,11 @@ class Generator {
69
71
log ('Generating .html files...' );
70
72
71
73
for (final file in files) {
72
- final relativeFileName = file.path.replaceAll ('${ contentDir .path }' , '' ).replaceFirst ('/' , '' );
74
+ final relativeFileName = file.path.replaceAll (contentDir.path, '' ).replaceFirst ('/' , '' );
73
75
final relativePath = path.dirname (relativeFileName).replaceFirst ('.' , '' );
74
76
final extension = path.extension (relativeFileName).replaceFirst ('.' , '' ).toLowerCase ();
75
77
76
- log ('\n File: ${ relativeFileName } , Path: $relativePath ' );
78
+ log ('\n File: $relativeFileName , Path: $relativePath ' );
77
79
78
80
final fileContents = file.readAsStringSync ();
79
81
fm.FrontMatterDocument fmDocument;
@@ -133,7 +135,7 @@ class Generator {
133
135
templateContent, pageOptions, _partialsResolver (partialsDir, isMarkdownSupported: config.usemarkdown)),
134
136
config);
135
137
136
- final outputFilename = '${path .basenameWithoutExtension (relativeFileName )}.${ outputExtension } ' ;
138
+ final outputFilename = '${path .basenameWithoutExtension (relativeFileName )}.$outputExtension ' ;
137
139
final outputPath = _createOutputPath (outputDir, relativePath);
138
140
final outputFile = File ('${outputPath .path }/$outputFilename ' );
139
141
@@ -143,7 +145,7 @@ class Generator {
143
145
}
144
146
145
147
for (final image in images) {
146
- final relativeFileName = image.path.replaceAll ('${ contentDir .path }' , '' ).replaceFirst ('/' , '' );
148
+ final relativeFileName = image.path.replaceAll (contentDir.path, '' ).replaceFirst ('/' , '' );
147
149
final relativePath = path.dirname (relativeFileName).startsWith ('.' )
148
150
? path.dirname (relativeFileName)
149
151
: path.dirname (relativeFileName).replaceFirst ('.' , '' );
@@ -157,7 +159,7 @@ class Generator {
157
159
}
158
160
159
161
for (final asset in assets) {
160
- final relativeFileName = asset.path.replaceAll ('${ assetsDir .path }' , '' ).replaceFirst ('/' , '' );
162
+ final relativeFileName = asset.path.replaceAll (assetsDir.path, '' ).replaceFirst ('/' , '' );
161
163
final relativePath = path.dirname (relativeFileName).replaceFirst ('.' , '' );
162
164
163
165
final outputPath = _createOutputPath (outputDir, relativePath);
@@ -182,13 +184,13 @@ class Generator {
182
184
///
183
185
void _resolvePartialsInYamlBlock (
184
186
final Directory partialsDir, final Map <String , dynamic > pageOptions, bool useMarkdown) {
185
- pageOptions.keys. forEach (( final String key) {
187
+ for ( var key in pageOptions.keys ) {
186
188
if (pageOptions[key] is String && (pageOptions[key] as String ).contains ('->' )) {
187
189
final partial = (pageOptions[key] as String ).replaceAll (RegExp (r'[^>]*>' ), '' );
188
190
pageOptions[key] = renderTemplate (
189
- '{{>${ partial } }}' , pageOptions, _partialsResolver (partialsDir, isMarkdownSupported: useMarkdown));
191
+ '{{>$partial }}' , pageOptions, _partialsResolver (partialsDir, isMarkdownSupported: useMarkdown));
190
192
}
191
- });
193
+ }
192
194
}
193
195
194
196
/// Returns a partials-Resolver. The partials-Resolver gets a dot separated name. This name is translated
@@ -225,7 +227,7 @@ class Generator {
225
227
226
228
Directory _createOutputPath (final Directory outputDir, final String relativePath) {
227
229
final relPath = relativePath.isNotEmpty ? '/' : '' ;
228
- final outputPath = Directory ('${outputDir .path }${ relPath }${ relativePath } ' );
230
+ final outputPath = Directory ('${outputDir .path }$relPath $ relativePath ' );
229
231
if (! outputPath.existsSync ()) {
230
232
outputPath.createSync (recursive: true );
231
233
}
@@ -318,9 +320,9 @@ class Generator {
318
320
.toList ();
319
321
}
320
322
321
- bool _isMarkdownSupported (final bool markdownForSite, final Map page_options ) {
323
+ bool _isMarkdownSupported (final bool markdownForSite, final Map < dynamic , dynamic > pageOptions ) {
322
324
return markdownForSite ||
323
- (page_options .containsKey ('markdown_templating' ) && page_options ['markdown_templating' ] as bool );
325
+ (pageOptions .containsKey ('markdown_templating' ) && pageOptions ['markdown_templating' ] as bool );
324
326
}
325
327
326
328
Map <String , dynamic > _fillInDefaultPageOptions (final String defaultDateFormat, final File file,
@@ -331,22 +333,22 @@ class Generator {
331
333
pageOptions['_site' ] = siteOptions;
332
334
333
335
/// See [DateFormat] (https://api.dartlang.org/docs/channels/stable/latest/intl/DateFormat.html) for formatting options
334
- final date_format = DateFormat (defaultDateFormat);
336
+ final dateFormat = DateFormat (defaultDateFormat);
335
337
336
338
if (pageOptions.containsKey ('date_format' )) {
337
- final page_date_format = DateFormat (pageOptions['date_format' ] as String );
338
- pageOptions['_date' ] = page_date_format .format (file.lastModifiedSync ());
339
+ final pageDateFormat = DateFormat (pageOptions['date_format' ] as String );
340
+ pageOptions['_date' ] = pageDateFormat .format (file.lastModifiedSync ());
339
341
} else {
340
- pageOptions['_date' ] = date_format .format (file.lastModifiedSync ());
342
+ pageOptions['_date' ] = dateFormat .format (file.lastModifiedSync ());
341
343
}
342
344
343
345
return pageOptions;
344
346
}
345
347
346
- Map _getDataMap (final List <File > dataFiles) {
348
+ Map < dynamic , dynamic > _getDataMap (final List <File > dataFiles) {
347
349
final dataMap = < String , dynamic > {};
348
350
349
- dataFiles. forEach (( final File file) {
351
+ for ( var file in dataFiles ) {
350
352
if (file.existsSync ()) {
351
353
dynamic data;
352
354
if (path.extension (file.path) == '.yaml' ) {
@@ -358,7 +360,7 @@ class Generator {
358
360
final filename = path.basenameWithoutExtension (file.path).toLowerCase ();
359
361
dataMap[filename] = data;
360
362
}
361
- });
363
+ }
362
364
363
365
return dataMap;
364
366
}
@@ -380,13 +382,13 @@ class Generator {
380
382
if (relativeFileName.contains ('/' )) {
381
383
nestingLevel = relativeFileName.split ('/' ).length - 1 ;
382
384
for (var counter = 0 ; counter < nestingLevel; counter++ ) {
383
- backPath = backPath + ' ../' ;
385
+ backPath = '$ backPath ../' ;
384
386
}
385
387
}
386
388
387
389
final pathWithoutExtension = path.withoutExtension (relativeFileName);
388
390
// final String portablePath = pathWithoutExtension.replaceAll( RegExp('(/|\\\\\)'),':');
389
- final pageIndicator = pathWithoutExtension.replaceAll (RegExp ('(/|\\\\\ ) ' ), '_' );
391
+ final pageIndicator = pathWithoutExtension.replaceAll (RegExp ('(/|\\\\ )' ), '_' );
390
392
pageOptions['_page' ] = {
391
393
'filename' : pathWithoutExtension,
392
394
'pageindicator' : pageIndicator,
@@ -402,17 +404,18 @@ class Generator {
402
404
}
403
405
404
406
File _getTemplateFor (
405
- final File file, final Map page_options, final List <File > templates, final String defaultTemplate) {
407
+ final File file, final Map <dynamic , dynamic > pageOptions, final List <File > templates,
408
+ final String defaultTemplate) {
406
409
final filenameWithoutExtension = path.basenameWithoutExtension (file.path);
407
410
final filepath = path.normalize (file.path);
408
411
409
412
File template;
410
413
//_logger.info('Templates: ${templates}, Default: ${defaultTemplate}');
411
414
412
415
try {
413
- if (page_options .containsKey ('template' )) {
416
+ if (pageOptions .containsKey ('template' )) {
414
417
template = templates
415
- .firstWhere ((final File file) => path.basenameWithoutExtension (file.path) == page_options ['template' ]);
418
+ .firstWhere ((final File file) => path.basenameWithoutExtension (file.path) == pageOptions ['template' ]);
416
419
} else if (defaultTemplate.isNotEmpty) {
417
420
template = templates.firstWhere ((final File file) {
418
421
return path.basenameWithoutExtension (file.path) == path.basenameWithoutExtension (defaultTemplate);
@@ -432,12 +435,12 @@ class Generator {
432
435
/// Currently only supports replacing Unix-style relative paths.
433
436
///
434
437
String _fixPathRefs (String html, final Config config) {
435
- var relative_output = path.relative (config.outputfolder, from: config.templatefolder);
438
+ var relativeOutput = path.relative (config.outputfolder, from: config.templatefolder);
436
439
437
- relative_output = '$relative_output /' .replaceAll ('\\ ' , '/' );
440
+ relativeOutput = '$relativeOutput /' .replaceAll ('\\ ' , '/' );
438
441
//_logger.info(relative_output);
439
442
440
- html = html.replaceAll ('src="$relative_output ' , 'src="' ).replaceAll ('href="$relative_output ' , 'href="' );
443
+ html = html.replaceAll ('src="$relativeOutput ' , 'src="' ).replaceAll ('href="$relativeOutput ' , 'href="' );
441
444
442
445
return html;
443
446
}
@@ -448,25 +451,25 @@ class Generator {
448
451
final Map <String , dynamic > pageOptions, final Config config) {
449
452
check (relativeFileName).isNotEmpty ();
450
453
451
- log (' --- ${(relativeFileName + " " ).padRight (76 , "-" )}' );
454
+ log (' --- ${("$ relativeFileName " ).padRight (76 , "-" )}' );
452
455
453
- void _showMap (final Map <String , dynamic > values, final int nestingLevel) {
456
+ void showMap (final Map <String , dynamic > values, final int nestingLevel) {
454
457
values.forEach ((final String key, final dynamic value) {
455
458
log (' ${"" .padRight (nestingLevel * 2 )} $key .' );
456
459
457
460
if (value is Map ) {
458
- _showMap (value as Map <String , dynamic >, nestingLevel + 1 );
461
+ showMap (value as Map <String , dynamic >, nestingLevel + 1 );
459
462
} else {
460
463
var valueAsString =
461
- value.toString ().replaceAll (RegExp ('(\n |\r |\\ s{2,}|${ _NEWLINE_PROTECTOR } )' , multiLine: true ), '' );
464
+ value.toString ().replaceAll (RegExp ('(\n |\r |\\ s{2,}|$_NEWLINE_PROTECTOR )' , multiLine: true ), '' );
462
465
463
466
valueAsString = valueAsString.substring (0 , math.min (50 , math.max (valueAsString.length, 0 )));
464
- log (' ${"" .padRight (nestingLevel * 2 )} $key -> [${ valueAsString } ]' );
467
+ log (' ${"" .padRight (nestingLevel * 2 )} $key -> [$valueAsString ]' );
465
468
}
466
469
});
467
470
}
468
471
469
- _showMap (pageOptions, 0 );
472
+ showMap (pageOptions, 0 );
470
473
log (' ${"" .padRight (80 , "-" )}' );
471
474
}
472
475
}
0 commit comments