summaryrefslogtreecommitdiff
path: root/lex/parse.go
blob: 00a6cbdce9c5757be77efd38ca50212efb8e3ded (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
package lex

import (
	"bufio"
	"io"
	"strings"
)

//Parse walks through the lex file, which contains the element of a screenplay,
//optionally followed by a colon and space and the actual contents of that element.
//Special elements exist: newpage, titlepage and metasection.
//These elements trigger pdf creation instructions.
func Parse(file io.Reader) (out Screenplay) {
	f := bufio.NewReader(file)
	var err error
	var s string
	for err == nil {
		var line Line
		s, err = f.ReadString('\n')
		split := strings.SplitN(s, ":", 2)
		switch len(split) {
		case 0, 1:
			line.Type = strings.Trim(s, ": \n\r")
		case 2:
			line.Type = split[0]
			line.Contents = strings.TrimSpace(split[1])
		}
		if strings.TrimSpace(split[0]) != "" {
			out = append(out, line)
		}
	}
	return out
}