forked from golang/gddo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.go
48 lines (41 loc) · 1.08 KB
/
graph.go
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
// Copyright 2013 The Go Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd.
package main
import (
"bytes"
"errors"
"fmt"
"os/exec"
"strings"
"github.com/golang/gddo/database"
"github.com/golang/gddo/doc"
)
func renderGraph(pdoc *doc.Package, pkgs []database.Package, edges [][2]int) ([]byte, error) {
var in, out bytes.Buffer
fmt.Fprintf(&in, "digraph %s { \n", pdoc.Name)
for i, pkg := range pkgs {
fmt.Fprintf(&in, " n%d [label=\"%s\", URL=\"/%s\", tooltip=\"%s\"];\n",
i, pkg.Path, pkg.Path,
strings.Replace(pkg.Synopsis, `"`, `\"`, -1))
}
for _, edge := range edges {
fmt.Fprintf(&in, " n%d -> n%d;\n", edge[0], edge[1])
}
in.WriteString("}")
cmd := exec.Command("dot", "-Tsvg")
cmd.Stdin = &in
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return nil, err
}
p := out.Bytes()
if i := bytes.Index(p, []byte("<svg")); i < 0 {
return nil, errors.New("<svg not found")
} else {
p = p[i:]
}
return p, nil
}