Skip to content

Commit

Permalink
Fixed handling of line endings in CSV files.
Browse files Browse the repository at this point in the history
  • Loading branch information
hoylen committed Jan 13, 2021
1 parent f0aa4de commit 4cfbf2e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

- Updated template syntax: commands start with _ and comments with #.
- Changed unused properties to other or hidden properties.
- Fixed handling of line endings in CSV files.

## 1.0.0

Expand Down
12 changes: 9 additions & 3 deletions lib/src/csv_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@ class CsvData {
//----------------------------------------------------------------
/// Parses the [text] as Comma Separated Variables (CSV) data.
factory CsvData.load(String csvText, {String eol = '\n'}) {
final data = CsvToListConverter(eol: eol, shouldParseNumbers: false)
.convert(csvText);
factory CsvData.load(String csvText) {
// Parse the CSV
//
// CSV package's detection of eol is unreliable, so do our own handling
// of CR-LF and treat everything as LF.

final data = CsvToListConverter(
eol: '\n', shouldParseNumbers: false, allowInvalid: false)
.convert(csvText.replaceAll('\r\n', '\n'));

final propertyNames = <String>[];
final records = <Record>[];
Expand Down
7 changes: 3 additions & 4 deletions lib/src/format_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,8 @@ p.timestamp {
</html>
<!--
generator: csv2html <https://github.com/qcif/csv2html>
generated: ${hText(DateTime.now().toUtc().toIso8601String())}
''');

// Hidden timestamps
Expand All @@ -688,10 +690,7 @@ p.timestamp {
buf.write('timestamp: ${hText(timestamp.toUtc().toIso8601String())}\n');
}

buf.write('''
generated: ${hText(DateTime.now().toUtc().toIso8601String())}
-->
''');
buf.write('-->\n');
}

//================================================================
Expand Down
9 changes: 6 additions & 3 deletions lib/src/template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,18 @@ class RecordTemplate {
}

//----------------------------------------------------------------
/// Create a template by loading it from a file.
/// Create a template by parsing the CSV representation of it.
RecordTemplate.load(String specification) {
RecordTemplate.load(String templateCsv) {
try {
// Parse specification as CSV

// CSV package's detection of eol is unreliable, so do our own handling
// of CR-LF and treat everything as LF.

final data = CsvToListConverter(
eol: '\n', shouldParseNumbers: false, allowInvalid: false)
.convert(specification);
.convert(templateCsv.replaceAll('\r\n', '\n'));

// Ignore header row
// - column name
Expand Down

0 comments on commit 4cfbf2e

Please sign in to comment.