diff options
-rw-r--r-- | fountain/parse.go | 24 | ||||
-rw-r--r-- | lex/example.go | 5 | ||||
-rw-r--r-- | lex/example.lex | 6 | ||||
-rw-r--r-- | lex/parse.go | 29 | ||||
-rw-r--r-- | lex/type.go | 14 | ||||
-rw-r--r-- | lex/write.go | 12 | ||||
-rw-r--r-- | main.go | 71 | ||||
-rw-r--r-- | pdf/create.go | 1 | ||||
-rw-r--r-- | rules/rules.go | 6 |
9 files changed, 141 insertions, 27 deletions
diff --git a/fountain/parse.go b/fountain/parse.go index b4273d7..ba3a3e8 100644 --- a/fountain/parse.go +++ b/fountain/parse.go @@ -1,12 +1,23 @@ -package parse +package fountain import ( + "github.com/lapingvino/lexington/lex" "strings" + "bufio" + "io" ) -func (t *Tree) ParseString(play string) { - toParse := strings.Split(play, "\n") +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" @@ -15,9 +26,9 @@ func (t *Tree) ParseString(play string) { action = "empty" } else { if i > 0 { - switch t.F[i-1].Format { + switch out[i-1].Type { case "allcaps": - t.F[i-1].Format = "speaker" + out[i-1].Type = "speaker" if row[0] == '(' && row[len(row)-1] == ')' { action = "paren" } else { @@ -28,6 +39,7 @@ func (t *Tree) ParseString(play string) { } } } - t.F = append(t.F, struct{ Format, Text string }{action, row}) + out = append(out, lex.Line{action, row}) } + return out } diff --git a/lex/example.go b/lex/example.go deleted file mode 100644 index 164e71a..0000000 --- a/lex/example.go +++ /dev/null @@ -1,5 +0,0 @@ -package lex - -var Example = ` - -` diff --git a/lex/example.lex b/lex/example.lex index 2b39de5..4a1dfee 100644 --- a/lex/example.lex +++ b/lex/example.lex @@ -1,12 +1,18 @@ scene: INT. HOUSE - DAY +empty speaker: MARY dialog: I can't believe how easy it is to write in Fountain. +empty speaker: TOM paren: (typing) dialog: Look! I just made a parenthetical! +empty action: SOMETHING HAPPENS! +action: action: (what? I don't know...) +empty scene: EXT. GARDEN +empty speaker: TOM dialog: What am I doing here now? dialog: To be honest, I have absolutely no idea! diff --git a/lex/parse.go b/lex/parse.go new file mode 100644 index 0000000..ebd4dda --- /dev/null +++ b/lex/parse.go @@ -0,0 +1,29 @@ +package lex + +import ( + "io" + "bufio" + "strings" +) + +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 +} diff --git a/lex/type.go b/lex/type.go index 47439f4..3b49eee 100644 --- a/lex/type.go +++ b/lex/type.go @@ -1,10 +1,5 @@ package lex -import ( - "io" - "fmt" -) - type Screenplay []Line type Line struct{ @@ -12,12 +7,3 @@ type Line struct{ Contents string } -func Parse(file io.Reader) (out Screenplay) { - var err error - line := Line{} - for err == nil { - _, err = fmt.Fscanf(file, "%s: %s", &line.Type, &line.Contents) - out = append(out, line) - } - return out -} diff --git a/lex/write.go b/lex/write.go new file mode 100644 index 0000000..b8ee6c2 --- /dev/null +++ b/lex/write.go @@ -0,0 +1,12 @@ +package lex + +import ( + "fmt" + "io" +) + +func Write(s Screenplay, out io.Writer) { + for _, line := range s { + fmt.Fprintf(out, "%s: %s\n", line.Type, line.Contents) + } +} @@ -2,10 +2,79 @@ package main import ( "github.com/lapingvino/lexington/lex" + "github.com/lapingvino/lexington/fountain" "github.com/lapingvino/lexington/pdf" "github.com/lapingvino/lexington/rules" + + "flag" + "log" + "os" + "io" ) func main() { - pdf.Create("fountain.pdf", rules.Default, lex.Screenplay{}) + input := flag.String("i", "-", "Input from provided filename") + output := flag.String("o", "-", "Output to provided filename") + from := flag.String("from", "fountain", "Input file type. Choose from fountain, fdx, lex") + to := flag.String("to", "pdf", "Output file type. Choose from pdf, html, epub, mobi, docx, odt, fountain, fdx, lex") + help := flag.Bool("help", false, "Show this help message") + flag.Parse() + + if *help { + flag.PrintDefaults() + return + } + + var infile io.Reader + var outfile io.Writer + var i lex.Screenplay + + 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 { + 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(infile) + default: + log.Printf("%s is not a valid input type", *from) + } + + log.Println("Output type is ", *to) + switch *to { + case "pdf": + if *output == "-" { + log.Println("Cannot write PDF to standard output") + return + } + pdf.Create(*output, rules.Default, i) + case "lex": + lex.Write(i, outfile) + default: + log.Printf("%s is not a valid output type", *to) + } } diff --git a/pdf/create.go b/pdf/create.go index abf0284..79184a4 100644 --- a/pdf/create.go +++ b/pdf/create.go @@ -41,6 +41,7 @@ func Create(file string, format rules.Set, contents lex.Screenplay) { pdf.SetMargins(1, 1, 1) pdf.SetXY(1, 1) f := Tree{ + PDF: pdf, Rules: format, F: contents, } diff --git a/rules/rules.go b/rules/rules.go index 07fe588..cd8e890 100644 --- a/rules/rules.go +++ b/rules/rules.go @@ -46,7 +46,7 @@ var Default = Set{ }, "paren": { Left: 3.6, - Width: 2, + Width: 4, }, "trans": { Left: 6, @@ -60,4 +60,8 @@ var Default = Set{ Left: 1.5, Width: 6, }, + "empty": { + Left: 1.5, + Width: 6, + }, } |