diff --git a/go.mod b/go.mod index 4e61d2c7..cc888d97 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.17 require ( github.com/PuerkitoBio/goquery v1.8.0 github.com/avelino/slugify v0.0.0-20180501145920-855f152bd774 + github.com/otiai10/copy v1.9.0 github.com/yuin/goldmark v1.4.13 golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 ) diff --git a/go.sum b/go.sum index 4565a4b5..8c0ebb96 100644 --- a/go.sum +++ b/go.sum @@ -109,6 +109,13 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/otiai10/copy v1.9.0 h1:7KFNiCgZ91Ru4qW4CWPf/7jqtxLagGRmIxWldPP9VY4= +github.com/otiai10/copy v1.9.0/go.mod h1:hsfX19wcn0UWIHUQ3/4fHuehhk2UyArQ9dVFAn3FczI= +github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE= +github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs= +github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= +github.com/otiai10/mint v1.4.0 h1:umwcf7gbpEwf7WFzqmWwSv0CzbeMsae2u9ZvpP8j2q4= +github.com/otiai10/mint v1.4.0/go.mod h1:gifjb2MYOoULtKLqUAEILUG/9KONW6f7YsJ6vQLTlFI= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= diff --git a/main.go b/main.go index 6c269b42..5c0bf81a 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "bytes" "fmt" + cp "github.com/otiai10/copy" "os" "path/filepath" "strings" @@ -27,15 +28,21 @@ type Object struct { // Source const readmePath = "README.md" +const assetsDir = "tmpl/assets" // Templates +const tplPath = "tmpl/tmpl.html" const tmplCategory = "tmpl/cat-tmpl.html" const tmplSitemap = "tmpl/sitemap-tmpl.xml" // Output + +// NOTE: trailing slash is required const outDir = "out/" -const outIndexFile = "index.html" -const outSitemapFile = "sitemap.xml" + +var outAssetsDir = filepath.Join(outDir, "assets") +var outIndexFile = filepath.Join(outDir, "index.html") +var outSitemapFile = filepath.Join(outDir, "sitemap.xml") func main() { outIndexAbs := filepath.Join(outDir, outIndexFile) @@ -44,7 +51,7 @@ func main() { panic(err) } - input, err := os.ReadFile(outIndexAbs) + input, err := os.ReadFile(outIndexFile) if err != nil { panic(err) } @@ -77,6 +84,10 @@ func main() { changeLinksInIndex(string(input), query, objs) makeSitemap(objs) + + if err := cp.Copy(assetsDir, outAssetsDir); err != nil { + panic(err) + } } func mkdirAll(path string) error { @@ -101,15 +112,15 @@ func mkdirAll(path string) error { func makeSiteStruct(objs map[string]*Object) error { for _, obj := range objs { - outDir := filepath.Join(outDir, obj.Slug) - if err := mkdirAll(outDir); err != nil { + categoryDir := filepath.Join(outDir, obj.Slug) + if err := mkdirAll(categoryDir); err != nil { return err } // FIXME: embed templates // FIXME: parse templates once at start t := template.Must(template.ParseFiles(tmplCategory)) - f, err := os.Create(filepath.Join(outDir, "index.html")) + f, err := os.Create(filepath.Join(categoryDir, "index.html")) if err != nil { return err } @@ -124,7 +135,7 @@ func makeSiteStruct(objs map[string]*Object) error { func makeSitemap(objs map[string]*Object) { t := template.Must(template.ParseFiles(tmplSitemap)) - f, _ := os.Create(filepath.Join(outDir, outSitemapFile)) + f, _ := os.Create(outSitemapFile) t.Execute(f, objs) } @@ -176,5 +187,5 @@ func changeLinksInIndex(html string, query *goquery.Document, objs map[string]*O } }) - os.WriteFile(filepath.Join(outDir, outIndexFile), []byte(html), 0644) + os.WriteFile(outIndexFile, []byte(html), 0644) } diff --git a/main_test.go b/main_test.go index 18184ca2..6e3a8ebd 100644 --- a/main_test.go +++ b/main_test.go @@ -69,6 +69,7 @@ func TestSeparator(t *testing.T) { } } } + func TestGenerateHTML(t *testing.T) { err := GenerateHTML(readmePath, outIndexFile) if err != nil { diff --git a/scripts.go b/scripts.go index 072e60c2..dc973511 100644 --- a/scripts.go +++ b/scripts.go @@ -37,9 +37,6 @@ type content struct { // GenerateHTML generate site html (index.html) from markdown file func GenerateHTML(srcFilename, outFilename string) error { - // options - const tplPath = "tmpl/tmpl.html" - input, err := ioutil.ReadFile(srcFilename) if err != nil { return err