commit e8d3bae93d99b45e0240ba7a1d160ff5687585e6
parent 25c178295fc5f5a9ac91cd6d7d015447fa5b3256
Author: gearsix <gearsix@tuta.io>
Date: Mon, 14 Mar 2022 12:55:52 +0000
gofmt
Diffstat:
5 files changed, 30 insertions(+), 29 deletions(-)
diff --git a/copy.go b/copy.go
@@ -70,9 +70,9 @@ func CopyFile(src, dst string) (err error) {
if err = os.MkdirAll(filepath.Dir(dst), 0777); err != nil {
return err
}
-
+
// only copy if dst doesnt exist or has different name/size
- if (dstfi == nil || srcfi.Name() == dstfi.Name() && srcfi.Size() == dstfi.Size()) {
+ if dstfi == nil || srcfi.Name() == dstfi.Name() && srcfi.Size() == dstfi.Size() {
cSrc := C.CString(src)
cDst := C.CString(dst)
if uint32(C.copyf(cSrc, cDst)) != 0 {
diff --git a/copy_test.go b/copy_test.go
@@ -13,7 +13,7 @@ func TestCopyFile(test *testing.T) {
src := filepath.Join(tdir, "/src")
srcData := []byte("data")
dst := filepath.Join(tdir, "/dst")
-
+
if err := os.WriteFile(src, srcData, 0666); err != nil {
test.Error("setup failed, could not write", tdir+"/src")
}
@@ -24,7 +24,7 @@ func TestCopyFile(test *testing.T) {
if _, err := os.Stat(dst); err != nil {
test.Fatalf("could not stat '%s'", dst)
}
-
+
if buf, err := os.ReadFile(dst); err != nil {
test.Errorf("could not read '%s'", dst)
} else if len(buf) < len(srcData) {
diff --git a/pagr_test.go b/pagr_test.go
@@ -3,10 +3,10 @@ package main
import (
"fmt"
"os"
- "time"
"path/filepath"
- "testing"
"strings"
+ "testing"
+ "time"
)
var templates = map[string]string{ // [ext]template
@@ -73,19 +73,20 @@ p2
pre2
p3`
-const contentsCm =`p1
+const contentsCm = `p1
p2
pre1
pre2
p3`
+
var contents = map[string]string{
- ".txt": contentsTxt,
+ ".txt": contentsTxt,
".html": contentsHtml,
- ".md": contentsMd,
- ".gfm": contentsGfm,
- ".cm": contentsCm,
+ ".md": contentsMd,
+ ".gfm": contentsGfm,
+ ".cm": contentsCm,
}
var asset = []byte{ // 5x5 black png image
@@ -129,7 +130,7 @@ func createTestContents(dir string) (err error) {
}
func validateTestPagesNav(t *testing.T, pages []Page) {
- for _, p := range pages {
+ for _, p := range pages {
var allUnique []string
for _, navp := range p.Nav.All {
for _, a := range allUnique {
@@ -140,11 +141,11 @@ func validateTestPagesNav(t *testing.T, pages []Page) {
}
allUnique = append(allUnique, navp.Path)
}
- if (len(p.Nav.All) != len(pages)) {
+ if len(p.Nav.All) != len(pages) {
t.Errorf("'%s' has %d in .Nav.All (should be %d)",
p.Path, len(p.Nav.All), len(pages))
}
-
+
foundAll := 0
for _, navp := range p.Nav.All {
for _, pp := range pages {
@@ -158,7 +159,7 @@ func validateTestPagesNav(t *testing.T, pages []Page) {
t.Errorf("found %d/%d pages in .Nav.All for '%s'",
foundAll, len(p.Nav.All), p.Path)
}
-
+
foundRoot := false
foundParent := false
for _, pp := range pages {
@@ -180,7 +181,7 @@ func validateTestPagesNav(t *testing.T, pages []Page) {
t.Errorf("could not find .Parent '%s' for '%s'",
p.Nav.Parent.Path, p.Path)
}
-
+
// TODO test .Nav.Children, figure out how many should exist
// TODO test .Nav.Crumbs, figure out how many should exist
}
diff --git a/sitemap.go b/sitemap.go
@@ -1,8 +1,8 @@
package main
import (
- "strings"
"sort"
+ "strings"
)
func findRootPage(pages []Page) (root *Page) {
@@ -45,21 +45,21 @@ func BuildCrumbs(p Page, pages []Page) (crumbs []*Page) {
// based on their `.Path` value. These values will be set in the returned Content
func BuildSitemap(pages []Page) []Page {
root := findRootPage(pages)
-
+
for i, p := range pages {
pdepth := readPageDepth(p)
-
+
p.Nav.Root = root
-
+
if pdepth == 1 && p.Path != "/" {
p.Nav.Parent = root
}
-
+
for j, pp := range pages {
ppdepth := readPageDepth(pp)
-
+
p.Nav.All = append(p.Nav.All, &pages[j])
-
+
if p.Nav.Parent == nil && ppdepth == pdepth-1 && strings.Contains(p.Path, pp.Path) {
p.Nav.Parent = &pages[j]
}
@@ -67,15 +67,15 @@ func BuildSitemap(pages []Page) []Page {
p.Nav.Children = append(p.Nav.Children, &pages[j])
}
}
-
+
sort.SliceStable(p.Nav.Children, func(i, j int) bool {
return sort.StringsAreSorted([]string{p.Nav.Children[j].Path, p.Nav.Children[j].Path})
})
-
+
p.Nav.Crumbs = BuildCrumbs(p, pages)
-
+
pages[i] = p
}
-
+
return pages
}
diff --git a/sitemap_test.go b/sitemap_test.go
@@ -9,7 +9,7 @@ func TestBuildCrumbs(test *testing.T) {
func TestBuildSitemap(test *testing.T) {
var err error
-
+
tdir := test.TempDir()
if err = createTestContents(tdir); err != nil {
test.Errorf("failed to create test content: %s", err)
@@ -19,7 +19,7 @@ func TestBuildSitemap(test *testing.T) {
if p, err = LoadPagesDir(tdir); err != nil {
test.Errorf("LoadPagesDir failed: %s", err)
}
-
+
p = BuildSitemap(p)
// TODO validate p
}