refactoring; began implementing embedding
This commit is contained in:
parent
12ebba687b
commit
4d1b18fd12
11 changed files with 96 additions and 50 deletions
|
@ -1 +0,0 @@
|
|||
package main
|
|
@ -6,7 +6,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/ficcdaf/zona/internal/build"
|
||||
"github.com/ficcdaf/zona/internal/builder"
|
||||
)
|
||||
|
||||
// // validateFile checks whether a given path
|
||||
|
@ -41,7 +41,8 @@ func main() {
|
|||
}
|
||||
|
||||
}
|
||||
err := build.Traverse(*rootPath, "foobar")
|
||||
settings := builder.GetSettings()
|
||||
err := builder.Traverse(*rootPath, "foobar", settings)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %s\n", err.Error())
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package build
|
||||
package builder
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
@ -6,6 +6,7 @@ import (
|
|||
"html/template"
|
||||
"strings"
|
||||
|
||||
"github.com/ficcdaf/zona/internal/util"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
|
@ -18,6 +19,7 @@ type PageData struct {
|
|||
NextPost string
|
||||
PrevPost string
|
||||
Footer string
|
||||
Template string
|
||||
}
|
||||
|
||||
type Metadata map[string]interface{}
|
||||
|
@ -41,37 +43,37 @@ func processWithYaml(f []byte) (Metadata, []byte, error) {
|
|||
return meta, []byte(split[2]), nil
|
||||
}
|
||||
|
||||
func buildPageData(m Metadata, path string) *PageData {
|
||||
func buildPageData(m Metadata, path string, settings *Settings) *PageData {
|
||||
p := &PageData{}
|
||||
if title, ok := m["title"].(string); ok {
|
||||
p.Title = wordsToTitle(title)
|
||||
p.Title = util.WordsToTitle(title)
|
||||
} else {
|
||||
p.Title = pathToTitle(path)
|
||||
p.Title = util.PathToTitle(path)
|
||||
}
|
||||
if icon, ok := m["icon"].(string); ok {
|
||||
p.Icon = icon
|
||||
} else {
|
||||
p.Icon = DefaultIcon
|
||||
p.Icon = settings.Icon
|
||||
}
|
||||
if style, ok := m["style"].(string); ok {
|
||||
p.Stylesheet = style
|
||||
} else {
|
||||
p.Stylesheet = DefaultStylesheet
|
||||
p.Stylesheet = settings.Stylesheet
|
||||
}
|
||||
if header, ok := m["header"].(string); ok {
|
||||
p.Header = header
|
||||
} else {
|
||||
p.Header = DefaultHeader
|
||||
p.Header = settings.Header
|
||||
}
|
||||
if footer, ok := m["footer"].(string); ok {
|
||||
p.Footer = footer
|
||||
} else {
|
||||
p.Footer = DefaultFooter
|
||||
p.Footer = settings.Footer
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func ConvertFile(in string, out string) error {
|
||||
func ConvertFile(in string, out string, settings *Settings) error {
|
||||
mdPre, err := ReadFile(in)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -80,7 +82,7 @@ func ConvertFile(in string, out string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pd := buildPageData(metadata, in)
|
||||
pd := buildPageData(metadata, in, settings)
|
||||
fmt.Println("Title: ", pd.Title)
|
||||
|
||||
// build according to template here
|
||||
|
@ -90,7 +92,7 @@ func ConvertFile(in string, out string) error {
|
|||
}
|
||||
pd.Content = template.HTML(html)
|
||||
|
||||
tmpl, err := template.New("webpage").Parse(DefaultTemplate)
|
||||
tmpl, err := template.New("webpage").Parse(settings.DefaultTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
|
@ -1,4 +1,8 @@
|
|||
package build
|
||||
package builder
|
||||
|
||||
import (
|
||||
"embed"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultHeader = ""
|
||||
|
@ -35,18 +39,31 @@ const (
|
|||
</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) *Settings {
|
||||
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)
|
||||
}
|
|
@ -1,12 +1,12 @@
|
|||
package build
|
||||
package builder
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/ficcdaf/zona/internal/util"
|
||||
"github.com/gomarkdown/markdown"
|
||||
"github.com/gomarkdown/markdown/ast"
|
||||
"github.com/gomarkdown/markdown/html"
|
||||
|
@ -47,7 +47,7 @@ func processLink(p string) string {
|
|||
// Only process if it points to an existing, local markdown file
|
||||
if ext == ".md" && filepath.IsLocal(p) {
|
||||
// fmt.Println("Markdown link detected...")
|
||||
return ChangeExtension(p, ".html")
|
||||
return util.ChangeExtension(p, ".html")
|
||||
} else {
|
||||
return p
|
||||
}
|
||||
|
@ -130,7 +130,3 @@ func CopyFile(inPath string, outPath string) error {
|
|||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func ChangeExtension(in string, outExt string) string {
|
||||
return strings.TrimSuffix(in, filepath.Ext(in)) + outExt
|
||||
}
|
|
@ -1,11 +1,11 @@
|
|||
package build_test
|
||||
package builder_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/ficcdaf/zona/internal/build"
|
||||
"github.com/ficcdaf/zona/internal/builder"
|
||||
"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 := build.MdToHTML(md)
|
||||
html, err := builder.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 := build.WriteFile(content, path)
|
||||
err := builder.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 := build.ReadFile(path)
|
||||
data, err := builder.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 = build.CopyFile(src, dst)
|
||||
err = builder.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 = build.ConvertFile(src, dst)
|
||||
err = builder.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 := build.ChangeExtension(input, ".html")
|
||||
output := builder.ChangeExtension(input, ".html")
|
||||
expected := "test.html"
|
||||
|
||||
if output != expected {
|
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
|
@ -1,4 +1,4 @@
|
|||
package build
|
||||
package builder
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
@ -8,7 +8,7 @@ import (
|
|||
"github.com/ficcdaf/zona/internal/util"
|
||||
)
|
||||
|
||||
func processFile(inPath string, entry fs.DirEntry, err error, outRoot string) error {
|
||||
func processFile(inPath string, entry fs.DirEntry, err error, outRoot string, settings *Settings) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -18,11 +18,11 @@ func processFile(inPath string, entry fs.DirEntry, err error, outRoot string) er
|
|||
switch ext {
|
||||
case ".md":
|
||||
// fmt.Println("Processing markdown...")
|
||||
outPath = ChangeExtension(outPath, ".html")
|
||||
outPath = util.ChangeExtension(outPath, ".html")
|
||||
if err := util.CreateParents(outPath); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ConvertFile(inPath, outPath); err != nil {
|
||||
if err := ConvertFile(inPath, outPath, settings); err != nil {
|
||||
return errors.Join(errors.New("Error processing file "+inPath), err)
|
||||
} else {
|
||||
return nil
|
||||
|
@ -44,10 +44,9 @@ func processFile(inPath string, entry fs.DirEntry, err error, outRoot string) er
|
|||
return nil
|
||||
}
|
||||
|
||||
func Traverse(root string, outRoot string) error {
|
||||
// err := filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error {
|
||||
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)
|
||||
return processFile(path, entry, err, outRoot, settings)
|
||||
}
|
||||
err := filepath.WalkDir(root, walkFunc)
|
||||
return err
|
|
@ -18,6 +18,10 @@ func CheckExtension(path, ext string) error {
|
|||
}
|
||||
}
|
||||
|
||||
func ChangeExtension(in string, outExt string) string {
|
||||
return strings.TrimSuffix(in, filepath.Ext(in)) + outExt
|
||||
}
|
||||
|
||||
func getRoot(path string) string {
|
||||
for {
|
||||
parent := filepath.Dir(path)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package build
|
||||
package util
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
@ -8,23 +8,23 @@ import (
|
|||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
// pathToWords takes a full path
|
||||
// PathToWords takes a full path
|
||||
// and strips separators and extension
|
||||
// from the file name
|
||||
func pathToWords(path string) string {
|
||||
func PathToWords(path string) string {
|
||||
stripped := ChangeExtension(filepath.Base(path), "")
|
||||
replaced := strings.NewReplacer("-", " ", "_", " ", `\ `, " ").Replace(stripped)
|
||||
return strings.ToTitle(replaced)
|
||||
}
|
||||
|
||||
func wordsToTitle(words string) string {
|
||||
func WordsToTitle(words string) string {
|
||||
caser := cases.Title(language.English)
|
||||
return caser.String(words)
|
||||
}
|
||||
|
||||
// pathToTitle converts a full path to a string
|
||||
// PathToTitle converts a full path to a string
|
||||
// in title case
|
||||
func pathToTitle(path string) string {
|
||||
words := pathToWords(path)
|
||||
return wordsToTitle(words)
|
||||
func PathToTitle(path string) string {
|
||||
words := PathToWords(path)
|
||||
return WordsToTitle(words)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue