Skip to content

Commit d812b7e

Browse files
committed
Merge pull request #39 from josegonzalez/override-home-template
Allow customizing a project landing page
2 parents 316c107 + bef1329 commit d812b7e

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ You can show documentation for different [branches](http://inconshreveable.viewd
2222

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

25-
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.
25+
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. If you create a `home.html` template, this will be used for your project's landing page.
2626

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

viewdocs.go

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,28 +98,48 @@ func fixRelativeLinks(doc, repo, ref, body string) (string, error) {
9898
return b.String(), nil
9999
}
100100

101+
func fetchTemplate(template chan string, user string, repo string, ref string, name string) {
102+
log.Println("Fetching template")
103+
fetched := fetchUrl(template, "https://raw.github.com/" + user + "/" + repo + "/" + ref + "/docs/" + name + ".html")
104+
if !fetched && name != "template" {
105+
fetched = fetchUrl(template, "https://raw.github.com/" + user + "/" + repo + "/" + ref + "/docs/template.html")
106+
}
107+
108+
if !fetched {
109+
template <- DefaultTemplate
110+
}
111+
}
112+
113+
func fetchUrl(channel chan string, url string) bool {
114+
resp, err := http.Get(url)
115+
if err == nil && resp.StatusCode != 404 {
116+
body, err := ioutil.ReadAll(resp.Body)
117+
resp.Body.Close()
118+
if err == nil {
119+
channel <- string(body)
120+
return true
121+
}
122+
}
123+
124+
return false
125+
}
126+
101127
func fetchAndRenderDoc(user, repo, ref, doc string) (string, error) {
102128
template := make(chan string)
129+
templateName := "template"
103130
templateRecv := false
104131
defer func() {
105132
if !templateRecv {
106133
<-template
107134
}
108135
}()
109-
go func() {
110-
resp, err := http.Get("https://raw.github.com/" + user + "/" + repo + "/" + ref + "/docs/template.html")
111-
if err != nil || resp.StatusCode == 404 {
112-
template <- DefaultTemplate
113-
return
114-
}
115-
body, err := ioutil.ReadAll(resp.Body)
116-
resp.Body.Close()
117-
if err != nil {
118-
template <- DefaultTemplate
119-
return
120-
}
121-
template <- string(body)
122-
}()
136+
137+
if doc == "index.md" {
138+
templateName = "home"
139+
}
140+
141+
go fetchTemplate(template, user, repo, ref, templateName)
142+
123143
// https://github.com/github/markup/blob/master/lib/github/markups.rb#L1
124144
mdExts := map[string]bool{
125145
".md": true,

0 commit comments

Comments
 (0)