Edit and submit pages but need to create new pages

master
bel 2019-10-20 00:48:52 +00:00
parent 7f25341906
commit 4729dc8c1a
6 changed files with 62 additions and 10 deletions

View File

@ -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))
}

View File

@ -12,6 +12,7 @@ type Path struct {
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,
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 {

View File

@ -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
View 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
View File

@ -0,0 +1 @@
package server

View File

@ -1,6 +1,10 @@
<html>
<header>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css -->
<style>
@charset "UTF-8";body{font-family:system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Fira Sans,Droid Sans,Helvetica Neue,sans-serif;line-height:1.4;max-width:800px;margin:20px auto;padding:0 10px;color:#dbdbdb;background:#202b38;text-rendering:optimizeLegibility}button,input,textarea{transition:background-color .1s linear,border-color .1s linear,color .1s linear,box-shadow .1s linear,transform .1s ease}h1{font-size:2.2em;margin-top:0}h1,h2,h3,h4,h5,h6{margin-bottom:12px}h1,h2,h3,h4,h5,h6,strong{color:#fff}b,h1,h2,h3,h4,h5,h6,strong,th{font-weight:600}blockquote{border-left:4px solid rgba(0,150,191,.67);margin:1.5em 0;padding:.5em 1em;font-style:italic}blockquote>footer{margin-top:10px;font-style:normal}address,blockquote cite{font-style:normal}a[href^=mailto]:before{content:"📧 "}a[href^=tel]:before{content:"📞 "}a[href^=sms]:before{content:"💬 "}button,input[type=button],input[type=checkbox],input[type=submit]{cursor:pointer}input:not([type=checkbox]):not([type=radio]),select{display:block}button,input,select,textarea{color:#fff;background-color:#161f27;font-family:inherit;font-size:inherit;margin-right:6px;margin-bottom:6px;padding:10px;border:none;border-radius:6px;outline:none}button,input:not([type=checkbox]):not([type=radio]),select,textarea{-webkit-appearance:none}textarea{margin-right:0;width:100%;box-sizing:border-box;resize:vertical}button,input[type=button],input[type=submit]{padding-right:30px;padding-left:30px}button:hover,input[type=button]:hover,input[type=submit]:hover{background:#324759}button:focus,input:focus,select:focus,textarea:focus{box-shadow:0 0 0 2px rgba(0,150,191,.67)}button:active,input[type=button]:active,input[type=checkbox]:active,input[type=radio]:active,input[type=submit]:active{transform:translateY(2px)}button:disabled,input:disabled,select:disabled,textarea:disabled{cursor:not-allowed;opacity:.5}::-webkit-input-placeholder{color:#a9a9a9}:-ms-input-placeholder{color:#a9a9a9}::-ms-input-placeholder{color:#a9a9a9}::placeholder{color:#a9a9a9}a{text-decoration:none;color:#41adff}a:hover{text-decoration:underline}code,kbd{background:#161f27;color:#ffbe85;padding:5px;border-radius:6px}pre>code{padding:10px;display:block;overflow-x:auto}img{max-width:100%}hr{border:none;border-top:1px solid #dbdbdb}table{border-collapse:collapse;margin-bottom:10px;width:100%}td,th{padding:6px;text-align:left}th{border-bottom:1px solid #dbdbdb}tbody tr:nth-child(2n){background-color:#161f27}::-webkit-scrollbar{height:10px;width:10px}::-webkit-scrollbar-track{background:#161f27;border-radius:6px}::-webkit-scrollbar-thumb{background:#324759;border-radius:6px}::-webkit-scrollbar-thumb:hover{background:#415c73}
</style>
</header>
<body height="100%">
{{{}}}