notes-server/server/path.go

59 lines
1.0 KiB
Go

package server
import (
"fmt"
"local/notes-server/config"
"path"
"strings"
)
type Path struct {
Local string
HREF string
Base string
}
func NewPath(p string) Path {
base := path.Base(p)
href := path.Join("/notes", p)
local := strings.TrimPrefix(p, "/")
local = strings.TrimPrefix(p, "notes")
local = strings.TrimPrefix(p, "/")
local = path.Join(config.Root, local)
return Path{
Base: base,
HREF: href,
Local: local,
}
}
func (p Path) MultiLink() string {
pa := path.Join("/notes", p.String())
full := ""
for pa != "/" {
base := path.Base(pa)
full = fmt.Sprintf(`/<a href=%q>%s</a>`, pa, base) + full
pa = path.Dir(pa)
}
return full
}
func (p Path) String() string {
root := path.Base(config.Root)
dir := p.dir
dirs := strings.SplitN(dir, root, 2)
dir = dirs[0]
if len(dirs) > 1 {
dir = dirs[1]
}
base := p.base
return path.Join(dir, base)
}
func (p Path) LI() string {
content := fmt.Sprintf(`<li><a href="%s">`, path.Join("/notes", p.String()))
content += p.base
content += "</li>"
return content
}