diff options
author | Rory Dudley | 2024-02-29 00:09:25 -0700 |
---|---|---|
committer | Rory Dudley | 2024-02-29 00:09:25 -0700 |
commit | 691444ca356820a0174d46a6cf1db0f65abae521 (patch) | |
tree | 383f6925514fd2031a0a2985612c9817afcc0556 | |
parent | 75407774ce163df53c28391fb653c3714abd1aef (diff) | |
download | dwarvish-691444ca356820a0174d46a6cf1db0f65abae521.tar.gz |
Parsing poems from a file
Added a match statement in the main parser loop that pushes a new verse
into the poem if a newline is found. This might happen if parsing a poem
from a file.
-rw-r--r-- | src/recite.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/recite.rs b/src/recite.rs index 9860eff..a655aa7 100644 --- a/src/recite.rs +++ b/src/recite.rs @@ -609,6 +609,28 @@ impl Poem { word.clear(); } + // The character is a newline (may happen if parsing from a file) + Some(char) if char == '\n' => { + // If there are chars on the word stack, push that word + // onto the stanza + if !word.is_empty() { + stanza.push(word.iter().collect()); + } + + // A newline indicates the end of a verse + if !stanza.is_empty() { + verses.push(Verse::new( + Stanza::new(stanza.clone()), + Meter::String, + Verse::couplet(prev), + )); + } + + // Clear the stacks + stanza.clear(); + word.clear(); + } + // The character is whitespace Some(char) if char == ' ' || char == '\t' => { // If there are chars on the word stack, push that word |