Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ You can show documentation for different [branches](http://inconshreveable.viewd

http://<github-username>.viewdocs.io/<repository-name>~<refname>

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).

<br />
Enjoy!<br />
Expand Down
File renamed without changes.
39 changes: 39 additions & 0 deletions themes/yue/template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!doctype html>
<head>
<meta charset="utf-8">
<title>{{NAME}} :: viewdocs.io</title>
<link rel="stylesheet" href="https://rawgithub.com/lepture/yue.css/master/yue.css">
<style type="text/css">
.yue {
width: 80em;
margin: 0 auto;
}
.yue header h1 {
font-weight: normal;
}
</style>
</head>
<body>
<div class="yue">
<section>
<header>
<h1><a href="https://github.com/{{USER}}/{{NAME}}">{{NAME}}</a> :: <a href="/{{NAME}}">index</a></h1>
</header>
<article class="instapaper_body">
{{CONTENT}}
</article>
</section>
</div>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-6824126-17', 'viewdocs.io');
ga('send', 'pageview');
</script>
<script src="//static.getclicky.com/js" type="text/javascript"></script>
<script type="text/javascript">try{ clicky.init(100672863); }catch(e){}</script>
<noscript><p><img alt="Clicky" width="1" height="1" src="//in.getclicky.com/100672863ns.gif" /></p></noscript>
</body>
</html>
55 changes: 40 additions & 15 deletions viewdocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 == "/" {
Expand Down