-
Notifications
You must be signed in to change notification settings - Fork 20
/
fountain.py
387 lines (356 loc) · 14.3 KB
/
fountain.py
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
"""
fountain.py
Ported to Python 3 by Colton J. Provias - cj@coltonprovias.com
Original Python code at https://gist.github.com/ColtonProvias/8232624
Based on Fountain by Nima Yousefi & John August
Original code for Objective-C at https://github.com/nyousefi/Fountain
Further Edited by Manuel Senfft
"""
COMMON_TRANSITIONS = {'FADE OUT.', 'CUT TO BLACK.', 'FADE TO BLACK.'}
class FountainElement:
def __init__(
self,
element_type,
element_text='',
section_depth=0,
scene_number='',
is_centered=False,
is_dual_dialogue=False,
original_line=0,
scene_abbreviation='.',
original_content=''
):
self.element_type = element_type
self.element_text = element_text
self.section_depth = section_depth
self.scene_number = scene_number
self.scene_abbreviation = scene_abbreviation
self.is_centered = is_centered
self.is_dual_dialogue = is_dual_dialogue
self.original_line = original_line
self.original_content = original_content
def __repr__(self):
return self.element_type + ': ' + self.element_text
class Fountain:
def __init__(self, string=None, path=None):
self.metadata = dict()
self.elements = list()
if path:
with open(path) as fp:
self.contents = fp.read()
else:
self.contents = string
if self.contents != '':
self.parse()
def parse(self):
contents = self.contents.strip().replace('\r', '')
contents_has_metadata = ':' in contents.splitlines()[0]
contents_has_body = '\n\n' in contents
if contents_has_metadata and contents_has_body:
script_head, script_body = contents.split('\n\n', 1)
self._parse_head(script_head.splitlines())
self._parse_body(script_body.splitlines())
elif contents_has_metadata and not contents_has_body:
self._parse_head(contents.splitlines())
else:
self._parse_body(contents.splitlines())
def _parse_head(self, script_head):
open_key = None
for line in script_head:
line = line.rstrip()
if line[0].isspace():
self.metadata[open_key].append(line.strip())
elif line[-1] == ':':
open_key = line[0:-1].lower()
self.metadata[open_key] = list()
else:
key, value = line.split(':', 1)
self.metadata[key.strip().lower()] = [value.strip()]
def _parse_body(self, script_body):
is_comment_block = False
is_inside_dialogue_block = False
newlines_before = 0
index = -1
comment_text = list()
for linenum, line in enumerate(script_body):
assert type(line) is str
index += 1
line = line.lstrip()
full_strip = line.strip()
if (not line or line.isspace()) and not is_comment_block:
self.elements.append(FountainElement('Empty Line'))
is_inside_dialogue_block = False
newlines_before += 1
continue
if line.startswith('/*'):
line = line.rstrip()
if line.endswith('*/'):
text = line.replace('/*', '').replace('*/', '')
self.elements.append(
FountainElement(
'Boneyard',
text,
original_line=linenum,
original_content=line
)
)
is_comment_block = False
newlines_before = 0
else:
is_comment_block = True
comment_text.append('')
continue
if line.rstrip().endswith('*/'):
text = line.replace('*/', '')
comment_text.append(text.strip())
self.elements.append(
FountainElement(
'Boneyard',
'\n'.join(comment_text),
original_line=linenum,
original_content=line
)
)
is_comment_block = False
comment_text = list()
newlines_before = 0
continue
if is_comment_block:
comment_text.append(line)
continue
if line.startswith('==='):
self.elements.append(
FountainElement(
'Page Break',
line,
original_line=linenum,
original_content=line
)
)
newlines_before = 0
continue
if len(full_strip) > 0 and full_strip[0] == '=':
self.elements.append(
FountainElement(
'Synopsis',
full_strip[1:].strip(),
original_line=linenum,
original_content=line
)
)
continue
if (
newlines_before > 0 and
full_strip.startswith('[[') and
full_strip.endswith(']]')
):
self.elements.append(
FountainElement(
'Comment',
full_strip, #.strip('[] \t')
original_line=linenum,
original_content=line
)
)
continue
if len(full_strip) > 0 and full_strip[0] == '#':
newlines_before = 0
depth = full_strip.split()[0].count('#')
self.elements.append(
FountainElement(
'Section Heading',
full_strip[depth:],
section_depth=depth,
original_line=linenum,
original_content=line
)
)
continue
if len(line) > 1 and line[0] == '.' and line[1] != '.':
newlines_before = 0
if full_strip[-1] == '#' and full_strip.count('#') > 1:
scene_number_start = len(full_strip) - \
full_strip[::-1].find('#', 1) - 1
self.elements.append(
FountainElement(
'Scene Heading',
full_strip[1:scene_number_start].strip(),
scene_number=full_strip[
scene_number_start:
].strip('#').strip(),
original_line=linenum,
original_content=line
)
)
else:
self.elements.append(
FountainElement(
'Scene Heading',
full_strip[1:].strip(),
original_line=linenum,
original_content=line
)
)
continue
if (
line[0:4].upper() in
['INT ', 'INT.', 'EXT ', 'EXT.', 'EST ', 'EST.', 'I/E ', 'I/E.'] or
line[0:8].upper() in ['INT/EXT ', 'INT/EXT.'] or
line[0:9].upper() in ['INT./EXT ', 'INT./EXT.']
):
newlines_before = 0
scene_name_start = line.find(line.split()[1])
if full_strip[-1] == '#' and full_strip.count('#') > 1:
scene_number_start = len(full_strip) - \
full_strip[::-1].find('#', 1) - 1
self.elements.append(
FountainElement(
'Scene Heading',
full_strip[
scene_name_start:scene_number_start
].strip(),
scene_number=full_strip[scene_number_start:].strip('#').strip(),
original_line=linenum,
scene_abbreviation=line.split()[0],
original_content=line
)
)
else:
self.elements.append(
FountainElement(
'Scene Heading',
full_strip[scene_name_start:].strip(),
original_line=linenum,
scene_abbreviation=line.split()[0],
original_content=line
)
)
continue
if full_strip.endswith(' TO:'):
newlines_before = 0
self.elements.append(
FountainElement(
'Transition',
full_strip,
original_line=linenum,
original_content=line
)
)
continue
if full_strip in COMMON_TRANSITIONS:
newlines_before = 0
self.elements.append(
FountainElement(
'Transition',
full_strip,
original_line=linenum,
original_content=line
)
)
continue
if full_strip[0] == '>':
newlines_before = 0
if full_strip.startswith('>') and full_strip.endswith('<'):
self.elements.append(
FountainElement(
'Action',
full_strip[1:-1].strip(),
is_centered=True,
original_line=linenum,
original_content=line
)
)
continue
if full_strip.startswith('>') and not full_strip.endswith('<'):
self.elements.append(
FountainElement(
'Transition',
full_strip[1:].strip(),
original_line=linenum,
original_content=line
)
)
continue
if full_strip[0] == '@':
self.elements.append(
FountainElement(
'Character',
full_strip[1:].strip(),
original_line=linenum,
original_content=line
)
)
is_inside_dialogue_block = True
continue
elif (
newlines_before > 0 and
index + 1 < len(script_body) and
script_body[index + 1] and
not line[0] in ['[', ']', ',', '(', ')']
):
newlines_before = 0
if full_strip[-1] == '^':
for element in reversed(self.elements):
if element.element_type == 'Character':
element.is_dual_dialogue = True
break
self.elements.append(
FountainElement(
'Character',
full_strip.rstrip('^').strip(),
is_dual_dialogue=True,
original_line=linenum,
original_content=line
)
)
is_inside_dialogue_block = True
else:
self.elements.append(
FountainElement(
'Character',
full_strip,
original_line=linenum,
original_content=line
)
)
is_inside_dialogue_block = True
continue
if is_inside_dialogue_block:
if newlines_before == 0 and full_strip[0] == '(':
self.elements.append(
FountainElement(
'Parenthetical',
full_strip,
original_line=linenum,
original_content=line
)
)
else:
if self.elements[-1].element_type == 'Dialogue':
self.elements[-1].element_text = '\n'.join(
[self.elements[-1].element_text, full_strip]
)
else:
self.elements.append(
FountainElement(
'Dialogue',
full_strip,
original_line=linenum,
original_content=line
)
)
continue
if newlines_before == 0 and len(self.elements) > 0:
self.elements[-1].element_text = '\n'.join(
[self.elements[-1].element_text, full_strip])
newlines_before = 0
else:
self.elements.append(
FountainElement(
'Action',
full_strip,
original_line=linenum,
original_content=line
)
)
newlines_before = 0