jumpcut 1.0.0

JumpCut is a library and CLI for converting Fountain-formatted text files into FDX, HTML, JSON, text, and PDF formats.
Documentation
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
use lazy_static::lazy_static;
use regex::Regex;
use std::borrow::Cow;
use std::collections::HashMap;
use std::str::Lines;

use crate::Element::PageBreak;
use crate::{
    Attributes, Element, ElementLayoutOverrides, ElementText, Metadata, Screenplay,
    blank_attributes, text_style_parser,
};
use ElementText::*;

const SCENE_LOCATORS: [&str; 16] = [
    "INT ",
    "INT.",
    "EXT ",
    "EXT.",
    "EST.",
    "EST ",
    "INT./EXT.",
    "INT./EXT ",
    "INT/EXT.",
    "INT/EXT ",
    "I/E.",
    "I/E ",
    "EXT./INT.",
    "EXT./INT ",
    "EXT/INT.",
    "EXT/INT ",
];

pub fn parse(text: &str) -> Screenplay {
    let fountain_string = prepare_text(text);
    let lines = fountain_string.lines();
    let hunks: Vec<Vec<&str>> = lines_to_hunks(lines);
    // println!("{:#?}", hunks);
    let mut elements: Vec<Element> = hunks_to_elements(hunks);
    // println!("{:#?}", elements);
    let mut metadata: Metadata = HashMap::new();
    match elements.first() {
        Some(Element::Action(Plain(txt), _)) if has_key_value(txt) => {
            process_metadata(&mut metadata, txt);
            elements.remove(0);
        }
        _ => (),
    }
    for element in elements.iter_mut() {
        element.parse_and_convert_markup();
    }
    let mut screenplay = Screenplay {
        metadata,
        imported_layout: None,
        imported_title_page: None,
        elements,
    };
    screenplay.apply_structural_act_break_policy();
    screenplay
}

fn has_key_value(txt: &str) -> bool {
    split_metadata_line(txt).is_some()
}

fn process_metadata(metadata: &mut Metadata, text: &str) {
    let lines = text.lines();
    let mut current_key = "".to_string();
    for line in lines {
        if let Some((key, current_value)) = split_metadata_line(line) {
            current_key = key.to_lowercase().to_string();
            if current_value.is_empty() {
                metadata.insert(current_key.to_string(), vec![]);
            } else {
                metadata.insert(
                    current_key.to_string(),
                    vec![parse_metadata_value(current_value.trim())],
                );
            }
        } else {
            // Means we have a line without a key and thus an additional value to push
            if let Some(values) = metadata.get_mut(&current_key) {
                values.push(parse_metadata_value(line.trim()));
            }
        }
    }
}

fn parse_metadata_value(value: &str) -> ElementText {
    let mut text = value.to_string();
    text_style_parser::parse_plain_text_markup(&mut text)
}

fn split_metadata_line(line: &str) -> Option<(&str, &str)> {
    let line = trim_classifier_start(line);
    let colon_index = line.find(':')?;
    let key = line.get(..colon_index)?;
    let first = key.chars().next()?;
    if key.chars().count() < 2
        || first.is_whitespace()
        || matches!(first, '!' | '.' | '@' | '~' | '>')
        || key.contains(['\n', '\r'])
    {
        return None;
    }

    let value = line.get(colon_index + 1..)?;
    Some((key, value))
}

/// Strips out problematic unicode and the boneyard element
fn prepare_text(text: &str) -> String {
    lazy_static! {
        static ref RE: Regex = Regex::new(r"/\*[^*]*\*/").unwrap();
    }
    RE.replace_all(text.trim_end(), "").to_string()
}

fn is_classifier_invisible(ch: char) -> bool {
    matches!(
        ch,
        '\u{061C}'
            | '\u{200B}'..='\u{200F}'
            | '\u{202A}'..='\u{202E}'
            | '\u{2060}'..='\u{2064}'
            | '\u{2066}'..='\u{206F}'
            | '\u{FEFF}'
    )
}

fn trim_classifier_start(line: &str) -> &str {
    let start = line
        .char_indices()
        .find(|(_, ch)| !is_classifier_invisible(*ch))
        .map(|(idx, _)| idx)
        .unwrap_or(line.len());
    &line[start..]
}

fn trim_classifier_end(line: &str) -> &str {
    let end = line
        .char_indices()
        .rev()
        .find(|(_, ch)| !is_classifier_invisible(*ch))
        .map(|(idx, ch)| idx + ch.len_utf8())
        .unwrap_or(0);
    &line[..end]
}

fn trim_classifier_edges(line: &str) -> &str {
    trim_classifier_end(trim_classifier_start(line))
}

fn classifier_trimmed(line: &str) -> &str {
    trim_classifier_edges(line).trim()
}

fn lines_to_hunks<'a>(lines: Lines<'a>) -> Vec<Vec<&'a str>> {
    let mut hunks = lines.fold(vec![vec![]], |mut acc, line: &str| {
        let classified = classifier_trimmed(line);
        match classified {
            // HANDLE BLANK LINES
            "" => {
                // If there are exactly two spaces in the line, it's intentional
                if line.len() == 2 {
                    acc.last_mut().unwrap().push(line);
                // If the previous element was blank but it was the first element, do nothing
                } else if acc.last().unwrap().is_empty() && acc.len() == 1 {
                    // do nothing
                } else if acc.last().unwrap().is_empty() {
                    // If the previous element was also blank, create an empty string
                    acc.last_mut().unwrap().push("");
                } else {
                    // Otherwise, start a new element by pushing a new empty vec
                    acc.push(vec![]);
                }
                acc
            }
            /* HANDLE SECTIONS
             * They don't follow the simple rules of blank line before or after.
             * So we need this special case to handle them.
             */
            l if l.starts_with('#') => {
                // If the previous hunk was empty, use it.
                if acc.last().unwrap().is_empty() {
                    acc.last_mut().unwrap().push(line);
                // If previous hunk wasn't empty, create a new one.
                } else {
                    acc.push(vec![line]);
                }
                acc
            }
            // HANDLE NORMAL, NON-EMPTY LINES
            _ => {
                let last_classified = acc
                    .last()
                    .unwrap()
                    .first()
                    .map(|line| classifier_trimmed(line));
                // If previous hunk was a section or blank, create a new hunk
                match last_classified {
                    Some(l) if l.starts_with('#') || l.is_empty() => acc.push(vec![]),
                    _ => (),
                }
                acc.last_mut().unwrap().push(line);
                acc
            }
        }
    });
    // Handle special case of an empty string
    if hunks.len() == 1
        && hunks
            .first()
            .expect("There will always be at least one vec.")
            .is_empty()
    {
        hunks.first_mut().unwrap().push("");
    };
    hunks
}

fn hunks_to_elements(hunks: Vec<Vec<&str>>) -> Vec<Element> {
    let initial: Vec<Element> = Vec::with_capacity(hunks.len());
    let mut elements = hunks
        .into_iter()
        .rev()
        .fold(initial, |mut acc, hunk: Vec<&str>| {
            if hunk.len() == 1 {
                let element = make_single_line_element(hunk[0]);
                if element == PageBreak {
                    // If the single line element was a PageBreak, we need to
                    // mark the next element as startsNewPage = true
                    let last_element = acc.last_mut();
                    match last_element {
                        Some(Element::Action(_, attributes))
                        | Some(Element::Character(_, attributes))
                        | Some(Element::SceneHeading(_, attributes))
                        | Some(Element::Lyric(_, attributes))
                        | Some(Element::Parenthetical(_, attributes))
                        | Some(Element::Dialogue(_, attributes))
                        | Some(Element::Transition(_, attributes))
                        | Some(Element::ColdOpening(_, attributes))
                        | Some(Element::NewAct(_, attributes))
                        | Some(Element::EndOfAct(_, attributes)) => {
                            attributes.starts_new_page = true
                        }
                        Some(_) | None => (),
                    }
                } else {
                    acc.push(element);
                }
            } else {
                let element = make_multi_line_element(hunk);
                match (acc.last_mut(), &element) {
                    // If the previous element was a dual dialogue block and it only contains one block
                    // then put this element into that block so long as it's a dialogue element
                    (Some(Element::DualDialogueBlock(dialogues)), Element::DialogueBlock(_))
                        if dialogues.len() == 1 =>
                    {
                        dialogues.insert(0, element);
                    }
                    (Some(Element::Section(_, attr, _)), Element::Synopsis(Plain(note))) => {
                        attr.notes = Some(vec![note.to_string()]);
                    }
                    _ => acc.push(element),
                }
            }
            acc
        });
    elements.reverse();
    elements
}

fn make_single_line_element(line: &str) -> Element {
    lazy_static! {
        static ref NOTE_REGEX: Regex = Regex::new(r"\[\[([^\]]+)\]\]").unwrap();
    }
    let mut attributes = blank_attributes();
    let line_has_note = has_note(line);
    if line_has_note {
        let (notes, layout_overrides) = retrieve_processed_notes(line);
        attributes = Attributes {
            notes,
            layout_overrides,
            ..attributes
        };
    }
    match make_forced(line) {
        Some(make_element) => {
            let stripped: &str = trim_classifier_start(line)
                .trim_start_matches(&['!', '@', '~', '.', '>', '='][..])
                .trim_start();

            if trim_classifier_edges(line).get(..1) == Some(".")
                && extract_scene_number(stripped).is_some()
            {
                // Handle special case of scene numbers on scene headings
                match extract_scene_number(stripped) {
                    None => make_element(Plain(stripped.to_string()), attributes),
                    Some((text_without_scene_number, scene_number)) => {
                        attributes = Attributes {
                            scene_number: Some(scene_number),
                            ..attributes
                        };
                        let final_text = if line_has_note {
                            remove_notes(&text_without_scene_number)
                        } else {
                            text_without_scene_number
                        };
                        make_element(Plain(final_text), attributes)
                    }
                }
            } else {
                let final_text = if line_has_note {
                    remove_notes(stripped)
                } else {
                    stripped.to_string()
                };
                make_element(Plain(final_text), attributes)
            }
        }
        _ if is_scene(line) => {
            let line = trim_classifier_edges(line);
            // Handle special case of scene numbers on scene headings
            if extract_scene_number(line).is_some() {
                match extract_scene_number(line) {
                    None => Element::SceneHeading(Plain(line.to_string()), attributes),
                    Some((text_without_scene_number, scene_number)) => {
                        attributes = Attributes {
                            scene_number: Some(scene_number),
                            ..attributes
                        };
                        let final_text = if line_has_note {
                            remove_notes(&text_without_scene_number)
                        } else {
                            text_without_scene_number
                        };
                        Element::SceneHeading(Plain(final_text), attributes)
                    }
                }
            } else {
                let final_text = if line_has_note {
                    remove_notes(line)
                } else {
                    line.to_string()
                };
                Element::SceneHeading(Plain(final_text), attributes)
            }
        }
        _ if is_transition(line) => {
            let line = classifier_trimmed(line);
            let final_text = if line_has_note {
                remove_notes(line)
            } else {
                line.to_string()
            };
            Element::Transition(Plain(final_text), attributes)
        }
        _ if is_centered(line) => {
            let line = trim_classifier_edges(line);
            let final_text = if line_has_note {
                remove_notes(trim_centered_marks(line))
            } else {
                trim_centered_marks(line).to_string()
            };
            if is_end_act(&final_text) {
                // Check end_act first because new act regex also matches end act regex
                Element::EndOfAct(
                    Plain(final_text),
                    Attributes {
                        centered: true,
                        ..attributes
                    },
                )
            } else if is_cold_opening(&final_text) {
                Element::ColdOpening(
                    Plain(final_text),
                    Attributes {
                        centered: true,
                        ..attributes
                    },
                )
            } else if is_new_act(&final_text) {
                Element::NewAct(
                    Plain(final_text),
                    Attributes {
                        centered: true,
                        ..attributes
                    },
                )
            } else {
                Element::Action(
                    Plain(final_text),
                    Attributes {
                        centered: true,
                        ..attributes
                    },
                )
            }
        }
        _ => {
            let final_text = if line_has_note {
                remove_notes(line)
            } else {
                line.to_string()
            };
            Element::Action(Plain(final_text), attributes)
        }
    }
}

fn extract_scene_number(line: &str) -> Option<(String, String)> {
    let hash_start = line.rfind(" #")?;
    let scene_number = line.get(hash_start + 1..)?;
    if !scene_number.starts_with('#') || !scene_number.ends_with('#') || scene_number.len() < 2 {
        return None;
    }

    let scene_number = scene_number.trim_matches('#').trim().to_string();
    let text_without_scene_number = line.get(..hash_start)?.to_string();
    Some((text_without_scene_number, scene_number))
}

fn make_multi_line_element(hunk: Vec<&str>) -> Element {
    let top_line = hunk[0];
    let top_classified = classifier_trimmed(top_line);
    let forced_element = make_forced(top_line);
    if top_classified.starts_with('@')
        || (forced_element.is_none()
            && is_character(top_line)
            && !hunk.iter().any(|&line| is_centered(line)))
    {
        return make_dialogue_block(hunk);
    }

    let mut attributes = blank_attributes();
    let joined_hunk_with_notes = hunk
        .iter()
        .any(|line| has_note(line))
        .then(|| hunk.join("\n"));
    if let Some(joined_hunk) = joined_hunk_with_notes.as_deref() {
        let (notes, layout_overrides) = retrieve_processed_notes(joined_hunk);
        attributes = Attributes {
            notes,
            layout_overrides,
            ..attributes
        };
    }
    match forced_element {
        Some(make_element) => {
            // Check if it's a forced character because that means dialogueblock
            if top_classified.get(..1) == Some("@") {
                let stripped_hunk = hunk
                    .into_iter()
                    .map(|l| trim_classifier_start(l).trim_start_matches('@'))
                    .collect::<Vec<&str>>();
                make_dialogue_block(stripped_hunk)
            } else {
                // It's not forced character, so we can create a string with newlines
                let stripped_string = hunk
                    .into_iter()
                    .map(|l| {
                        trim_classifier_start(l)
                            .trim_start_matches(&['!', '@', '~', '.', '>', '='][..])
                    })
                    .collect::<Vec<&str>>()
                    .join("\n");
                let final_text = remove_notes(&stripped_string);
                make_element(Plain(final_text), attributes)
            }
        }
        // Check if the text is centered
        _ if hunk.iter().any(|&line| is_centered(line)) => {
            let cleaned_text = hunk
                .into_iter()
                .map(trim_centered_marks)
                .collect::<Vec<&str>>()
                .join("\n");
            let final_text = remove_notes(&cleaned_text);
            Element::Action(
                Plain(final_text),
                Attributes {
                    centered: true,
                    ..attributes
                },
            )
        }
        _ if is_character(hunk[0]) => make_dialogue_block(hunk),
        _ => {
            let final_text = match joined_hunk_with_notes.as_deref() {
                Some(joined_hunk) => remove_notes(joined_hunk),
                None => hunk.join("\n"),
            };
            Element::Action(Plain(final_text), attributes)
        }
    }
}

fn is_scene(line: &str) -> bool {
    let line = trim_classifier_start(line);
    SCENE_LOCATORS.iter().any(|&locator| {
        line.get(..locator.len())
            .is_some_and(|prefix| prefix.eq_ignore_ascii_case(locator))
    })
}

fn is_transition(line: &str) -> bool {
    let line = classifier_trimmed(line);
    line.len() >= 4
        && line
            .get(line.len() - 4..)
            .is_some_and(|suffix| suffix.eq_ignore_ascii_case(" TO:"))
}

pub fn is_end_act(line: &str) -> bool {
    let owned = line.to_lowercase();
    let tokens = split_lowercase_tokens(&owned);
    if tokens.first().copied() != Some("end") {
        return false;
    }

    let mut cursor = 1;
    while tokens.get(cursor).copied() == Some("of") {
        cursor += 1;
    }
    is_act_marker(&tokens[cursor..])
}

pub fn is_new_act(line: &str) -> bool {
    let owned = line.to_lowercase();
    let tokens = split_lowercase_tokens(&owned);
    is_act_marker(&tokens)
}

pub fn is_cold_opening(line: &str) -> bool {
    let owned = line.to_lowercase();
    let tokens = split_lowercase_tokens(&owned);
    matches!(
        tokens.as_slice(),
        ["cold", "open", ..] | ["cold", "opening", ..]
    )
}

fn split_lowercase_tokens(line: &str) -> Vec<&str> {
    line.split(|ch: char| !ch.is_ascii_alphanumeric())
        .filter(|token| !token.is_empty())
        .collect()
}

fn is_act_marker(tokens: &[&str]) -> bool {
    match tokens {
        ["teaser", ..] => true,
        ["cold", "open", ..] => true,
        ["cold", "opening", ..] => true,
        ["tag", ..] => true,
        ["pilot", ..] => true,
        ["act", label, ..] => is_supported_act_label(label),
        _ => false,
    }
}

fn is_supported_act_label(label: &str) -> bool {
    matches!(
        label,
        "0" | "1"
            | "2"
            | "3"
            | "4"
            | "5"
            | "6"
            | "7"
            | "8"
            | "9"
            | "one"
            | "two"
            | "three"
            | "four"
            | "five"
            | "six"
            | "seven"
            | "eight"
            | "nine"
            | "ten"
    )
}

fn remove_notes(line: &str) -> String {
    lazy_static! {
        static ref NOTE_REGEX: Regex = Regex::new(r"\[\[([^\]]+)\]\]").unwrap();
    }
    NOTE_REGEX.replace_all(line, "").to_string()
}

fn is_centered(line: &str) -> bool {
    let trimmed = classifier_trimmed(line);
    trimmed.starts_with('>') && trimmed.ends_with('<')
}

fn trim_centered_marks(line: &str) -> &str {
    trim_classifier_edges(line)
        .trim_matches(&['>', '<'][..])
        .trim()
}

fn is_character(line: &str) -> bool {
    !line.chars().any(char::is_lowercase)
}

fn is_parenthetical(line: &str) -> bool {
    let trimmed = classifier_trimmed(line);
    trimmed.starts_with('(') && trimmed.ends_with(')')
}

fn is_lyric(line: &str) -> bool {
    classifier_trimmed(line).starts_with('~')
}

fn is_dual_dialogue(line: &str) -> bool {
    classifier_trimmed(line).ends_with('^')
}

fn has_note(line: &str) -> bool {
    line.contains("[[")
}

fn make_forced(line: &str) -> Option<fn(ElementText, Attributes) -> Element> {
    let line = trim_classifier_edges(line);
    match line.get(..1) {
        Some("!") => Some(Element::Action),
        Some("@") => Some(Element::Character),
        Some("~") => Some(Element::Lyric),
        Some(".") => {
            // check for starting ellipsis
            if line.starts_with("..") {
                None
            } else {
                Some(Element::SceneHeading)
            }
        }
        Some(">") => {
            // check for centered text
            if line.ends_with('<') {
                None
            } else {
                Some(Element::Transition)
            }
        }
        Some("#") => Some(make_section),
        Some("=") => {
            if line.trim().starts_with("===") {
                Some(make_page_break)
            } else {
                Some(make_synopsis)
            }
        }
        // This could also be page-break ("==="),
        // so we have to run a check in hunks_to_elements
        _ => None,
    }
}

fn make_section(line: ElementText, _: Attributes) -> Element {
    match line {
        Plain(txt) => {
            let trimmed = txt.trim().trim_start_matches('#');
            let level: u8 = (txt.len() - trimmed.len()).try_into().unwrap();
            Element::Section(Plain(trimmed.trim().to_string()), blank_attributes(), level)
        }
        _ => panic!("Shouldn't be receiving Styled text here."),
    }
}

fn make_page_break(_line: ElementText, _: Attributes) -> Element {
    PageBreak
}

fn make_synopsis(line: ElementText, _: Attributes) -> Element {
    match line {
        Plain(line) => {
            let trimmed = line.trim().trim_start_matches('=').trim();
            Element::Synopsis(Plain(trimmed.to_string()))
        }
        _ => panic!("Shouldn't be receiving Styled text here."),
    }
}

fn make_dialogue_block(hunk: Vec<&str>) -> Element {
    let mut elements = Vec::with_capacity(hunk.len());
    let raw_name: &str = hunk[0];
    let clean_name: &str = trim_classifier_edges(raw_name)
        .trim_start_matches('@')
        .trim_end_matches('^')
        .trim();
    let character: Element = Element::Character(Plain(clean_name.to_string()), blank_attributes());
    elements.push(character);
    for line in hunk[1..].iter() {
        let (processed_line, attributes) = if has_note(line) {
            let (notes, layout_overrides) = retrieve_processed_notes(line);
            (
                Cow::Owned(remove_notes(line)),
                Attributes {
                    notes,
                    layout_overrides,
                    ..blank_attributes()
                },
            )
        } else {
            (Cow::Borrowed(*line), blank_attributes())
        };
        if is_parenthetical(processed_line.as_ref()) {
            elements.push(Element::Parenthetical(
                Plain(classifier_trimmed(processed_line.as_ref()).to_string()),
                attributes,
            ));
        } else if is_lyric(processed_line.as_ref()) {
            let stripped_line = classifier_trimmed(processed_line.as_ref())
                .trim_start_matches('~')
                .trim();
            if let Element::Lyric(Plain(s), _) = elements.last_mut().unwrap() {
                // if previous element was lyric and so is this one, add this line to that previous lyric
                s.push('\n');
                s.push_str(stripped_line);
            } else {
                // this line is lyric but previous line wasn't, create new lyric element
                elements.push(Element::Lyric(Plain(stripped_line.to_string()), attributes));
            }
        } else if let Element::Dialogue(Plain(s), _) = elements.last_mut().unwrap() {
            // if previous element was dialogue, add this line to that dialogue
            s.push('\n');
            let trimmed = processed_line.as_ref();
            let trimmed = if trimmed.trim().is_empty() {
                trimmed
            } else {
                trimmed.trim_start()
            };
            s.push_str(trimmed);
        } else {
            // otherwise this is a new dialogue
            elements.push(Element::Dialogue(
                Plain(if processed_line.trim().is_empty() {
                    processed_line.to_string()
                } else {
                    processed_line.trim_start().to_string()
                }),
                attributes,
            ));
        }
    }
    if is_dual_dialogue(raw_name) {
        let mut blocks = Vec::with_capacity(2);
        blocks.push(Element::DialogueBlock(elements));
        Element::DualDialogueBlock(blocks)
    } else {
        Element::DialogueBlock(elements)
    }
}

fn retrieve_notes(line: &str) -> Option<Vec<String>> {
    lazy_static! {
        static ref RE: Regex = Regex::new(r"\[\[([^\]]+)\]\]").unwrap();
    }
    let mut result = vec![];
    for mat in RE.find_iter(line) {
        if let Some(str) = line.get(mat.start() + 2..mat.end() - 2) {
            result.push(str.to_string())
        }
    }
    Some(result)
}

fn retrieve_processed_notes(line: &str) -> (Option<Vec<String>>, ElementLayoutOverrides) {
    extract_layout_modifiers_from_notes(retrieve_notes(line))
}

fn extract_layout_modifiers_from_notes(
    notes: Option<Vec<String>>,
) -> (Option<Vec<String>>, ElementLayoutOverrides) {
    let Some(notes) = notes else {
        return (None, ElementLayoutOverrides::default());
    };

    let mut remaining_notes = Vec::new();
    let mut layout_overrides = ElementLayoutOverrides::default();

    for note in notes {
        let (remaining_note, note_overrides) = extract_layout_modifiers_from_note(&note);
        if let Some(space_before_delta) = note_overrides.space_before_delta {
            layout_overrides.space_before_delta =
                Some(layout_overrides.space_before_delta.unwrap_or(0.0) + space_before_delta);
        }
        if let Some(right_indent_delta) = note_overrides.right_indent_delta {
            layout_overrides.right_indent_delta =
                Some(layout_overrides.right_indent_delta.unwrap_or(0.0) + right_indent_delta);
        }
        if let Some(remaining_note) = remaining_note {
            remaining_notes.push(remaining_note);
        }
    }

    (
        (!remaining_notes.is_empty()).then_some(remaining_notes),
        layout_overrides,
    )
}

fn extract_layout_modifiers_from_note(note: &str) -> (Option<String>, ElementLayoutOverrides) {
    let mut layout_overrides = ElementLayoutOverrides::default();
    let mut remaining_tokens = Vec::new();
    let mut saw_modifier = false;

    for token in note.split_whitespace() {
        match parse_layout_modifier_token(token) {
            Some((space_before_delta, right_indent_delta)) => {
                saw_modifier = true;
                if let Some(space_before_delta) = space_before_delta {
                    layout_overrides.space_before_delta = Some(
                        layout_overrides.space_before_delta.unwrap_or(0.0) + space_before_delta,
                    );
                }
                if let Some(right_indent_delta) = right_indent_delta {
                    layout_overrides.right_indent_delta = Some(
                        layout_overrides.right_indent_delta.unwrap_or(0.0) + right_indent_delta,
                    );
                }
            }
            None => remaining_tokens.push(token),
        }
    }

    if !saw_modifier {
        return (Some(note.to_string()), layout_overrides);
    }

    let remaining_note = (!remaining_tokens.is_empty()).then(|| remaining_tokens.join(" "));
    (remaining_note, layout_overrides)
}

fn parse_layout_modifier_token(token: &str) -> Option<(Option<f32>, Option<f32>)> {
    lazy_static! {
        static ref LAYOUT_MODIFIER_RE: Regex =
            Regex::new(r"^\.(lift|widen)(?:-([0-9]+))?$").unwrap();
    }

    let captures = LAYOUT_MODIFIER_RE.captures(token)?;
    let kind = captures.get(1)?.as_str();
    let magnitude = captures
        .get(2)
        .and_then(|value| value.as_str().parse::<u32>().ok())
        .unwrap_or(1) as f32;

    match kind {
        "lift" => Some((Some(-magnitude), None)),
        "widen" => Some((None, Some(0.125 * magnitude))),
        _ => None,
    }
}

// * Tests
#[cfg(test)]
mod tests {
    use super::*;
    use crate::{ElementText::Styled, p, tr};

    #[test]
    fn test_lines_to_hunks() {
        let mut lines = "hello hello hello\n\nwelcome back\ngoodbye".lines();
        let mut expected = vec![vec!["hello hello hello"], vec!["welcome back", "goodbye"]];
        assert_eq!(
            lines_to_hunks(lines),
            expected,
            "it should handle simple line spacing"
        );

        lines = "".lines();
        expected = vec![vec![""]];

        assert_eq!(
            lines_to_hunks(lines),
            expected,
            "it should handle an empty string"
        );

        lines = "# Act 1\nINT. HOUSE\n\nAn ugly place.".lines();
        expected = vec![vec!["# Act 1"], vec!["INT. HOUSE"], vec!["An ugly place."]];

        assert_eq!(
            lines_to_hunks(lines),
            expected,
            "it should put sections in their own vec"
        );

        lines = "SALLY\nYou're screwed!\n\n# Act 1\nINT. HOUSE\n\nAn ugly place.".lines();
        expected = vec![
            vec!["SALLY", "You're screwed!"],
            vec!["# Act 1"],
            vec!["INT. HOUSE"],
            vec!["An ugly place."],
        ];

        assert_eq!(
            lines_to_hunks(lines),
            expected,
            "it should handle sections in middle of content"
        );

        lines = "# Act 1\n## John finds the horse\n\nJOHN\nWhoa!".lines();
        expected = vec![
            vec!["# Act 1"],
            vec!["## John finds the horse"],
            vec!["JOHN", "Whoa!"],
        ];

        assert_eq!(
            lines_to_hunks(lines),
            expected,
            "it should handle two newlines after a section"
        );

        lines = "John examines the gun.\n\n\n\n\n\n\n\n\n\nBANG!".lines();
        expected = vec![
            vec!["John examines the gun."],
            vec![""],
            vec![""],
            vec![""],
            vec![""],
            vec!["BANG!"],
        ];

        assert_eq!(
            lines_to_hunks(lines),
            expected,
            "it should create blank lines from multiple newlines in a row"
        );
    }

    #[test]
    fn test_lines_to_hunks_odd_number_of_blanks() {
        let lines = "CHARACTER\nTalking talking talking--\n\n\nINT. PLACE - LATER\n\nA row of interview windows."
            .lines();
        let expected = vec![
            vec!["CHARACTER", "Talking talking talking--"],
            vec![""],
            vec!["INT. PLACE - LATER"],
            vec!["A row of interview windows."],
        ];

        assert_eq!(
            lines_to_hunks(lines),
            expected,
            "it should handle three returns in a row without creating weird groupings"
        );
    }

    #[test]
    fn test_lines_to_hunks_intentional_blanks() {
        let lines = "hello hello hello\n\nwelcome back\n  \ngoodbye".lines();
        let expected = vec![
            vec!["hello hello hello"],
            vec!["welcome back", "  ", "goodbye"],
        ];
        assert_eq!(lines_to_hunks(lines), expected);
    }

    #[test]
    fn test_prepare_text_preserves_internal_invisible_chars() {
        let unicode_string = "Hello\u{200B}, \u{200D}\u{FEFF}World!";
        assert_eq!(prepare_text(unicode_string), unicode_string);
    }

    #[test]
    fn test_parse_handles_leading_bom_before_metadata() {
        let fountain = "\u{FEFF}Title: Example Script\nAuthor: Test Writer\n";
        let screenplay = parse(fountain);
        assert_eq!(
            screenplay.metadata.get("title"),
            Some(&vec![p("Example Script")])
        );
        assert_eq!(
            screenplay.metadata.get("author"),
            Some(&vec![p("Test Writer")])
        );
    }

    #[test]
    fn test_parse_preserves_styled_title_page_metadata() {
        let fountain = "Title: _**BRICK & STEEL**_\nCredit: Written by\nAuthor: *Stu Maschwitz*\n";
        let screenplay = parse(fountain);

        assert_eq!(
            screenplay.metadata.get("title"),
            Some(&vec![Styled(vec![tr(
                "BRICK & STEEL",
                vec!["Bold", "Underline"]
            )])])
        );
        assert_eq!(
            screenplay.metadata.get("author"),
            Some(&vec![Styled(vec![tr("Stu Maschwitz", vec!["Italic"])])])
        );
    }

    #[test]
    fn test_parse_handles_leading_format_chars_before_scene_heading() {
        let fountain = "\u{200B}\u{2060}INT. HOUSE - DAY";
        assert_eq!(
            parse(fountain).elements,
            vec![Element::SceneHeading(
                p("INT. HOUSE - DAY"),
                blank_attributes()
            )]
        );
    }

    #[test]
    fn test_prepare_text_trims_only_trailing_whitespace() {
        let fountain = "Title: Example  \n\n\t";
        assert_eq!(prepare_text(fountain), "Title: Example");
    }

    #[test]
    fn test_remove_boneyard() {
        let boneyard = "/* boneyard */Hello, World!\n\n/* More bones \n Lower bones*/Goodbye!";
        assert_eq!(prepare_text(boneyard), "Hello, World!\n\nGoodbye!");
    }

    #[test]
    fn test_remove_boneyard_preserves_line_boundaries() {
        let boneyard = "Title: Example\n/* note\nstill note */\nINT. ROOM - DAY";
        assert_eq!(prepare_text(boneyard), "Title: Example\n\nINT. ROOM - DAY");
    }

    #[test]
    fn test_prepare_text_preserves_zwj_emoji_sequence() {
        let emoji = "Family: ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ";
        assert_eq!(prepare_text(emoji), emoji);
    }
}