Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
43d44a4518 | ||
|
|
8bf7503c8e | ||
|
|
785215bd3c | ||
|
|
952e04815a | ||
|
|
d6c95a536c | ||
|
|
9785ef5e1e | ||
|
|
3b7aa70e44 | ||
|
|
c067b426a9 | ||
|
|
da968c2fd4 | ||
|
|
563eb7bb61 | ||
|
|
b7f13bf33d | ||
|
|
d73cbe9e0c | ||
|
|
f582410c40 | ||
|
|
f53fc80f68 | ||
|
|
16335d796b | ||
|
|
a7df97aae5 | ||
|
|
4a7efd4016 | ||
|
|
5be556a4cf | ||
|
|
72894cd5cc |
@@ -1,8 +1,11 @@
|
|||||||
FROM frolvlad/alpine-glibc:alpine-3.9_glibc-2.29
|
|
||||||
RUN apk update && apk add --no-cache ca-certificates git
|
|
||||||
|
|
||||||
|
FROM golang:1.13-alpine as certs
|
||||||
|
RUN apk update && apk add --no-cache ca-certificates
|
||||||
|
|
||||||
|
FROM busybox:glibc
|
||||||
RUN mkdir -p /var/log
|
RUN mkdir -p /var/log
|
||||||
WORKDIR /main
|
WORKDIR /main
|
||||||
|
COPY --from=certs /etc/ssl/certs /etc/ssl/certs
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
@@ -10,3 +13,4 @@ ENV GOPATH=""
|
|||||||
ENV MNT="/mnt/"
|
ENV MNT="/mnt/"
|
||||||
ENTRYPOINT ["/main/exec-notes-server"]
|
ENTRYPOINT ["/main/exec-notes-server"]
|
||||||
CMD []
|
CMD []
|
||||||
|
|
||||||
|
|||||||
118
config/config.go
118
config/config.go
File diff suppressed because one or more lines are too long
129
config/rotate.py
Executable file
129
config/rotate.py
Executable file
@@ -0,0 +1,129 @@
|
|||||||
|
#! /usr/local/bin/python3
|
||||||
|
|
||||||
|
def main(args) :
|
||||||
|
print("args", args)
|
||||||
|
for i in args :
|
||||||
|
rotate(i)
|
||||||
|
|
||||||
|
def rotate(x) :
|
||||||
|
print("input: {}", x)
|
||||||
|
rgb = hex_to_rgb(x)
|
||||||
|
print("rgb: {}", rgb)
|
||||||
|
hsl = rgb_to_hsl(rgb)
|
||||||
|
print("hsl: {}", hsl)
|
||||||
|
import os
|
||||||
|
env = os.environ
|
||||||
|
if "DEG" in env :
|
||||||
|
deg = int(env["DEG"])
|
||||||
|
else :
|
||||||
|
deg = 110
|
||||||
|
hsl = rotate_hsl(hsl, deg)
|
||||||
|
print("hsl': {}", hsl)
|
||||||
|
rgb = hsl_to_rgb(hsl)
|
||||||
|
print("rgb': {}", rgb)
|
||||||
|
print(rgb_to_hex(rgb))
|
||||||
|
|
||||||
|
def hex_to_rgb(x) :
|
||||||
|
if x.startswith("#") :
|
||||||
|
x = x[1:]
|
||||||
|
r = x[0:2]
|
||||||
|
g = x[2:4]
|
||||||
|
b = x[4:6]
|
||||||
|
return (
|
||||||
|
hex_to_dec(r),
|
||||||
|
hex_to_dec(g),
|
||||||
|
hex_to_dec(b),
|
||||||
|
)
|
||||||
|
|
||||||
|
def hex_to_dec(x) :
|
||||||
|
s = 0
|
||||||
|
mul = 1
|
||||||
|
for i in range(len(x)):
|
||||||
|
c = x[len(x)-i-1]
|
||||||
|
c = c.upper()
|
||||||
|
digit = ord(c) - ord('0')
|
||||||
|
if not c.isdigit() :
|
||||||
|
digit = ord(c) - ord('A') + 10
|
||||||
|
s += mul * digit
|
||||||
|
mul *= 16
|
||||||
|
return s
|
||||||
|
|
||||||
|
def rgb_to_hsl(rgb) :
|
||||||
|
return (
|
||||||
|
compute_h(rgb),
|
||||||
|
compute_s(rgb),
|
||||||
|
compute_l(rgb),
|
||||||
|
)
|
||||||
|
|
||||||
|
def compute_h(rgb) :
|
||||||
|
r, g, b, cmax, cmin, delta = compute_hsl_const(rgb)
|
||||||
|
if delta == 0 :
|
||||||
|
return 0
|
||||||
|
if r == cmax :
|
||||||
|
return 60 * ( ( (g - b)/delta ) % 6)
|
||||||
|
if g == cmax :
|
||||||
|
return 60 * ( ( (b - r)/delta ) + 2)
|
||||||
|
if b == cmax :
|
||||||
|
return 60 * ( ( (r - g)/delta ) + 4)
|
||||||
|
|
||||||
|
def compute_s(rgb) :
|
||||||
|
r, g, b, cmax, cmin, delta = compute_hsl_const(rgb)
|
||||||
|
if delta == 0 :
|
||||||
|
return 0
|
||||||
|
return delta / ( 1 - (abs(2*compute_l(rgb)-1)) )
|
||||||
|
|
||||||
|
def compute_l(rgb) :
|
||||||
|
r, g, b, cmax, cmin, delta = compute_hsl_const(rgb)
|
||||||
|
return (cmax + cmin) / 2
|
||||||
|
|
||||||
|
def compute_hsl_const(rgb) :
|
||||||
|
rgb = [ i/255.0 for i in rgb ]
|
||||||
|
return rgb[:] + [max(rgb), min(rgb), max(rgb)-min(rgb)]
|
||||||
|
|
||||||
|
def rotate_hsl(hsl, deg) :
|
||||||
|
return (
|
||||||
|
(hsl[0] + deg) % 360,
|
||||||
|
hsl[1],
|
||||||
|
hsl[2],
|
||||||
|
)
|
||||||
|
|
||||||
|
def hsl_to_rgb(hsl) :
|
||||||
|
h = hsl[0]
|
||||||
|
s = hsl[1]
|
||||||
|
l = hsl[2]
|
||||||
|
|
||||||
|
c = s * (1 - abs(2 * l))
|
||||||
|
x = c * (1 - abs((h / 60) % 2 - 1))
|
||||||
|
m = l - (c / 2)
|
||||||
|
|
||||||
|
rgbp = ()
|
||||||
|
if h < 60 :
|
||||||
|
rgbp = (c, x, 0)
|
||||||
|
if h < 120 :
|
||||||
|
rgbp = (x, c, 0)
|
||||||
|
if h < 180 :
|
||||||
|
rgbp = (0, c, x)
|
||||||
|
if h < 240 :
|
||||||
|
rgbp = (0, x, c)
|
||||||
|
if h < 300 :
|
||||||
|
rgbp = (x, 0, c)
|
||||||
|
else:
|
||||||
|
rgbp = (c, 0, x)
|
||||||
|
|
||||||
|
r = rgbp[0]
|
||||||
|
g = rgbp[1]
|
||||||
|
b = rgbp[2]
|
||||||
|
return (
|
||||||
|
(r+m)*255,
|
||||||
|
(g+m)*255,
|
||||||
|
(b+m)*255,
|
||||||
|
)
|
||||||
|
|
||||||
|
def rgb_to_hex(rgb) :
|
||||||
|
r = min(max(int(rgb[0]), 0), 255)
|
||||||
|
g = min(max(int(rgb[1]), 0), 255)
|
||||||
|
b = min(max(int(rgb[2]), 0), 255)
|
||||||
|
return "#{:02x}{:02x}{:02x}".format(r, g, b)
|
||||||
|
|
||||||
|
from sys import argv
|
||||||
|
main(argv[1:])
|
||||||
1
config/water.css
Submodule
1
config/water.css
Submodule
Submodule config/water.css added at 576eee5b82
@@ -20,9 +20,20 @@ func NewPathFromLocal(p string) Path {
|
|||||||
if strings.HasPrefix(root, "./") {
|
if strings.HasPrefix(root, "./") {
|
||||||
root = root[2:]
|
root = root[2:]
|
||||||
}
|
}
|
||||||
|
if strings.HasSuffix(root, "/") {
|
||||||
|
root = root[:len(root)-1]
|
||||||
|
}
|
||||||
splits := strings.SplitN(p, root, 2)
|
splits := strings.SplitN(p, root, 2)
|
||||||
|
for len(splits) > 0 && splits[0] == "" {
|
||||||
|
splits = splits[1:]
|
||||||
|
}
|
||||||
|
if len(splits) == 0 {
|
||||||
|
splits = []string{"", ""}
|
||||||
|
}
|
||||||
href := splits[0]
|
href := splits[0]
|
||||||
if len(splits) > 1 && (splits[0] == "" || splits[0] == "/") {
|
if len(splits) == 1 && (splits[0] == root || splits[0] == config.Root) {
|
||||||
|
href = ""
|
||||||
|
} else if splits[0] == "" || splits[0] == "/" {
|
||||||
href = splits[1]
|
href = splits[1]
|
||||||
}
|
}
|
||||||
href = path.Join("/notes", href)
|
href = path.Join("/notes", href)
|
||||||
|
|||||||
61
notes/comment.go
Executable file
61
notes/comment.go
Executable file
@@ -0,0 +1,61 @@
|
|||||||
|
package notes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"local/notes-server/filetree"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (n *Notes) Comment(urlPath string, lineno int, comment string) error {
|
||||||
|
p := filetree.NewPathFromURL(urlPath)
|
||||||
|
if stat, err := os.Stat(p.Local); err != nil {
|
||||||
|
return errors.New("cannot comment as it does not exist")
|
||||||
|
} else if stat.IsDir() {
|
||||||
|
return errors.New("cannot comment on a dir")
|
||||||
|
}
|
||||||
|
f, err := os.Open(p.Local)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
f2, err := ioutil.TempFile(os.TempDir(), path.Base(p.Local)+".*")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f2.Close()
|
||||||
|
reader := bufio.NewReader(f)
|
||||||
|
writer := io.Writer(f2)
|
||||||
|
for i := 0; i < lineno+1; i++ {
|
||||||
|
line, _, err := reader.ReadLine()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = io.Copy(writer, bytes.NewReader(line))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = writer.Write([]byte("\n"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
formatted := "\n"
|
||||||
|
formatted += fmt.Sprintf("> *%s*\n", comment)
|
||||||
|
_, err = io.Copy(writer, strings.NewReader(formatted))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = io.Copy(writer, reader)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
f2.Close()
|
||||||
|
return os.Rename(f2.Name(), p.Local)
|
||||||
|
}
|
||||||
56
notes/comment_test.go
Normal file
56
notes/comment_test.go
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package notes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io/ioutil"
|
||||||
|
"local/notes-server/config"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestComment(t *testing.T) {
|
||||||
|
d, err := ioutil.TempDir(os.TempDir(), "testComment.*")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(d)
|
||||||
|
f, err := ioutil.TempFile(d, "testFile.*")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
f.Write([]byte(`
|
||||||
|
hello
|
||||||
|
world
|
||||||
|
# i have a heading
|
||||||
|
## i have a subber heading
|
||||||
|
### i have a[heading with](http://google.com) a hyperlink
|
||||||
|
## *I think this heading is in italics*
|
||||||
|
`))
|
||||||
|
f.Close()
|
||||||
|
fpath := path.Join("/comment", strings.TrimPrefix(f.Name(), d))
|
||||||
|
config.Root = d
|
||||||
|
n := &Notes{}
|
||||||
|
t.Logf("d=%s, fpath=%s", d, fpath)
|
||||||
|
|
||||||
|
if err := n.Comment("/comment/a", 5, "a"); err == nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := n.Comment(fpath, -1, "illegal line no"); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := n.Comment(fpath, 10000, "big line no"); err == nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := n.Comment(fpath, 0, "first_line_OK"); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else if b, err := ioutil.ReadFile(f.Name()); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else if !bytes.Contains(b, []byte("> *first_line_OK*\n")) {
|
||||||
|
t.Errorf("%s", b)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,15 +1,11 @@
|
|||||||
package notes
|
package notes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"local/notes-server/filetree"
|
"local/notes-server/filetree"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (n *Notes) Delete(urlPath string) error {
|
func (n *Notes) Delete(urlPath string) error {
|
||||||
p := filetree.NewPathFromURL(urlPath)
|
p := filetree.NewPathFromURL(urlPath)
|
||||||
if p.IsDir() {
|
|
||||||
return errors.New("path is dir")
|
|
||||||
}
|
|
||||||
return os.Remove(p.Local)
|
return os.Remove(p.Local)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,4 +17,27 @@ func TestDelete(t *testing.T) {
|
|||||||
if _, err := os.Stat("/tmp/a"); err == nil {
|
if _, err := os.Stat("/tmp/a"); err == nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
d, err := ioutil.TempDir(os.TempDir(), "trydel*")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
f, err := ioutil.TempFile(d, "file*")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
f.Close()
|
||||||
|
}
|
||||||
|
if err := n.Delete(d); err == nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
e, err := ioutil.TempDir(os.TempDir(), "trydel*")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := n.Delete(e); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ func editFile(p filetree.Path) string {
|
|||||||
return fmt.Sprintf(`
|
return fmt.Sprintf(`
|
||||||
<form action="/submit/%s" method="post" style="width:100%%; height: 90%%">
|
<form action="/submit/%s" method="post" style="width:100%%; height: 90%%">
|
||||||
<table style="width:100%%; height: 90%%">
|
<table style="width:100%%; height: 90%%">
|
||||||
<textarea name="content" style="width:100%%; min-height:90%%">%s</textarea>
|
<textarea name="content" style="width:100%%; min-height:90%%; cursor:crosshair;">%s</textarea>
|
||||||
</table>
|
</table>
|
||||||
<button type="submit">Submit</button>
|
<button type="submit">Submit</button>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
@@ -1,11 +1,20 @@
|
|||||||
package notes
|
package notes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"local/notes-server/filetree"
|
"local/notes-server/filetree"
|
||||||
|
"log"
|
||||||
|
"path"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gomarkdown/markdown"
|
"github.com/gomarkdown/markdown"
|
||||||
|
"github.com/gomarkdown/markdown/ast"
|
||||||
"github.com/gomarkdown/markdown/html"
|
"github.com/gomarkdown/markdown/html"
|
||||||
"github.com/gomarkdown/markdown/parser"
|
"github.com/gomarkdown/markdown/parser"
|
||||||
)
|
)
|
||||||
@@ -18,8 +27,80 @@ func (n *Notes) File(urlPath string) (string, error) {
|
|||||||
b, _ := ioutil.ReadFile(p.Local)
|
b, _ := ioutil.ReadFile(p.Local)
|
||||||
renderer := html.NewRenderer(html.RendererOptions{
|
renderer := html.NewRenderer(html.RendererOptions{
|
||||||
Flags: html.CommonFlags | html.TOC,
|
Flags: html.CommonFlags | html.TOC,
|
||||||
|
RenderNodeHook: n.commentFormer(urlPath, b),
|
||||||
})
|
})
|
||||||
parser := parser.NewWithExtensions(parser.CommonExtensions | parser.HeadingIDs | parser.AutoHeadingIDs | parser.Titleblock)
|
parser := parser.NewWithExtensions(parser.CommonExtensions | parser.HeadingIDs | parser.AutoHeadingIDs | parser.Titleblock)
|
||||||
content := markdown.ToHTML(b, parser, renderer)
|
content := markdown.ToHTML(b, parser, renderer)
|
||||||
return string(content) + "\n", nil
|
return string(content) + "\n", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *Notes) commentFormer(urlPath string, md []byte) html.RenderNodeFunc {
|
||||||
|
urlPath = strings.TrimPrefix(urlPath, "/")
|
||||||
|
urlPath = strings.TrimPrefix(urlPath, strings.Split(urlPath, "/")[0])
|
||||||
|
lines := bytes.Split(md, []byte("\n"))
|
||||||
|
cur := -1
|
||||||
|
nextHeader := func() {
|
||||||
|
cur++
|
||||||
|
for cur < len(lines) {
|
||||||
|
if bytes.Contains(lines[cur], []byte("```")) {
|
||||||
|
cur++
|
||||||
|
for cur < len(lines) && !bytes.Contains(lines[cur], []byte("```")) {
|
||||||
|
cur++
|
||||||
|
}
|
||||||
|
cur++
|
||||||
|
}
|
||||||
|
if cur >= len(lines) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
line := lines[cur]
|
||||||
|
if ok, err := regexp.Match(`^\s*#+\s*[^\s]+\s*$`, line); err != nil {
|
||||||
|
panic(err)
|
||||||
|
} else if ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cur++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
|
||||||
|
if heading, ok := node.(*ast.Heading); ok && !entering {
|
||||||
|
log.Printf("%+v", heading)
|
||||||
|
nextHeader()
|
||||||
|
fmt.Fprintf(w, `
|
||||||
|
<form method="POST" action=%q class="comment">
|
||||||
|
<input name="lineno" type="number" style="display:none" value="%d"/>
|
||||||
|
<input autocomplete="off" name="content" type="text"/>
|
||||||
|
<input type="submit"/>
|
||||||
|
</form>
|
||||||
|
`, path.Join("/comment", urlPath)+"#"+heading.HeadingID, cur)
|
||||||
|
}
|
||||||
|
return ast.GoToNext, false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Notes) commentFormerOld() html.RenderNodeFunc {
|
||||||
|
return func(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
|
||||||
|
if heading, ok := node.(*ast.Heading); ok {
|
||||||
|
if !entering {
|
||||||
|
literal := ""
|
||||||
|
ast.WalkFunc(heading, func(n ast.Node, e bool) ast.WalkStatus {
|
||||||
|
if leaf := n.AsLeaf(); e && leaf != nil {
|
||||||
|
if literal != "" {
|
||||||
|
literal += ".*"
|
||||||
|
}
|
||||||
|
literal += string(leaf.Literal)
|
||||||
|
}
|
||||||
|
return ast.GoToNext
|
||||||
|
})
|
||||||
|
level := heading.Level
|
||||||
|
id := base64.URLEncoding.EncodeToString([]byte(fmt.Sprintf(`^[ \t]*%s\s*%s(].*)?\s*$`, strings.Repeat("#", level), literal)))
|
||||||
|
fmt.Fprintf(w, `
|
||||||
|
<form method="POST" target="/comment" name="%s" class="comment">
|
||||||
|
<input autocomplete="off" name="content" type="text"/>
|
||||||
|
<input type="submit"/>
|
||||||
|
</form>
|
||||||
|
`, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ast.GoToNext, false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,17 +2,57 @@ package notes
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"errors"
|
||||||
"local/notes-server/filetree"
|
"local/notes-server/filetree"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type searcher struct {
|
||||||
|
patterns []*regexp.Regexp
|
||||||
|
}
|
||||||
|
|
||||||
|
func newSearcher(phrase string) (*searcher, error) {
|
||||||
|
phrases := strings.Split(phrase, " ")
|
||||||
|
patterns := make([]*regexp.Regexp, 0)
|
||||||
|
for _, phrase := range phrases {
|
||||||
|
if len(phrase) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
pattern, err := regexp.Compile("(?i)" + phrase)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
patterns = append(patterns, pattern)
|
||||||
|
}
|
||||||
|
if len(patterns) < 1 {
|
||||||
|
return nil, errors.New("no search specified")
|
||||||
|
}
|
||||||
|
return &searcher{
|
||||||
|
patterns: patterns,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *searcher) matches(input []byte) bool {
|
||||||
|
for _, pattern := range s.patterns {
|
||||||
|
if !pattern.Match(input) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (n *Notes) Search(phrase string) (string, error) {
|
func (n *Notes) Search(phrase string) (string, error) {
|
||||||
|
searcher, err := newSearcher(phrase)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
files := filetree.NewFiles()
|
files := filetree.NewFiles()
|
||||||
err := filepath.Walk(n.root,
|
err = filepath.Walk(n.root,
|
||||||
func(walked string, info os.FileInfo, err error) error {
|
func(walked string, info os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -23,7 +63,7 @@ func (n *Notes) Search(phrase string) (string, error) {
|
|||||||
if size := info.Size(); size < 1 || size > (5*1024*1024) {
|
if size := info.Size(); size < 1 || size > (5*1024*1024) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
ok, err := grepFile(walked, []byte(phrase))
|
ok, err := grepFile(walked, searcher)
|
||||||
if err != nil && err.Error() == "bufio.Scanner: token too long" {
|
if err != nil && err.Error() == "bufio.Scanner: token too long" {
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
@@ -40,7 +80,7 @@ func (n *Notes) Search(phrase string) (string, error) {
|
|||||||
return filetree.Paths(*files).List(true), err
|
return filetree.Paths(*files).List(true), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func grepFile(file string, phrase []byte) (bool, error) {
|
func grepFile(file string, searcher *searcher) (bool, error) {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
@@ -48,7 +88,7 @@ func grepFile(file string, phrase []byte) (bool, error) {
|
|||||||
defer f.Close()
|
defer f.Close()
|
||||||
scanner := bufio.NewScanner(f)
|
scanner := bufio.NewScanner(f)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
if bytes.Contains(scanner.Bytes(), phrase) {
|
if searcher.matches(scanner.Bytes()) {
|
||||||
return true, scanner.Err()
|
return true, scanner.Err()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1,3 @@
|
|||||||
hi
|
# Hello
|
||||||
|
|
||||||
|
## World
|
||||||
37
server/comment.go
Executable file
37
server/comment.go
Executable file
@@ -0,0 +1,37 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html"
|
||||||
|
"local/notes-server/filetree"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"path"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
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" {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
linenos := r.FormValue("lineno")
|
||||||
|
lineno, err := strconv.Atoi(linenos)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
comment := r.FormValue("content")
|
||||||
|
comment = html.UnescapeString(comment)
|
||||||
|
comment = strings.ReplaceAll(comment, "\r", "")
|
||||||
|
|
||||||
|
err = s.Notes.Comment(r.URL.Path, lineno, comment)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
url := *r.URL
|
||||||
|
url.Path = path.Join("/notes/", filetree.NewPathFromURL(r.URL.Path).BaseHREF)
|
||||||
|
http.Redirect(w, r, url.String(), http.StatusSeeOther)
|
||||||
|
}
|
||||||
@@ -22,8 +22,8 @@ func h1(content string) string {
|
|||||||
return h("1", content)
|
return h("1", content)
|
||||||
}
|
}
|
||||||
|
|
||||||
func h2(content string) string {
|
func h2(content string, style ...string) string {
|
||||||
return h("2", content)
|
return h("2", content, style...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func h3(content string) string {
|
func h3(content string) string {
|
||||||
@@ -38,6 +38,10 @@ func h5(content string) string {
|
|||||||
return h("5", content)
|
return h("5", content)
|
||||||
}
|
}
|
||||||
|
|
||||||
func h(level, content string) string {
|
func h(level, content string, style ...string) string {
|
||||||
return fmt.Sprintf("\n<h%s>\n%s\n</h%s>\n", level, content, level)
|
s := ""
|
||||||
|
if len(style) > 0 {
|
||||||
|
s = fmt.Sprintf("style=%q", style[0])
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("\n<h%s %s>\n%s\n</h%s>\n", level, s, content, level)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ func TestBlock(t *testing.T) {
|
|||||||
|
|
||||||
func TestH(t *testing.T) {
|
func TestH(t *testing.T) {
|
||||||
s := strings.ReplaceAll(strings.TrimSpace(h2("hi")), "\n", ".")
|
s := strings.ReplaceAll(strings.TrimSpace(h2("hi")), "\n", ".")
|
||||||
if ok, err := regexp.MatchString("<h2>.*hi.*<.h2>", s); err != nil {
|
if ok, err := regexp.MatchString("<h2[ ]*>.*hi.*<.h2>", s); err != nil {
|
||||||
t.Fatal(err, s)
|
t.Fatal(err, s)
|
||||||
} else if !ok {
|
} else if !ok {
|
||||||
t.Fatal(ok, s)
|
t.Fatal(ok, s)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"local/notes-server/config"
|
||||||
"local/notes-server/filetree"
|
"local/notes-server/filetree"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
@@ -23,12 +24,7 @@ func (s *Server) notes(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func notesHead(w http.ResponseWriter, p filetree.Path) {
|
func notesHead(w http.ResponseWriter, p filetree.Path) {
|
||||||
fmt.Fprintln(w, h2(p.MultiLink()))
|
fmt.Fprintln(w, h2(p.MultiLink()))
|
||||||
fmt.Fprintf(w, `
|
htmlSearch(w)
|
||||||
<form action=%q method="post">
|
|
||||||
<input type="text" name="keywords"></input>
|
|
||||||
<button type="submit">Search</button>
|
|
||||||
</form>
|
|
||||||
`, "/search")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) dir(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) dir(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -43,12 +39,8 @@ func (s *Server) dir(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func dirHead(w http.ResponseWriter, baseHREF string) {
|
func dirHead(w http.ResponseWriter, baseHREF string) {
|
||||||
fmt.Fprintf(w, `
|
htmlCreate(w, baseHREF)
|
||||||
<form action=%q method="get">
|
htmlDelete(w, baseHREF)
|
||||||
<input type="text" name="base"></input>
|
|
||||||
<button type="submit">Create</button>
|
|
||||||
</form>
|
|
||||||
`, path.Join("/create/", baseHREF))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) file(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) file(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -62,10 +54,45 @@ func (s *Server) file(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func fileHead(w http.ResponseWriter, baseHREF string) {
|
func fileHead(w http.ResponseWriter, baseHREF string) {
|
||||||
fmt.Fprintf(w, `
|
htmlEdit(w, baseHREF)
|
||||||
<a href=%q><input type="button" value="Edit"></input></a>
|
htmlDelete(w, baseHREF)
|
||||||
`, path.Join("/edit/", baseHREF))
|
}
|
||||||
fmt.Fprintf(w, `
|
|
||||||
<a href=%q><input type="button" value="Delete"></input></a>
|
func htmlEdit(w http.ResponseWriter, baseHREF string) {
|
||||||
`, path.Join("/delete/", baseHREF))
|
if config.ReadOnly {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, `<div style='display:inline-block'>
|
||||||
|
<a href=%q><input type="button" value="Edit"></input></a>
|
||||||
|
</div><br>`, path.Join("/edit/", baseHREF))
|
||||||
|
}
|
||||||
|
|
||||||
|
func htmlDelete(w http.ResponseWriter, baseHREF string) {
|
||||||
|
if config.ReadOnly {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, `<div style='display:inline-block'>
|
||||||
|
<a href=%q><input type="button" value="Delete" onclick="return confirm('Delete?');"></input></a>
|
||||||
|
</div><br>`, path.Join("/delete/", baseHREF))
|
||||||
|
}
|
||||||
|
|
||||||
|
func htmlCreate(w http.ResponseWriter, baseHREF string) {
|
||||||
|
if config.ReadOnly {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, `
|
||||||
|
<form action=%q method="get">
|
||||||
|
<input type="text" name="base"></input>
|
||||||
|
<button type="submit">Create</button>
|
||||||
|
</form>
|
||||||
|
`, path.Join("/create/", baseHREF))
|
||||||
|
}
|
||||||
|
|
||||||
|
func htmlSearch(w http.ResponseWriter) {
|
||||||
|
fmt.Fprintf(w, `
|
||||||
|
<form action=%q method="post" >
|
||||||
|
<input type="text" name="keywords"></input>
|
||||||
|
<button type="submit">Search</button>
|
||||||
|
</form>
|
||||||
|
`, "/search")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,48 +2,54 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"local/gziphttp"
|
||||||
|
"local/notes-server/config"
|
||||||
"local/router"
|
"local/router"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Server) Routes() error {
|
func (s *Server) Routes() error {
|
||||||
wildcard := router.Wildcard
|
wildcard := router.Wildcard
|
||||||
endpoints := []struct {
|
endpoints := map[string]struct {
|
||||||
path string
|
|
||||||
handler http.HandlerFunc
|
handler http.HandlerFunc
|
||||||
}{
|
}{
|
||||||
{
|
"/": {
|
||||||
path: "/",
|
|
||||||
handler: s.root,
|
handler: s.root,
|
||||||
},
|
},
|
||||||
{
|
fmt.Sprintf("notes/%s%s", wildcard, wildcard): {
|
||||||
path: fmt.Sprintf("notes/%s%s", wildcard, wildcard),
|
handler: s.gzip(s.authenticate(s.notes)),
|
||||||
handler: s.authenticate(s.notes),
|
|
||||||
},
|
},
|
||||||
{
|
fmt.Sprintf("edit/%s%s", wildcard, wildcard): {
|
||||||
path: fmt.Sprintf("edit/%s%s", wildcard, wildcard),
|
handler: s.gzip(s.authenticate(s.edit)),
|
||||||
handler: s.authenticate(s.edit),
|
|
||||||
},
|
},
|
||||||
{
|
fmt.Sprintf("delete/%s%s", wildcard, wildcard): {
|
||||||
path: fmt.Sprintf("delete/%s%s", wildcard, wildcard),
|
handler: s.gzip(s.authenticate(s.delete)),
|
||||||
handler: s.authenticate(s.delete),
|
|
||||||
},
|
},
|
||||||
{
|
fmt.Sprintf("submit/%s%s", wildcard, wildcard): {
|
||||||
path: fmt.Sprintf("submit/%s%s", wildcard, wildcard),
|
handler: s.gzip(s.authenticate(s.submit)),
|
||||||
handler: s.authenticate(s.submit),
|
|
||||||
},
|
},
|
||||||
{
|
fmt.Sprintf("create/%s%s", wildcard, wildcard): {
|
||||||
path: fmt.Sprintf("create/%s%s", wildcard, wildcard),
|
handler: s.gzip(s.authenticate(s.create)),
|
||||||
handler: s.authenticate(s.create),
|
|
||||||
},
|
},
|
||||||
{
|
fmt.Sprintf("comment/%s%s", wildcard, wildcard): {
|
||||||
path: fmt.Sprintf("search"),
|
handler: s.gzip(s.authenticate(s.comment)),
|
||||||
handler: s.authenticate(s.search),
|
},
|
||||||
|
fmt.Sprintf("search"): {
|
||||||
|
handler: s.gzip(s.authenticate(s.search)),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, endpoint := range endpoints {
|
for path, endpoint := range endpoints {
|
||||||
if err := s.Add(endpoint.path, endpoint.handler); err != nil {
|
if config.ReadOnly {
|
||||||
|
for _, prefix := range []string{"edit/", "delete/", "delete/", "create/", "submit/"} {
|
||||||
|
if strings.HasPrefix(path, prefix) {
|
||||||
|
endpoint.handler = http.NotFound
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := s.Add(path, endpoint.handler); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -54,3 +60,17 @@ func (s *Server) root(w http.ResponseWriter, r *http.Request) {
|
|||||||
r.URL.Path = "/notes"
|
r.URL.Path = "/notes"
|
||||||
http.Redirect(w, r, r.URL.String(), http.StatusPermanentRedirect)
|
http.Redirect(w, r, r.URL.String(), http.StatusPermanentRedirect)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) gzip(h http.HandlerFunc) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if gziphttp.Can(r) {
|
||||||
|
gz := gziphttp.New(w)
|
||||||
|
defer gz.Close()
|
||||||
|
w = gz
|
||||||
|
}
|
||||||
|
if filepath.Ext(r.URL.Path) == ".css" {
|
||||||
|
w.Header().Set("Content-Type", "text/css; charset=utf-8")
|
||||||
|
}
|
||||||
|
h(w, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ func (s *Server) search(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
head(w, r)
|
head(w, r)
|
||||||
fmt.Fprintln(w, h2(filetree.NewPathFromURL("/notes").MultiLink()))
|
fmt.Fprintln(w, h2(filetree.NewPathFromURL("/notes").MultiLink()))
|
||||||
|
htmlSearch(w)
|
||||||
fmt.Fprintln(w, h1(keywords), results)
|
fmt.Fprintln(w, h1(keywords), results)
|
||||||
foot(w, r)
|
foot(w, r)
|
||||||
}
|
}
|
||||||
|
|||||||
31
wrapper.html
31
wrapper.html
@@ -1,31 +0,0 @@
|
|||||||
<html>
|
|
||||||
<header>
|
|
||||||
<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>
|
|
||||||
<style>
|
|
||||||
nav {
|
|
||||||
display: block;
|
|
||||||
background: #161f27;
|
|
||||||
padding: .5pt;
|
|
||||||
border-radius: 6px;
|
|
||||||
}
|
|
||||||
nav li li li li {
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
img {
|
|
||||||
max-height: 400px;
|
|
||||||
}
|
|
||||||
body {
|
|
||||||
font-size: 125%;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</header>
|
|
||||||
<body height="100%">
|
|
||||||
{{{}}}
|
|
||||||
</body>
|
|
||||||
<footer>
|
|
||||||
</footer>
|
|
||||||
</html>
|
|
||||||
Reference in New Issue
Block a user