Home

ugit @3e7f0123397623b931dc5698376a99e08e5b7e90 - refs - log -
-
https://git.jolheiser.com/ugit.git
The code powering this h*ckin' site
ugit / internal / html / generate.go
- raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//go:build generate

package main

import (
	"bytes"
	_ "embed"
	"fmt"
	"go.jolheiser.com/ugit/internal/html/markup"
	"go/format"
	"os"
	"os/exec"

	"github.com/alecthomas/chroma/v2/styles"
)

var (
	tailwindCSS = `
@tailwind base;
@tailwind components;
@tailwind utilities;
`
	//go:embed generate.css
	otherCSS string
)

//go:generate templ generate
//go:generate go run generate.go
func main() {
	if err := tailwind(); err != nil {
		panic(err)
	}
}

// Generate tailwind code from templ templates and combine with other misc CSS
func tailwind() error {
	fmt.Println("generating tailwind...")

	tmp, err := os.CreateTemp(os.TempDir(), "ugit-tailwind*")
	if err != nil {
		return err
	}
	defer os.Remove(tmp.Name())
	if _, err := tmp.WriteString(tailwindCSS + otherCSS); err != nil {
		return err
	}

	fmt.Println("generating chroma styles...")

	latte := styles.Get("catppuccin-latte")
	if err := markup.Formatter.WriteCSS(tmp, latte); err != nil {
		return err
	}

	tmp.WriteString("@media (prefers-color-scheme: dark) {")
	mocha := styles.Get("catppuccin-mocha")
	if err := markup.Formatter.WriteCSS(tmp, mocha); err != nil {
		return err
	}
	tmp.WriteString("}")

	fmt.Println("finished generating chroma styles")

	tmp.Close()

	styles, err := os.Create("tailwind.go")
	if err != nil {
		return err
	}
	defer styles.Close()

	var buf bytes.Buffer
	cmd := exec.Command("tailwind-ctp", "-i", tmp.Name(), "--minify")
	cmd.Stdout = &buf
	if err := cmd.Run(); err != nil {
		return err
	}

	code := fmt.Sprintf(`// Code generated by generate.go - DO NOT EDIT.
package html

		import "net/http"
		
		func TailwindHandler(w http.ResponseWriter, r *http.Request) {
			w.Header().Set("Content-Type", "text/css")
			w.Write([]byte(%q))
		}`, buf.String())
	formatted, err := format.Source([]byte(code))
	if err != nil {
		return err
	}
	styles.Write(formatted)

	fmt.Println("finished generating tailwind")
	return nil
}