commit d23da41d61958db4cf03286094214dde56c41976
parent fdda0004f3e92f122abd0534ec57134c0f1d0d8e
Author: gearsix <gearsix@tuta.io>
Date: Thu, 31 Mar 2022 09:50:17 +0100
bugfixes to last commit; gofmt; updated tests for new .Assets
Diffstat:
11 files changed, 39 insertions(+), 45 deletions(-)
diff --git a/config_test.go b/config_test.go
@@ -45,7 +45,7 @@ func TestNewConfigFromFile(test *testing.T) {
} else {
test.Fatal(err)
}
-
+
if err := os.RemoveAll(tdir); err != nil {
test.Error(err)
}
diff --git a/content.go b/content.go
@@ -10,6 +10,7 @@ import (
goldmarkhtml "github.com/yuin/goldmark/renderer/html"
"io"
"io/ioutil"
+ "mime"
"notabug.org/gearsix/suti"
"os"
"os/exec"
@@ -50,15 +51,13 @@ func gitModTime(fpath string) (mod time.Time, err error) {
if fpath, err = filepath.Abs(fpath); err != nil {
return
}
-
+
git := exec.Command(gitBin, "-C", filepath.Dir(fpath), "log", "-1", "--format='%ad'", "--", fpath)
var out []byte
if out, err = git.Output(); err == nil {
outstr := strings.ReplaceAll(string(out), "'", "")
outstr = strings.TrimSuffix(outstr, "\n")
mod, err = time.Parse("Mon Jan 2 15:04:05 2006 -0700", outstr)
- } else {
- fmt.Println(err)
}
return
}
@@ -80,12 +79,12 @@ func lastPageMod(fpath string) (t time.Time) {
if f.IsDir() {
continue
}
-
+
var ft time.Time
if ft, err = gitModTime(filepath.Join(fpath, f.Name())); err != nil {
ft = fd.ModTime()
}
-
+
if i == 0 || ft.After(t) {
t = ft
}
@@ -147,19 +146,19 @@ func LoadContentDir(dir string) (p []Page, e error) {
return
}
-func loadContentFile(p Page, defs map[string]Meta, fpath string, ppath string) (Page, map[string]Meta, error) {
+func loadContentFile(p Page, def map[string]Meta, fpath string, ppath string) (Page, map[string]Meta, error) {
var err error
fname := strings.TrimSuffix(filepath.Base(fpath), filepath.Ext(fpath))
-
+
if suti.IsSupportedDataLang(filepath.Ext(fpath)) != -1 &&
(fname == "defaults" || fname == "meta") {
var m Meta
if err = suti.LoadDataFilepath(fpath, &m); err == nil {
if fname == "defaults" || fname == "default" {
- if meta, ok := d[ppath]; ok {
+ if meta, ok := def[ppath]; ok {
m.MergeMeta(meta, false)
- defs[ppath] = m
}
+ def[ppath] = m
} else if fname == "meta" {
p.Meta.MergeMeta(m, true)
}
@@ -169,10 +168,10 @@ func loadContentFile(p Page, defs map[string]Meta, fpath string, ppath string) (
} else {
a := filepath.Join(ppath, filepath.Base(fpath))
p.Assets.All = append(p.Assets.All, a)
- ref := &p.Asset.all[len(p.Assets.All)-1]
+ ref := &p.Assets.All[len(p.Assets.All)-1]
mimetype := mime.TypeByExtension(filepath.Ext(fpath))
if strings.Contains(mimetype, "image/") {
- p.Assets.Image = append(p.Assets.Images, ref)
+ p.Assets.Image = append(p.Assets.Image, ref)
} else if strings.Contains(mimetype, "video") {
p.Assets.Video = append(p.Assets.Video, ref)
} else if strings.Contains(mimetype, "audio") {
@@ -181,7 +180,7 @@ func loadContentFile(p Page, defs map[string]Meta, fpath string, ppath string) (
p.Assets.Misc = append(p.Assets.Misc, ref)
}
}
- return p, d, err
+ return p, def, err
}
// NewContentFromFile loads the file from `fpath` and converts it to HTML
diff --git a/content_test.go b/content_test.go
@@ -1,15 +1,15 @@
package main
import (
+ "io/ioutil"
"os"
"path/filepath"
- "io/ioutil"
"testing"
)
func TestLoadContentDir(test *testing.T) {
test.Parallel()
-
+
var err error
tdir := filepath.Join(os.TempDir(), "pagr_test_TestLoadContentDir")
if err := os.MkdirAll(tdir, 0775); err != nil {
@@ -25,7 +25,7 @@ func TestLoadContentDir(test *testing.T) {
}
validateTestPages(test, p, err)
-
+
if err = os.RemoveAll(tdir); err != nil {
test.Error(err)
}
@@ -63,7 +63,7 @@ func TestNewContentFromFile(test *testing.T) {
test.Fatal("NewContentFromFile failed for", ftype, err)
}
}
-
+
if err = os.RemoveAll(tdir); err != nil {
test.Error(err)
}
diff --git a/copy.go b/copy.go
@@ -32,7 +32,7 @@ int copyf(const char *src, const char *dst)
total += w;
}
} while (!feof(srcf));
-
+
if (total == siz) ret = EXIT_SUCCESS;
ABORT:
diff --git a/copy_test.go b/copy_test.go
@@ -1,8 +1,8 @@
package main
import (
- "os"
"io/ioutil"
+ "os"
"path/filepath"
"testing"
)
@@ -36,7 +36,7 @@ func TestCopyFile(test *testing.T) {
} else if string(buf) != string(srcData) {
test.Fatalf("copied srcData (%s) does not match source (%s)", buf, srcData)
}
-
+
if err := os.RemoveAll(tdir); err != nil {
test.Error(err)
}
diff --git a/page.go b/page.go
@@ -46,11 +46,11 @@ type Page struct {
}
type Assets struct {
- All []string
- Audio []*string
- Image []*string
- Video []*string
- Misc []*string
+ All []string
+ Audio []*string
+ Image []*string
+ Video []*string
+ Misc []*string
}
// Nav is a struct that provides a set of pointers for navigating a
diff --git a/page_test.go b/page_test.go
@@ -1,9 +1,9 @@
package main
import (
+ "io/ioutil"
"notabug.org/gearsix/suti"
"os"
- "io/ioutil"
"path/filepath"
"testing"
"time"
@@ -63,15 +63,12 @@ func TestTemplateName(test *testing.T) {
test.Parallel()
p := NewPage("/test", time.Now())
- if p.TemplateName() != DefaultTemplateName {
- test.Fatalf("'%s' not returned from TemplateName()", DefaultTemplateName)
- }
p.Meta["Template"] = "test1"
- if p.TemplateName() != "test1" {
+ if p.TemplateName("foo") != "test1" {
test.Fatalf("'test1' not returned from TemplateName()")
}
p.Meta["template"] = "test2"
- if p.TemplateName() != "test2" {
+ if p.TemplateName("foo") != "test2" {
test.Fatalf("'test2' not returned from TemplateName()")
}
}
@@ -101,7 +98,7 @@ func TestBuild(test *testing.T) {
if string(fbuf) != "Test p" {
test.Fatalf("invalid result parsed: '%s', expected: 'Test p'", string(fbuf))
}
-
+
if err := os.RemoveAll(tdir); err != nil {
test.Error(err)
}
diff --git a/pagr.go b/pagr.go
@@ -71,14 +71,14 @@ func main() {
pagec := 0
for _, p := range content {
vlog("+ %s", p.Path)
-
+
_, err = p.Build(config.Output, findPageTemplate(p, templates))
if err != nil {
ilog.Printf("skipping %s: %s\n", p.Path, err)
continue
}
- for _, asset := range p.Assets {
+ for _, asset := range p.Assets.All {
src := filepath.Join(config.Contents, asset)
dst := filepath.Join(config.Output, asset)
check(CopyFile(src, dst))
@@ -86,7 +86,7 @@ func main() {
}
pagec++
- assetc += len(p.Assets)
+ assetc += len(p.Assets.All)
}
ilog.Printf("generated %d html files, copied %d asset files\n", pagec, assetc)
@@ -138,5 +138,3 @@ func copyAssets() (count int) {
}
return
}
-
-
diff --git a/pagr_test.go b/pagr_test.go
@@ -2,8 +2,8 @@ package main
import (
"fmt"
- "os"
"io/ioutil"
+ "os"
"path/filepath"
"testing"
"time"
@@ -227,7 +227,7 @@ func validateTestPages(t *testing.T, pages []Page, e error) {
if len(p.Contents) == 0 {
t.Error("empty Contents for page:", p.Path)
}
- if len(p.Assets) == 0 {
+ if len(p.Assets.All) == 0 {
t.Error("empty Assets for page:", p.Path)
}
if pt, e = time.Parse(timefmt, p.Updated); e != nil {
diff --git a/sitemap_test.go b/sitemap_test.go
@@ -2,8 +2,8 @@ package main
import (
"os"
- "testing"
"path/filepath"
+ "testing"
)
func TestBuildCrumbs(test *testing.T) {
@@ -24,7 +24,7 @@ func TestBuildCrumbs(test *testing.T) {
}
validateTestPagesNav(test, p)
-
+
if err = os.RemoveAll(tdir); err != nil {
test.Error(err)
}
@@ -45,10 +45,10 @@ func TestBuildSitemap(test *testing.T) {
var p []Page
if p, err = LoadContentDir(tdir); err != nil {
test.Errorf("LoadContentDir failed: %s", err)
- }
+ }
validateTestPagesNav(test, p)
-
+
if err = os.RemoveAll(tdir); err != nil {
test.Error(err)
}
diff --git a/template_test.go b/template_test.go
@@ -2,8 +2,8 @@ package main
import (
"os"
- "testing"
"path/filepath"
+ "testing"
)
func TestLoadTemplateDir(t *testing.T) {
@@ -26,7 +26,7 @@ func TestLoadTemplateDir(t *testing.T) {
t.Fatalf("number of returned templates is %d (should be %d)",
len(tmpls), len(templates))
}
-
+
if err = os.RemoveAll(tdir); err != nil {
t.Error(err)
}