cantara-songlib 0.1.6

Functionalities to import, manage and export songs in various formats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! This module contains functions to import songs from the classic Cantara song format.
//! The Cantara song format is a simple text format that is used to write songs in plain text files.
//! You can find a documentation here: <https://www.cantara.app/tutorial/where-to-get-the-songs/index.html#the-song-file-format>

use std::collections::HashMap;
use std::error::Error;
use std::{cell::RefCell, rc::Rc};
use std::sync::OnceLock;

extern crate regex;
use regex::{Regex,RegexBuilder};

use crate::importer::errors::CantaraImportNoContentError;
use crate::song::{
    LyricLanguage, 
    Song, 
    SongPart, 
    SongPartContent, 
    SongPartContentType, 
    SongPartType, 
};

use crate::slides::*;
use crate::templating::render_metadata;

use crate::importer::metadata::*;

fn parse_block(block: &str, song: Song) -> Result<Song, Box<dyn Error>> {
    if block.is_empty() {
        return Ok(song);
    }

    let mut cloned_song: Song = song.clone();

    // If first letter is a #, then parse the tags
    if block.starts_with('#') {
        
        // With that we make sure that the regex is only compiled once.
        let tags_regex = { 
            static TAGS_REGEX: OnceLock<Regex> = OnceLock::new();
            TAGS_REGEX.get_or_init(|| {
                RegexBuilder::new(r"\s*#(\w+):\s*(.+)$")
                    .multi_line(true)
                    .build()
                    .unwrap()
            })
        };        

        tags_regex
            .captures_iter(block)
            .for_each(|capture: regex::Captures| {
                let tag: &str = capture.get(1).unwrap().as_str();
                let value: &str = capture.get(2).unwrap().as_str();
                let tag_lowercase = tag.to_lowercase();
                cloned_song.add_tag(tag_lowercase.as_str(), value);
                if tag_lowercase == "title" {
                    cloned_song.title = value.to_string();
                }
            });
        return Ok(cloned_song);
    }

    // We will find first whether the content is already in the song, if yes, we have most likely a chorus.
    // If not, we will add a new verse.
    // If the content is already in the song, we will change the part type to chorus and add the content as a new chorus part.
    
    let content_vector = song.find_content_in_part(block);
    let (part_type, part_reference) = match content_vector.len() {
        0 => (SongPartType::Verse, None),
        _ => (SongPartType::Chorus, Some(content_vector.last().unwrap().clone())),
    };

    let lyric_language: LyricLanguage = LyricLanguage::Default;
    let lyrics_content: SongPartContent = SongPartContent {
        voice_type: SongPartContentType::Lyrics {
            language: lyric_language,
        },
        content: block.to_string(),
    };
    
    if part_reference.is_none() {    
        let song_part_reference: Rc<RefCell<SongPart>> = cloned_song.add_part_of_type(part_type, None);

        let mut song_part: std::cell::RefMut<SongPart> = song_part_reference.borrow_mut();
        let _ = &mut song_part.add_content(lyrics_content);
        song_part.set_repition(part_reference);
    } else {
        let unwrapped_reference = part_reference.unwrap();
        let mut previous_song_part: std::cell::RefMut<SongPart> = unwrapped_reference.borrow_mut();
        {
            let _ = &mut previous_song_part.set_type(SongPartType::Chorus);
        }
        previous_song_part.number = 1;
        let _ = &mut previous_song_part.update_id();
    }

    Ok(cloned_song)
}

/// Imports a song from a str which contains the song in the Cantara classic song format.
/// The function reads the content of the str and returns a result with a Song or an error.
/// The function guesses the part types (Refrain/Chorus, Verse, Bridge, etc.) based on the content and
/// keeps the song order which is provided.
pub fn import_song(content: &str) -> Result<Song, Box<dyn Error>> {
    if content.is_empty() {
        return Err(Box::new(CantaraImportNoContentError {}));
    }

    // Get the title either from the content or the filename
    let title: String = match get_title_from_file_content(content) {
        Some(title_string) => title_string,
        None => "".to_string()
    };

    let mut song: Song = Song::new(&title);

    let mut part: String = String::new();
    // Parse the blocks
    for line in content.trim().lines(){
        match line.trim() {
            "" => {
                if part.is_empty() {
                    continue;
                }
                song = parse_block(&part, song.clone()).unwrap();
                part.clear();
            }
            _ => {
                part.push_str(line.trim());
                part.push('\n');
            }
        }
    }
    if !(part.is_empty()) {
        song = parse_block(&part, song.clone()).unwrap();
        part.clear();
    }
    
    Ok(song)
}

/// Generates slides from a classic song content which is provided as &str
/// 
/// # Arguments
/// - `content`: The content of the classic song file given as a &str
/// - `presentation_settings`: A PresentationSettings struct which provides all settings for the creation of presentation slides
/// - `backup_title`: The title (String) which will be used if no #title - tag is specified in the content. This is most likely coming from the filename.
/// 
/// # Returns
/// A Vec<Slide> with the slides. This can be integrated into a PresentationChapter and a Presentation.
pub fn slides_from_classic_song(
    content: &str,
    slide_settings: &SlideSettings,
    backup_title: String) -> Vec<Slide> {
    
    /// Defines the current parsing state (which area is to be parsed)
    enum WritingArea {
        // The main block
        MainBlock,
        // The SecondarybBlock
        SecondaryBlock
    }
    
    // The emptyness of the line before (in the loop)
    let mut empty_line = false;
    // A new block has been started (in the iteration before)
    let mut start_block_flag = true;
    // The current block is a meta block
    let mut meta_block_flag = false;
    // All (main) blocks
    let mut blocks: Vec<Vec<String>> = vec![];
    // All secondary blocks. There will be always as many secondary blocks as there are primary blocks. 
    // Empty String equals None
    let mut secondary_blocks: Vec<Vec<String>> = vec![];
    
    // The current string of the block (used in the algorithm below)
    let mut cur_block_string: String = "".to_string();
    // The current string of the second block (used in the algorithm below)
    let mut cur_secundary_block_string: String = "".to_string();
    
    // The metadata of the song
    let mut metadata: HashMap<String, String> = HashMap::new();
    // Which block is currently written to (Main Block/Secondary Block)
    let mut writing_area: WritingArea = WritingArea::MainBlock;
    
    // A sub function for handling a block (putting it at the right position)
    // As this code is used twice in the code, it is outsourced into this function
    fn handle_block(metadata: &mut HashMap<String, String>, 
        meta_block_flag: &bool, 
        backup_title: &String,
        cur_block_string: &String, 
        cur_secundary_block_string: &String, 
        blocks: &mut Vec<Vec<String>>, 
        secondary_blocks: &mut Vec<Vec<String>>
        ) {
        match meta_block_flag {
                true => { 
                    parse_metadata_block(cur_block_string)
                    .iter()
                    .for_each(|(key, value)| {
                        metadata.insert(key.clone(), value.clone());
                    }); 
                    if metadata.get("title").is_none() {
                        metadata.insert("title".to_string(), backup_title.clone());
                    }
                },
                false => { 
                    if !cur_block_string.trim().is_empty() {
                        blocks.push(
                            cur_block_string.lines()
                            .map(|str| str.to_string()).collect()
                        );
                        secondary_blocks.push(
                            cur_secundary_block_string.lines()
                            .map(|str| str.to_string()).collect()
                        );
                    }
                },
            }
    }
                
    for line in content.trim().lines() {
        if empty_line { start_block_flag = true };
        
        if start_block_flag && !line.is_empty() {
            meta_block_flag = match line.chars().next().unwrap() {
                '#' => true,
                _   => false,
            };
            start_block_flag = false;
        }
        
        if line.trim().is_empty() {
            empty_line = true;
            writing_area = WritingArea::MainBlock;
            
            // Skip anything below if the line is empty as well
            if cur_block_string.is_empty() {
                continue;
            }

            handle_block(&mut metadata, 
                &meta_block_flag, 
                &backup_title, 
                &cur_block_string, 
                &cur_secundary_block_string, 
                &mut blocks, 
                &mut secondary_blocks
            );
            
            cur_block_string = "".to_string();
            cur_secundary_block_string = "".to_string();
            
        }
        // The --- delimiter starts a secondary block in a stanza
        else if line.trim() == "---" {
            writing_area = WritingArea::SecondaryBlock;
        }
        else {
            match writing_area {
                WritingArea::MainBlock => {
                    cur_block_string.push('\n');
                    cur_block_string.push_str(line);
                },
                WritingArea::SecondaryBlock => {
                    cur_secundary_block_string.push('\n');
                    cur_secundary_block_string.push_str(line);
                }
            }
            
        }
    }
    handle_block(&mut metadata, 
        &meta_block_flag, 
        &backup_title, 
        &cur_block_string, 
        &cur_secundary_block_string, 
        &mut blocks, 
        &mut secondary_blocks
    );

    if slide_settings.max_lines.is_some() {
        let wrapped_blocks_output: Vec<Vec<Vec<String>>> = wrap_blocks(&vec![blocks, secondary_blocks], slide_settings.max_lines.unwrap(), true);
        blocks = wrapped_blocks_output.first().unwrap().clone();
        secondary_blocks = wrapped_blocks_output.get(1).unwrap().clone();
    }

    // Create the Presentation
    
    let mut slides: Vec<Slide> = vec![];

    let meta_text_rendering_result = render_metadata(
        &slide_settings.meta_syntax,
        &metadata
    );
    let mut meta_text: String = "".to_string();

    let meta_text_showable: bool = match meta_text_rendering_result {
        Ok(str) => {
            meta_text = str.clone();
            !str.is_empty()
        },
        Err(_) => false,
    };

    // Make sure that the meta tag title is available (bugfix...)
    if metadata.get("title").is_none() {
        metadata.insert("title".to_string(), backup_title.clone());
    }

    if slide_settings.title_slide {
        let displayed_meta_text = match meta_text_showable {
            true => Some(meta_text.clone()),
            false => None,
        };
        
        slides.push(
            Slide::new_title_slide(
                metadata.get("title").unwrap().into(),                                          
                displayed_meta_text
            )
        )
    }
    
    let count = blocks.len();
    for (index, block) in blocks.iter().enumerate() {
        let displayed_meta_text = match meta_text_showable && (slide_settings.show_meta_information.on_first_slide() && index == 1) || (slide_settings.show_meta_information.on_last_slide() && index == count -1) {
            true => Some(meta_text.clone()),
            false => None,
        };
        
        let secondary_block = secondary_blocks.get(index).unwrap();
        if secondary_block.is_empty() {
            match blocks.get(index+1) {
                Some(next_block) => {
                    slides.push(
                        Slide::new_content_slide(block.join("\n"), Some(next_block.join("\n")), displayed_meta_text)
                    )       
                },
                None => {
                    slides.push(
                        Slide::new_content_slide(
                            block.join("\n"), None, 
                            displayed_meta_text
                        )
                    )
                }
            }
        } else {
            slides.push(
                Slide::new_content_slide(block.join("\n"),
                    Some(secondary_block.join("\n")), 
                    displayed_meta_text
                )
            );
        }
    }
    
    if slide_settings.empty_last_slide {
        slides.push(
            Slide::new_empty_slide(false)    
        );
    }
    
    slides

}

#[cfg(test)]
mod test {
    use crate::importer::import_song_from_file;

    use super::*;

    #[test]
    fn test_import_song() {
        let content: String = String::from("#title: Test Song");
        let song = import_song(&content).unwrap();
        assert_eq!(song.title, "Test Song");
    }

    #[test]
    fn test_import_song_with_tags() {
        let content: String = String::from(
            "#title: Test Song
            #author: Test Author
            #key: C"
        );
        let song = import_song(&content).unwrap();
        assert_eq!(song.title, "Test Song");
        assert_eq!(song.get_tag("author").unwrap(), "Test Author");
        assert_eq!(song.get_tag("key").unwrap(), "C");
    }

    #[test]
    fn test_import_song_with_verse() {
        let content: String = 
            "#title: Test Song
            
            This is a verse
            
            And a refrain
            
            The second verse
            
            And a refrain"
            .to_string();
        let song = import_song(&content).unwrap();
        assert_eq!(song.get_part_count(SongPartType::Verse), 2);
    }

    #[test]
    fn test_file_amazing_grace() {
        let song: Song = import_song_from_file("testfiles/Amazing Grace.song").unwrap();
        assert_eq!(song.title, "Amazing Grace");
        assert_eq!(song.get_tag("author").unwrap(), "John Newton");
        assert_eq!(song.get_part_count(SongPartType::Verse), 3)
    }

    #[test]
    fn test_song_with_refrain() {
        let song: Song = import_song_from_file("testfiles/O What A Savior That He Died For Me.song").unwrap();
        assert_eq!(song.title, "O What A Savior That He Died For Me");
        assert_eq!(song.get_part_count(SongPartType::Verse), 4);
        assert_eq!(song.get_part_count(SongPartType::Chorus), 1);
        dbg!(song);
    }
    
    #[test]
    fn generate_slides() {
        let testfile = std::fs::read_to_string("testfiles/O What A Savior That He Died For Me.song").unwrap();
        
        let presentation_settings = SlideSettings { 
            title_slide: true,
            meta_syntax: "{{title}} ({{author}})".to_string(), 
            show_meta_information: ShowMetaInformation::FirstSlideAndLastSlide, 
            empty_last_slide: true, 
            show_spoiler: true ,
            max_lines: Some(10),
        };
        
        let slides: Vec<Slide> = slides_from_classic_song(
            &testfile, 
            &presentation_settings,
            "Verily, Verily".to_string()
        );
        
        assert!(!slides.is_empty());
        
        dbg!(slides);
    }

    #[test]
    fn test_metadata_displayed_correctly() {
        let testfile = std::fs::read_to_string("testfiles/O What A Savior That He Died For Me.song").unwrap();
        
        let mut presentation_settings   = SlideSettings { 
            title_slide: false,
            meta_syntax: "{{title}} ({{author}})".to_string(), 
            show_meta_information: ShowMetaInformation::None,
            empty_last_slide: true, 
            show_spoiler: true,
            max_lines: None,
        };

        let slides: Vec<Slide> = slides_from_classic_song(
            &testfile, 
            &presentation_settings,
            "Verily, Verily".to_string()
        );

        slides.iter().for_each(|slide| assert!(!slide.has_meta_text()));

        presentation_settings.show_meta_information = ShowMetaInformation::FirstSlide;
        
        let slides: Vec<Slide> = slides_from_classic_song(
            &testfile, 
            &presentation_settings,
            "Verily, Verily".to_string()
        );

        assert!(slides.get(1).unwrap().has_meta_text());
    }

}