This repository was archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
118 lines (103 loc) · 2.43 KB
/
utils.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
//go:generate statik -src=./assets
//go:generate go fmt statik/statik.go
package pica
import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"regexp"
"github.com/fixate/go-qs"
"github.com/jerloo/funny"
"github.com/rakyll/statik/fs"
"github.com/shurcooL/github_flavored_markdown"
)
func HttpHeaders2VmMap(httpHeader http.Header) map[string]funny.Value {
var headers = map[string]funny.Value{}
for k, _ := range httpHeader {
headers[k] = httpHeader.Get(k)
}
return headers
}
func VmMap2HttpHeaders(vmMap map[string]funny.Value) http.Header {
headers := http.Header{}
for k, v := range vmMap {
headers.Set(k, v.(string))
}
return headers
}
func CompileURL(url string, vm *funny.Funny) (string, Query, error) {
queryValue := vm.LookupDefault("query", nil)
query := Query{}
if queryValue != nil {
valQuery := queryValue.(map[string]funny.Value)
for key, val := range valQuery {
query[key] = val
}
}
reg, err := regexp.Compile("<(.*?)>")
if err != nil {
return "", nil, err
}
result := reg.ReplaceAllStringFunc(url, func(repl string) string {
repl = repl[1 : len(repl)-1]
val := vm.Lookup(repl)
switch val := val.(type) {
case int:
return fmt.Sprint(val)
case string:
return val
case float64:
return strconv.FormatFloat(val, 'f', -1, 64)
default:
panic(fmt.Errorf("unsupport type [%s], only support [int][string]", funny.Typing(val)))
}
})
if query == nil || len(query) == 0 {
return result, nil, nil
}
qs, err := query.String()
if err != nil {
return "", nil, err
}
if qs != "?" {
return fmt.Sprintf("%s?%s", result, qs), nil, nil
}
return result, query, nil
}
func BuildHTML(input []byte) string {
output := github_flavored_markdown.Markdown(input)
statikFS, _ := fs.New()
tFile, err := statikFS.Open("/doc_template.html")
if err != nil {
panic(err)
}
template, err := ioutil.ReadAll(tFile)
if err != nil {
panic(err)
}
rs := strings.Replace(string(template), "[body]", string(output), -1)
return rs
}
type Query map[string]interface{}
func NewQuery(m map[string]interface{}) Query {
query := Query{}
for k, v := range m {
query[k] = v
}
return query
}
func ParseQuery(queryString string) (Query, error) {
r, err := qs.Unmarshal(queryString)
if err != nil {
return nil, err
}
return NewQuery(r), nil
}
func (query Query) String() (string, error) {
return qs.Marshal(query)
}
func PicaContextFromRunner(runner *APIRunner) *PicaContext {
return &PicaContext{}
}