add .txt rendering in-template

This commit is contained in:
Ben Harris 2018-11-07 20:51:38 -05:00
parent c02423e084
commit d02b465035
Signed by untrusted user: ben
GPG Key ID: 4E0AF802FFF7960C
1 changed files with 12 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package gopherproxy
import (
"bytes"
"fmt"
"html/template"
"io"
@ -125,13 +126,23 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, uri
}
if res.Body != nil {
// handle markdown
if strings.HasSuffix(uri, ".md") {
// handle markdown
tpl.Execute(w, struct {
Title string
MdText template.HTML
Gophermap bool
}{uri, renderMd(res.Body), false})
} else if strings.HasSuffix(uri, ".txt") {
// handle .txt files
buf := new(bytes.Buffer)
buf.ReadFrom(res.Body)
tpl.Execute(w, struct {
Title string
MdText string
Gophermap bool
}{uri, buf.String(), false})
} else {
io.Copy(w, res.Body)
}