ADD: Add JSON output (-j, --json).

This commit is contained in:
Thomas M. Edwards 2021-02-10 16:22:52 -06:00
parent 33a6da381c
commit 3a8dfb2344
8 changed files with 175 additions and 92 deletions

View file

@ -8,6 +8,7 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
@ -17,6 +18,40 @@ import (
"time"
)
func (s *story) toJSON(startName string) []byte {
var passages = make([]*passageJSON, 0, 32)
for _, p := range s.passages {
if p.name == "StoryTitle" || p.name == "StoryData" {
continue
}
passages = append(passages, p.toJSON())
}
marshaled, err := json.MarshalIndent(
&storyJSON{
s.name,
s.ifid,
startName,
twine2OptionsMapToSlice(s.twine2.options),
s.twine2.format,
s.twine2.formatVersion,
strings.Title(tweegoName),
tweegoVersion.Version(),
passages,
},
"",
"\t",
)
if err != nil {
// NOTE: We should never be able to see an error here. If we do,
// then something truly exceptional—in a bad way—has happened, so
// we get our panic on.
panic(err)
}
return marshaled
}
func (s *story) toTwee(outMode outputMode) []byte {
var data []byte
for _, p := range s.passages {