step/document.go

70 lines
1.7 KiB
Go

package step
import "io"
import "time"
import "strings"
import "html/template"
// Document represents a STEP file.
type Document struct {
Author string
Title string
Extends string
FrontMatter FrontMatter
// WORM:
environment *Environment
name string
parseTime time.Time
template *template.Template
}
// Execute executes this document and writes the result to the output. This
// method is safe for concurrent use by multiple goroutines.
func (this *Document) Execute (output io.Writer, data ExecutionData) error {
if this.Extends == "" {
// no parent
return this.template.Execute(output, data)
}
// execute into string builder
outputBuilder := strings.Builder { }
err := this.template.Execute(&outputBuilder, data)
if err != nil { return err }
// execute parent with this document's result
parent, err := this.environment.Parse(this.Extends)
if err != nil { return err }
return parent.Execute(output, ExecutionData {
Data: data.Data,
Child: &ExecutionResult {
Author: this.Author,
Title: this.Title,
Extends: this.Extends,
FrontMatter: this.FrontMatter,
Body: template.HTML(outputBuilder.String()),
},
})
}
// Environment returns the environment this document is in.
func (this *Document) Environment () *Environment {
return this.environment
}
// ExecutionData is data made available to documents as they are being exeucted.
type ExecutionData struct {
Data any // Custom data
Child *ExecutionResult // The child document's result, if applicable
}
// ExecutionResult is the result of executing a document.
type ExecutionResult struct {
Author string
Title string
Extends string
FrontMatter FrontMatter
Body template.HTML
}