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
|
# Copyright (c) 2011 Martin Vilcans
# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license.php
import unittest2
from screenplain.parsers import fountain
from screenplain.types import (
Slug, Action, Dialog, DualDialog, Transition, Section, PageBreak
)
from screenplain.richstring import plain, italic, empty_string
from StringIO import StringIO
def parse(lines):
content = '\n'.join(lines)
return fountain.parse(StringIO(content))
class SlugTests(unittest2.TestCase):
def test_slug_with_prefix(self):
paras = list(parse([
'INT. SOMEWHERE - DAY',
'',
'THIS IS JUST ACTION',
]))
self.assertEquals([Slug, Action], [type(p) for p in paras])
def test_slug_must_be_single_line(self):
paras = list(parse([
'INT. SOMEWHERE - DAY',
'ANOTHER LINE',
'',
'Some action',
]))
self.assertEquals([Dialog, Action], [type(p) for p in paras])
# What looks like a scene headingis parsed as a character name.
# Unexpected perhaps, but that's how I interpreted the spec.
self.assertEquals(plain('INT. SOMEWHERE - DAY'), paras[0].character)
self.assertEquals([plain('Some action')], paras[1].lines)
def test_action_is_not_a_slug(self):
paras = list(parse([
'',
'THIS IS JUST ACTION',
]))
self.assertEquals([Action], [type(p) for p in paras])
def test_two_lines_creates_no_slug(self):
types = [type(p) for p in parse([
'',
'',
'This is a slug',
'',
])]
# This used to be Slug. Changed in the Jan 2012 version of the spec.
self.assertEquals([Action], types)
def test_period_creates_slug(self):
paras = parse([
'.SNIPER SCOPE POV',
'',
])
self.assertEquals(1, len(paras))
self.assertEquals(Slug, type(paras[0]))
self.assertEquals(plain('SNIPER SCOPE POV'), paras[0].line)
def test_more_than_one_period_does_not_create_slug(self):
paras = parse([
'..AND THEN...',
'',
])
self.assertEquals(1, len(paras))
self.assertEquals(Action, type(paras[0]))
self.assertEquals(plain('..AND THEN...'), paras[0].lines[0])
def test_scene_number_is_parsed(self):
paras = parse(['EXT SOMEWHERE - DAY #42#'])
self.assertEquals(plain('EXT SOMEWHERE - DAY'), paras[0].line)
self.assertEquals(plain('42'), paras[0].scene_number)
def test_only_last_two_hashes_in_slug_used_for_scene_number(self):
paras = parse(['INT ROOM #237 #42#'])
self.assertEquals(plain('42'), paras[0].scene_number)
self.assertEquals(plain('INT ROOM #237'), paras[0].line)
def test_scene_number_must_be_alphanumeric(self):
paras = parse(['.SOMEWHERE #*HELLO*#'])
self.assertIsNone(paras[0].scene_number)
self.assertEquals(
(plain)(u'SOMEWHERE #') + (italic)(u'HELLO') + (plain)(u'#'),
paras[0].line
)
class SectionTests(unittest2.TestCase):
def test_section_parsed_correctly(self):
paras = parse([
'# first level',
'',
'## second level',
])
self.assertEquals([Section, Section], [type(p) for p in paras])
self.assertEquals(1, paras[0].level)
self.assertEquals(plain('first level'), paras[0].text)
self.assertEquals(2, paras[1].level)
self.assertEquals(plain('second level'), paras[1].text)
def test_multiple_sections_in_one_paragraph(self):
paras = parse([
'# first level',
'## second level',
'# first level again'
])
self.assertEquals(
[Section, Section, Section],
[type(p) for p in paras]
)
self.assertEquals(1, paras[0].level)
self.assertEquals(plain('first level'), paras[0].text)
self.assertEquals(2, paras[1].level)
self.assertEquals(plain('second level'), paras[1].text)
self.assertEquals(1, paras[2].level)
self.assertEquals(plain('first level again'), paras[2].text)
def test_multiple_sections_with_synopsis(self):
paras = parse([
'# first level',
'= level one synopsis',
'## second level',
])
self.assertEquals([
Section(plain(u'first level'), 1, 'level one synopsis'),
Section(plain(u'second level'), 2, None),
], paras)
class DialogTests(unittest2.TestCase):
# A Character element is any line entirely in caps, with one empty
# line before it and without an empty line after it.
def test_all_caps_is_character(self):
paras = [p for p in parse([
'SOME GUY',
'Hello',
])]
self.assertEquals(1, len(paras))
dialog = paras[0]
self.assertEquals(Dialog, type(dialog))
self.assertEquals(plain('SOME GUY'), dialog.character)
# Fountain would not be able to support a character named "23". We
# might need a syntax to force a character element.
def test_nonalpha_character(self):
paras = list(parse([
'23',
'Hello',
]))
self.assertEquals([Action], [type(p) for p in paras])
def test_twospaced_line_is_not_character(self):
paras = list(parse([
'SCANNING THE AISLES... ',
'Where is that pit boss?',
]))
self.assertEquals([Action], [type(p) for p in paras])
def test_simple_parenthetical(self):
paras = list(parse([
'STEEL',
'(starting the engine)',
'So much for retirement!',
]))
self.assertEquals(1, len(paras))
dialog = paras[0]
self.assertEqual(2, len(dialog.blocks))
self.assertEqual(
(True, plain('(starting the engine)')),
dialog.blocks[0]
)
self.assertEqual(
(False, plain('So much for retirement!')),
dialog.blocks[1]
)
def test_twospace_keeps_dialog_together(self):
paras = list(parse([
'SOMEONE',
'One',
' ',
'Two',
]))
self.assertEquals([Dialog], [type(p) for p in paras])
self.assertEquals([
(False, plain('One')),
(False, empty_string),
(False, plain('Two')),
], paras[0].blocks)
def test_dual_dialog(self):
paras = list(parse([
'BRICK',
'Fuck retirement.',
'',
'STEEL ^',
'Fuck retirement!',
]))
self.assertEquals([DualDialog], [type(p) for p in paras])
dual = paras[0]
self.assertEquals(plain('BRICK'), dual.left.character)
self.assertEquals(
[(False, plain('Fuck retirement.'))],
dual.left.blocks
)
self.assertEquals(plain('STEEL'), dual.right.character)
self.assertEquals(
[(False, plain('Fuck retirement!'))],
dual.right.blocks
)
def test_dual_dialog_without_previous_dialog_is_ignored(self):
paras = list(parse([
'Brick strolls down the street.',
'',
'BRICK ^',
'Nice retirement.',
]))
self.assertEquals([Action, Dialog], [type(p) for p in paras])
dialog = paras[1]
self.assertEqual(plain('BRICK ^'), dialog.character)
self.assertEqual([
(False, plain('Nice retirement.'))
], dialog.blocks)
def test_leading_and_trailing_spaces_in_dialog(self):
paras = list(parse([
'JULIET',
'O Romeo, Romeo! wherefore art thou Romeo?',
' Deny thy father and refuse thy name; ',
'Or, if thou wilt not, be but sworn my love,',
" And I'll no longer be a Capulet.",
]))
self.assertEquals([Dialog], [type(p) for p in paras])
self.assertEquals([
(False, plain(u'O Romeo, Romeo! wherefore art thou Romeo?')),
(False, plain(u'Deny thy father and refuse thy name;')),
(False, plain(u'Or, if thou wilt not, be but sworn my love,')),
(False, plain(u"And I'll no longer be a Capulet.")),
], paras[0].blocks)
class TransitionTests(unittest2.TestCase):
def test_standard_transition(self):
paras = list(parse([
'Jack begins to argue vociferously in Vietnamese (?)',
'',
'CUT TO:',
'',
"EXT. BRICK'S POOL - DAY",
]))
self.assertEquals([Action, Transition, Slug], [type(p) for p in paras])
def test_transition_must_end_with_to(self):
paras = list(parse([
'CUT TOO:',
'',
"EXT. BRICK'S POOL - DAY",
]))
self.assertEquals([Action, Slug], [type(p) for p in paras])
def test_transition_needs_to_be_upper_case(self):
paras = list(parse([
'Jack begins to argue vociferously in Vietnamese (?)',
'',
'cut to:',
'',
"EXT. BRICK'S POOL - DAY",
]))
self.assertEquals([Action, Action, Slug], [type(p) for p in paras])
def test_not_a_transition_on_trailing_whitespace(self):
paras = list(parse([
'Jack begins to argue vociferously in Vietnamese (?)',
'',
'CUT TO: ',
'',
"EXT. BRICK'S POOL - DAY",
]))
self.assertEquals([Action, Action, Slug], [type(p) for p in paras])
def test_transition_does_not_have_to_be_followed_by_slug(self):
# The "followed by slug" requirement is gone from the Jan 2012 spec
paras = list(parse([
'Bill lights a cigarette.',
'',
'CUT TO:',
'',
'SOME GUY mowing the lawn.',
]))
self.assertEquals(
[Action, Transition, Action],
[type(p) for p in paras]
)
def test_greater_than_sign_means_transition(self):
paras = list(parse([
'Bill blows out the match.',
'',
'> FADE OUT.',
'',
'.DARKNESS',
]))
self.assertEquals([Action, Transition, Slug], [type(p) for p in paras])
self.assertEquals(plain('FADE OUT.'), paras[1].line)
def test_centered_text_is_not_parsed_as_transition(self):
paras = list(parse([
'Bill blows out the match.',
'',
'> THE END. <',
'',
'bye!'
]))
self.assertEquals([Action, Action, Action], [type(p) for p in paras])
def test_transition_at_end(self):
paras = list(parse([
'They stroll hand in hand down the street.',
'',
'> FADE OUT.',
]))
self.assertEquals([Action, Transition], [type(p) for p in paras])
self.assertEquals(plain('FADE OUT.'), paras[1].line)
class ActionTests(unittest2.TestCase):
def test_action_preserves_leading_whitespace(self):
paras = list(parse([
'hello',
'',
' two spaces',
' three spaces ',
]))
self.assertEquals([Action, Action], [type(p) for p in paras])
self.assertEquals(
[
plain(u' two spaces'),
plain(u' three spaces'),
], paras[1].lines
)
def test_single_centered_line(self):
paras = list(parse(['> center me! <']))
self.assertEquals([Action], [type(p) for p in paras])
self.assertTrue(paras[0].centered)
def test_full_centered_paragraph(self):
lines = [
'> first! <',
' > second! <',
'> third!< ',
]
paras = list(parse(lines))
self.assertEquals([Action], [type(p) for p in paras])
self.assertTrue(paras[0].centered)
self.assertEquals([
plain('first!'),
plain('second!'),
plain('third!'),
], paras[0].lines)
def test_upper_case_centered_not_parsed_as_dialog(self):
paras = list(parse([
'> FIRST! <',
' > SECOND! <',
'> THIRD! <',
]))
self.assertEquals([Action], [type(p) for p in paras])
self.assertTrue(paras[0].centered)
def test_centering_marks_in_middle_of_paragraphs_are_verbatim(self):
lines = [
'first!',
'> second! <',
'third!',
]
paras = list(parse(lines))
self.assertEquals([Action], [type(p) for p in paras])
self.assertFalse(paras[0].centered)
self.assertEquals([plain(line) for line in lines], paras[0].lines)
class SynopsisTests(unittest2.TestCase):
def test_synopsis_after_slug_adds_synopsis_to_scene(self):
paras = list(parse([
"EXT. BRICK'S PATIO - DAY",
'',
"= Set up Brick & Steel's new life."
'',
]))
self.assertEquals([Slug], [type(p) for p in paras])
self.assertEquals(
"Set up Brick & Steel's new life.",
paras[0].synopsis
)
def test_synopsis_in_section(self):
paras = list(parse([
'# section one',
'',
'= In which we get to know our characters'
]))
self.assertEquals([Section], [type(p) for p in paras])
self.assertEquals(
'In which we get to know our characters',
paras[0].synopsis
)
def test_synopsis_syntax_parsed_as_literal(self):
paras = list(parse([
'Some action',
'',
'= A line that just happens to look like a synopsis'
]))
self.assertEquals([Action, Action], [type(p) for p in paras])
self.assertEquals(
[plain('= A line that just happens to look like a synopsis')],
paras[1].lines
)
class TitlePageTests(unittest2.TestCase):
def test_basic_title_page(self):
lines = [
'Title:',
' _**BRICK & STEEL**_',
' _**FULL RETIRED**_',
'Author: Stu Maschwitz',
]
self.assertDictEqual(
{
'Title': ['_**BRICK & STEEL**_', '_**FULL RETIRED**_'],
'Author': ['Stu Maschwitz'],
},
fountain.parse_title_page(lines)
)
def test_multiple_values(self):
lines = [
'Title: Death',
'Title: - a love story',
'Title:',
' (which happens to be true)',
]
self.assertDictEqual(
{
'Title': [
'Death',
'- a love story',
'(which happens to be true)'
]
},
fountain.parse_title_page(lines)
)
def test_empty_value_ignored(self):
lines = [
'Title:',
'Author: John August',
]
self.assertDictEqual(
{'Author': ['John August']},
fountain.parse_title_page(lines)
)
def test_unparsable_title_page_returns_none(self):
lines = [
'Title: Inception',
' additional line',
]
self.assertIsNone(fountain.parse_title_page(lines))
class PageBreakTests(unittest2.TestCase):
def test_page_break_sets_page_break_flag_on_next_paragraph(self):
paras = list(parse([
'====',
'',
'So here we go'
]))
self.assertEquals([PageBreak, Action], [type(p) for p in paras])
if __name__ == '__main__':
unittest2.main()
|