aboutsummaryrefslogtreecommitdiff
path: root/pkg/handler/static/handler.go
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2025-11-01 17:49:20 +0100
committerGabriel A. Giovanini <mail@gabrielgio.me>2025-11-01 17:49:20 +0100
commit5dd940eb52b40c78a2078ed0a02440e84bee0306 (patch)
tree81e6b5a148c42a5687a0fde0a77001b5ab81cfb4 /pkg/handler/static/handler.go
parented1fc6854a634f7fcff7d1a500c40e7502031ea7 (diff)
downloadcerrado-5dd940eb52b40c78a2078ed0a02440e84bee0306.tar.gz
cerrado-5dd940eb52b40c78a2078ed0a02440e84bee0306.tar.bz2
cerrado-5dd940eb52b40c78a2078ed0a02440e84bee0306.zip
feat: Write css dark/light theme on standalone url
This will allow tho write both themes into the same css file. Also mend with the css generate by chroma so it can be nested.
Diffstat (limited to 'pkg/handler/static/handler.go')
-rw-r--r--pkg/handler/static/handler.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/pkg/handler/static/handler.go b/pkg/handler/static/handler.go
index cdb2ae6..779c786 100644
--- a/pkg/handler/static/handler.go
+++ b/pkg/handler/static/handler.go
@@ -1,6 +1,8 @@
package static
import (
+ "fmt"
+ "io"
"io/fs"
"mime"
"net/http"
@@ -8,6 +10,9 @@ import (
"git.gabrielgio.me/cerrado/pkg/ext"
"git.gabrielgio.me/cerrado/static"
+ "github.com/alecthomas/chroma/v2"
+ "github.com/alecthomas/chroma/v2/formatters/html"
+ "github.com/alecthomas/chroma/v2/styles"
)
func ServeStaticHandler() (ext.ErrorRequestHandler, error) {
@@ -28,3 +33,47 @@ func ServeStaticHandler() (ext.ErrorRequestHandler, error) {
return nil
}, nil
}
+
+func ServeStaticCSSHandler(lightTheme, darkTheme string) (ext.ErrorRequestHandler, error) {
+ var (
+ lightStyle = styles.Get(lightTheme)
+ darkStyle = styles.Get(darkTheme)
+ formatter = html.New(
+ html.WithCSSComments(false),
+ )
+ )
+
+ return func(w http.ResponseWriter, r *ext.Request) error {
+ ext.SetMIME(w, "text/css")
+
+ var style *chroma.Style
+ style = darkStyle
+ w.Write([]byte("[data-bs-theme=\"dark\"] {\n"))
+ err := formatter.WriteCSS(&ws{w}, style)
+ if err != nil {
+ return err
+ }
+ w.Write([]byte("}\n"))
+
+ style = lightStyle
+ w.Write([]byte("[data-bs-theme=\"light\"] {\n"))
+ err = formatter.WriteCSS(&ws{w}, style)
+ if err != nil {
+ return err
+ }
+ w.Write([]byte("\n}"))
+
+ return nil
+ }, nil
+}
+
+type ws struct {
+ inner io.Writer
+}
+
+// This is very cursed, and rely on the fact that it writes every css rule at time.
+// it adds & to the begging so it can be nested by the ServeStaticCSSHandler.
+// This will allow the follow bootstrap data-bs-theme.
+func (w *ws) Write(p []byte) (n int, err error) {
+ return fmt.Fprintf(w.inner, "& %s", string(p))
+}