File: regen.go

package info (click to toggle)
panicparse 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 744 kB
  • sloc: sh: 13; makefile: 5
file content (82 lines) | stat: -rw-r--r-- 1,700 bytes parent folder | download | duplicates (2)
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
// Copyright 2017 Marc-Antoine Ruel. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.

//go:build tools
// +build tools

package main

import (
	"bytes"
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"os"
	"regexp"
	"strconv"
	"text/template"
)

const content = `// Code generated by regen.go. DO NOT EDIT.

package stack

import (
	"html/template"
)

const indexHTML = {{.IndexHTML}}

// favicon is the bomb emoji U+1F4A3 in Noto Emoji as a 128x128 base64 encoded
// PNG.
//
// See README.md for license and how to retrieve it.
const favicon template.HTML = "{{.Favicon}}"
`

// loadGoroutines returns "goroutines.tpl" slightly processed for density.
func loadGoroutines() ([]byte, error) {
	htmlRaw, err := ioutil.ReadFile("goroutines.tpl")
	if err != nil {
		return nil, err
	}
	// Strip out leading whitespace.
	re := regexp.MustCompile("(\\n[ \\t]*)+")
	htmlRaw = re.ReplaceAll(htmlRaw, []byte("\n"))
	return htmlRaw, nil
}

func mainImpl() error {
	htmlRaw, err := loadGoroutines()
	if err != nil {
		return err
	}

	// See README.md how to generate it.
	iconRaw, err := ioutil.ReadFile("emoji_u1f4a3_64.gif")
	if err != nil {
		return err
	}

	t, err := template.New("t").Parse(content)
	if err != nil {
		return err
	}
	data := map[string]string{
		"IndexHTML": strconv.Quote(string(htmlRaw)),
		"Favicon":   base64.StdEncoding.EncodeToString(iconRaw),
	}
	b := bytes.Buffer{}
	if err := t.Execute(&b, data); err != nil {
		return err
	}
	return ioutil.WriteFile("data.go", b.Bytes(), 0666)
}

func main() {
	if err := mainImpl(); err != nil {
		fmt.Fprintf(os.Stderr, "Failed: %s\n", err)
		os.Exit(1)
	}
}