/
content.go
143 lines (111 loc) · 3.75 KB
/
content.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
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"time"
"qiniupkg.com/http/httputil.v2"
"qiniupkg.com/x/log.v7"
xbytes "qiniupkg.com/x/bytes.v7"
)
// ---------------------------------------------------
var (
srcPoweredBy = []byte(` by  <a href="http://www.doxygen.org/index.html">`)
destPoweredBy = []byte(`. <a href="javascript:document.getElementsByName('x-refresh')[0].submit();" title="Refresh this page from the source.">Refresh now</a>. <a href="./?tools">Tools</a> for package owners. Powered by <a href="http://qiniu.com/"><img class="footer" src="http://assets.qiniu.com/qiniu-white-97x51.png" alt="qiniu"/></a> <a href="http://www.doxygen.org/index.html">`)
)
var (
srcFooter = []byte(`<hr class="footer"/>`)
destFooterTempl = `<hr class="footer"/><form name="x-refresh" method="POST" action="/-/refresh"><input type="hidden" name="path" value="%s"></form>`
)
func serveContent(
w http.ResponseWriter, req *http.Request,
pkg, name string, modtime time.Time, content io.ReadSeeker) {
if !strings.HasSuffix(name, ".html") {
http.ServeContent(w, req, name, modtime, content)
return
}
b, err := ioutil.ReadAll(content)
if err != nil {
httputil.Error(w, err)
return
}
var destFooter bytes.Buffer
fmt.Fprintf(&destFooter, destFooterTempl, pkg)
b = xbytes.Replace(b, srcFooter, destFooter.Bytes(), 1)
b = xbytes.Replace(b, srcPoweredBy, destPoweredBy, 1)
http.ServeContent(w, req, name, modtime, bytes.NewReader(b))
}
// ---------------------------------------------------
const (
srcMainPageDocStartTempl = `<div class="title">%s Documentation</div>`
destMainPageDocStart = `<div class="title">`
destMainPageDocEnd = `</div>`
)
var (
srcStyleSheet = []byte(`</head>`)
destStyleSheet = []byte(`<link crossorigin="anonymous" href="http://qiniupkg.com/assets/github-20150713-1.css" media="all" rel="stylesheet"/>
<link crossorigin="anonymous" href="http://qiniupkg.com/assets/github-20150713-2.css" media="all" rel="stylesheet"/>
</head>`)
)
func makeMainPage(indexFile string, pkg string) (err error) {
log.Info("makeMainPage", indexFile, "of", pkg)
b, err := ioutil.ReadFile(indexFile)
if err != nil {
return
}
readmeFile := "https://" + pkg + "/blob/master/README.md"
div, err := renderGithubMarkdown(readmeFile)
if err != nil {
return
}
var mainPageDocStart bytes.Buffer
fmt.Fprintf(&mainPageDocStart, srcMainPageDocStartTempl, projectNameOf(pkg))
pos := bytes.Index(b, mainPageDocStart.Bytes())
if pos >= 0 {
destMainPage := make([]byte, 0, len(div) + len(destMainPageDocStart) + len(destMainPageDocEnd))
destMainPage = append(destMainPage, destMainPageDocStart...)
destMainPage = append(destMainPage, div...)
destMainPage = append(destMainPage, destMainPageDocEnd...)
b = xbytes.ReplaceAt(b, pos, mainPageDocStart.Len(), destMainPage)
b = xbytes.Replace(b, srcStyleSheet, destStyleSheet, 1)
err = ioutil.WriteFile(indexFile, b, 0666)
}
return
}
func projectNameOf(pkg string) string {
pos := strings.LastIndex(pkg, "/")
if pos < 0 {
return pkg
}
return pkg[pos+1:]
}
// ---------------------------------------------------
func renderGithubMarkdown(mdUrl string) (div []byte, err error) {
resp, err := http.Get(mdUrl)
if err != nil {
return
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
pos := bytes.Index(b, markdownStart)
if pos < 0 {
return nil, ErrInvalidGithubMarkdown
}
from := pos + len(markdownStart)
n := bytes.Index(b[from:], markdownEnd1)
if n < 0 {
return nil, ErrInvalidGithubMarkdown
}
return b[from:from+n+len(markdownEnd1)], nil
}
var (
markdownStart = []byte(`<div id="readme" class="blob instapaper_body">`)
markdownEnd1 = []byte(`</article>`)
)
// ---------------------------------------------------