Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cbd287e20e | ||
|
|
479caef353 | ||
|
|
8fefc60f6b | ||
|
|
014076dd06 | ||
|
|
d408229ba9 | ||
|
|
47d6c37819 | ||
|
|
1eb8fa7223 | ||
|
|
f9c28b3c45 | ||
|
|
a26d34c2b3 | ||
|
|
4d076ede3d | ||
|
|
f60d0618e6 | ||
|
|
b975a7c103 | ||
|
|
0e9150b8f6 | ||
|
|
4fdbee6df5 | ||
|
|
e6a126ea0b |
@@ -102,9 +102,22 @@ const defaultWrapper = `
|
|||||||
font-size: 125%;
|
font-size: 125%;
|
||||||
background: #3b242b;
|
background: #3b242b;
|
||||||
margin-top: 0;
|
margin-top: 0;
|
||||||
|
margin-bottom: 0;
|
||||||
|
height: calc(100vh - 1em);
|
||||||
|
overflow: auto;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
}
|
}
|
||||||
body > form > textarea {
|
body > .form {
|
||||||
margin-top: 2.5em;
|
flex-grow: 2;
|
||||||
|
}
|
||||||
|
body > .form > form {
|
||||||
|
height: 100%;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
body > .form > form > textarea {
|
||||||
|
flex-grow: 2;
|
||||||
}
|
}
|
||||||
rendered > h2:nth-child(2) {
|
rendered > h2:nth-child(2) {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
@@ -135,10 +148,11 @@ const defaultWrapper = `
|
|||||||
margin: 0;
|
margin: 0;
|
||||||
margin-bottom: .5em;
|
margin-bottom: .5em;
|
||||||
padding: .3em 0 .3em 0;
|
padding: .3em 0 .3em 0;
|
||||||
position: sticky;
|
|
||||||
top: 0;
|
|
||||||
background-color: #3b242b;
|
background-color: #3b242b;
|
||||||
z-index: 999;
|
z-index: 999;
|
||||||
|
position: -webkit-sticky;
|
||||||
|
position: sticky;
|
||||||
|
top: 0;
|
||||||
}
|
}
|
||||||
h1:hover > .comment,
|
h1:hover > .comment,
|
||||||
h2:hover > .comment,
|
h2:hover > .comment,
|
||||||
@@ -152,9 +166,17 @@ const defaultWrapper = `
|
|||||||
.comment:focus-within {
|
.comment:focus-within {
|
||||||
display: inline-flex;
|
display: inline-flex;
|
||||||
}
|
}
|
||||||
|
.attachments > details > form {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
.attachments > details > form > input:first-of-type {
|
||||||
|
flex-grow: 2;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</header>
|
</header>
|
||||||
<body height="100%">
|
<body>
|
||||||
{{{}}}
|
{{{}}}
|
||||||
</body>
|
</body>
|
||||||
<footer>
|
<footer>
|
||||||
|
|||||||
@@ -34,6 +34,9 @@ func (n *Notes) Comment(urlPath string, lineno int, comment string) error {
|
|||||||
writer := io.Writer(f2)
|
writer := io.Writer(f2)
|
||||||
for i := 0; i < lineno+1; i++ {
|
for i := 0; i < lineno+1; i++ {
|
||||||
line, _, err := reader.ReadLine()
|
line, _, err := reader.ReadLine()
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -46,14 +49,13 @@ func (n *Notes) Comment(urlPath string, lineno int, comment string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
formatted := "\n"
|
formatted := fmt.Sprintf("> *%s*\n\n", comment)
|
||||||
formatted += fmt.Sprintf("> *%s*\n", comment)
|
|
||||||
_, err = io.Copy(writer, strings.NewReader(formatted))
|
_, err = io.Copy(writer, strings.NewReader(formatted))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = io.Copy(writer, reader)
|
_, err = io.Copy(writer, reader)
|
||||||
if err != nil {
|
if err != nil && err != io.EOF {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
f2.Close()
|
f2.Close()
|
||||||
|
|||||||
@@ -26,11 +26,13 @@ func editFile(p filetree.Path) string {
|
|||||||
}
|
}
|
||||||
b, _ := ioutil.ReadFile(p.Local)
|
b, _ := ioutil.ReadFile(p.Local)
|
||||||
return fmt.Sprintf(`
|
return fmt.Sprintf(`
|
||||||
<form action="/submit/%s" method="post" style="width:100%%; height: 90%%">
|
<div class="form">
|
||||||
<table style="width:100%%; height: 90%%">
|
<form action="/submit/%s" method="post">
|
||||||
<textarea name="content" style="width:100%%; min-height:90%%; cursor:crosshair;">%s</textarea>
|
<table>
|
||||||
|
<textarea name="content" style="cursor:crosshair;">%s</textarea>
|
||||||
</table>
|
</table>
|
||||||
<button type="submit">Submit</button>
|
<button type="submit">Submit</button>
|
||||||
</form>
|
</form>
|
||||||
|
</div>
|
||||||
`, href, b)
|
`, href, b)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,15 +8,16 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"local/notes-server/filetree"
|
"local/notes-server/filetree"
|
||||||
"log"
|
"local/notes-server/notes/md"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gomarkdown/markdown"
|
"github.com/fairlyblank/md2min"
|
||||||
"github.com/gomarkdown/markdown/ast"
|
"github.com/gomarkdown/markdown/ast"
|
||||||
"github.com/gomarkdown/markdown/html"
|
"github.com/gomarkdown/markdown/html"
|
||||||
"github.com/gomarkdown/markdown/parser"
|
"github.com/yuin/goldmark"
|
||||||
|
blackfriday "gopkg.in/russross/blackfriday.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (n *Notes) File(urlPath string) (string, error) {
|
func (n *Notes) File(urlPath string) (string, error) {
|
||||||
@@ -25,13 +26,37 @@ func (n *Notes) File(urlPath string) (string, error) {
|
|||||||
return "", errors.New("path is dir")
|
return "", errors.New("path is dir")
|
||||||
}
|
}
|
||||||
b, _ := ioutil.ReadFile(p.Local)
|
b, _ := ioutil.ReadFile(p.Local)
|
||||||
renderer := html.NewRenderer(html.RendererOptions{
|
return n.gomarkdown(urlPath, b)
|
||||||
Flags: html.CommonFlags | html.TOC,
|
return n.blackfriday(urlPath, b)
|
||||||
RenderNodeHook: n.commentFormer(urlPath, b),
|
return n.goldmark(urlPath, b)
|
||||||
|
return n.md2min(urlPath, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Notes) blackfriday(urlPath string, b []byte) (string, error) {
|
||||||
|
renderer := blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
|
||||||
|
Flags: blackfriday.TOC | blackfriday.Smartypants | blackfriday.SmartypantsFractions | blackfriday.SmartypantsDashes | blackfriday.SmartypantsQuotesNBSP | blackfriday.CompletePage,
|
||||||
})
|
})
|
||||||
parser := parser.NewWithExtensions(parser.CommonExtensions | parser.HeadingIDs | parser.AutoHeadingIDs | parser.Titleblock)
|
return string(blackfriday.Run(
|
||||||
content := markdown.ToHTML(b, parser, renderer)
|
b,
|
||||||
return string(content) + "\n", nil
|
blackfriday.WithRenderer(renderer),
|
||||||
|
)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Notes) goldmark(urlPath string, b []byte) (string, error) {
|
||||||
|
buff := bytes.NewBuffer(nil)
|
||||||
|
err := goldmark.Convert(b, buff)
|
||||||
|
return string(buff.Bytes()), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Notes) md2min(urlPath string, b []byte) (string, error) {
|
||||||
|
mdc := md2min.New("h1")
|
||||||
|
buff := bytes.NewBuffer(nil)
|
||||||
|
err := mdc.Parse(b, buff)
|
||||||
|
return string(buff.Bytes()), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Notes) gomarkdown(urlPath string, b []byte) (string, error) {
|
||||||
|
return md.Gomarkdown(b, n.commentFormer(urlPath, b))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Notes) commentFormer(urlPath string, md []byte) html.RenderNodeFunc {
|
func (n *Notes) commentFormer(urlPath string, md []byte) html.RenderNodeFunc {
|
||||||
@@ -42,18 +67,21 @@ func (n *Notes) commentFormer(urlPath string, md []byte) html.RenderNodeFunc {
|
|||||||
nextHeader := func() {
|
nextHeader := func() {
|
||||||
cur++
|
cur++
|
||||||
for cur < len(lines) {
|
for cur < len(lines) {
|
||||||
if bytes.Contains(lines[cur], []byte("```")) {
|
for _, opener_closer := range [][]string{{"```", "```"}, {`<summary>`, `</summary>`}} {
|
||||||
|
if bytes.Contains(lines[cur], []byte(opener_closer[0])) {
|
||||||
cur++
|
cur++
|
||||||
for cur < len(lines) && !bytes.Contains(lines[cur], []byte("```")) {
|
for cur < len(lines) && !bytes.Contains(lines[cur], []byte(opener_closer[1])) {
|
||||||
cur++
|
cur++
|
||||||
}
|
}
|
||||||
cur++
|
cur++
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if cur >= len(lines) {
|
if cur >= len(lines) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
line := lines[cur]
|
line := lines[cur]
|
||||||
if ok, err := regexp.Match(`^\s*#+\s*[^\s]+\s*$`, line); err != nil {
|
ok, err := regexp.Match(`^\s*#+\s*.+$`, line)
|
||||||
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
} else if ok {
|
} else if ok {
|
||||||
return
|
return
|
||||||
@@ -62,8 +90,7 @@ func (n *Notes) commentFormer(urlPath string, md []byte) html.RenderNodeFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
|
return func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
|
||||||
if heading, ok := node.(*ast.Heading); ok && !entering {
|
if heading, ok := node.(*ast.Heading); !n.RO && ok && !entering {
|
||||||
log.Printf("%+v", heading)
|
|
||||||
nextHeader()
|
nextHeader()
|
||||||
fmt.Fprintf(w, `
|
fmt.Fprintf(w, `
|
||||||
<form method="POST" action=%q class="comment">
|
<form method="POST" action=%q class="comment">
|
||||||
|
|||||||
35
notes/md/file.go
Executable file
35
notes/md/file.go
Executable file
@@ -0,0 +1,35 @@
|
|||||||
|
package md
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gomarkdown/markdown"
|
||||||
|
"github.com/gomarkdown/markdown/html"
|
||||||
|
"github.com/gomarkdown/markdown/parser"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Gomarkdown(b []byte, renderHook html.RenderNodeFunc) (string, error) {
|
||||||
|
renderer := html.NewRenderer(html.RendererOptions{
|
||||||
|
Flags: html.CommonFlags | html.TOC,
|
||||||
|
RenderNodeHook: renderHook,
|
||||||
|
})
|
||||||
|
ext := parser.NoExtensions
|
||||||
|
for _, extension := range []parser.Extensions{
|
||||||
|
parser.NoIntraEmphasis,
|
||||||
|
parser.Tables,
|
||||||
|
parser.FencedCode,
|
||||||
|
parser.Autolink,
|
||||||
|
parser.Strikethrough,
|
||||||
|
parser.SpaceHeadings,
|
||||||
|
parser.HeadingIDs,
|
||||||
|
parser.BackslashLineBreak,
|
||||||
|
parser.DefinitionLists,
|
||||||
|
parser.MathJax,
|
||||||
|
parser.Titleblock,
|
||||||
|
parser.AutoHeadingIDs,
|
||||||
|
parser.Includes,
|
||||||
|
} {
|
||||||
|
ext |= extension
|
||||||
|
}
|
||||||
|
parser := parser.NewWithExtensions(ext)
|
||||||
|
content := markdown.ToHTML(b, parser, renderer)
|
||||||
|
return string(content) + "\n", nil
|
||||||
|
}
|
||||||
@@ -4,10 +4,12 @@ import "local/notes-server/config"
|
|||||||
|
|
||||||
type Notes struct {
|
type Notes struct {
|
||||||
root string
|
root string
|
||||||
|
RO bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *Notes {
|
func New() *Notes {
|
||||||
return &Notes{
|
return &Notes{
|
||||||
root: config.Root,
|
root: config.Root,
|
||||||
|
RO: config.ReadOnly,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
package notes
|
|
||||||
|
|
||||||
import (
|
|
||||||
"html"
|
|
||||||
"local/notes-server/filetree"
|
|
||||||
"net/http"
|
|
||||||
"path"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (n *Notes) Create(w http.ResponseWriter, r *http.Request) {
|
|
||||||
content := r.FormValue("base")
|
|
||||||
content = html.UnescapeString(content)
|
|
||||||
content = strings.ReplaceAll(content, "\r", "")
|
|
||||||
urlPath := path.Join(r.URL.Path, content)
|
|
||||||
p := filetree.NewPathFromURL(urlPath)
|
|
||||||
if p.IsDir() {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
url := *r.URL
|
|
||||||
url.Path = path.Join("/edit/", p.BaseHREF)
|
|
||||||
http.Redirect(w, r, url.String(), http.StatusSeeOther)
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
package notes
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
package notes
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"path"
|
|
||||||
)
|
|
||||||
|
|
||||||
func notesDir(p Path, w http.ResponseWriter, r *http.Request) {
|
|
||||||
dirs, files := lsDir(p)
|
|
||||||
content := dirs.List()
|
|
||||||
notesDirHead(p, w)
|
|
||||||
block(content, w)
|
|
||||||
fmt.Fprintln(w, files.List())
|
|
||||||
}
|
|
||||||
|
|
||||||
func notesDirHead(p Path, w http.ResponseWriter) {
|
|
||||||
fmt.Fprintf(w, `
|
|
||||||
<form action=%q method="get">
|
|
||||||
<input type="text" name="base"></input>
|
|
||||||
<button type="submit">Create</button>
|
|
||||||
</form>
|
|
||||||
`, path.Join("/create/", p.BaseHREF))
|
|
||||||
}
|
|
||||||
|
|
||||||
func lsDir(path Path) (Paths, Paths) {
|
|
||||||
dirs := newDirs()
|
|
||||||
files := newFiles()
|
|
||||||
|
|
||||||
found, _ := ioutil.ReadDir(path.Local)
|
|
||||||
for _, f := range found {
|
|
||||||
dirs.Push(path, f)
|
|
||||||
files.Push(path, f)
|
|
||||||
}
|
|
||||||
return Paths(*dirs), Paths(*files)
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
package notes
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestLsDir(t *testing.T) {
|
|
||||||
p := Path{Local: "/usr/local"}
|
|
||||||
dirs, files := lsDir(p)
|
|
||||||
if len(dirs) == 0 {
|
|
||||||
t.Fatal(len(dirs))
|
|
||||||
}
|
|
||||||
if len(files) == 0 {
|
|
||||||
t.Fatal(len(files))
|
|
||||||
}
|
|
||||||
t.Log(dirs)
|
|
||||||
t.Log(files)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNotesDir(t *testing.T) {
|
|
||||||
path := Path{Local: "/usr/local"}
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
notesDir(path, w, nil)
|
|
||||||
t.Logf("%s", w.Body.Bytes())
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
package notes
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (s *Server) edit(w http.ResponseWriter, r *http.Request) {
|
|
||||||
p := NewPathFromURL(r.URL.Path)
|
|
||||||
if p.IsDir() {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
head(w, r)
|
|
||||||
editHead(w, p)
|
|
||||||
editFile(w, p)
|
|
||||||
foot(w, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func editHead(w http.ResponseWriter, p Path) {
|
|
||||||
fmt.Fprintln(w, h2(p.MultiLink()))
|
|
||||||
}
|
|
||||||
|
|
||||||
func editFile(w http.ResponseWriter, p Path) {
|
|
||||||
href := p.HREF
|
|
||||||
href = strings.TrimPrefix(href, "/")
|
|
||||||
hrefs := strings.SplitN(href, "/", 2)
|
|
||||||
href = hrefs[0]
|
|
||||||
if len(hrefs) > 1 {
|
|
||||||
href = hrefs[1]
|
|
||||||
}
|
|
||||||
b, _ := ioutil.ReadFile(p.Local)
|
|
||||||
fmt.Fprintf(w, `
|
|
||||||
<form action="/submit/%s" method="post" style="width:100%%; height: 90%%">
|
|
||||||
<table style="width:100%%; height: 90%%">
|
|
||||||
<textarea name="content" style="width:100%%; min-height:90%%">%s</textarea>
|
|
||||||
</table>
|
|
||||||
<button type="submit">Submit</button>
|
|
||||||
</form>
|
|
||||||
`, href, b)
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
package notes
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
package notes
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"path"
|
|
||||||
|
|
||||||
"github.com/gomarkdown/markdown"
|
|
||||||
"github.com/gomarkdown/markdown/html"
|
|
||||||
"github.com/gomarkdown/markdown/parser"
|
|
||||||
)
|
|
||||||
|
|
||||||
func notesFile(p Path, w http.ResponseWriter, r *http.Request) {
|
|
||||||
b, _ := ioutil.ReadFile(p.Local)
|
|
||||||
notesFileHead(p, w)
|
|
||||||
renderer := html.NewRenderer(html.RendererOptions{
|
|
||||||
Flags: html.CommonFlags | html.TOC,
|
|
||||||
})
|
|
||||||
parser := parser.NewWithExtensions(parser.CommonExtensions | parser.HeadingIDs | parser.AutoHeadingIDs | parser.Titleblock)
|
|
||||||
content := markdown.ToHTML(b, parser, renderer)
|
|
||||||
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))
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
package notes
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http/httptest"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestNotesFile(t *testing.T) {
|
|
||||||
f, err := ioutil.TempFile(os.TempDir(), "until*")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer os.Remove(f.Name())
|
|
||||||
fmt.Fprintln(f, `
|
|
||||||
# Hello
|
|
||||||
## World
|
|
||||||
* This
|
|
||||||
* is
|
|
||||||
* bullets
|
|
||||||
|
|
||||||
| My | table | goes |
|
|
||||||
|----|-------|------|
|
|
||||||
| h | e | n |
|
|
||||||
|
|
||||||
`)
|
|
||||||
f.Close()
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
p := Path{Local: f.Name()}
|
|
||||||
notesFile(p, w, nil)
|
|
||||||
s := string(w.Body.Bytes())
|
|
||||||
shouldContain := []string{
|
|
||||||
"tbody",
|
|
||||||
"h1",
|
|
||||||
"h2",
|
|
||||||
}
|
|
||||||
for _, should := range shouldContain {
|
|
||||||
if !strings.Contains(s, should) {
|
|
||||||
t.Fatalf("%s: %s", should, s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
t.Logf("%s", s)
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
package notes
|
|
||||||
|
|
||||||
import "local/notes-server/config"
|
|
||||||
|
|
||||||
type Notes struct {
|
|
||||||
root string
|
|
||||||
}
|
|
||||||
|
|
||||||
func New() *Notes {
|
|
||||||
return &Notes{
|
|
||||||
root: config.Root,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
package notes
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (s *Server) notes(w http.ResponseWriter, r *http.Request) {
|
|
||||||
p := NewPathFromURL(r.URL.Path)
|
|
||||||
if p.IsDir() {
|
|
||||||
head(w, r)
|
|
||||||
notesHead(w, p)
|
|
||||||
notesDir(p, w, r)
|
|
||||||
foot(w, r)
|
|
||||||
} else if p.IsFile() {
|
|
||||||
head(w, r)
|
|
||||||
notesHead(w, p)
|
|
||||||
notesFile(p, w, r)
|
|
||||||
foot(w, r)
|
|
||||||
} else {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func notesHead(w http.ResponseWriter, p Path) {
|
|
||||||
fmt.Fprintln(w, h2(p.MultiLink()))
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
package notes
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
package notes
|
|
||||||
|
|
||||||
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)
|
|
||||||
os.MkdirAll(path.Dir(p.Local), os.ModePerm)
|
|
||||||
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 +0,0 @@
|
|||||||
package notes
|
|
||||||
48
server/attach.go
Executable file
48
server/attach.go
Executable file
@@ -0,0 +1,48 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"local/notes-server/filetree"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Server) attach(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if err := s._attach(w, r); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) _attach(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
r.ParseMultipartForm(100 << 20)
|
||||||
|
file, handler, err := r.FormFile("file")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
ft := filetree.NewPathFromURL(r.URL.Path)
|
||||||
|
local := ft.Local
|
||||||
|
target := path.Join(path.Dir(local), "."+path.Base(local)+".attachments", handler.Filename)
|
||||||
|
|
||||||
|
os.MkdirAll(path.Dir(target), os.ModePerm)
|
||||||
|
if fi, err := os.Stat(target); err != nil && !os.IsNotExist(err) {
|
||||||
|
return err
|
||||||
|
} else if err == nil && fi.IsDir() {
|
||||||
|
return errors.New("invalid path")
|
||||||
|
}
|
||||||
|
f, err := os.Create(target)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
if _, err := io.Copy(f, file); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Redirect(w, r, "/notes"+strings.TrimPrefix(ft.HREF, "/attach"), http.StatusSeeOther)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -3,7 +3,6 @@ package server
|
|||||||
import (
|
import (
|
||||||
"html"
|
"html"
|
||||||
"local/notes-server/filetree"
|
"local/notes-server/filetree"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -11,7 +10,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (s *Server) comment(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) comment(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println("COMMAND", r.Method, r.FormValue("lineno"), r.FormValue("content"))
|
|
||||||
if r.Method != "POST" {
|
if r.Method != "POST" {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import (
|
|||||||
"local/notes-server/filetree"
|
"local/notes-server/filetree"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Server) notes(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) notes(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -49,13 +51,15 @@ func (s *Server) file(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fileHead(w, filetree.NewPathFromURL(r.URL.Path).BaseHREF)
|
s.fileHead(w, r.URL.Path)
|
||||||
fmt.Fprintln(w, file)
|
fmt.Fprintln(w, file)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileHead(w http.ResponseWriter, baseHREF string) {
|
func (s *Server) fileHead(w http.ResponseWriter, path string) {
|
||||||
|
baseHREF := filetree.NewPathFromURL(path).BaseHREF
|
||||||
htmlEdit(w, baseHREF)
|
htmlEdit(w, baseHREF)
|
||||||
htmlDelete(w, baseHREF)
|
htmlDelete(w, baseHREF)
|
||||||
|
s.htmlAttachments(w, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func htmlEdit(w http.ResponseWriter, baseHREF string) {
|
func htmlEdit(w http.ResponseWriter, baseHREF string) {
|
||||||
@@ -76,6 +80,40 @@ func htmlDelete(w http.ResponseWriter, baseHREF string) {
|
|||||||
</div><br>`, path.Join("/delete/", baseHREF))
|
</div><br>`, path.Join("/delete/", baseHREF))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) htmlAttachments(w http.ResponseWriter, urlPath string) {
|
||||||
|
dir := path.Dir(urlPath)
|
||||||
|
f := "." + path.Base(urlPath) + ".attachments"
|
||||||
|
_, files, _ := s.Notes.Dir(path.Join(dir, f))
|
||||||
|
// TODO replace <a with <a download UNLESS img, then... hrm
|
||||||
|
form := fmt.Sprintf(`
|
||||||
|
<form enctype="multipart/form-data" action="/attach/%s" method="post">
|
||||||
|
<input type="file" name="file" required/>
|
||||||
|
<input type="submit" value="+"/>
|
||||||
|
</form>
|
||||||
|
`, strings.TrimPrefix(urlPath, "/notes/"))
|
||||||
|
if config.ReadOnly {
|
||||||
|
form = ""
|
||||||
|
}
|
||||||
|
lines := strings.Split(files, "\n")
|
||||||
|
for i := range lines {
|
||||||
|
pattern := regexp.MustCompile(`\.(png|jpg|jpeg|gif)">`)
|
||||||
|
if !pattern.MatchString(lines[i]) {
|
||||||
|
lines[i] = strings.ReplaceAll(lines[i], "<a", "<a download")
|
||||||
|
}
|
||||||
|
lines[i] = strings.ReplaceAll(lines[i], `href="/notes`, `href="/raw`)
|
||||||
|
}
|
||||||
|
files = strings.Join(lines, "\n")
|
||||||
|
fmt.Fprintf(w, `<div style='display:inline-block' class="attachments">
|
||||||
|
<details>
|
||||||
|
<summary style="display: flex">
|
||||||
|
<span style="flex-grow: 2">Attachments</span>
|
||||||
|
</summary>
|
||||||
|
%s
|
||||||
|
%s
|
||||||
|
</details>
|
||||||
|
</div><br>`, form, files)
|
||||||
|
}
|
||||||
|
|
||||||
func htmlCreate(w http.ResponseWriter, baseHREF string) {
|
func htmlCreate(w http.ResponseWriter, baseHREF string) {
|
||||||
if config.ReadOnly {
|
if config.ReadOnly {
|
||||||
return
|
return
|
||||||
|
|||||||
33
server/raw.go
Executable file
33
server/raw.go
Executable file
@@ -0,0 +1,33 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"local/notes-server/filetree"
|
||||||
|
"local/simpleserve/simpleserve"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *Server) raw(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if err := s._raw(w, r); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) _raw(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
simpleserve.SetContentTypeIfMedia(w, r)
|
||||||
|
p := filetree.NewPathFromURL(r.URL.Path)
|
||||||
|
if !p.IsFile() {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
f, err := os.Open(p.Local)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
if _, err := io.Copy(w, f); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -18,6 +18,9 @@ func (s *Server) Routes() error {
|
|||||||
"/": {
|
"/": {
|
||||||
handler: s.root,
|
handler: s.root,
|
||||||
},
|
},
|
||||||
|
fmt.Sprintf("raw/%s%s", wildcard, wildcard): {
|
||||||
|
handler: s.gzip(s.authenticate(s.raw)),
|
||||||
|
},
|
||||||
fmt.Sprintf("notes/%s%s", wildcard, wildcard): {
|
fmt.Sprintf("notes/%s%s", wildcard, wildcard): {
|
||||||
handler: s.gzip(s.authenticate(s.notes)),
|
handler: s.gzip(s.authenticate(s.notes)),
|
||||||
},
|
},
|
||||||
@@ -33,6 +36,9 @@ func (s *Server) Routes() error {
|
|||||||
fmt.Sprintf("create/%s%s", wildcard, wildcard): {
|
fmt.Sprintf("create/%s%s", wildcard, wildcard): {
|
||||||
handler: s.gzip(s.authenticate(s.create)),
|
handler: s.gzip(s.authenticate(s.create)),
|
||||||
},
|
},
|
||||||
|
fmt.Sprintf("attach/%s%s", wildcard, wildcard): {
|
||||||
|
handler: s.gzip(s.authenticate(s.attach)),
|
||||||
|
},
|
||||||
fmt.Sprintf("comment/%s%s", wildcard, wildcard): {
|
fmt.Sprintf("comment/%s%s", wildcard, wildcard): {
|
||||||
handler: s.gzip(s.authenticate(s.comment)),
|
handler: s.gzip(s.authenticate(s.comment)),
|
||||||
},
|
},
|
||||||
@@ -43,7 +49,13 @@ func (s *Server) Routes() error {
|
|||||||
|
|
||||||
for path, endpoint := range endpoints {
|
for path, endpoint := range endpoints {
|
||||||
if config.ReadOnly {
|
if config.ReadOnly {
|
||||||
for _, prefix := range []string{"edit/", "delete/", "delete/", "create/", "submit/"} {
|
for _, prefix := range []string{
|
||||||
|
"edit/",
|
||||||
|
"delete/",
|
||||||
|
"create/",
|
||||||
|
"submit/",
|
||||||
|
"attach/",
|
||||||
|
} {
|
||||||
if strings.HasPrefix(path, prefix) {
|
if strings.HasPrefix(path, prefix) {
|
||||||
endpoint.handler = http.NotFound
|
endpoint.handler = http.NotFound
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user