diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a0830d4..e8f15eb 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -26,7 +26,7 @@ build:site: - cp -r src/* "$SRC_DIR"/ script: - templ generate - - go run . -gen "$SRC_DIR" + - go run . -minify -gen "$SRC_DIR" artifacts: expire_in: 1 hour paths: diff --git a/flake.nix b/flake.nix index 400a00d..d76d163 100644 --- a/flake.nix +++ b/flake.nix @@ -87,7 +87,7 @@ templ generate ''; buildPhase = '' - go run . -gen src + go run . -minify -gen src ''; installPhase = '' mkdir -p "$out" diff --git a/main.go b/main.go index 2181a3e..8a595f0 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "path/filepath" "strings" + "github.com/a-h/templ" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/log" "github.com/labstack/echo/v4" @@ -63,11 +64,31 @@ func minifyInPlace(mediatype, path string) error { return minifier.Minify(mediatype, outputFile, inputFile) } -func traverse(prefix, path string, d fs.DirEntry) error { - trimedPath := strings.TrimPrefix(path, prefix+"/") +func genIndexFile(path string, c templ.Component) error { + indexFile, err := os.Create(filepath.Join(path, "index.html")) + if err != nil { + return err + } + defer closeFile(indexFile) - // Generate index.html file if we hit a directory + buf := new(bytes.Buffer) + if err := c.Render(ctx, buf); err != nil { + return err + } + + return minifier.Minify("text/html", indexFile, buf) +} + +func traverse(prefix, path string, d fs.DirEntry) error { + // Generate home page for the root directory. + if prefix == path { + logger.Info("create index.html for root directory") + return genIndexFile(path, indexPage()) + } + + // Otherwise generate a listing page if d.IsDir() { + trimedPath := strings.TrimPrefix(strings.TrimPrefix(path, prefix), "/") logger.Info("create index.html", "dir", trimedPath) entries, err := os.ReadDir(path) @@ -75,24 +96,7 @@ func traverse(prefix, path string, d fs.DirEntry) error { return err } - indexFile, err := os.Create(filepath.Join(path, "index.html")) - if err != nil { - return err - } - defer closeFile(indexFile) - - buf := new(bytes.Buffer) - if err := listingPage(trimedPath, entries).Render(ctx, buf); err != nil { - return err - } - - return minifier.Minify("text/html", indexFile, buf) - } - - // Otherwise minify it, if it is a CSS file - if d.Type().IsRegular() && filepath.Ext(path) == ".css" { - logger.Info("minify CSS", "file", trimedPath) - return minifyInPlace("text/css", path) + return genIndexFile(path, listingPage(trimedPath, entries)) } return nil @@ -128,6 +132,7 @@ func preview(root, port string) error { func main() { genFlag := flag.Bool("gen", false, "Generate index files") previewFlag := flag.Bool("preview", false, "Preview the built website") + minifyFlag := flag.Bool("minify", false, "Minify existing CSS files") flag.Usage = func() { fmt.Printf(`Usage: go run . [OPTIONS] DIR @@ -185,31 +190,30 @@ Options: logger.Fatal("unable to get root directory", "err", err) } - if *genFlag { + // Run minification first, to ensure file size is calculated correctly later + if *minifyFlag { err = filepath.WalkDir(rootDir, func(path string, d fs.DirEntry, err error) error { if err != nil { return err } - // Generate the home page for the root directory. - // NOTE: there isn't anything else in the root directory, so don't check each entry for minification - if rootDir == path { - logger.Info("create index.html for root directory") - indexFile, err := os.Create(filepath.Join(path, "index.html")) - if err != nil { - return err - } - defer closeFile(indexFile) - - buf := new(bytes.Buffer) - if err := indexPage().Render(ctx, buf); err != nil { - return err - } - - return minifier.Minify("text/html", indexFile, buf) + if d.Type().IsRegular() && filepath.Ext(path) == ".css" { + logger.Info("minify CSS", "file", strings.TrimPrefix(path, rootDir+"/")) + return minifyInPlace("text/css", path) } - // Otherwise generate listing pages and minify CSS files + return nil + }) + if err != nil { + logger.Fatal("failed minifying CSS files", "err", err) + } + } + + if *genFlag { + err = filepath.WalkDir(rootDir, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } return traverse(rootDir, path, d) }) if err != nil { diff --git a/pages.templ b/pages.templ index 733e540..d947c45 100644 --- a/pages.templ +++ b/pages.templ @@ -21,7 +21,7 @@ templ pageTemplate(path string) { - + @@ -58,10 +58,11 @@ templ indexPage() { templ listingPage(path string, entries []fs.DirEntry) { @pageTemplate(path) { -

+

Directory listing

+

@splitPathIntoLinks(strings.Split(path, "/")) -

+
for _, entry := range entries { diff --git a/src/styles/self.css b/src/styles/self.css index dc97c8f..8184624 100644 --- a/src/styles/self.css +++ b/src/styles/self.css @@ -47,11 +47,11 @@ a:hover { border-bottom: 0.1rem solid var(--color-cyan); } -h1 > a { +h2 > a { color: var(--color-fg); } -h1 > a:hover { +h2 > a:hover { color: var(--color-fg); border: none; }