change to index handling and initial markdown support
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user