screenplay-doc-parser-rs 0.1.10

Tools to parse Screenplay-formatted documents into semantically-typed structs.
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
use crate::{pdf_document, };
use core::panic;
use std::{
    collections::{HashMap, HashSet,},
    
    hash::Hash,
    ops::{Deref, DerefMut, },
    time::{Instant, },
    vec,
};
use uuid::Uuid;

#[derive(PartialEq, Clone, Debug)]
pub enum TimeOfDay {
    Day(String),
    Night(String),
    Morning(String),
    Evening(String),
    Afternoon(String),
    Extras(Option<HashMap<String, String>>),
}


#[derive(PartialEq, Clone, Debug)]
pub struct TimeOfDayCollection {
    pub day: TimeOfDay,
    pub night: TimeOfDay,
    pub morning: TimeOfDay,
    pub evening: TimeOfDay,
    pub afternoon: TimeOfDay,
    pub extras: Option<HashMap<String, String>>,
}
impl Default for TimeOfDayCollection {
    fn default() -> Self {
        return Self {
            day: TimeOfDay::Day("DAY".into()),
            night: TimeOfDay::Night("NIGHT".into()),
            morning: TimeOfDay::Morning("MORNING".into()),
            evening: TimeOfDay::Evening("EVENING".into()),
            afternoon: TimeOfDay::Afternoon("AFTERNOON".into()),
            extras: None,
        };
    }
}
impl TimeOfDayCollection {
    pub fn is_time_of_day(&self, target: &String) -> bool {
        let vars: Vec<&TimeOfDay> = vec![
            &self.day,
            &self.night,
            &self.morning,
            &self.evening,
            &self.afternoon,
        ];

        for time in vars {
            match time {
                TimeOfDay::Day(string)
                | TimeOfDay::Night(string)
                | TimeOfDay::Morning(string)
                | TimeOfDay::Evening(string)
                | TimeOfDay::Afternoon(string) => {
                    if string == target {
                        return true;
                    }
                }
                _ => {}
            }
        }

        match &self.extras {
            None => {
                return false;
            }
            Some(e) => {
                for (_, string) in e {
                    if target == string {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    pub fn get_time_of_day(&self, target: &String) -> Option<TimeOfDay> {
        let vars: Vec<&TimeOfDay> = vec![
            &self.day,
            &self.night,
            &self.morning,
            &self.evening,
            &self.afternoon,
        ];

        for time in vars {
            match time {
                TimeOfDay::Day(string)
                | TimeOfDay::Night(string)
                | TimeOfDay::Morning(string)
                | TimeOfDay::Evening(string)
                | TimeOfDay::Afternoon(string) => {
                    if string == target {
                        return Some(time.clone());
                    }
                }
                _ => {}
            }
        }

        match &self.extras {
            None => {
                return None;
            }
            Some(e) => {
                for (_, string) in e {
                    if target == string {
                        return Some(TimeOfDay::Extras(None)); // this is FUCKING horrendous what the fuck man
                    }
                }
            }
        }

        return None;
    }
}

#[derive(PartialEq, Clone, Copy, Debug)]
pub enum PageFormat {
    US,
    A4,
    OTHER,
}

/// # SPType
///
/// The various Element Types found in a Screenplay.
///
/// Types can be assigned to both individual `TextElements` and `Lines`.
///
/// Some `Line`s will only contain a single type.
///
/// An `SPType::SP_ACTION` Line will contain only `SPType::SP_ACTION` text elements, for example.
///
/// But a `SP_CHARACTER` line will potentially contain one or more `SP_CHARACTER` elements, as well as one or more `SP_CHARACTER_EXTENSION` elements:
///
/// ```text
/// ...
///
///         CHARLIE (V.O.)
///     I always wanted to be a gangster.
///
/// ...
///
/// ```
///
/// Notice how `CHARLIE` is the `SP_CHARACTER` and the `(V.O.)` is the `SP_CHARACTER_EXTENSION`.
///
/// ## Scene Headings
///
/// A Scene Heading will consist of multiple types, such as the `SP_ENVIRONMENT`, meaning interior and/or exterior (INT./EXT.), the location, sublocation, and time of day:
///
/// `EXT. BASEBALL FIELD - PITCHER'S MOUND - DAY`
///
/// Scene headings can contain more element types, such as a Time Period, or multiple Sublocations.
#[derive(Default, Clone, Debug, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
#[allow(non_camel_case_types)]
pub enum SPType {
    SP_ACTION = 0,

    SP_CHARACTER,
    SP_CHARACTER_EXTENSION, // require context to parse (previous word type)
    SP_DG_MORE_CONTINUED,   // specifically has MORE or CONTINUED or CONT'D within parentheses
    SP_PARENTHETICAL,
    SP_DIALOGUE,
    SP_TRANSITION,

    /// SCENE HEADING
    ///
    SP_SCENE_HEADING(SceneHeadingElement), // begins with INT. , EXT. , or I./E.

    /// `INT.`, `EXT.`, `INT./EXT.`, etc.
    //SP_ENVIRONMENT,
    //SP_LOCATION,
    //SP_SCENE_HEADING_SUB_ELEMENT,
    //SP_SCENE_HEADING_SEPARATOR, /// Breaks up a slugline -- EXT. BASEBALL FIELD - PITCHER'S MOUND - PAST - NIGHT
    //SP_SCENE_TIMEPERIOD, // PAST, PRESENT, FUTURE, arbitrary timeframe "BEFORE DINNER", "AFTER THE EXPLOSION", etc.
    //SP_SUBLOCATION,
    //SP_TIME_OF_DAY,
    SP_SHOT_ANGLE, // SHOT or ANGLE on something, NOT a full scene heading / location

    SP_PAGENUM,  // Nominal page number
    SP_SCENENUM, // Nominal scene number

    SP_PAGE_HEADER, //LINE --contains the PAGE NUM and potentially a page Revision label
    SP_PAGE_REVISION_LABEL, //may or may not include the date / color (I think it's two lines usually, but it could be one line potentially...?)
    SP_LINE_REVISION_MARKER, // asterisks in the left and/or right margins indicate a line or lines have been revised

    SP_MORE_CONTINUED,
    SP_FOOTER, // Not sure what footers are used for but....

    //DUAL DIALOGUE
    SP_DUAL_CHARACTERS,
    SP_DUAL_DIALOGUES,

    SP_DD_L_CHARACTER,
    SP_DD_L_CHARACTER_EXTENSION,
    SP_DD_L_PARENTHETICAL,
    SP_DD_L_DIALOGUE,
    SP_DD_L_MORE_CONTINUED,

    SP_DD_R_CHARACTER,
    SP_DD_R_CHARACTER_EXTENSION,
    SP_DD_R_PARENTHETICAL,
    SP_DD_R_DIALOGUE,
    SP_DD_R_MORE_CONTINUED,

    // TITLE PAGE
    TP_TITLE,
    TP_BYLINE,
    TP_AUTHOR,
    TP_DRAFT_DATE,
    TP_CONTACT,
    // -------------
    SP_OTHER,
    SP_BLANK, // BLANK element?
    SP_OMITTED,
    // Non- content text (asterisks and/or scene numbers in the margins, headers and footers, page numbers, etc.)
    NON_CONTENT_TOP,
    NON_CONTENT_BOTTOM,
    NON_CONTENT_LEFT,
    NON_CONTENT_RIGHT,

    #[default]
    NONE,
    _TYPECOUNT,
}

// -------- SCREENPLAY TYPED STRUCTS / ENUMS

// -------------------- CHARACTER
#[derive(Default, PartialEq, Clone, Debug, Eq, Hash)]
pub struct CharacterID(Uuid);
impl Deref for CharacterID {
    type Target = Uuid;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl DerefMut for CharacterID {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}
impl CharacterID {
    pub fn new() -> Self {
        CharacterID(Uuid::new_v4())
    }
}

#[derive(Default, PartialEq, Clone, Debug, Eq, Hash)]
pub struct Character {
    pub name: String,
    pub id: CharacterID,
}
impl Character {
    pub fn is_line(&self, line: &Line) -> bool {
        let mut maybe_character_name = String::new();
        let mut previous_type: Option<SPType> = Some(SPType::NONE);
        for text_element in &line.text_elements {
            if previous_type != text_element.element_type {
                if maybe_character_name == self.name {
                    println!("'howdy y'all");
                    return true;
                }
                maybe_character_name = String::new();
            }
            match text_element.element_type {
                Some(SPType::SP_CHARACTER)
                | Some(SPType::SP_DD_L_CHARACTER)
                | Some(SPType::SP_DD_R_CHARACTER) => {
                    if !maybe_character_name.is_empty() {
                        maybe_character_name.push(' ');
                    }
                    maybe_character_name.push_str(&text_element.text.clone());
                }
                _ => {}
            }
            previous_type = text_element.element_type.clone();
        }
        if maybe_character_name == self.name {
            //println!("HOO WEE!");
            return true;
        }

        false
    }
}

// -------------------- PAGE
#[derive(Default, PartialEq, Clone, Debug, Eq, Hash)]
pub struct PageID(pub Uuid);
impl Deref for PageID {
    type Target = Uuid;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for PageID {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

#[derive(Default, PartialEq, Clone, Debug)]
pub struct PageNumber(pub String);
impl Deref for PageNumber {
    type Target = String;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl DerefMut for PageNumber {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

// -------------------- SCENE
#[derive(Default, PartialEq, Clone, Debug)]
pub struct SceneNumber(pub String);

#[derive(Default, PartialEq, Clone, Copy, Debug, Hash, Eq)]
pub struct SceneID(pub Uuid);
impl Deref for SceneID {
    type Target = Uuid;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl DerefMut for SceneID {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}
impl SceneID {
    pub fn new() -> Self {
        SceneID(Uuid::new_v4())
    }
}

//TODO:
// make the SP_SCENE_HEADING element take one of THESE as data,
// instead of having the scene elements flattened out among the SP_TYPEs
// maybe also do this technique with CHARACTER, DIALOGUE, etc. ,
// basically make each element have the LINE TYPE, which contains the ELEMENT TYPE as data...
#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash)]
pub enum SceneHeadingElement {
    Line, // The Line Itself
    Environment,
    Location,
    SubLocation,
    TimeOfDay,
    Continuity, // CONTINUOUS
    TimePeriod, // EALIER, LATER, 1950s, WEDNESDAY, etc.
    Separator,  // hyphen
    SceneNumber,
    SlugOther,
}

//TODO: add get_scene_from_id func to ScreenplayDocument struct
#[derive(PartialEq, Clone, Debug)]
pub struct Scene {
    pub start: ScreenplayCoordinate,

    pub environment: Environment,
    pub number: Option<SceneNumber>,
    pub revised: bool,

    pub story_locations: Vec<LocationID>,
    pub story_time_of_day: Option<TimeOfDay>, // DAY, NIGHT, etc.
}

pub struct EnvironmentStrings {
    pub int: Vec<String>,
    pub ext: Vec<String>,
    pub combo: Vec<String>,
}
impl Default for EnvironmentStrings {
    fn default() -> Self {
        EnvironmentStrings {
            int: vec!["INT.".into()],
            ext: vec!["EXT.".into()],
            combo: vec![
                "INT./EXT.".into(),
                "I./E.".into(),
                "EXT./INT.".into(),
                "E./I.".into(),
            ],
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum Environment {
    Int,
    Ext,
    Combo(Option<Vec<Environment>>),
}
impl Environment {
    pub fn from_str(string: &String, current_env_strs: &EnvironmentStrings) -> Option<Self> {
        if current_env_strs.int.contains(&string) {
            return Some(Environment::Int);
        }
        if current_env_strs.ext.contains(&string) {
            return Some(Environment::Ext);
        }
        if current_env_strs.combo.contains(&string) {
            // TODO: actually hanndle combos (4 total possibilities, int/int, int/ext, ext/int, and ext/ext)
            return Some(Environment::Combo(None));
        }
        None
    }
}

#[derive(Default, PartialEq, Clone, Debug, Eq, Hash)]
pub struct LocationID(Uuid);

impl Deref for LocationID {
    type Target = Uuid;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl DerefMut for LocationID {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}
impl LocationID {
    pub fn new() -> Self {
        LocationID(Uuid::new_v4())
    }
}

#[derive(Default, PartialEq, Clone, Debug)]
pub struct LocationNode {
    pub string: String,
    pub sublocations: HashSet<LocationID>, // list of IDs for other locations
    pub superlocation: Option<LocationID>, //
}
impl LocationNode {
    pub fn add_sublocation(&mut self, new_id: LocationID) -> bool {
        self.sublocations.insert(new_id)
    }

    ///
    /// Determines if a path exists under this LocationNode.
    ///
    /// ```
    /// let mut screenplay_doc = screenplay_document::ScreenplayDocument::new();
    /// ```
    ///
    pub fn subpath_exists<'a>(
        &'a self,
        this_location_id: &'a LocationID,
        subpath: &[String],
        screenplay: &'a ScreenplayDocument,
    ) -> Option<(&'a LocationID, Vec<String>)> {
        if subpath.is_empty() {
            return None;
        }

        let subpath_root = &subpath[0];

        for id in &self.sublocations {
            let Some(sublocation) = screenplay.locations.get(id) else {
                continue;
            };
            if sublocation.string == *subpath_root {
                if subpath.len() == 1 {
                    return Some((id, Vec::new()));
                }
                if subpath.len() > 1 && sublocation.sublocations.is_empty() {
                    return Some((id, Vec::from(&subpath[1..])));
                }
                return sublocation.subpath_exists(id, &subpath[1..], screenplay);
            }
        }

        Some((this_location_id, subpath.to_vec()))
    }
}

// --------------- BASIC DOCUMENT COMPONENTS ---------------

#[derive(Default, PartialEq, Clone, Debug)]
pub struct TextElement {
    pub text: String,
    pub element_type: Option<SPType>,
    pub preceding_whitespace_chars: u64,
    pub element_position: Option<pdf_document::TextPosition>,
}

#[derive(Default, PartialEq, Clone, Debug)]
pub struct Line {
    pub text_elements: Vec<TextElement>,
    pub scene_number: Option<String>,
    pub scene_id: Option<SceneID>,
    pub line_type: Option<SPType>,
    pub preceding_empty_lines: u64,
    pub revised: bool,
    pub blank: bool,
}

#[derive(Default, PartialEq, Clone, Debug)]
pub struct Page {
    pub lines: Vec<Line>,
    pub page_number: Option<PageNumber>,
    pub revised: bool,
    pub revision_label: Option<String>,
    pub revision_date: Option<String>,
    pub page_format: Option<PageFormat>,
}

#[derive(Default, PartialEq, Clone, Debug, Hash, Eq, PartialOrd)]
pub struct ScreenplayCoordinate {
    pub page: usize,
    pub line: usize,
    pub element: Option<u64>,
}

#[derive(Default, PartialEq, Clone, Debug)]
pub struct ScreenplayDocument {
    pub pages: Vec<Page>,
    pub revisions: Option<Vec<String>>, // current (and possible previous) revision date(s) from the title page
    pub scenes: HashMap<SceneID, Scene>,
    pub locations: HashMap<LocationID, LocationNode>,
    pub characters: HashSet<Character>,
    pub page_numbers: HashMap<PageID, PageNumber>,
}
impl ScreenplayDocument {
    pub fn new() -> Self {
        ScreenplayDocument {
            pages: Vec::new(),
            revisions: None,
            scenes: HashMap::new(),
            locations: HashMap::new(),
            characters: HashSet::new(),
            page_numbers: HashMap::new(),
        }
    }

    
}