added expand option

This commit is contained in:
zeushammer 2022-02-24 00:58:22 +00:00
parent 243cf70ed7
commit 93e9a79494
2 changed files with 26 additions and 2 deletions

View file

@ -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

View file

@ -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 {