Debian Codegraph @

generate.go

raw symbols SCIP
1 //go:build ignore
2
3 package main
4
5 import (
6 "bytes"
7 "encoding/json"
8 "flag"
9 "fmt"
10 "io"
11 "log"
12 "os"
13 "path/filepath"
14 "sort"
15 "strings"
16
17 "dmitri.shuralyov.com/text/kebabcase"
18 "github.com/shurcooL/go-goon"
19 "golang.org/x/net/html"
20 "golang.org/x/net/html/atom"
21 )
22
23 var oFlag = flag.String("o", "", "write output to `file` (default standard output)")
24
25 func main() {
26 flag.Parse()
27
28 err := run()
29 if err != nil {
30 log.Fatalln(err)
31 }
32 }
33
34 func run() error {
35 f, err := os.Open(filepath.Join("_data", "data.json"))
36 if err != nil {
37 return err
38 }
39 defer f.Close()
40
41 var octicons map[string]octicon
42 err = json.NewDecoder(f).Decode(&octicons)
43 if err != nil {
44 return err
45 }
46
47 var names []string
48 for name := range octicons {
49 names = append(names, name)
50 }
51 sort.Strings(names)
52
53 var buf bytes.Buffer
54 fmt.Fprint(&buf, `package octicon
55
56 import (
57 "strconv"
58
59 "golang.org/x/net/html"
60 "golang.org/x/net/html/atom"
61 )
62
63 // Icon returns the named Octicon SVG node.
64 // It returns nil if name is not a valid Octicon symbol name.
65 func Icon(name string) *html.Node {
66 switch name {
67 `)
68 for _, name := range names {
69 fmt.Fprintf(&buf, " case %q:\n return %v()\n", name, kebabcase.Parse(name).ToMixedCaps())
70 }
71 fmt.Fprint(&buf, ` default:
72 return nil
73 }
74 }
75
76 // SetSize sets size of icon, and returns a reference to it.
77 func SetSize(icon *html.Node, size int) *html.Node {
78 icon.Attr[`, widthAttrIndex, `].Val = strconv.Itoa(size)
79 icon.Attr[`, heightAttrIndex, `].Val = strconv.Itoa(size)
80 return icon
81 }
82 `)
83
84 // Write all individual Octicon functions.
85 for _, name := range names {
86 generateAndWriteOcticon(&buf, octicons, name)
87 }
88
89 var w io.Writer
90 switch *oFlag {
91 case "":
92 w = os.Stdout
93 default:
94 f, err := os.Create(*oFlag)
95 if err != nil {
96 return err
97 }
98 defer f.Close()
99 w = f
100 }
101
102 _, err = w.Write(buf.Bytes())
103 return err
104 }
105
106 type octicon struct {
107 Path string
108 Width int
109 Height float64
110 }
111
112 func generateAndWriteOcticon(w io.Writer, octicons map[string]octicon, name string) {
113 svgXML := generateOcticon(octicons[name])
114
115 svg := parseOcticon(svgXML)
116 // Clear these fields to remove cycles in the data structure, since go-goon
117 // cannot print those in a way that's valid Go code. The generated data structure
118 // is not a proper *html.Node with all fields set, but it's enough for rendering
119 // to be successful.
120 svg.LastChild = nil
121 svg.FirstChild.Parent = nil
122
123 fmt.Fprintln(w)
124 fmt.Fprintf(w, "// %s returns an %q Octicon SVG node.\n", kebabcase.Parse(name).ToMixedCaps(), name)
125 fmt.Fprintf(w, "func %s() *html.Node {\n", kebabcase.Parse(name).ToMixedCaps())
126 fmt.Fprint(w, " return ")
127 goon.Fdump(w, svg)
128 fmt.Fprintln(w, "}")
129 }
130
131 // These constants are used during generation of SetSize function.
132 // Keep them in sync with generateOcticon below.
133 const (
134 widthAttrIndex = 1
135 heightAttrIndex = 2
136 )
137
138 // TODO: Short-circuit generateOcticon and parseOcticon.
139
140 func generateOcticon(o octicon) (svgXML string) {
141 path := o.Path
142 if strings.HasPrefix(path, `<path fill-rule="evenodd" `) {
143 // Skip fill-rule, if present. It has no effect on displayed SVG, but takes up space.
144 path = `<path ` + path[len(`<path fill-rule="evenodd" `):]
145 }
146 // Note, SetSize relies on the absolute position of the width, height attributes.
147 // Keep them in sync with widthAttrIndex and heightAttrIndex.
148 return fmt.Sprintf(`<svg xmlns="http://www.w3.org/2000/svg" width=16 height=16 viewBox="0 0 %v %v">%s</svg>`,
149 o.Width, o.Height, path)
150 }
151
152 func parseOcticon(svgXML string) *html.Node {
153 e, err := html.ParseFragment(strings.NewReader(svgXML), nil)
154 if err != nil {
155 panic(fmt.Errorf("internal error: html.ParseFragment failed: %v", err))
156 }
157 svg := e[0].LastChild.FirstChild // TODO: Is there a better way to just get the <svg>...</svg> element directly, skipping <html><head></head><body><svg>...</svg></body></html>?
158 svg.Parent.RemoveChild(svg)
159 svg.Attr = append(svg.Attr, html.Attribute{Key: atom.Style.String(), Val: `fill: currentColor; vertical-align: top;`})
160 return svg
161 }