markdown works

This commit is contained in:
Ben Harris 2018-10-07 18:48:10 -04:00
parent cc91b77dec
commit c40c0c4745
Signed by untrusted user: ben
GPG Key ID: 4E0AF802FFF7960C
4 changed files with 47 additions and 20 deletions

View File

@ -4,7 +4,7 @@ import (
"flag"
"log"
"github.com/prologic/gopherproxy"
"tildegit.org/team/gopherproxy"
)
var (

View File

@ -1,7 +1,6 @@
package gopherproxy
import (
"bytes"
"fmt"
"html/template"
"io"
@ -13,8 +12,6 @@ import (
gopher "github.com/prologic/go-gopher"
"github.com/temoto/robotstxt"
"github.com/gomarkdown/markdown"
)
type Item struct {
@ -73,9 +70,10 @@ func renderDirectory(w http.ResponseWriter, tpl *template.Template, hostport str
}
return tpl.Execute(w, struct {
Title string
Lines []Item
}{title, out})
Title string
Lines []Item
Gophermap bool
}{title, out, true})
}
// GopherHandler returns a Handler that proxies requests
@ -127,18 +125,13 @@ func GopherHandler(tpl *template.Template, robotsdata *robotstxt.RobotsData, uri
}
if res.Body != nil {
// handle markdown
if strings.HasSuffix(uri, ".md") {
out := make([]Item, 1)
buf := new(bytes.Buffer)
buf.ReadFrom(res.Body)
out[0] = Item{
Text: string(markdown.ToHTML(buf.Bytes(), nil, nil)),
Type: "",
}
tpl.Execute(w, struct {
Title string
Lines []Item
}{uri, out})
Title string
MdText template.HTML
Gophermap bool
}{uri, renderMd(res.Body), false})
} else {
io.Copy(w, res.Body)
}

31
markdown.go Normal file
View File

@ -0,0 +1,31 @@
package gopherproxy
import (
"bytes"
"html/template"
"io"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/ast"
)
func renderHookImageResponsive(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
if _, ok := node.(*ast.Image); !ok {
return ast.GoToNext, false
}
container := node.AsContainer()
container.Classes = append(container.Classes, []byte("img-reponsive"))
return ast.GoToNext, true
}
func renderMd(b io.Reader) template.HTML {
buf := new(bytes.Buffer)
buf.ReadFrom(b)
// opts := html.RendererOptions{
// Flags: html.CommonFlags,
// RenderNodeHook: renderHookImageResponsive,
// }
// renderer := html.NewRenderer(opts)
// TODO: get the img-responsive thing to work
return template.HTML(string(markdown.ToHTML(buf.Bytes(), nil, nil)))
}

View File

@ -11,12 +11,14 @@ var tpltext = `<!doctype html>
<body>
<div class="container">
<input placeholder="url">
<section>
<pre>
{{if .Gophermap}}<pre>
{{range .Lines}} {{if .Link}}({{.Type}}) <a class="{{ .Type }}" href="{{.Link}}">{{.Text}}</a>{{else}} {{.Text}}{{end}}
{{end}}
</pre>
{{else}}
{{.MdText}}
{{end}}
</section>
</div>
@ -34,4 +36,5 @@ while (i--) {
</script>
</body>
</html>`
</html>
`