addBlankLinesBeforeHeaders function
- String input
We assume a header line is non-blank and does not contain a colon (":"). If a header line is found and the preceding line is not blank, an empty line is inserted.
Implementation
String addBlankLinesBeforeHeaders(String input) {
final lines = input.split('\n');
final List<String> outputLines = [];
for (int i = 0; i < lines.length; i++) {
final line = lines[i];
final trimmed = line.trim();
// Consider a line a header if it's non-empty and does NOT contain a colon.
final isHeader = trimmed.isNotEmpty && !trimmed.contains(':');
// For header lines (except the very first line), if the previous line in
// the output is not blank, add an empty line.
if (isHeader &&
outputLines.isNotEmpty &&
outputLines.last.trim().isNotEmpty) {
outputLines.add('');
}
outputLines.add(line);
}
return outputLines.join('\n');
}