added build processed files
untested!
This commit is contained in:
parent
4315348cf5
commit
7644a31016
5 changed files with 97 additions and 9 deletions
|
@ -43,7 +43,7 @@ func main() {
|
||||||
}
|
}
|
||||||
settings := builder.GetSettings(*rootPath, "foobar")
|
settings := builder.GetSettings(*rootPath, "foobar")
|
||||||
// err := builder.Traverse(*rootPath, "foobar", settings)
|
// err := builder.Traverse(*rootPath, "foobar", settings)
|
||||||
err := builder.ProcessTraverse(*rootPath, "foobar", settings)
|
pm, err := builder.ProcessTraverse(*rootPath, "foobar", settings)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error: %s\n", err.Error())
|
fmt.Printf("Error: %s\n", err.Error())
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package builder
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
|
@ -101,10 +102,7 @@ func buildPageData(m Metadata, in string, out string, settings *Settings) *PageD
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
// WARNING: This is a reference implementation
|
func _BuildHtmlFile(in string, out string, settings *Settings) error {
|
||||||
// with passing tests but not likely to work in
|
|
||||||
// the broader scope of the program!
|
|
||||||
func BuildHtmlFile(in string, out string, settings *Settings) error {
|
|
||||||
mdPre, err := util.ReadFile(in)
|
mdPre, err := util.ReadFile(in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -133,3 +131,50 @@ func BuildHtmlFile(in string, out string, settings *Settings) error {
|
||||||
err = util.WriteFile(output.Bytes(), out)
|
err = util.WriteFile(output.Bytes(), out)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BuildFile(f *File, settings *Settings) error {
|
||||||
|
if f.ShouldCopy {
|
||||||
|
if err := util.CreateParents(f.OutPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := util.CopyFile(f.InPath, f.OutPath); err != nil {
|
||||||
|
return errors.Join(errors.New("Error processing file "+f.InPath), err)
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := util.CreateParents(f.OutPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := BuildHtmlFile(f.FrontMatterLen, f.InPath, f.OutPath, f.Data, settings); err != nil {
|
||||||
|
return errors.Join(errors.New("Error processing file "+f.InPath), err)
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BuildHtmlFile(l int, in string, out string, pd *PageData, settings *Settings) error {
|
||||||
|
md, err := util.ReadLineRange(in, l, -1)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println("Title: ", pd.Title)
|
||||||
|
|
||||||
|
// build according to template here
|
||||||
|
html := MdToHTML(md)
|
||||||
|
pd.Content = template.HTML(html)
|
||||||
|
|
||||||
|
tmpl, err := template.New("webpage").Parse(pd.Template)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var output bytes.Buffer
|
||||||
|
if err := tmpl.Execute(&output, pd); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = util.WriteFile(output.Bytes(), out)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ type File struct {
|
||||||
OutPath string
|
OutPath string
|
||||||
ShouldCopy bool
|
ShouldCopy bool
|
||||||
HasFrontmatter bool
|
HasFrontmatter bool
|
||||||
|
FrontMatterLen int
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewProcessMemory initializes an empty
|
// NewProcessMemory initializes an empty
|
||||||
|
@ -68,9 +69,11 @@ func processFile(inPath string, entry fs.DirEntry, err error, outRoot string, se
|
||||||
|
|
||||||
var pd *PageData
|
var pd *PageData
|
||||||
hasFrontmatter := false
|
hasFrontmatter := false
|
||||||
|
l := 0
|
||||||
if toProcess {
|
if toProcess {
|
||||||
// process its frontmatter here
|
// process its frontmatter here
|
||||||
m, _, err := processFrontmatter(inPath)
|
m, le, err := processFrontmatter(inPath)
|
||||||
|
l = le
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -89,6 +92,7 @@ func processFile(inPath string, entry fs.DirEntry, err error, outRoot string, se
|
||||||
outPath,
|
outPath,
|
||||||
!toProcess,
|
!toProcess,
|
||||||
hasFrontmatter,
|
hasFrontmatter,
|
||||||
|
l,
|
||||||
}
|
}
|
||||||
if pd != nil && pd.Type == "post" {
|
if pd != nil && pd.Type == "post" {
|
||||||
pm.Posts = append(pm.Posts, file)
|
pm.Posts = append(pm.Posts, file)
|
||||||
|
@ -96,3 +100,13 @@ func processFile(inPath string, entry fs.DirEntry, err error, outRoot string, se
|
||||||
pm.Files = append(pm.Files, file)
|
pm.Files = append(pm.Files, file)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BuildProcessedFiles(pm *ProcessMemory, settings *Settings) error {
|
||||||
|
for _, f := range pm.Files {
|
||||||
|
err := BuildFile(f, settings)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ func buildFile(inPath string, entry fs.DirEntry, err error, outRoot string, sett
|
||||||
if err := util.CreateParents(outPath); err != nil {
|
if err := util.CreateParents(outPath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := BuildHtmlFile(inPath, outPath, settings); err != nil {
|
if err := _BuildHtmlFile(inPath, outPath, settings); err != nil {
|
||||||
return errors.Join(errors.New("Error processing file "+inPath), err)
|
return errors.Join(errors.New("Error processing file "+inPath), err)
|
||||||
} else {
|
} else {
|
||||||
return nil
|
return nil
|
||||||
|
@ -53,11 +53,11 @@ func Traverse(root string, outRoot string, settings *Settings) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func ProcessTraverse(root string, outRoot string, settings *Settings) error {
|
func ProcessTraverse(root string, outRoot string, settings *Settings) (*ProcessMemory, error) {
|
||||||
pm := NewProcessMemory()
|
pm := NewProcessMemory()
|
||||||
walkFunc := func(path string, entry fs.DirEntry, err error) error {
|
walkFunc := func(path string, entry fs.DirEntry, err error) error {
|
||||||
return processFile(path, entry, err, outRoot, settings, pm)
|
return processFile(path, entry, err, outRoot, settings, pm)
|
||||||
}
|
}
|
||||||
err := filepath.WalkDir(root, walkFunc)
|
err := filepath.WalkDir(root, walkFunc)
|
||||||
return err
|
return pm, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,3 +80,32 @@ func ReadNLines(filename string, n int) ([]byte, error) {
|
||||||
|
|
||||||
return buffer.Bytes(), nil
|
return buffer.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadLineRange reads a file in a given range of lines
|
||||||
|
func ReadLineRange(filename string, start int, end int) ([]byte, error) {
|
||||||
|
file, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
i := 0
|
||||||
|
for scanner.Scan() {
|
||||||
|
i++
|
||||||
|
if i >= start && (i <= end || end == -1) {
|
||||||
|
buffer.Write(scanner.Bytes())
|
||||||
|
buffer.WriteByte('\n')
|
||||||
|
}
|
||||||
|
if i > end && end != -1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue