summaryrefslogtreecommitdiff
path: root/fountain/parse.go
blob: a3e17479158ac40df90df22aa915f6a7b713e8c7 (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
package fountain

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

func Parse(file io.Reader) (out lex.Screenplay) {
	var err error
	var s string
	var toParse []string
	f := bufio.NewReader(file)
	for err == nil {
		s, err = f.ReadString('\n')
		toParse = append(toParse, s)
	}
	for i, row := range toParse {
		row = strings.TrimSpace(row)
		action := "action"
		if row == strings.ToUpper(row) {
			action = "allcaps"
		}
		if row == "" {
			action = "empty"
			continue
		}
		if i <= 0 {
			continue
		}
		switch out[i-1].Type {
		case "allcaps":
			out[i-1].Type = "speaker"
			if row[0] == '(' && row[len(row)-1] == ')' {
				action = "paren"
			} else {
				action = "dialog"
			}
		case "paren", "dialog":
			action = "dialog"
		}
		out = append(out, lex.Line{action, row})
	}
	return out
}