summaryrefslogtreecommitdiff
path: root/fountain/write.go
blob: 25eb41b3a09bdda4d62235d35b7ec4434e763b53 (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
package fountain

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

func Write(f io.Writer, scene []string, screenplay lex.Screenplay) {
	var titlepage = "start"
	for _, line := range screenplay {
		element := line.Type
		if titlepage == "start" && line.Type != "titlepage" {
			titlepage = ""
		}
		if titlepage != "" {
			element = titlepage
		}
		switch element {
		case "start":
			titlepage = "titlepage"
		case "titlepage":
			if line.Type == "metasection" {
				continue
			}
			if line.Type == "newpage" {
				fmt.Fprintln(f, "")
				titlepage = ""
				continue
			}
			fmt.Fprintf(f, "%s: %s\n", line.Type, line.Contents)
		case "newpage":
			fmt.Fprintln(f, "===")
		case "empty":
			fmt.Fprintln(f, "")
		case "speaker":
			if line.Contents != strings.ToUpper(line.Contents) {
				fmt.Fprint(f, "@")
			}
			fmt.Fprintln(f, line.Contents)
		case "scene":
			var supported bool
			for _, prefix := range scene {
				if strings.HasPrefix(line.Contents, prefix+" ") ||
				strings.HasPrefix(line.Contents, prefix+".") {
					supported = true
				}
			}
			if !supported {
				fmt.Fprint(f, ".")
			}
			fmt.Fprintln(f, line.Contents)
		case "lyrics":
			fmt.Fprintf(f, "~%s\n", line.Contents)
		case "action":
			if line.Contents == strings.ToUpper(line.Contents) {
				fmt.Fprint(f, "!")
			}
			fmt.Fprintln(f, line.Contents)
		default:
			fmt.Fprintln(f, line.Contents)
		}
	}
}