diff --git a/docs/index.md b/docs/index.md index 5a9ddcf..0bc9867 100644 --- a/docs/index.md +++ b/docs/index.md @@ -18,9 +18,9 @@ You can show documentation for different [branches](http://inconshreveable.viewd http://.viewdocs.io/~ -You can also customize the look and layout of your docs. Make your own `docs/template.html` based on the [default template](https://github.com/progrium/viewdocs/blob/master/docs/template.html) and your pages will be rendered with that template. +You can also customize the look and layout of your docs. Make your own `docs/template.html` based on the [default template](https://github.com/progrium/viewdocs/blob/master/docs/template.html) and your pages will be rendered with that template. -I also highly recommend you [read the source](https://github.com/progrium/viewdocs/blob/master/viewdocs.go) to this app. It's less than 200 lines of Go. If you want to hack on Viewdocs, [check this out](/viewdocs/development). +I also highly recommend you [read the source](https://github.com/progrium/viewdocs/blob/master/viewdocs.go) to this app. It's just around 200 lines of Go. If you want to hack on Viewdocs, [check this out](/viewdocs/development).
Enjoy!
diff --git a/docs/template.html b/themes/gistio/template.html similarity index 100% rename from docs/template.html rename to themes/gistio/template.html diff --git a/themes/yue/template.html b/themes/yue/template.html new file mode 100644 index 0000000..c4a2190 --- /dev/null +++ b/themes/yue/template.html @@ -0,0 +1,39 @@ + + + + {{NAME}} :: viewdocs.io + + + + +
+
+
+

{{NAME}} :: index

+
+
+ {{CONTENT}} +
+
+
+ + + + + + diff --git a/viewdocs.go b/viewdocs.go index e762aa0..d1c569a 100644 --- a/viewdocs.go +++ b/viewdocs.go @@ -12,12 +12,14 @@ import ( "time" "code.google.com/p/vitess/go/cache" + "github.com/globocom/config" ) const CacheCapacity = 256 * 1024 * 1024 // 256MB const CacheTTL = 60 // raw.github.com cache TTL is ~120 +const DefaultTheme = "gistio" -var DefaultTemplate string +var Templates = make(map[string]string) type CacheValue struct { Value string @@ -53,22 +55,40 @@ func parseRequest(r *http.Request) (user, repo, ref, doc string, err error) { } func fetchAndRenderDoc(user, repo, ref, doc string) (string, error) { + rawRoot := "https://raw.github.com/" + user + "/" + repo + "/" + ref + resp, err := http.Get(rawRoot + "/docs/.viewdocs") + if err == nil { + body, err := ioutil.ReadAll(resp.Body) + resp.Body.Close() + if err == nil { + config.ReadConfigBytes(body) + } + } template := make(chan string) go func() { - resp, err := http.Get("https://raw.github.com/" + user + "/" + repo + "/" + ref + "/docs/template.html") + resp, err := http.Get(rawRoot + "/docs/template.html") if err != nil || resp.StatusCode == 404 { - template <- DefaultTemplate - return + t, err := config.GetString("theme") + if err != nil { + template <- Templates[DefaultTheme] + return + } + body, ok := Templates[t] + if ok { + template <- body + } else { + template <- Templates[DefaultTheme] + } } body, err := ioutil.ReadAll(resp.Body) resp.Body.Close() if err != nil { - template <- DefaultTemplate + template <- Templates[DefaultTheme] return } template <- string(body) }() - resp, err := http.Get("https://raw.github.com/" + user + "/" + repo + "/" + ref + "/docs/" + doc + ".md") + resp, err = http.Get(rawRoot + "/docs/" + doc + ".md") if err != nil { return "", err } @@ -125,16 +145,21 @@ func main() { lru := cache.NewLRUCache(CacheCapacity) - resp, err := http.Get("https://raw.github.com/progrium/viewdocs/master/docs/template.html") - if err != nil || resp.StatusCode == 404 { - log.Fatal("Unable to fetch default template") - } - body, err := ioutil.ReadAll(resp.Body) - resp.Body.Close() - if err != nil { - log.Fatal(err) + for _, t := range []string{ + "gistio", + "yue", + } { + resp, err := http.Get("https://raw.github.com/progrium/viewdocs/master/themes/" + t + "/template.html") + if err != nil || resp.StatusCode == 404 { + log.Fatal("Unable to fetch " + t + " theme") + } + body, err := ioutil.ReadAll(resp.Body) + resp.Body.Close() + if err != nil { + log.Fatal(err) + } + Templates[t] = string(body) } - DefaultTemplate = string(body) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if r.RequestURI == "/" {