aboutsummaryrefslogtreecommitdiffstats
path: root/screenplain/parsers/fountain.py
blob: 5434c1a03cc833d98dee76a51422312c1c0a9867 (plain) (blame)
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
# Copyright (c) 2011 Martin Vilcans
# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license.php

import itertools
from itertools import takewhile
import re
from six import next

from screenplain.types import (
    Slug, Action, Dialog, DualDialog, Transition, Section, PageBreak,
    Screenplay
)
from screenplain.richstring import parse_emphasis, plain

slug_regexes = (
    re.compile(r'^(INT|EXT|EST)[ .]'),
    re.compile(r'^(INT\.?/EXT\.?)[ .]'),
    re.compile(r'^I/E[ .]'),
)

boneyard_re = re.compile(r'/\*.*?\*/', flags=re.DOTALL)

TWOSPACE = ' ' * 2

linebreak_re = re.compile('\r\n|\n|\r')

title_page_key_re = re.compile(r'([^:]+):\s*(.*)')
title_page_value_re = re.compile(r'(?:\s{3,}|\t)(.+)')

centered_re = re.compile(r'\s*>\s*(.*?)\s*<\s*$')
dual_dialog_re = re.compile(r'^(.+?)(\s*\^)$')
slug_re = re.compile(r'(?:(\.)(?=[^.])\s*)?(\S.*?)\s*$')
scene_number_re = re.compile(r'(.*?)\s*(?:#([\w\-.]+)#)\s*$')
section_re = re.compile(r'^(#{1,6})\s*([^#].*)$')
transition_re = re.compile(r'(>?)\s*(.+?)(TO:)?$')
page_break_re = re.compile(r'^={3,}$')


def _sequence_to_rich(lines):
    """Converts a sequence of strings into a list of RichString."""
    return [parse_emphasis(line) for line in lines]


def _string_to_rich(line):
    """Converts a single string into a RichString.
    """
    return parse_emphasis(line)


class InputParagraph(object):
    def __init__(self, lines):
        self.lines = lines

    def update_list(self, previous_paragraphs):
        """Inserts this paragraph into a list.
        Modifies the `previous_paragraphs` list.
        """
        (
            self.append_page_break(previous_paragraphs) or
            self.append_synopsis(previous_paragraphs) or
            self.append_sections_and_synopsises(previous_paragraphs) or
            self.append_slug(previous_paragraphs) or
            self.append_centered_action(previous_paragraphs) or
            self.append_dialog(previous_paragraphs) or
            self.append_transition(previous_paragraphs) or
            self.append_action(previous_paragraphs)
        )

    def append_slug(self, paragraphs):
        if len(self.lines) != 1:
            return False

        match = slug_re.match(self.lines[0])
        if not match:
            return False

        period, text = match.groups()
        text = text.upper()
        if not period and not any(regex.match(text) for regex in slug_regexes):
            return False

        match = scene_number_re.match(text)
        if match:
            text, scene_number = match.groups()
            paragraphs.append(Slug(_string_to_rich(text), plain(scene_number)))
        else:
            paragraphs.append(Slug(_string_to_rich(text)))
        return True

    def append_sections_and_synopsises(self, paragraphs):

        new_paragraphs = []

        for line in self.lines:
            match = section_re.match(line)
            if match:
                hashes, text = match.groups()
                section = Section(_string_to_rich(text), len(hashes))
                new_paragraphs.append(section)
            elif (
                line.startswith('=') and
                new_paragraphs and
                hasattr(new_paragraphs[-1], 'set_synopsis')
            ):
                new_paragraphs[-1].set_synopsis(line[1:].lstrip())
            else:
                return False

        paragraphs += new_paragraphs
        return True

    def append_centered_action(self, paragraphs):
        if not all(centered_re.match(line) for line in self.lines):
            return False
        paragraphs.append(Action(_sequence_to_rich(
            centered_re.match(line).group(1) for line in self.lines
        ), centered=True))
        return True

    def _create_dialog(self, character):
        return Dialog(
            parse_emphasis(character.strip()),
            _sequence_to_rich(line.strip() for line in self.lines[1:])
        )

    def append_dialog(self, paragraphs):
        if len(self.lines) < 2:
            return False

        character = self.lines[0]
        if character.endswith(TWOSPACE):
            return False
        if character.startswith('@') and len(character) >= 2:
            character = character[1:]
        elif not character.isupper():
            return False

        if paragraphs and isinstance(paragraphs[-1], Dialog):
            dual_match = dual_dialog_re.match(character)
            if dual_match:
                previous = paragraphs.pop()
                dialog = self._create_dialog(dual_match.group(1))
                paragraphs.append(DualDialog(previous, dialog))
                return True

        paragraphs.append(self._create_dialog(character))
        return True

    def append_transition(self, paragraphs):
        if len(self.lines) != 1:
            return False

        match = transition_re.match(self.lines[0])
        if not match:
            return False
        greater_than, text, to_colon = match.groups()

        if greater_than:
            paragraphs.append(
                Transition(_string_to_rich(text.upper() + (to_colon or '')))
            )
            return True

        if text.isupper() and to_colon:
            paragraphs.append(
                Transition(_string_to_rich(text + to_colon))
            )
            return True

        return False

    def append_action(self, paragraphs):
        paragraphs.append(
            Action(_sequence_to_rich(line.rstrip() for line in self.lines))
        )
        return True

    def append_synopsis(self, paragraphs):
        if (
            len(self.lines) == 1 and
            self.lines[0].startswith('=') and
            paragraphs and
            hasattr(paragraphs[-1], 'set_synopsis')
        ):
            paragraphs[-1].set_synopsis(self.lines[0][1:].lstrip())
            return True
        else:
            return False

    def append_page_break(self, paragraphs):
        if len(self.lines) == 1 and page_break_re.match(self.lines[0]):
            paragraphs.append(PageBreak())
            return True
        else:
            return False


def _preprocess_line(raw_line):
    r"""Replaces tabs with spaces and removes trailing end of line markers.

    >>> _preprocess_line('foo \r\n\n')
    'foo '

    """
    return raw_line.expandtabs(4).rstrip('\r\n')


def _is_blank(line):
    return line == ''


def parse(stream):
    """Parses Fountain source.

    Returns a Screenplay object.

    """
    content = stream.read()
    content = boneyard_re.sub('', content)
    lines = linebreak_re.split(content)
    del content
    return parse_lines(lines)


def parse_lines(source):
    """Reads raw text input and generates paragraph objects.

    Returns a Screenplay object.

    """
    source = (_preprocess_line(line) for line in source)

    title_page_lines = list(takewhile(lambda line: line != '', source))
    title_page = parse_title_page(title_page_lines)

    if title_page:
        # The first lines were a title page.
        # Parse the rest of the source as screenplay body.
        return Screenplay(title_page, parse_body(source))
    else:
        # The first lines were not a title page.
        # Parse them as part of the screenplay body.
        return Screenplay(
            {},
            parse_body(itertools.chain(title_page_lines, [''], source))
        )


def parse_body(source):
    """Reads lines of the main screenplay and generates paragraph objects."""

    paragraphs = []
    for blank, input_lines in itertools.groupby(source, _is_blank):
        if not blank:
            paragraph = InputParagraph(list(input_lines))
            paragraph.update_list(paragraphs)

    return paragraphs


def parse_title_page(lines):
    """Parse the title page.

    Spec: http://fountain.io/syntax#section-titlepage
    Returns None if the document does not have a title page section,
    otherwise a dictionary with the data.
    """
    result = {}

    it = iter(lines)
    try:
        line = next(it)
        while True:
            key_match = title_page_key_re.match(line)
            if not key_match:
                return None
            key, value = key_match.groups()
            if value:
                # Single line key/value
                result.setdefault(key, []).append(value)
                line = next(it)
            else:
                for line in it:
                    value_match = title_page_value_re.match(line)
                    if not value_match:
                        break
                    result.setdefault(key, []).append(value_match.group(1))
                else:
                    # Last line has been processed
                    break
    except StopIteration:
        pass
    return result