summaryrefslogtreecommitdiff
path: root/fountain/parse.go
blob: abc3164b441044540ecdfc6e316a9eb933eaea9d (plain)
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
// Fountain is a Markdown-like language for screenplays and the main inspiration for this tool.
// Read more at fountain.io
package fountain

import (
	"bufio"
	"github.com/lapingvino/lexington/lex"
	"io"
	"strings"
)

var Scene = []string{"INT", "EXT", "EST", "INT./EXT", "INT/EXT", "EXT/INT", "EXT./INT", "I/E"}

func last(out *lex.Screenplay, i int) *lex.Line {
	if len(*out) >= i {
		return &(*out)[len(*out)-i]
	} else {
		line := lex.Line{Type: "empty"}
		return &line
	}
}

func CheckScene(row string) (bool, string, string) {
	var scene bool
	row = strings.ToUpper(row)
	for _, prefix := range Scene {
		if strings.HasPrefix(row, prefix+" ") ||
			strings.HasPrefix(row, prefix+".") {
			scene = true
		}
	}
	if strings.HasPrefix(row, ".") {
		row = row[1:]
		scene = true
	}
	return scene, "scene", row
}

func CheckCrow(row string) (bool, string, string) {
	var crow bool
	var el string
	row = strings.ToUpper(row)
	if strings.HasPrefix(row, ">") || strings.HasSuffix(row, " TO:") {
		crow = true
		el = "trans"
	}
	if strings.HasPrefix(row, ">") && strings.HasSuffix(row, "<") {
		el = "center"
	}
	return crow, el, strings.Trim(row, ">< ")
}

func CheckEqual(row string) (bool, string, string) {
	var equal bool
	var el string
	if strings.HasPrefix(row, "=") {
		equal = true
		el = "synopse"
	}
	if len(row) >= 3 && strings.Trim(row, "=") == "" {
		el = "newpage"
	}
	return equal, el, strings.TrimLeft(row, "= ")
}

func CheckSection(row string) (bool, string, string) {
	var section bool
	if strings.HasPrefix(row, "#") {
		section = true
	}
	return section, "section", row
}

func CheckForce(row string) (bool, string, string) {
	var force = true
	var ftype string
	if len(row) < 1 {
		return false, "", ""
	}
	switch row[0] {
	case '@':
		ftype = "speaker"
	case '~':
		ftype = "lyrics"
	case '!':
		ftype = "action"
	default:
		force = false
	}
	if force {
		row = row[1:]
	}
	return force, ftype, row
}

// This is a Fountain parser, trying to be as close as possible to the description
// found at https://fountain.io/syntax but it can be incomplete.
// Over time more and more parts should be configurable here, e.g. INT/EXT translatable to other languages.
func Parse(file io.Reader) (out lex.Screenplay) {
	var err error
	var titlepage, dialog bool = true, false
	var s, titletag string
	var toParse []string // Fill with two to avoid out of bounds when backtracking
	f := bufio.NewReader(file)
	for err == nil {
		s, err = f.ReadString('\n')
		toParse = append(toParse, s)
	}
	toParse = append(toParse, "") // Trigger the backtracking also for the last line
	for _, row := range toParse {
		row = strings.TrimRight(row, "\n\r")
		action := "action"
		if row == "" {
			action = "empty"
			if titlepage {
				titlepage = false
				action = "newpage"
			}
		}
		if last(&out, 1).Type != "action" {
			last(&out, 1).Contents = strings.TrimSpace(last(&out, 1).Contents)
		}

		// Backtracking to check for dialog sequence
		if dialog {
			if row == "" {
				dialog = false
				action = "empty"
				if last(&out, 1).Type == "speaker" {
					last(&out, 1).Type = "action"
				}
			} else {
				if row[0] == '(' && row[len(row)-1] == ')' {
					action = "paren"
				} else {
					action = "dialog"
				}
			}
		}
		if row == strings.ToUpper(row) && action == "action" {
			action = "speaker"
			dialog = true
		}

		checkfuncs := []func(string) (bool, string, string){
			CheckScene, // should actually check for empty lines, but doing that creates more problems than it solves
			CheckCrow,
			CheckEqual,
			CheckSection,
			CheckForce,
		}
		for _, checkfunc := range checkfuncs {
			check, element, contents := checkfunc(row)
			if check {
				action = element
				row = contents
				if action == "speaker" {
					dialog = true
				}
				break
			}
		}

		if titlepage {
			if titletag == "" {
				out = append(out, lex.Line{Type: "titlepage"})
			}
			split := strings.SplitN(row, ":", 2)
			if len(split) == 2 {
				action = split[0]
				switch strings.ToLower(action) {
				case "title", "credit", "author", "authors":
					titletag = "title"
				default:
					if titletag == "title" {
						out = append(out, lex.Line{Type: "metasection"})
					}
					titletag = action
				}
				row = strings.TrimSpace(split[1])
				if row == "" {
					continue
				}
			} else {
				action = titletag
				row = strings.TrimSpace(row)
			}
		}
		if titlepage && titletag == "" {
			titlepage = false
			action = "newpage"
		}
		out = append(out, lex.Line{action, row})
	}
	return out
}