added build processed files
untested!
This commit is contained in:
parent
4315348cf5
commit
7644a31016
5 changed files with 97 additions and 9 deletions
|
@ -2,6 +2,7 @@ package builder
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
|
@ -101,10 +102,7 @@ func buildPageData(m Metadata, in string, out string, settings *Settings) *PageD
|
|||
return p
|
||||
}
|
||||
|
||||
// WARNING: This is a reference implementation
|
||||
// with passing tests but not likely to work in
|
||||
// the broader scope of the program!
|
||||
func BuildHtmlFile(in string, out string, settings *Settings) error {
|
||||
func _BuildHtmlFile(in string, out string, settings *Settings) error {
|
||||
mdPre, err := util.ReadFile(in)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -133,3 +131,50 @@ func BuildHtmlFile(in string, out string, settings *Settings) error {
|
|||
err = util.WriteFile(output.Bytes(), out)
|
||||
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
|
||||
ShouldCopy bool
|
||||
HasFrontmatter bool
|
||||
FrontMatterLen int
|
||||
}
|
||||
|
||||
// NewProcessMemory initializes an empty
|
||||
|
@ -68,9 +69,11 @@ func processFile(inPath string, entry fs.DirEntry, err error, outRoot string, se
|
|||
|
||||
var pd *PageData
|
||||
hasFrontmatter := false
|
||||
l := 0
|
||||
if toProcess {
|
||||
// process its frontmatter here
|
||||
m, _, err := processFrontmatter(inPath)
|
||||
m, le, err := processFrontmatter(inPath)
|
||||
l = le
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -89,6 +92,7 @@ func processFile(inPath string, entry fs.DirEntry, err error, outRoot string, se
|
|||
outPath,
|
||||
!toProcess,
|
||||
hasFrontmatter,
|
||||
l,
|
||||
}
|
||||
if pd != nil && pd.Type == "post" {
|
||||
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)
|
||||
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 {
|
||||
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)
|
||||
} else {
|
||||
return nil
|
||||
|
@ -53,11 +53,11 @@ func Traverse(root string, outRoot string, settings *Settings) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func ProcessTraverse(root string, outRoot string, settings *Settings) error {
|
||||
func ProcessTraverse(root string, outRoot string, settings *Settings) (*ProcessMemory, error) {
|
||||
pm := NewProcessMemory()
|
||||
walkFunc := func(path string, entry fs.DirEntry, err error) error {
|
||||
return processFile(path, entry, err, outRoot, settings, pm)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// 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