From 7febcd7d5ecebccc866e10d3d31ef771941be538 Mon Sep 17 00:00:00 2001 From: "R. S. Doiel" Date: Mon, 11 Mar 2024 09:29:10 -0700 Subject: [PATCH] maintenance, added to YAML conversions --- cli.go | 11 ++++++ go.mod | 6 ++-- go.sum | 10 +++--- scripttool.go | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++ version.go | 2 +- 5 files changed, 116 insertions(+), 8 deletions(-) diff --git a/cli.go b/cli.go index ff6a585..70cc67f 100644 --- a/cli.go +++ b/cli.go @@ -102,6 +102,10 @@ func RunScripttool(in *os.File, out *os.File, eout *os.File, args []string) erro } return fmt.Errorf("A FadeIn filename required for input") + case "fadein2yaml": + if inputFName != "" { + return FadeInToYAML(inputFName, out) + } case "fadein2osf": if inputFName != "" { return FadeInToOSF(inputFName, out) @@ -119,6 +123,8 @@ func RunScripttool(in *os.File, out *os.File, eout *os.File, args []string) erro return FdxToFountain(in, out) case "fdx2json": return FdxToJSON(in, out) + case "fdx2yaml": + return FdxToYAML(in, out) case "fdx2osf": return FdxToOSF(in, out) @@ -132,6 +138,8 @@ func RunScripttool(in *os.File, out *os.File, eout *os.File, args []string) erro return FountainToFdx(in, out) case "fountain2json": return FountainToJSON(in, out) + case "fountain2yaml": + return FountainToYAML(in, out) case "fountain2osf": return FountainToOSF(in, out) @@ -147,6 +155,8 @@ func RunScripttool(in *os.File, out *os.File, eout *os.File, args []string) erro return OSFToFountain(in, out) case "osf2json": return OSFToJSON(in, out) + case "osf2yaml": + return OSFToYAML(in, out) // Utility fountain functions case "fountain2fountain": @@ -169,4 +179,5 @@ func RunScripttool(in *os.File, out *os.File, eout *os.File, args []string) erro default: return fmt.Errorf("do not understand %q in %q", verb, strings.Join(args, " ")) } + return nil } diff --git a/go.mod b/go.mod index e2323e6..b9388cd 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module github.com/rsdoiel/scripttool go 1.22.0 require ( - github.com/rsdoiel/fdx v1.0.1 + github.com/rsdoiel/fdx v1.0.2 github.com/rsdoiel/fountain v1.0.1 - github.com/rsdoiel/osf v0.0.5 + github.com/rsdoiel/osf v0.0.7 ) -require gopkg.in/yaml.v3 v3.0.1 // indirect +require gopkg.in/yaml.v3 v3.0.1 diff --git a/go.sum b/go.sum index 055f954..da8b314 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,11 @@ -github.com/rsdoiel/fdx v1.0.1 h1:BXQWLWQtosLaMXzSboXiuru2pxELc4qV3JJ4Y3cv9vQ= -github.com/rsdoiel/fdx v1.0.1/go.mod h1:hXJBkY2UoujfU1EZ1tiR8aa3AZgcReOydZZ7dAuyg9o= +github.com/rsdoiel/fdx v1.0.2 h1:TMBGgYdG7CaPbnM6kMAkRebYyCfWK9dK/qooPhEITU8= +github.com/rsdoiel/fdx v1.0.2/go.mod h1:hXJBkY2UoujfU1EZ1tiR8aa3AZgcReOydZZ7dAuyg9o= github.com/rsdoiel/fountain v1.0.1 h1:m5JZbnasFendZbYzoTsOsJ/gcup7VNTUjJGTANshQck= github.com/rsdoiel/fountain v1.0.1/go.mod h1:Sd3MZuWObP+tssyGIuUUfl3+nslvRVndkEEpBVsWAJU= -github.com/rsdoiel/osf v0.0.5 h1:wV919Li4KPiDM5x/qFn86Tn5d3YPtPeqmGYmeIh1vUw= -github.com/rsdoiel/osf v0.0.5/go.mod h1:UzyqbsRFk5rL3WTiFvlfvL4jsqgt/EPHVlbs9FShFwI= +github.com/rsdoiel/osf v0.0.6 h1:5ijMEfFH1zzBsYTeChyCi06sYQhboMBX7rSr/gW7ZnY= +github.com/rsdoiel/osf v0.0.6/go.mod h1:UzyqbsRFk5rL3WTiFvlfvL4jsqgt/EPHVlbs9FShFwI= +github.com/rsdoiel/osf v0.0.7 h1:woMTMPktKWhQJdPGVt1zmVbq9jJFmPiq0lqzWNnmR/s= +github.com/rsdoiel/osf v0.0.7/go.mod h1:jmOePx0mqICIH+G7GuIFnWGckQB7XWovQhdGMp9dPgE= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/scripttool.go b/scripttool.go index bbe881e..bd23be5 100644 --- a/scripttool.go +++ b/scripttool.go @@ -48,6 +48,9 @@ import ( "github.com/rsdoiel/fdx" "github.com/rsdoiel/fountain" "github.com/rsdoiel/osf" + + // 3rdParty Packages + "gopkg.in/yaml.v3" ) var ( @@ -142,6 +145,27 @@ func FdxToJSON(in io.Reader, out io.Writer) error { return nil } +// FdxToYAML converts an input buffer from a .fdx file to a .yaml format +func FdxToYAML(in io.Reader, out io.Writer) error { + src, err := ioutil.ReadAll(in) + if err != nil { + return err + } + document, err := fdx.Parse(src) + if err != nil { + return err + } + fmt.Printf("DEBUG document -> %+v\n", document) + + encoder := yaml.NewEncoder(out) + encoder.SetIndent(2) + err = encoder.Encode(document) + if err != nil { + return err + } + return nil +} + // FdxToOSF concerts an input buffer from .fdx to Open Screenplay Format 2.0 func FdxToOSF(in io.Reader, out io.Writer) error { src, err := ioutil.ReadAll(in) @@ -271,6 +295,25 @@ func OSFToJSON(in io.Reader, out io.Writer) error { return nil } +// OSFToYAML converts the input buffer from .osf to .yaml format. +func OSFToYAML(in io.Reader, out io.Writer) error { + src, err := ioutil.ReadAll(in) + if err != nil { + return err + } + document, err := osf.Parse(src) + if err != nil { + return err + } + encoder := yaml.NewEncoder(out) + encoder.SetIndent(2) + err = encoder.Encode(document) + if err != nil { + return err + } + return nil +} + // Fountain2Fountain reads a input buffer as .fountain and pretty prints output as .fountain func FountainToFountain(in io.Reader, out io.Writer) error { src, err := ioutil.ReadAll(in) @@ -435,6 +478,43 @@ func FadeInToJSON(inputFName string, out io.Writer) error { return nil } +// FadeInToYAML converts an input file to YAML format. +func FadeInToYAML(inputFName string, out io.Writer) error { + // NOTE: Need to unzip, extract document.xml then pass the source + // of document.xml to osf.Parse() + r, err := zip.OpenReader(inputFName) + if err != nil { + return err + } + defer r.Close() + src := []byte{} + for _, f := range r.File { + if f.Name == "document.xml" { + rc, err := f.Open() + if err != nil { + return err + } + src, err = ioutil.ReadAll(rc) + if err != nil { + return err + } + rc.Close() + break + } + } + document, err := osf.Parse(src) + if err != nil { + return err + } + encoder := yaml.NewEncoder(out) + encoder.SetIndent(2) + err = encoder.Encode(document) + if err != nil { + return err + } + return nil +} + // FadeInToOSF converts an input file to .fountain format. func FadeInToOSF(inputFName string, out io.Writer) error { // NOTE: Need to unzip, extract document.xml then pass the source @@ -626,3 +706,18 @@ func FountainToJSON(in io.Reader, out io.Writer) error { _, err = out.Write(src) return err } + +// FountainToYAML convert .fountain file to YAML +func FountainToYAML(in io.Reader, out io.Writer) error { + src, err := ioutil.ReadAll(in) + if err != nil { + return err + } + screenplay, err := fountain.Parse(src) + if err != nil { + return err + } + encoder := yaml.NewEncoder(out) + encoder.SetIndent(2) + return encoder.Encode(screenplay) +} diff --git a/version.go b/version.go index 4a3ad03..e64021b 100644 --- a/version.go +++ b/version.go @@ -12,7 +12,7 @@ const ( ReleaseDate = "2024-03-11" // ReleaseHash, the Git hash when version.go was generated - ReleaseHash = "8cbd563" + ReleaseHash = "3653ea2" LicenseText = ` All rights reserved.