forked from asciinema/asciinema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asciicast.go
168 lines (136 loc) · 3.37 KB
/
asciicast.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package asciicast
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/asciinema/asciinema/Godeps/_workspace/src/golang.org/x/net/html"
)
type Env struct {
Term string `json:"TERM"`
Shell string `json:"SHELL"`
}
type Duration float64
func (d Duration) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`%.6f`, d)), nil
}
type Asciicast struct {
Version int `json:"version"`
Width int `json:"width"`
Height int `json:"height"`
Duration Duration `json:"duration"`
Command string `json:"command"`
Title string `json:"title"`
Env *Env `json:"env"`
Stdout []Frame `json:"stdout"`
}
func NewAsciicast(width, height int, duration float64, command, title string, frames []Frame, env map[string]string) *Asciicast {
return &Asciicast{
Version: 1,
Width: width,
Height: height,
Duration: Duration(duration),
Command: command,
Title: title,
Env: &Env{Term: env["TERM"], Shell: env["SHELL"]},
Stdout: frames,
}
}
func Save(asciicast *Asciicast, path string) error {
bytes, err := json.MarshalIndent(asciicast, "", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(path, bytes, 0644)
if err != nil {
return err
}
return nil
}
// asciinema play file.json
// asciinema play https://asciinema.org/a/123.json
// asciinema play https://asciinema.org/a/123
// asciinema play ipfs://ipfs/QmbdpNCwqeZgnmAWBCQcs8u6Ts6P2ku97tfKAycE1XY88p
// asciinema play -
func getAttr(t *html.Token, name string) string {
for _, a := range t.Attr {
if a.Key == name {
return a.Val
}
}
return ""
}
func extractJSONURL(htmlDoc io.Reader) (string, error) {
z := html.NewTokenizer(htmlDoc)
for {
tt := z.Next()
switch {
case tt == html.ErrorToken:
return "", fmt.Errorf("expected alternate <link> not found in fetched HTML document")
case tt == html.StartTagToken:
t := z.Token()
if t.Data == "link" && getAttr(&t, "rel") == "alternate" && getAttr(&t, "type") == "application/asciicast+json" {
return getAttr(&t, "href"), nil
}
}
}
}
func getSource(url string) (io.ReadCloser, error) {
var source io.ReadCloser
var isHTML bool
var err error
if strings.HasPrefix(url, "ipfs:/") {
url = fmt.Sprintf("https://ipfs.io/%v", url[6:])
} else if strings.HasPrefix(url, "fs:/") {
url = fmt.Sprintf("https://ipfs.io/%v", url[4:])
}
if url == "-" {
source = os.Stdin
} else if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
resp.Body.Close()
return nil, fmt.Errorf("got status %v when requesting %v", resp.StatusCode, url)
}
source = resp.Body
if strings.HasPrefix(resp.Header.Get("Content-Type"), "text/html") {
isHTML = true
}
} else {
source, err = os.Open(url)
if err != nil {
return nil, err
}
if strings.HasSuffix(url, ".html") {
isHTML = true
}
}
if isHTML {
defer source.Close()
url, err = extractJSONURL(source)
if err != nil {
return nil, err
}
return getSource(url)
}
return source, nil
}
func Load(url string) (*Asciicast, error) {
source, err := getSource(url)
if err != nil {
return nil, err
}
defer source.Close()
dec := json.NewDecoder(source)
asciicast := &Asciicast{}
if err = dec.Decode(asciicast); err != nil {
return nil, err
}
return asciicast, nil
}