Home

blog @615129be36f02b57ea0065ba9af6196f34014938 - refs - log -
-
https://git.jolheiser.com/blog.git
My nonexistent blog
blog / cmd / chromacss / main.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
package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/alecthomas/chroma/v2/formatters/html"
	"github.com/alecthomas/chroma/v2/styles"
	"go.jolheiser.com/blog"
)

func main() {
	if err := maine(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

func maine() error {
	fs := flag.NewFlagSet("chromacss", flag.ExitOnError)
	lightFlag := fs.String("light", "catppuccin-latte", "Light theme")
	fs.StringVar(lightFlag, "l", *lightFlag, "--light")
	darkFlag := fs.String("dark", "catppuccin-mocha", "Dark theme")
	fs.StringVar(darkFlag, "d", *darkFlag, "--dark")
	outFlag := fs.String("out", "", "Output (default: stdout)")
	fs.StringVar(outFlag, "o", *outFlag, "--out")
	if err := fs.Parse(os.Args[1:]); err != nil {
		return err
	}

	out := os.Stdout
	if *outFlag != "" {
		fi, err := os.Create(*outFlag)
		if err != nil {
			return err
		}
		defer fi.Close()
		out = fi
	}

	formatter := html.New(blog.ChromaOpts...)

	lightStyle := *lightFlag
	darkStyle := *darkFlag

	if lightStyle == "" {
		lightStyle = "catppuccin-latte"
		if darkStyle != "" {
			lightStyle = darkStyle
			darkStyle = ""
		}
	}

	styles.Fallback = styles.Get("catppuccin-latte")
	style := styles.Get(lightStyle)
	if err := formatter.WriteCSS(out, style); err != nil {
		return err
	}

	if darkStyle != "" {
		out.WriteString("@media (prefers-color-scheme: dark) {")
		styles.Fallback = styles.Get("catpuccin-mocha")
		style = styles.Get(darkStyle)
		if err := formatter.WriteCSS(out, style); err != nil {
			return err
		}
		out.WriteString("}")
	}

	return nil
}