Home

ugit @5f408c1fb1ddaa63a3ee9bf239f552bcef9b0778 - 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/format"
	"os"
	"os/exec"

	"go.jolheiser.com/ugit/internal/html"

	"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)
	}
}

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 := html.Formatter.WriteCSS(tmp, latte); err != nil {
		return err
	}

	tmp.WriteString("@media (prefers-color-scheme: dark) {")
	mocha := styles.Get("catppuccin-mocha")
	if err := html.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
}