refactoring; began implementing embedding
This commit is contained in:
parent
12ebba687b
commit
4d1b18fd12
11 changed files with 96 additions and 50 deletions
107
internal/builder/build_page.go
Normal file
107
internal/builder/build_page.go
Normal file
|
@ -0,0 +1,107 @@
|
|||
package builder
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"strings"
|
||||
|
||||
"github.com/ficcdaf/zona/internal/util"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type PageData struct {
|
||||
Title string
|
||||
Icon string
|
||||
Stylesheet string
|
||||
Header string
|
||||
Content template.HTML
|
||||
NextPost string
|
||||
PrevPost string
|
||||
Footer string
|
||||
Template string
|
||||
}
|
||||
|
||||
type Metadata map[string]interface{}
|
||||
|
||||
func processWithYaml(f []byte) (Metadata, []byte, error) {
|
||||
// Check if the file has valid metadata
|
||||
if !bytes.HasPrefix(f, []byte("---\n")) {
|
||||
// No valid yaml, so return the entire content
|
||||
return nil, f, nil
|
||||
}
|
||||
// Separate YAML from rest of document
|
||||
split := strings.SplitN(string(f), "---\n", 3)
|
||||
if len(split) < 3 {
|
||||
return nil, nil, fmt.Errorf("Invalid frontmatter format.")
|
||||
}
|
||||
var meta Metadata
|
||||
// Parse YAML
|
||||
if err := yaml.Unmarshal([]byte(split[1]), &meta); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return meta, []byte(split[2]), nil
|
||||
}
|
||||
|
||||
func buildPageData(m Metadata, path string, settings *Settings) *PageData {
|
||||
p := &PageData{}
|
||||
if title, ok := m["title"].(string); ok {
|
||||
p.Title = util.WordsToTitle(title)
|
||||
} else {
|
||||
p.Title = util.PathToTitle(path)
|
||||
}
|
||||
if icon, ok := m["icon"].(string); ok {
|
||||
p.Icon = icon
|
||||
} else {
|
||||
p.Icon = settings.Icon
|
||||
}
|
||||
if style, ok := m["style"].(string); ok {
|
||||
p.Stylesheet = style
|
||||
} else {
|
||||
p.Stylesheet = settings.Stylesheet
|
||||
}
|
||||
if header, ok := m["header"].(string); ok {
|
||||
p.Header = header
|
||||
} else {
|
||||
p.Header = settings.Header
|
||||
}
|
||||
if footer, ok := m["footer"].(string); ok {
|
||||
p.Footer = footer
|
||||
} else {
|
||||
p.Footer = settings.Footer
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func ConvertFile(in string, out string, settings *Settings) error {
|
||||
mdPre, err := ReadFile(in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
metadata, md, err := processWithYaml(mdPre)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pd := buildPageData(metadata, in, settings)
|
||||
fmt.Println("Title: ", pd.Title)
|
||||
|
||||
// build according to template here
|
||||
html, err := MdToHTML(md)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pd.Content = template.HTML(html)
|
||||
|
||||
tmpl, err := template.New("webpage").Parse(settings.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
|
||||
}
|
69
internal/builder/config.go
Normal file
69
internal/builder/config.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
package builder
|
||||
|
||||
import (
|
||||
"embed"
|
||||
)
|
||||
|
||||
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>`
|
||||
)
|
||||
|
||||
//go:embed embed
|
||||
var embedDir embed.FS
|
||||
|
||||
type Settings struct {
|
||||
Header string
|
||||
Footer string
|
||||
Stylesheet string
|
||||
Icon string
|
||||
DefaultTemplate string
|
||||
}
|
||||
|
||||
func NewSettings(header string, footer string, style string, icon string, temp string) *Settings {
|
||||
return &Settings{
|
||||
header,
|
||||
footer,
|
||||
style,
|
||||
icon,
|
||||
temp,
|
||||
}
|
||||
}
|
||||
|
||||
func GetSettings() *Settings {
|
||||
// TODO: Read a config file to override defaults
|
||||
// "Defaults" should be a default config file via embed package,
|
||||
// so the settings func should need to handle one case:
|
||||
// check if config file exists, if not, use embedded one
|
||||
return NewSettings(DefaultHeader, DefaultFooter, DefaultStylesheet, DefaultIcon, DefaultTemplate)
|
||||
}
|
132
internal/builder/convert.go
Normal file
132
internal/builder/convert.go
Normal file
|
@ -0,0 +1,132 @@
|
|||
package builder
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/ficcdaf/zona/internal/util"
|
||||
"github.com/gomarkdown/markdown"
|
||||
"github.com/gomarkdown/markdown/ast"
|
||||
"github.com/gomarkdown/markdown/html"
|
||||
"github.com/gomarkdown/markdown/parser"
|
||||
)
|
||||
|
||||
// This function takes a Markdown document and returns an HTML document.
|
||||
func MdToHTML(md []byte) ([]byte, error) {
|
||||
// create parser with extensions
|
||||
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
|
||||
p := parser.NewWithExtensions(extensions)
|
||||
doc := p.Parse(md)
|
||||
|
||||
// build HTML renderer
|
||||
htmlFlags := html.CommonFlags | html.HrefTargetBlank
|
||||
opts := html.RendererOptions{Flags: htmlFlags}
|
||||
renderer := newZonaRenderer(opts)
|
||||
|
||||
return markdown.Render(doc, renderer), nil
|
||||
}
|
||||
|
||||
// PathIsValid checks if a path is valid.
|
||||
// If requireFile is set, directories are not considered valid.
|
||||
func PathIsValid(path string, requireFile bool) bool {
|
||||
s, err := os.Stat(path)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
} else if requireFile {
|
||||
// fmt.Printf("Directory status: %s\n", strconv.FormatBool(s.IsDir()))
|
||||
return !s.IsDir()
|
||||
}
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func processLink(p string) string {
|
||||
// fmt.Println("Processing link...")
|
||||
ext := filepath.Ext(p)
|
||||
// Only process if it points to an existing, local markdown file
|
||||
if ext == ".md" && filepath.IsLocal(p) {
|
||||
// fmt.Println("Markdown link detected...")
|
||||
return util.ChangeExtension(p, ".html")
|
||||
} else {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
func renderLink(w io.Writer, l *ast.Link, entering bool) {
|
||||
if entering {
|
||||
destPath := processLink(string(l.Destination))
|
||||
fmt.Fprintf(w, `<a href="%s"`, destPath)
|
||||
for _, attr := range html.BlockAttrs(l) {
|
||||
fmt.Fprintf(w, ` %s`, attr)
|
||||
}
|
||||
io.WriteString(w, ">")
|
||||
} else {
|
||||
io.WriteString(w, "</a>")
|
||||
}
|
||||
}
|
||||
|
||||
func htmlRenderHook(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
|
||||
if link, ok := node.(*ast.Link); ok {
|
||||
renderLink(w, link, entering)
|
||||
return ast.GoToNext, true
|
||||
}
|
||||
return ast.GoToNext, false
|
||||
}
|
||||
|
||||
func newZonaRenderer(opts html.RendererOptions) *html.Renderer {
|
||||
opts.RenderNodeHook = htmlRenderHook
|
||||
return html.NewRenderer(opts)
|
||||
}
|
||||
|
||||
// WriteFile writes a given byte array to the given path.
|
||||
func WriteFile(b []byte, p string) error {
|
||||
f, err := os.Create(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = f.Write(b)
|
||||
defer f.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadFile reads a byte array from a given path.
|
||||
func ReadFile(p string) ([]byte, error) {
|
||||
f, err := os.Open(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result []byte
|
||||
buf := make([]byte, 1024)
|
||||
for {
|
||||
n, err := f.Read(buf)
|
||||
// check for a non EOF error
|
||||
if err != nil && err != io.EOF {
|
||||
return nil, err
|
||||
}
|
||||
// n==0 when there are no chunks left to read
|
||||
if n == 0 {
|
||||
defer f.Close()
|
||||
break
|
||||
}
|
||||
result = append(result, buf[:n]...)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// CopyFile reads the file at the input path, and write
|
||||
// it to the output path.
|
||||
func CopyFile(inPath string, outPath string) error {
|
||||
inB, err := ReadFile(inPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := WriteFile(inB, outPath); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
122
internal/builder/convert_test.go
Normal file
122
internal/builder/convert_test.go
Normal file
|
@ -0,0 +1,122 @@
|
|||
package builder_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/ficcdaf/zona/internal/builder"
|
||||
"github.com/ficcdaf/zona/internal/util"
|
||||
)
|
||||
|
||||
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 := builder.MdToHTML(md)
|
||||
nHtml := util.NormalizeContent(string(html))
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
if nHtml != nExpectedHTML {
|
||||
t.Errorf("Expected:\n%s\nGot:\n%s", expectedHTML, html)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteFile(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "test.txt")
|
||||
content := []byte("Hello, World!")
|
||||
|
||||
err := builder.WriteFile(content, path)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
// Verify file content
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Error reading file: %v", err)
|
||||
}
|
||||
if string(data) != string(content) {
|
||||
t.Errorf("Expected:\n%s\nGot:\n%s", content, data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFile(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "test.txt")
|
||||
content := []byte("Hello, World!")
|
||||
|
||||
err := os.WriteFile(path, content, 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("Error writing file: %v", err)
|
||||
}
|
||||
|
||||
data, err := builder.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
if string(data) != string(content) {
|
||||
t.Errorf("Expected:\n%s\nGot:\n%s", content, data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopyFile(t *testing.T) {
|
||||
src := filepath.Join(t.TempDir(), "source.txt")
|
||||
dst := filepath.Join(t.TempDir(), "dest.txt")
|
||||
content := []byte("File content for testing.")
|
||||
|
||||
err := os.WriteFile(src, content, 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("Error writing source file: %v", err)
|
||||
}
|
||||
|
||||
err = builder.CopyFile(src, dst)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
// Verify destination file content
|
||||
data, err := os.ReadFile(dst)
|
||||
if err != nil {
|
||||
t.Fatalf("Error reading destination file: %v", err)
|
||||
}
|
||||
if string(data) != string(content) {
|
||||
t.Errorf("Expected:\n%s\nGot:\n%s", content, data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertFile(t *testing.T) {
|
||||
src := filepath.Join(t.TempDir(), "test.md")
|
||||
dst := filepath.Join(t.TempDir(), "test.html")
|
||||
mdContent := []byte("# Test Title\n\nThis is Markdown content.")
|
||||
nExpectedHTML := util.NormalizeContent("<h1 id=\"test-title\">Test Title</h1>\n<p>This is Markdown content.</p>\n")
|
||||
|
||||
err := os.WriteFile(src, mdContent, 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("Error writing source Markdown file: %v", err)
|
||||
}
|
||||
|
||||
err = builder.ConvertFile(src, dst)
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
// Verify destination HTML content
|
||||
data, err := os.ReadFile(dst)
|
||||
if err != nil {
|
||||
t.Fatalf("Error reading HTML file: %v", err)
|
||||
}
|
||||
if util.NormalizeContent(string(data)) != nExpectedHTML {
|
||||
t.Errorf("Expected:\n%s\nGot:\n%s", nExpectedHTML, data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChangeExtension(t *testing.T) {
|
||||
input := "test.md"
|
||||
output := builder.ChangeExtension(input, ".html")
|
||||
expected := "test.html"
|
||||
|
||||
if output != expected {
|
||||
t.Errorf("Expected %s, got %s", expected, output)
|
||||
}
|
||||
}
|
28
internal/builder/embed/article.html
Normal file
28
internal/builder/embed/article.html
Normal file
|
@ -0,0 +1,28 @@
|
|||
<!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>
|
0
internal/builder/embed/config.yml
Normal file
0
internal/builder/embed/config.yml
Normal file
53
internal/builder/traverse.go
Normal file
53
internal/builder/traverse.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
package builder
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/ficcdaf/zona/internal/util"
|
||||
)
|
||||
|
||||
func processFile(inPath string, entry fs.DirEntry, err error, outRoot string, settings *Settings) 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 = util.ChangeExtension(outPath, ".html")
|
||||
if err := util.CreateParents(outPath); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ConvertFile(inPath, outPath, settings); 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, settings *Settings) error {
|
||||
walkFunc := func(path string, entry fs.DirEntry, err error) error {
|
||||
return processFile(path, entry, err, outRoot, settings)
|
||||
}
|
||||
err := filepath.WalkDir(root, walkFunc)
|
||||
return err
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue