Edit and submit pages but need to create new pages
This commit is contained in:
@@ -4,12 +4,20 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"path"
|
||||
|
||||
"github.com/gomarkdown/markdown"
|
||||
)
|
||||
|
||||
func notesFile(path Path, w http.ResponseWriter, r *http.Request) {
|
||||
b, _ := ioutil.ReadFile(path.Local)
|
||||
func notesFile(p Path, w http.ResponseWriter, r *http.Request) {
|
||||
b, _ := ioutil.ReadFile(p.Local)
|
||||
notesFileHead(p, w)
|
||||
content := markdown.ToHTML(b, nil, nil)
|
||||
fmt.Fprintf(w, "%s\n", content)
|
||||
}
|
||||
|
||||
func notesFileHead(p Path, w http.ResponseWriter) {
|
||||
fmt.Fprintf(w, `
|
||||
<a href=%q><input type="button" value="Edit"></input></a>
|
||||
`, path.Join("/edit/", p.BaseHREF))
|
||||
}
|
||||
|
||||
@@ -9,9 +9,10 @@ import (
|
||||
)
|
||||
|
||||
type Path struct {
|
||||
Local string
|
||||
HREF string
|
||||
Base string
|
||||
Local string
|
||||
HREF string
|
||||
Base string
|
||||
BaseHREF string
|
||||
}
|
||||
|
||||
func NewPathFromLocal(p string) Path {
|
||||
@@ -34,21 +35,25 @@ func NewPathFromURL(p string) Path {
|
||||
if len(locals) > 1 {
|
||||
local = locals[1]
|
||||
}
|
||||
baseHREF := local
|
||||
local = path.Join(config.Root, local)
|
||||
if strings.Trim(p, "/") == base {
|
||||
local = config.Root
|
||||
baseHREF = "/"
|
||||
}
|
||||
return Path{
|
||||
Base: base,
|
||||
HREF: href,
|
||||
Local: local,
|
||||
Base: base,
|
||||
HREF: href,
|
||||
Local: local,
|
||||
BaseHREF: baseHREF,
|
||||
}
|
||||
}
|
||||
|
||||
func (p Path) MultiLink() string {
|
||||
href := p.HREF
|
||||
href := p.BaseHREF
|
||||
href = strings.TrimPrefix(href, "/")
|
||||
href = strings.TrimSuffix(href, "/")
|
||||
href = path.Join("notes/", href)
|
||||
segments := strings.Split(href, "/")
|
||||
full := ""
|
||||
for i := range segments {
|
||||
|
||||
@@ -20,6 +20,10 @@ func (s *Server) Routes() error {
|
||||
path: fmt.Sprintf("edit/%s%s", wildcard, wildcard),
|
||||
handler: s.edit,
|
||||
},
|
||||
{
|
||||
path: fmt.Sprintf("submit/%s%s", wildcard, wildcard),
|
||||
handler: s.submit,
|
||||
},
|
||||
}
|
||||
|
||||
for _, endpoint := range endpoints {
|
||||
|
||||
30
server/submit.go
Normal file
30
server/submit.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Server) submit(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
content := r.FormValue("content")
|
||||
content = html.UnescapeString(content)
|
||||
content = strings.ReplaceAll(content, "\r", "")
|
||||
p := NewPathFromURL(r.URL.Path)
|
||||
if err := ioutil.WriteFile(p.Local, []byte(content), os.ModePerm); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintln(w, err)
|
||||
} else {
|
||||
url := *r.URL
|
||||
url.Path = path.Join("/notes/", p.BaseHREF)
|
||||
http.Redirect(w, r, url.String(), http.StatusSeeOther)
|
||||
}
|
||||
}
|
||||
1
server/submit_test.go
Normal file
1
server/submit_test.go
Normal file
@@ -0,0 +1 @@
|
||||
package server
|
||||
Reference in New Issue
Block a user