From 4cfbf2e24bb09f2a8bc8c30bc422f8f31229e203 Mon Sep 17 00:00:00 2001 From: Hoylen Sue Date: Wed, 13 Jan 2021 23:01:59 +1000 Subject: [PATCH] Fixed handling of line endings in CSV files. --- CHANGELOG.md | 1 + lib/src/csv_data.dart | 12 +++++++++--- lib/src/format_html.dart | 7 +++---- lib/src/template.dart | 9 ++++++--- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 320c998..ce456ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/src/csv_data.dart b/lib/src/csv_data.dart index 2056043..fa65d4e 100644 --- a/lib/src/csv_data.dart +++ b/lib/src/csv_data.dart @@ -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 = []; final records = []; diff --git a/lib/src/format_html.dart b/lib/src/format_html.dart index af80bb5..6bc1b80 100644 --- a/lib/src/format_html.dart +++ b/lib/src/format_html.dart @@ -680,6 +680,8 @@ p.timestamp { -'''); + buf.write('-->\n'); } //================================================================ diff --git a/lib/src/template.dart b/lib/src/template.dart index 6dd4202..ee59ed2 100644 --- a/lib/src/template.dart +++ b/lib/src/template.dart @@ -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