package filetree import ( "fmt" "gitea.inhome.blapointe.com/local/notes-server/config" "os" "path" "strings" ) type Path struct { Local string HREF string Base string BaseHREF string } func NewPathFromLocal(p string) Path { root := config.Root + "/" if strings.HasPrefix(root, "./") { root = root[2:] } if strings.HasSuffix(root, "/") { root = root[:len(root)-1] } splits := strings.SplitN(p, root, 2) for len(splits) > 0 && splits[0] == "" { splits = splits[1:] } if len(splits) == 0 { splits = []string{"", ""} } href := splits[0] if len(splits) == 1 && (splits[0] == root || splits[0] == config.Root) { href = "" } else if splits[0] == "" || splits[0] == "/" { href = splits[1] } href = path.Join("/notes", href) return NewPathFromURL(href) } func NewPathFromURL(p string) Path { p = path.Clean(p) base := path.Base(p) href := p local := strings.TrimPrefix(p, "/") locals := strings.SplitN(local, "/", 2) local = locals[0] 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.BaseHREF href = strings.TrimPrefix(href, "/") href = strings.TrimSuffix(href, "/") href = path.Join("notes/", href) segments := strings.Split(href, "/") full := "" for i := range segments { href := "/" + strings.Join(segments[:i], "/") + "/" href += segments[i] href = path.Clean(href) base := segments[i] add := fmt.Sprintf(`/%s`, href, base) full += add } return full } func (p Path) FullLI() string { return fmt.Sprintf(`