-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (129 loc) · 4.08 KB
/
main.go
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
// Lexington is a command line tool to convert between several formats used for screenwriting.
// When you write a screenplay in Fountain, you can use this tool to convert it into other formats.
// The tool uses an internal format called lex which can be used to tweak the output.
// Run the compiled tool with --help to get information about usage.
package main
import (
"github.com/lapingvino/lexington/fountain"
"github.com/lapingvino/lexington/lex"
"github.com/lapingvino/lexington/pdf"
"github.com/lapingvino/lexington/rules"
"flag"
"io"
"log"
"os"
"strings"
"time"
)
func main() {
start := time.Now()
defer func() {
log.Printf("Conversion took %v", time.Since(start))
}()
config := flag.String("config", "lexington.toml", "Configuration file to use.")
dump := flag.Bool("dumpconfig", false, "Dump the default configuration to the location of --config to be adapted manually.")
scenein := flag.String("scenein", "", "Configuration to use for scene header detection on input.")
sceneout := flag.String("sceneout", "", "Configuration to use for scene header detection on output.")
elements := flag.String("e", "default", "Element settings from settings file to use.")
input := flag.String("i", "-", "Input from provided filename. - means standard input.")
output := flag.String("o", "-", "Output to provided filename. - means standard output.")
from := flag.String("from", "", "Input file type. Choose from fountain, lex [, fdx]. Formats between angle brackets are planned to be supported, but are not supported by this binary.")
to := flag.String("to", "", "Output file type. Choose from pdf (built-in, doesn't support inline markup), lex (helpful for troubleshooting and correcting fountain parsing), fountain, [html, epub*, mobi*, docx*, odt*, fdx]. Formats marked with a little star need an additional external tool to work. Formats between angle brackets are planned to be supported, but are not supported by this binary.")
help := flag.Bool("help", false, "Show this help message")
flag.Parse()
if *help {
flag.PrintDefaults()
return
}
if *dump {
err := rules.Dump(*config)
if err != nil {
log.Println("Error dumping configuration: ", err)
}
log.Println("Configuration dumped to ", *config)
return
}
ins := strings.Split(*input, ".")
if len(ins) > 1 && *from == "" {
*from = ins[len(ins)-1]
}
if len(ins) > 2 && *scenein == "" {
*scenein = ins[len(ins)-2]
}
outs := strings.Split(*output, ".")
if len(outs) > 1 && *to == "" {
*to = outs[len(outs)-1]
}
if len(outs) > 2 && *sceneout == "" {
*sceneout = outs[len(outs)-2]
}
if *from == "" {
*from = "fountain"
}
if *to == "" && *output == "-" {
*to = "lex"
}
if *scenein == "" {
*scenein = "en"
}
if *sceneout == "" {
*sceneout = "en"
}
log.Printf("Scenein: %s ; Sceneout: %s ;\n", *scenein, *sceneout)
var infile io.Reader
var outfile io.Writer
var i lex.Screenplay
conf := rules.GetConf(*config)
if *input == "-" {
infile = os.Stdin
log.Println("Reading from Stdin")
} else {
var err error
infile, err = os.Open(*input)
if err != nil {
log.Println("Error opening file: ", err)
return
}
}
if *output == "-" {
outfile = os.Stdout
log.Println("Writing to Stdout")
} else {
if *output == "" && len(ins) > 0 && ins[0] != "" {
*output = ins[0] + "." + *to
}
var err error
outfile, err = os.Create(*output)
if err != nil {
log.Println("Error creating output file: ", err)
return
}
}
log.Println("Input type is ", *from)
switch *from {
case "lex":
i = lex.Parse(infile)
case "fountain":
i = fountain.Parse(conf.Scenes[*scenein], infile)
default:
log.Printf("%s is not a valid input type", *from)
}
log.Println("Output type is ", *to)
switch *to {
case "pdf":
if *output == "-" && len(ins) > 0 && ins[0] != "" {
*output = ins[0] + ".pdf"
}
if *output == "-" {
log.Println("Cannot write PDF to standard output")
return
}
pdf.Create(*output, conf.Elements[*elements], i)
case "lex":
lex.Write(i, outfile)
case "fountain":
fountain.Write(outfile, conf.Scenes[*sceneout], i)
default:
log.Printf("%s is not a valid output type", *to)
}
}