diff --git a/go.mod b/go.mod index 1fdb6ba..a2f9a94 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815 github.com/go-swagger/go-swagger v0.25.0 github.com/golang/gddo v0.0.0-20200831202555-721e228c7686 // indirect + github.com/gomarkdown/markdown v0.0.0-20200824053859-8c8b3816f167 github.com/gorilla/mux v1.8.0 github.com/h2non/filetype v1.1.0 github.com/remyoudompheng/go-liblzma v0.0.0-20190506200333-81bf2d431b96 diff --git a/go.sum b/go.sum index b0ee0d4..2311a7b 100644 --- a/go.sum +++ b/go.sum @@ -222,6 +222,8 @@ github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QD github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/snappy v0.0.0-20170215233205-553a64147049/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/gomarkdown/markdown v0.0.0-20200824053859-8c8b3816f167 h1:LP/6EfrZ/LyCc+SXvANDrIJ4sP9u2NAtqyv6QknetNQ= +github.com/gomarkdown/markdown v0.0.0-20200824053859-8c8b3816f167/go.mod h1:aii0r/K0ZnHv7G0KF7xy1v0A7s2Ljrb5byB7MO5p6TU= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.1.1-0.20171103154506-982329095285/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= @@ -518,6 +520,7 @@ go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= +golang.org/dl v0.0.0-20190829154251-82a15e2f2ead/go.mod h1:IUMfjQLJQd4UTqG1Z90tenwKoCX93Gn3MAQJMOSBsDQ= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/src/routes/artifact.go b/src/routes/artifact.go index 41e211b..8e2ca18 100644 --- a/src/routes/artifact.go +++ b/src/routes/artifact.go @@ -15,6 +15,10 @@ import ( "github.com/gorilla/mux" "sour.is/x/toolbox/httpsrv" "sour.is/x/toolbox/log" + + "github.com/gomarkdown/markdown" + "github.com/gomarkdown/markdown/html" + "github.com/gomarkdown/markdown/parser" ) func init() { @@ -78,7 +82,7 @@ func (a *Artifact) get(w http.ResponseWriter, r *http.Request) { f, err := os.Open(fname) if err != nil { - log.Fatal(err) + log.Error(err) } defer f.Close() @@ -107,7 +111,7 @@ func (a *Artifact) get(w http.ResponseWriter, r *http.Request) { } tr := tar.NewReader(rdr) - if path == "..." { + if path == "@" { var paths []string for { hdr, err := tr.Next() @@ -115,7 +119,7 @@ func (a *Artifact) get(w http.ResponseWriter, r *http.Request) { break // End of archive } if err != nil { - log.Fatal(err) + log.Error(err) } paths = append(paths, hdr.Name) } @@ -130,9 +134,37 @@ func (a *Artifact) get(w http.ResponseWriter, r *http.Request) { break // End of archive } if err != nil { - log.Fatal(err) + log.Error(err) } + if path == "~" && hdr.Name == "index.html" { + path = hdr.Name + } + if path == "~" && hdr.Name == "index.md" { + path = hdr.Name + } + if hdr.Name == path { + if strings.HasSuffix(hdr.Name, ".md") { + md, err := ioutil.ReadAll(tr) + if err != nil { + httpsrv.WriteError(w, http.StatusInternalServerError, err.Error()) + return + } + + w.Header().Set("Content-Type", "text/html") + extensions := parser.CommonExtensions | parser.AutoHeadingIDs + p := parser.NewWithExtensions(extensions) + + htmlFlags := html.CommonFlags | html.HrefTargetBlank + opts := html.RendererOptions{Flags: htmlFlags} + renderer := html.NewRenderer(opts) + + b := markdown.ToHTML(md, p, renderer) + w.Write(b) + w.WriteHeader(http.StatusOK) + return + } + pr := NewPreviewReader(tr) mime, err := ReadMIME(pr, hdr.Name) @@ -143,7 +175,7 @@ func (a *Artifact) get(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", mime) if _, err := io.Copy(w, pr.Drain()); err != nil { - log.Fatal(err) + log.Error(err) } w.WriteHeader(http.StatusOK) return