diff --git a/src/lib.rs b/src/lib.rs index d365a88..3125425 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -108,9 +108,34 @@ fn transform_line_endings(input: String, mode: TransformMode) -> String { String::from_utf8(output.into_inner()).unwrap() } +fn segments_from_string(string: String) -> Vec { + let mut output:Vec = Vec::new(); + // are we inside `"""\n...\n"""`? if so, ignore empty lines + let mut inside_escaped_block = false; + let mut current_segment : Vec = Vec::new(); + let lines: Vec<&str> = string.lines().collect(); + + for line in lines { + if line == "\"\"\"" { + inside_escaped_block = ! inside_escaped_block; + } + + if line == "" && !inside_escaped_block { + output.push(current_segment.join("\n")); + current_segment = Vec::new(); + } else { + current_segment.push(line.to_string()); + } + } + + output.push(current_segment.join("\n")); + + output +} + #[cfg(test)] mod test { - use crate::{from_base36, ToBase36, optional_data_line, mock}; + use crate::{from_base36, ToBase36, optional_data_line, mock, segments_from_string}; #[test] fn test_from_base36() { @@ -129,4 +154,20 @@ mod test { let output = optional_data_line("NAME", mock::item().name); assert_eq!(output, "\nNAME door".to_string()); } + + #[test] + fn test_string_to_segments() { + let output = segments_from_string( + include_str!("./test-resources/segments").to_string() + ); + + let expected = vec![ + "\"\"\"\nthe first segment is a long bit of text\n\n\nit contains empty lines\n\n\"\"\"".to_string(), + "this is a new segment\nthis is still the second segment\nblah\nblah".to_string(), + "DLG SEGMENT_3\n\"\"\"\nthis is a short \"long\" bit of text\n\"\"\"".to_string(), + "this is the last segment".to_string(), + ]; + + assert_eq!(output, expected); + } } diff --git a/src/test-resources/segments b/src/test-resources/segments new file mode 100644 index 0000000..e8c661e --- /dev/null +++ b/src/test-resources/segments @@ -0,0 +1,19 @@ +""" +the first segment is a long bit of text + + +it contains empty lines + +""" + +this is a new segment +this is still the second segment +blah +blah + +DLG SEGMENT_3 +""" +this is a short "long" bit of text +""" + +this is the last segment \ No newline at end of file