-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskate.go
68 lines (57 loc) · 1.25 KB
/
skate.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
)
type SkateResponse struct {
Html string
}
func parseSkateResponse(inlineVersion string) string {
str := inlineVersion
res := &SkateResponse{}
json.Unmarshal([]byte(str), &res)
return res.Html
}
func getInlineVersion(html string) string {
data := url.Values{}
data.Set("source", html)
client := &http.Client{}
req, _ := http.NewRequest("POST", "http://zurb.com/ink/skate-proxy.php", bytes.NewBufferString(data.Encode()))
req.Header.Add("Referer", "http://github.com/aitor/skate")
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return parseSkateResponse(string(body))
}
func checkReading(e error) {
if e != nil {
panic(e)
}
}
func readSourceFromStdin() string {
content, err := ioutil.ReadAll(os.Stdin)
checkReading(err)
return string(content)
}
func readSourceFromFile(fileName string) string {
content, err := ioutil.ReadFile(fileName)
checkReading(err)
return string(content)
}
func main() {
var source string
flag.Parse()
switch name := flag.Arg(0); {
case name == "":
source = readSourceFromStdin()
default:
source = readSourceFromFile(name)
}
fmt.Print(getInlineVersion(source))
}