tweego/tweego.go
Thomas M. Edwards 1bb4278938 [New, Update] Messy commit.
* [Update] Add module paths to watch files.  From the old Bitbucket repo, submitted by `MarkSill`.
* [Fix] Trim erroneous whitespace surrounding the story title.  From the old Bitbucket repo.
* [Update, Devel, Docs] Changed import and doc paths to point to the GitHub repo and removed Mercurial references.
* [Fix] Leading whitespace on passages is trimmed as intended.
* [New, Docs] Added an option to disable passage trimming (`--no-trim`).
* [Update] Update IFID commentary and cleanup.
* [Update] Switched to another SemVer module due to a few issues with the old one.
* [Update] Purged commented legacy & debug code.
2019-12-23 14:25:57 -06:00

121 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
tweego (a twee compiler in Go)
Copyright © 20142019 Thomas Michael Edwards. All rights reserved.
Use of this source code is governed by a Simplified BSD License which
can be found in the LICENSE file.
*/
package main
import (
"log"
)
const tweegoName = "tweego"
func init() {
// Clear standard logger flags.
log.SetFlags(0)
}
func main() {
// Create a new config instance.
c := newConfig()
// Build the output and, possibly, log various stats.
if c.watchFiles {
buildName := relPath(c.outFile)
paths := append(c.sourcePaths, c.modulePaths...)
watchFilesystem(paths, c.outFile, func() {
log.Printf("BUILDING: %s", buildName)
buildOutput(c)
})
} else {
buildOutput(c)
// Logging.
if c.logFiles {
log.Println()
statsLogFiles()
log.Println()
}
if c.logStats {
if !c.logFiles {
log.Println()
}
statsLog()
log.Println()
}
}
}
func buildOutput(c *config) *story {
// Get the source and module paths.
sourcePaths := getFilenames(c.sourcePaths, c.outFile)
modulePaths := getFilenames(c.modulePaths, c.outFile)
// Create a new story instance and load the source files.
s := newStory()
s.load(sourcePaths, c)
// Finalize the config with values from the `StoryData` passage, if any.
c.mergeStoryConfig(s)
// 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 {
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 {
log.Fatalf(`error: %s`, err.Error())
}
case outModeTwine1Archive:
// Write out the project as Twine 1 archived HTML.
if _, err := fileWriteAll(c.outFile, s.toTwine1Archive(c.startName)); err != nil {
log.Fatalf(`error: %s`, err.Error())
}
default:
// Basic sanity checks.
if !s.has(c.startName) {
log.Fatalf("error: Starting passage %q not found.", c.startName)
}
if (s.format.isTwine1Style() || s.name == "") && !s.has("StoryTitle") {
log.Fatal(`error: Special passage "StoryTitle" not found.`)
}
if s.format.isTwine2Style() {
// Write out the project as Twine 2 compiled HTML.
if _, err := fileWriteAll(
c.outFile,
modifyHead(
s.toTwine2HTML(c.startName),
modulePaths,
c.headFile,
c.encoding,
),
); err != nil {
log.Fatalf(`error: %s`, err.Error())
}
} else {
// Write out the project as Twine 1 compiled HTML.
if _, err := fileWriteAll(
c.outFile,
modifyHead(
s.toTwine1HTML(c.startName),
modulePaths,
c.headFile,
c.encoding,
),
); err != nil {
log.Fatalf(`error: %s`, err.Error())
}
}
}
return s
}