implemented basic templating and default settings
This commit is contained in:
parent
11f724732d
commit
64e243773a
7 changed files with 143 additions and 70 deletions
|
@ -1,4 +1,4 @@
|
|||
package convert
|
||||
package build
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
@ -49,26 +49,32 @@ func pathToTitle(path string) string {
|
|||
return strings.ToTitle(replaced)
|
||||
}
|
||||
|
||||
func buildPageData(m Metadata) *PageData {
|
||||
func buildPageData(m Metadata, path string) *PageData {
|
||||
p := &PageData{}
|
||||
if title, ok := m["title"].(string); ok {
|
||||
p.Title = title
|
||||
} else {
|
||||
p.Title = pathToTitle(m["path"].(string))
|
||||
p.Title = pathToTitle(path)
|
||||
}
|
||||
if icon, ok := m["icon"].(string); ok {
|
||||
p.Icon = icon
|
||||
} else {
|
||||
p.Icon = ""
|
||||
p.Icon = DefaultIcon
|
||||
}
|
||||
if style, ok := m["style"].(string); ok {
|
||||
p.Stylesheet = style
|
||||
} else {
|
||||
p.Stylesheet = DefaultStylesheet
|
||||
}
|
||||
if header, ok := m["header"].(string); ok {
|
||||
p.Header = header
|
||||
} else {
|
||||
p.Header = DefaultHeader
|
||||
}
|
||||
if footer, ok := m["footer"].(string); ok {
|
||||
p.Footer = footer
|
||||
} else {
|
||||
p.Footer = DefaultFooter
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
@ -82,18 +88,25 @@ func ConvertFile(in string, out string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
metadata["path"] = in
|
||||
pd := buildPageData(metadata)
|
||||
fmt.Println("Page title: ", pd.Title)
|
||||
pd.Content = template.HTML(md)
|
||||
|
||||
tmlp, err := template.New("webpage").Parse("placeholder")
|
||||
pd := buildPageData(metadata, in)
|
||||
|
||||
// build according to template here
|
||||
html, err := MdToHTML(md)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = WriteFile(html, out)
|
||||
pd.Content = template.HTML(html)
|
||||
|
||||
tmpl, err := template.New("webpage").Parse(DefaultTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var output bytes.Buffer
|
||||
if err := tmpl.Execute(&output, pd); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = WriteFile(output.Bytes(), out)
|
||||
return err
|
||||
}
|
52
internal/build/config.go
Normal file
52
internal/build/config.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
package build
|
||||
|
||||
const (
|
||||
DefaultHeader = ""
|
||||
DefaultFooter = ""
|
||||
DefaultStylesheet = "/style/zonaDefault.css"
|
||||
DefaultIcon = ""
|
||||
DefaultTemplate = `<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>{{ .Title }}</title>
|
||||
<link rel="icon" href="{{ .Icon }}" type="image/x-icon" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta charset="UTF-8" />
|
||||
<link
|
||||
href="{{ .Stylesheet }}"
|
||||
rel="stylesheet"
|
||||
type="text/css"
|
||||
media="all"
|
||||
/>
|
||||
</head>
|
||||
<body>
|
||||
<div id="container">
|
||||
<header id="header">{{ .Header }}</header>
|
||||
<article id="content">
|
||||
{{ .Content }}
|
||||
<nav id="nextprev">
|
||||
{{ .NextPost }}<br />
|
||||
{{ .PrevPost }}
|
||||
</nav>
|
||||
</article>
|
||||
<footer id="footer">{{ .Footer }}</footer>
|
||||
</div>
|
||||
</body>
|
||||
</html>`
|
||||
)
|
||||
|
||||
type Settings struct {
|
||||
Header string
|
||||
Footer string
|
||||
Stylesheet string
|
||||
Icon string
|
||||
}
|
||||
|
||||
func NewSettings(header string, footer string, style string, icon string) *Settings {
|
||||
return &Settings{
|
||||
header,
|
||||
footer,
|
||||
style,
|
||||
icon,
|
||||
}
|
||||
}
|
|
@ -1,15 +1,12 @@
|
|||
package convert
|
||||
package build
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/ficcdaf/zona/internal/util"
|
||||
"github.com/gomarkdown/markdown"
|
||||
"github.com/gomarkdown/markdown/ast"
|
||||
"github.com/gomarkdown/markdown/html"
|
||||
|
@ -137,48 +134,3 @@ func CopyFile(inPath string, outPath string) error {
|
|||
func ChangeExtension(in string, outExt string) string {
|
||||
return strings.TrimSuffix(in, filepath.Ext(in)) + outExt
|
||||
}
|
||||
|
||||
func processFile(inPath string, entry fs.DirEntry, err error, outRoot string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !entry.IsDir() {
|
||||
ext := filepath.Ext(inPath)
|
||||
outPath := util.ReplaceRoot(inPath, outRoot)
|
||||
switch ext {
|
||||
case ".md":
|
||||
fmt.Println("Processing markdown...")
|
||||
outPath = ChangeExtension(outPath, ".html")
|
||||
if err := util.CreateParents(outPath); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ConvertFile(inPath, outPath); err != nil {
|
||||
return errors.Join(errors.New("Error processing file "+inPath), err)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
// If it's not a file we need to process,
|
||||
// we simply copy it to the destination path.
|
||||
default:
|
||||
if err := util.CreateParents(outPath); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := CopyFile(inPath, outPath); err != nil {
|
||||
return errors.Join(errors.New("Error processing file "+inPath), err)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
// fmt.Printf("Visited: %s\n", inPath)
|
||||
return nil
|
||||
}
|
||||
|
||||
func Traverse(root string, outRoot string) error {
|
||||
// err := filepath.WalkDir(root, 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)
|
||||
}
|
||||
err := filepath.WalkDir(root, walkFunc)
|
||||
return err
|
||||
}
|
|
@ -1,11 +1,11 @@
|
|||
package convert_test
|
||||
package build_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/ficcdaf/zona/internal/convert"
|
||||
"github.com/ficcdaf/zona/internal/build"
|
||||
"github.com/ficcdaf/zona/internal/util"
|
||||
)
|
||||
|
||||
|
@ -13,7 +13,7 @@ func TestMdToHTML(t *testing.T) {
|
|||
md := []byte("# Hello World\n\nThis is a test.")
|
||||
expectedHTML := "<h1 id=\"hello-world\">Hello World</h1>\n<p>This is a test.</p>\n"
|
||||
nExpectedHTML := util.NormalizeContent(expectedHTML)
|
||||
html, err := convert.MdToHTML(md)
|
||||
html, err := build.MdToHTML(md)
|
||||
nHtml := util.NormalizeContent(string(html))
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
|
@ -27,7 +27,7 @@ func TestWriteFile(t *testing.T) {
|
|||
path := filepath.Join(t.TempDir(), "test.txt")
|
||||
content := []byte("Hello, World!")
|
||||
|
||||
err := convert.WriteFile(content, path)
|
||||
err := build.WriteFile(content, path)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ func TestReadFile(t *testing.T) {
|
|||
t.Fatalf("Error writing file: %v", err)
|
||||
}
|
||||
|
||||
data, err := convert.ReadFile(path)
|
||||
data, err := build.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
@ -70,7 +70,7 @@ func TestCopyFile(t *testing.T) {
|
|||
t.Fatalf("Error writing source file: %v", err)
|
||||
}
|
||||
|
||||
err = convert.CopyFile(src, dst)
|
||||
err = build.CopyFile(src, dst)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
@ -96,7 +96,7 @@ func TestConvertFile(t *testing.T) {
|
|||
t.Fatalf("Error writing source Markdown file: %v", err)
|
||||
}
|
||||
|
||||
err = convert.ConvertFile(src, dst)
|
||||
err = build.ConvertFile(src, dst)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ func TestConvertFile(t *testing.T) {
|
|||
|
||||
func TestChangeExtension(t *testing.T) {
|
||||
input := "test.md"
|
||||
output := convert.ChangeExtension(input, ".html")
|
||||
output := build.ChangeExtension(input, ".html")
|
||||
expected := "test.html"
|
||||
|
||||
if output != expected {
|
55
internal/build/traverse.go
Normal file
55
internal/build/traverse.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package build
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/ficcdaf/zona/internal/util"
|
||||
)
|
||||
|
||||
func processFile(inPath string, entry fs.DirEntry, err error, outRoot string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !entry.IsDir() {
|
||||
ext := filepath.Ext(inPath)
|
||||
outPath := util.ReplaceRoot(inPath, outRoot)
|
||||
switch ext {
|
||||
case ".md":
|
||||
fmt.Println("Processing markdown...")
|
||||
outPath = ChangeExtension(outPath, ".html")
|
||||
if err := util.CreateParents(outPath); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ConvertFile(inPath, outPath); err != nil {
|
||||
return errors.Join(errors.New("Error processing file "+inPath), err)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
// If it's not a file we need to process,
|
||||
// we simply copy it to the destination path.
|
||||
default:
|
||||
if err := util.CreateParents(outPath); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := CopyFile(inPath, outPath); err != nil {
|
||||
return errors.Join(errors.New("Error processing file "+inPath), err)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
// fmt.Printf("Visited: %s\n", inPath)
|
||||
return nil
|
||||
}
|
||||
|
||||
func Traverse(root string, outRoot string) error {
|
||||
// err := filepath.WalkDir(root, 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)
|
||||
}
|
||||
err := filepath.WalkDir(root, walkFunc)
|
||||
return err
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue