8 Commits
v1.9 ... v1.12

Author SHA1 Message Date
Bel LaPointe
43d44a4518 Header floats kthnx css and comments 2020-08-07 12:42:50 -06:00
Bel LaPointe
8bf7503c8e Add comments per heading 2020-08-07 12:30:27 -06:00
Bel LaPointe
785215bd3c name from recursive resolved plaintext 2020-08-07 11:35:52 -06:00
Bel LaPointe
952e04815a read only mode a gogo 2020-08-06 21:20:29 -06:00
bel
d6c95a536c fix selection color 2020-04-05 01:00:26 +00:00
bel
9785ef5e1e Add serach bar to search page 2020-03-09 03:09:45 +00:00
bel
3b7aa70e44 fix table and toc colors 2020-03-09 03:06:54 +00:00
Bel LaPointe
c067b426a9 Fix colors breaking floating css because fuck 2020-03-08 20:37:48 -06:00
11 changed files with 469 additions and 62 deletions

File diff suppressed because one or more lines are too long

129
config/rotate.py Executable file
View 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

Submodule config/water.css added at 576eee5b82

61
notes/comment.go Executable file
View 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
View 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)
}
}

View File

@@ -1,11 +1,20 @@
package notes
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"io"
"io/ioutil"
"local/notes-server/filetree"
"log"
"path"
"regexp"
"strings"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/ast"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
)
@@ -18,8 +27,80 @@ func (n *Notes) File(urlPath string) (string, error) {
b, _ := ioutil.ReadFile(p.Local)
renderer := html.NewRenderer(html.RendererOptions{
Flags: html.CommonFlags | html.TOC,
RenderNodeHook: n.commentFormer(urlPath, b),
})
parser := parser.NewWithExtensions(parser.CommonExtensions | parser.HeadingIDs | parser.AutoHeadingIDs | parser.Titleblock)
content := markdown.ToHTML(b, parser, renderer)
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
}
}

37
server/comment.go Executable file
View 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)
}

View File

@@ -2,6 +2,7 @@ package server
import (
"fmt"
"local/notes-server/config"
"local/notes-server/filetree"
"net/http"
"path"
@@ -22,7 +23,7 @@ func (s *Server) notes(w http.ResponseWriter, r *http.Request) {
}
func notesHead(w http.ResponseWriter, p filetree.Path) {
fmt.Fprintln(w, h2(p.MultiLink(), "margin: 0; position: fixed; padding: .25em; background-color: #202b38; width: 100%; top: 0;"))
fmt.Fprintln(w, h2(p.MultiLink()))
htmlSearch(w)
}
@@ -58,18 +59,27 @@ func fileHead(w http.ResponseWriter, baseHREF string) {
}
func htmlEdit(w http.ResponseWriter, baseHREF string) {
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>
@@ -80,7 +90,7 @@ func htmlCreate(w http.ResponseWriter, baseHREF string) {
func htmlSearch(w http.ResponseWriter) {
fmt.Fprintf(w, `
<form action=%q method="post" style="padding-top: 2.5em">
<form action=%q method="post" >
<input type="text" name="keywords"></input>
<button type="submit">Search</button>
</form>

View File

@@ -3,49 +3,53 @@ package server
import (
"fmt"
"local/gziphttp"
"local/notes-server/config"
"local/router"
"net/http"
"path/filepath"
"strings"
)
func (s *Server) Routes() error {
wildcard := router.Wildcard
endpoints := []struct {
path string
endpoints := map[string]struct {
handler http.HandlerFunc
}{
{
path: "/",
"/": {
handler: s.root,
},
{
path: fmt.Sprintf("notes/%s%s", wildcard, wildcard),
fmt.Sprintf("notes/%s%s", wildcard, wildcard): {
handler: s.gzip(s.authenticate(s.notes)),
},
{
path: fmt.Sprintf("edit/%s%s", wildcard, wildcard),
fmt.Sprintf("edit/%s%s", wildcard, wildcard): {
handler: s.gzip(s.authenticate(s.edit)),
},
{
path: fmt.Sprintf("delete/%s%s", wildcard, wildcard),
fmt.Sprintf("delete/%s%s", wildcard, wildcard): {
handler: s.gzip(s.authenticate(s.delete)),
},
{
path: fmt.Sprintf("submit/%s%s", wildcard, wildcard),
fmt.Sprintf("submit/%s%s", wildcard, wildcard): {
handler: s.gzip(s.authenticate(s.submit)),
},
{
path: fmt.Sprintf("create/%s%s", wildcard, wildcard),
fmt.Sprintf("create/%s%s", wildcard, wildcard): {
handler: s.gzip(s.authenticate(s.create)),
},
{
path: fmt.Sprintf("search"),
fmt.Sprintf("comment/%s%s", wildcard, wildcard): {
handler: s.gzip(s.authenticate(s.comment)),
},
fmt.Sprintf("search"): {
handler: s.gzip(s.authenticate(s.search)),
},
}
for _, endpoint := range endpoints {
if err := s.Add(endpoint.path, endpoint.handler); err != nil {
for path, endpoint := range endpoints {
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
}
}

View File

@@ -21,6 +21,7 @@ func (s *Server) search(w http.ResponseWriter, r *http.Request) {
}
head(w, r)
fmt.Fprintln(w, h2(filetree.NewPathFromURL("/notes").MultiLink()))
htmlSearch(w)
fmt.Fprintln(w, h1(keywords), results)
foot(w, r)
}

View File

@@ -1,33 +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%;
filter: hue-rotate(120deg);
background: #3b242b;
}
</style>
</header>
<body height="100%">
{{{}}}
</body>
<footer>
</footer>
</html>