Skip to content

Commit

Permalink
Merge pull request #23 from maks/lists-sortby
Browse files Browse the repository at this point in the history
List sorting and date formating function
  • Loading branch information
maks authored Aug 7, 2020
2 parents 55acadd + a788b59 commit b13ac12
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,16 @@ Listing files are in yaml format with the following parameters supported: `path`

The `path` is a path relative to `.dsg/html/_content`.
The `filter` specifies which files to list within the given path.
The `sort_by` specifies a property.
The `sort_by` specifies a property **AND** the `ASC` or `DESC`.

eg.
eg. to sort in chronological descending order based on a front-matter property called `pub_date`

```yaml
path: blog
filter: "*.md"
sort_by: lastModified
sort_by: pub_date DESC
```
**note:** `sortby` not yet implemented.

**DSG** injects your listings into a global `_lists` variable. For each file all of the files YAML front-matter properties are available along with the files:

* `filename`: the files name without extension.
Expand Down Expand Up @@ -336,6 +334,25 @@ dart: ->usage.badge.dart
---
```

## Template Functions

Mustache provides for the use of "lambdas" which are essentially names functions that can be accessed in templates using the section syntax.
The currently available functions in DSG are listed below.

### formatDate

Allows formating either a hard coded string or another template variable written in the format `yyyy-MM-dd` the function will ouput the it using the format specified in `date_format` in `site.yaml`.

eg. given in site.yaml a `date_format: dd MMM yyyy` and then a variable defined in your front-matter like `article_date: 2020-01-26`
and then a template with:
```mustache
Article published on: {{# formatDate }} {{ date }} {{/ formatDate }}
```
will output: `Article published on: 26 Jan 2020`


## SASS
If DSG finds a .scss file in your output dir (web) it compiles it to the corresponding .css file.
Install instruction for SASS can be found [here](https://sass-lang.com/install)
Expand Down
1 change: 1 addition & 0 deletions lib/dsg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ part 'src/config.dart';
part 'src/generator.dart';
part 'src/init.dart';
part 'src/options.dart';
part 'src/dates.dart';

bool _runsOnOSX() => (SysInfo.operatingSystemName == 'Mac OS X');

Expand Down
13 changes: 13 additions & 0 deletions lib/src/dates.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
part of dsg;

String _parseDate(String dateStr, String parseFormat, String outputFormat) {
final _logger = Logger('dsg.dates');

try {
final date = DateFormat(parseFormat).parseLoose(dateStr.trim());
return DateFormat(outputFormat).format(date);
} catch (e) {
_logger.warning('FAILED to parse date: $dateStr $e');
return dateStr;
}
}
11 changes: 11 additions & 0 deletions lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ typedef TemplateRenderer = String Function(
/// Resolved partial-names into mustache.Templates
typedef PartialsResolver = mustache.Template Function(String name);

String _outputFormat = 'MMMM dd yyyy';

/// Can be set to define a custom [rendering function](TemplateRenderer) to handle your template files
/// and use any templating language of your choice.
///
Expand All @@ -19,6 +21,13 @@ TemplateRenderer renderTemplate =
final template = mustache.Template(source,
htmlEscapeValues: false, partialResolver: resolver, lenient: true);

final inFormat = 'yyyy-MM-dd';

final formatDate = (mustache.LambdaContext ctx) =>
'${_parseDate(ctx.renderString(), inFormat, _outputFormat)}';

options['formatDate'] = formatDate;

return template.renderString(options);
};

Expand Down Expand Up @@ -57,6 +66,8 @@ class Generator {

final dataMap = _getDataMap(dataFiles);

_outputFormat = config.dateformat;

_logger.info('Listings... ${listingsMap?.keys}');

_logger.info('Generating .html files...');
Expand Down
15 changes: 13 additions & 2 deletions lib/src/listings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,19 @@ Future<Map<String, List<Map>>> getListingsMap(
await listingsMap.forEach((key, value) async {
listings[key] = await value;

// TODO:
_logger.info('SORT listing: $key BY: ${configsMap[key].sortby}');
final sortBy = configsMap[key].sortby.split(' ')[0];
final sortDirection = configsMap[key].sortby.split(' ')[1];

final asc = (dynamic a, dynamic b) =>
a[sortBy].toString().compareTo(b[sortBy].toString());
final desc = (dynamic a, dynamic b) =>
b[sortBy].toString().compareTo(a[sortBy].toString());

final sortFunction = (sortDirection == 'DESC') ? desc : asc;

_logger.info('SORT listing: $key BY: $sortBy');

listings[key].sort(sortFunction);
});
return listings;
}
Expand Down

0 comments on commit b13ac12

Please sign in to comment.