diff --git a/structure/job.go b/structure/job.go new file mode 100644 index 0000000..257770a --- /dev/null +++ b/structure/job.go @@ -0,0 +1,22 @@ +// Copyright 2024 Jelly Terra +// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0 +// that can be found in the LICENSE file and https://mozilla.org/MPL/2.0/. + +package structure + +const ( + _ = iota + + WAITING // Waiting for other content being generated. + FAILED // Error occurred. + DONE // Content generated successfully. +) + +type Job struct { + Requires []string `toml:"requires"` +} + +func (j *Job) Execute() error { + // TODO + return nil +} diff --git a/structure/page.go b/structure/page.go index b5c5ce3..c7d6827 100644 --- a/structure/page.go +++ b/structure/page.go @@ -5,9 +5,10 @@ package structure import ( + "bytes" . "github.com/webpagine/go-pagine/path" "io" - "os" + "path/filepath" "text/template" ) @@ -18,15 +19,15 @@ type Page struct { // RelativePath where the webpage attr file is. RelativePath string - // TemplatePath the pages uses. - TemplatePath string `toml:"template"` + // Templates the pages uses. + Templates map[string]string `toml:"templates"` // Contents sources: content.[string] = string // It is designed for different part of content used in template. // // Example: // content = "/content_000.md" => invoke "md" generator, assign the output to the key "content" - Contents map[string]string `toml:"content"` + Contents map[string]string `toml:"contents"` // Customized data. Data map[string]any `toml:"data"` @@ -34,17 +35,6 @@ type Page struct { func (p *Page) Generate(root Path, w io.Writer) error { - // Parse Go template. - templateBody, err := os.ReadFile(root.AbsolutePathOf(p.TemplatePath)) - if err != nil { - return err - } - - t, err := template.New(p.RelativePath).Parse(string(templateBody)) - if err != nil { - return err - } - var contentMap = map[string]any{} for contentKey, contentRelativePath := range p.Contents { @@ -63,8 +53,29 @@ func (p *Page) Generate(root Path, w io.Writer) error { contentMap[dataKey] = dataValue } - // Template - err = t.Execute(w, contentMap) // No + var templateMap = map[string]string{} + + contentMap["templates"] = templateMap + + for templateKey, templateRelativePath := range p.Templates { + path := root.AbsolutePathOf(templateRelativePath) + + t, err := template.New(filepath.Base(path)).ParseFiles(path) + if err != nil { + return err + } + + b := bytes.NewBuffer(nil) + + err = t.Execute(b, contentMap) + if err != nil { + return err + } + + templateMap[templateKey] = b.String() + } + + _, err := w.Write([]byte(templateMap["main"])) if err != nil { return err }