From 93e9a79494e6f97f9ec771f15494a5877ec41490 Mon Sep 17 00:00:00 2001 From: zeushammer Date: Thu, 24 Feb 2022 00:58:22 +0000 Subject: [PATCH] added expand option --- config.go | 4 ++++ tweego.go | 24 ++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index 319175b..65bf4e8 100644 --- a/config.go +++ b/config.go @@ -37,6 +37,7 @@ type config struct { common encoding string // input encoding + expand bool // expand story passages into single files sourcePaths []string // slice of paths to seach for source files modulePaths []string // slice of paths to seach for module files headFile string // name of the head file @@ -126,6 +127,7 @@ func newConfig() *config { options.Add("archive_twine2", "-a|--archive-twine2") options.Add("archive_twine1", "--archive-twine1") options.Add("decompile_twee3", "-d|--decompile-twee3|--decompile") // NOTE: "--decompile" is deprecated. + options.Add("expand", "--expand") options.Add("decompile_twee1", "--decompile-twee1") options.Add("encoding", "-c=s|--charset=s") options.Add("format", "-f=s|--format=s") @@ -156,6 +158,8 @@ func newConfig() *config { c.outMode = outModeTwee1 case "encoding": c.encoding = val.(string) + case "expand": + c.expand = true case "format": c.cmdline.formatID = val.(string) c.formatID = c.cmdline.formatID diff --git a/tweego.go b/tweego.go index 49daf49..b626296 100644 --- a/tweego.go +++ b/tweego.go @@ -10,6 +10,7 @@ package main import ( "log" + "regexp" ) const tweegoName = "tweego" @@ -65,10 +66,29 @@ func buildOutput(c *config) *story { // Write the output. switch c.outMode { case outModeTwee3, outModeTwee1: - // Write out the project as Twee source. - if _, err := fileWriteAll(c.outFile, alignRecordSeparators(s.toTwee(c.outMode))); err != nil { + + switch c.expand { + case true: + // Make a Regex to say we only want letters and numbers + reg, err := regexp.Compile("[^a-zA-Z0-9]+") + if err != nil { + log.Fatal(err) + } + + for _, p := range s.passages { + processedString := reg.ReplaceAllString(p.name, "") + if _, err := fileWriteAll(processedString, []byte(p.toTwee(c.outMode))); err != nil { + log.Fatalf(`error: %s`, err.Error()) + } + } + case false: + // Write out the project as Twee source. + if _, err := fileWriteAll(c.outFile, alignRecordSeparators(s.toTwee(c.outMode))); err != nil { log.Fatalf(`error: %s`, err.Error()) } + } + + case outModeTwine2Archive: // Write out the project as Twine 2 archived HTML. if _, err := fileWriteAll(c.outFile, s.toTwine2Archive(c.startName)); err != nil {