From 3bf64f4c091d702ddef6f1f67222a6b30505b07b Mon Sep 17 00:00:00 2001 From: Mark Steve Samson Date: Tue, 12 Nov 2013 18:12:51 +0000 Subject: [PATCH 01/10] Start templates support --- docs/template.html => templates/gistio.html | 0 viewdocs.go | 57 +++++++++++++++------ 2 files changed, 42 insertions(+), 15 deletions(-) rename docs/template.html => templates/gistio.html (100%) diff --git a/docs/template.html b/templates/gistio.html similarity index 100% rename from docs/template.html rename to templates/gistio.html diff --git a/viewdocs.go b/viewdocs.go index e762aa0..e44686b 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 DefaultTemplate = "gistio" -var DefaultTemplate string +var Templates = make(map[string]string) type CacheValue struct { Value string @@ -52,23 +54,44 @@ func parseRequest(r *http.Request) (user, repo, ref, doc string, err error) { return } +func setDefaultConfig() { + config.Set("template", "gistio") +} + func fetchAndRenderDoc(user, repo, ref, doc string) (string, error) { template := make(chan string) + rawRoot := "https://raw.github.com/" + user + "/" + repo + "/" + ref + resp, err := http.Get(rawRoot + "/docs/.viewdocs") + if err != nil || resp.StatusCode == 404 { + setDefaultConfig() + } else { + body, err := ioutil.ReadAll(resp.Body) + resp.Body.Close() + if err != nil { + setDefaultConfig() + } else { + config.ReadConfigBytes([]byte(body)) + } + } 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("template") + if err != nil { + template <- Templates[DefaultTemplate] + return + } + template <- Templates[t] } body, err := ioutil.ReadAll(resp.Body) resp.Body.Close() if err != nil { - template <- DefaultTemplate + template <- Templates[DefaultTemplate] 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 +148,20 @@ 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", + } { + resp, err := http.Get("https://raw.github.com/marksteve/viewdocs/master/templates/" + t + ".html") + if err != nil || resp.StatusCode == 404 { + log.Fatal("Unable to fetch " + t + " template") + } + 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 == "/" { From 2998aa1f4b3d30b951e1e87d99a5a1c24122780d Mon Sep 17 00:00:00 2001 From: Mark Steve Samson Date: Tue, 12 Nov 2013 18:33:05 +0000 Subject: [PATCH 02/10] Minor refactoring --- viewdocs.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/viewdocs.go b/viewdocs.go index e44686b..3d0560e 100644 --- a/viewdocs.go +++ b/viewdocs.go @@ -59,20 +59,16 @@ func setDefaultConfig() { } func fetchAndRenderDoc(user, repo, ref, doc string) (string, error) { - template := make(chan string) rawRoot := "https://raw.github.com/" + user + "/" + repo + "/" + ref resp, err := http.Get(rawRoot + "/docs/.viewdocs") - if err != nil || resp.StatusCode == 404 { - setDefaultConfig() - } else { + if err == nil { body, err := ioutil.ReadAll(resp.Body) resp.Body.Close() - if err != nil { - setDefaultConfig() - } else { - config.ReadConfigBytes([]byte(body)) + if err == nil { + config.ReadConfigBytes(body) } } + template := make(chan string) go func() { resp, err := http.Get(rawRoot + "/docs/template.html") if err != nil || resp.StatusCode == 404 { From df73776913fe5a601f52cd6bcb3c5b89143876b7 Mon Sep 17 00:00:00 2001 From: Mark Steve Samson Date: Tue, 12 Nov 2013 18:33:44 +0000 Subject: [PATCH 03/10] Use default template when given invalid template --- viewdocs.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/viewdocs.go b/viewdocs.go index 3d0560e..cbc50d7 100644 --- a/viewdocs.go +++ b/viewdocs.go @@ -77,7 +77,12 @@ func fetchAndRenderDoc(user, repo, ref, doc string) (string, error) { template <- Templates[DefaultTemplate] return } - template <- Templates[t] + body, ok := Templates[t] + if ok { + template <- body + } else { + template <- Templates[DefaultTemplate] + } } body, err := ioutil.ReadAll(resp.Body) resp.Body.Close() From 513df3447371a20923c9c188246fdfdd656cf3a9 Mon Sep 17 00:00:00 2001 From: Mark Steve Samson Date: Tue, 12 Nov 2013 18:40:12 +0000 Subject: [PATCH 04/10] Add yue template --- templates/yue.html | 34 ++++++++++++++++++++++++++++++++++ viewdocs.go | 5 +---- 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 templates/yue.html diff --git a/templates/yue.html b/templates/yue.html new file mode 100644 index 0000000..7c35d4e --- /dev/null +++ b/templates/yue.html @@ -0,0 +1,34 @@ + + + + {{NAME}} :: viewdocs.io + + + + + + + +
+
+

{{NAME}} :: index

+
+
+ {{CONTENT}} +
+
+ + + + + + diff --git a/viewdocs.go b/viewdocs.go index cbc50d7..769caca 100644 --- a/viewdocs.go +++ b/viewdocs.go @@ -54,10 +54,6 @@ func parseRequest(r *http.Request) (user, repo, ref, doc string, err error) { return } -func setDefaultConfig() { - config.Set("template", "gistio") -} - func fetchAndRenderDoc(user, repo, ref, doc string) (string, error) { rawRoot := "https://raw.github.com/" + user + "/" + repo + "/" + ref resp, err := http.Get(rawRoot + "/docs/.viewdocs") @@ -151,6 +147,7 @@ func main() { for _, t := range []string{ "gistio", + "yue", } { resp, err := http.Get("https://raw.github.com/marksteve/viewdocs/master/templates/" + t + ".html") if err != nil || resp.StatusCode == 404 { From a2b17831fedefafe29a8b1b321ec3fe7e267d150 Mon Sep 17 00:00:00 2001 From: Mark Steve Samson Date: Wed, 13 Nov 2013 02:42:26 +0000 Subject: [PATCH 05/10] templates -> themes --- templates/gistio.html => themes/gistio/template.html | 0 templates/yue.html => themes/yue/template.html | 0 viewdocs.go | 12 ++++++------ 3 files changed, 6 insertions(+), 6 deletions(-) rename templates/gistio.html => themes/gistio/template.html (100%) rename templates/yue.html => themes/yue/template.html (100%) diff --git a/templates/gistio.html b/themes/gistio/template.html similarity index 100% rename from templates/gistio.html rename to themes/gistio/template.html diff --git a/templates/yue.html b/themes/yue/template.html similarity index 100% rename from templates/yue.html rename to themes/yue/template.html diff --git a/viewdocs.go b/viewdocs.go index 769caca..ecee0f3 100644 --- a/viewdocs.go +++ b/viewdocs.go @@ -17,7 +17,7 @@ import ( const CacheCapacity = 256 * 1024 * 1024 // 256MB const CacheTTL = 60 // raw.github.com cache TTL is ~120 -const DefaultTemplate = "gistio" +const DefaultTheme = "gistio" var Templates = make(map[string]string) @@ -68,22 +68,22 @@ func fetchAndRenderDoc(user, repo, ref, doc string) (string, error) { go func() { resp, err := http.Get(rawRoot + "/docs/template.html") if err != nil || resp.StatusCode == 404 { - t, err := config.GetString("template") + t, err := config.GetString("theme") if err != nil { - template <- Templates[DefaultTemplate] + template <- Templates[DefaultTheme] return } body, ok := Templates[t] if ok { template <- body } else { - template <- Templates[DefaultTemplate] + template <- Templates[DefaultTheme] } } body, err := ioutil.ReadAll(resp.Body) resp.Body.Close() if err != nil { - template <- Templates[DefaultTemplate] + template <- Templates[DefaultTheme] return } template <- string(body) @@ -149,7 +149,7 @@ func main() { "gistio", "yue", } { - resp, err := http.Get("https://raw.github.com/marksteve/viewdocs/master/templates/" + t + ".html") + resp, err := http.Get("https://raw.github.com/marksteve/viewdocs/master/themes/" + t + "/template.html") if err != nil || resp.StatusCode == 404 { log.Fatal("Unable to fetch " + t + " template") } From e88f10e5bdfe5042afb8dc70c40175e5cf274d1d Mon Sep 17 00:00:00 2001 From: Mark Steve Samson Date: Wed, 13 Nov 2013 03:08:42 +0000 Subject: [PATCH 06/10] Update yue template --- themes/yue/template.html | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/themes/yue/template.html b/themes/yue/template.html index 7c35d4e..9005f6b 100644 --- a/themes/yue/template.html +++ b/themes/yue/template.html @@ -2,20 +2,23 @@ {{NAME}} :: viewdocs.io - - - -
+
-

{{NAME}} :: index

+

{{NAME}} :: index

-
+
{{CONTENT}}
From 50113086c8e36cb51aa4045ca1c9f14a238f97c9 Mon Sep 17 00:00:00 2001 From: Mark Steve Samson Date: Wed, 13 Nov 2013 03:09:29 +0000 Subject: [PATCH 07/10] Remove useless style in yue theme --- themes/yue/template.html | 1 - 1 file changed, 1 deletion(-) diff --git a/themes/yue/template.html b/themes/yue/template.html index 9005f6b..3773d13 100644 --- a/themes/yue/template.html +++ b/themes/yue/template.html @@ -8,7 +8,6 @@ margin: 0 1em; } header h1 { - font-size: 1em; font-weight: normal; } From 565db4e5bbd20eba0bdf171123e10e0c99d96207 Mon Sep 17 00:00:00 2001 From: Mark Steve Samson Date: Wed, 13 Nov 2013 03:14:03 +0000 Subject: [PATCH 08/10] Use fixed width in yue theme --- themes/yue/template.html | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/themes/yue/template.html b/themes/yue/template.html index 3773d13..c4a2190 100644 --- a/themes/yue/template.html +++ b/themes/yue/template.html @@ -4,23 +4,26 @@ {{NAME}} :: viewdocs.io - -
-
-

{{NAME}} :: index

-
-
- {{CONTENT}} -
-
+ +
+
+
+

{{NAME}} :: index

+
+
+ {{CONTENT}} +
+
+