fix: fixed wrong path generation for links.

This commit is contained in:
Andrey Parhomenko 2024-03-01 05:36:45 +05:00
parent 1aa089ab40
commit 9e69d1bd28
7 changed files with 141 additions and 29 deletions

View file

@ -6,12 +6,30 @@ import (
"vultras.su/core/bond/contents" "vultras.su/core/bond/contents"
"vultras.su/util/pp" "vultras.su/util/pp"
"path/filepath" "path/filepath"
"path"
"os" "os"
"fmt"
//"strings"
"bytes"
) )
const htmlHead = `
<!doctype html>
<html><head>
<title>Title</title>
<link rel="stylesheet" href="/web/main.css">
</head><body>
`
const htmlFooter = `
</body></html>
`
type ServerOptions struct { type ServerOptions struct {
WikiPath string WikiPath string
WikiExt string
WebPath string WebPath string
AddFileNavigation bool
} }
type Server struct { type Server struct {
@ -44,39 +62,65 @@ func (srv *Server) ProcessPmd(pth string, bts []byte) ([]byte, error) {
return []byte(prep), nil return []byte(prep), nil
} }
func (srv *Server) ProcessToHtml(bts []byte) ([]byte, error) { func (srv *Server) pageHead() string {
html := markdown.ToHTML(bts, nil, nil) return htmlHead
ret := append([]byte(htmlHead), html...)
return append(ret, []byte(htmlFooter)...), nil
} }
func (srv *Server) wikiFilePath(pth string) string { func (srv *Server) pageFooter() string {
if pth == "/" || pth == "" { return htmlFooter
return srv.options.WikiPath + "/index.pmd"
} }
if pth[len(pth)-1] == '/' {
pth += "index" func (srv *Server) ProcessToHtml(urlPath, filePath string, bts []byte) ([]byte, error) {
var b bytes.Buffer
main_section := markdown.ToHTML(bts, nil, nil)
fmt.Fprint(&b, srv.pageHead())
if srv.options.AddFileNavigation {
fileDirPath := filepath.Dir(filePath)
urlDirPath := path.Dir(urlPath)
entries, err := os.ReadDir(fileDirPath)
if err != nil {
fmt.Println("leaving", fileDirPath)
return nil, err
} }
return filepath.FromSlash(
srv.options.WikiPath + "/" + pth + ".pmd", fmt.Fprint(&b, "<header><nav class=\"base\">")
fmt.Fprintf(
&b, "<a href=\"..\">&lt&lt&lt</a><a href=\".\">%s</a>",
srv.makeFileName(fileDirPath, "index"+srv.options.WikiExt),
)
fmt.Fprint(&b, "</nav><nav class=\"dirs\">")
for _, entry := range entries {
if !entry.IsDir() {
continue
}
fmt.Fprintf(
&b, "<a href=%s>%s</a>",
srv.makeFileLink(urlDirPath, entry.Name()) + "/",
srv.makeFileName(fileDirPath, entry.Name()),
) )
} }
fmt.Fprint(&b, "</nav><nav class=\"files\">")
for _, entry := range entries {
if entry.IsDir() || entry.Name() == "index" + srv.options.WikiExt{
continue
}
fmt.Fprintf(
&b, "<a href=\"%s\">%s</a>",
srv.makeFileLink(urlDirPath, entry.Name()) ,
srv.makeFileName(fileDirPath, entry.Name()),
)
}
fmt.Fprint(&b, "</nav></header>")
}
fmt.Fprint(&b, "<main>", string(main_section), "</main>")
fmt.Fprint(&b, srv.pageFooter())
return b.Bytes(), nil
}
const htmlHead = `
<!doctype html>
<html><head>
<title>Title</title>
<link rel="stylesheet" href="/web/main.css">
</head><body>
<main>
`
const htmlFooter = `
</main>
</body></html>
`
var makeRootHandler = func(opts ServerOptions) bond.Handler { var makeRootHandler = func(opts ServerOptions) bond.Handler {
return bond.Root(bond.Path(). return bond.Root(bond.Path().
@ -90,17 +134,26 @@ var makeRootHandler = func(opts ServerOptions) bond.Handler {
Func(func(c *Context){ Func(func(c *Context){
pth := c.wikiFilePath(c.Path()) pth := c.wikiFilePath(c.Path())
bts, err := os.ReadFile(pth) bts, err := os.ReadFile(pth)
if err != nil {
pth = c.wikiFilePath(c.Path()+"/")
bts, err = os.ReadFile(pth)
if err != nil { if err != nil {
c.NotFound() c.NotFound()
return return
} }
}
prep, err := c.ProcessPmd(pth, bts) prep, err := c.ProcessPmd(pth, bts)
if err != nil { if err != nil {
c.InternalServerError(err) c.InternalServerError(err)
return return
} }
bts, _ = c.ProcessToHtml(prep) fmt.Println(c.Path(), pth)
bts, err = c.ProcessToHtml(c.Path(), pth, prep)
if err != nil {
c.InternalServerError(err)
return
}
c.SetContentType(contents.Html, contents.Utf8) c.SetContentType(contents.Html, contents.Utf8)
c.W.Write(bts) c.W.Write(bts)
}), }),

43
server/path.go Normal file
View file

@ -0,0 +1,43 @@
package server
import (
"strings"
"path"
"path/filepath"
)
func (srv *Server) makeFileName(dir, name string) string {
name, _ = strings.CutSuffix(name, srv.options.WikiExt)
if name == "index" {
name = filepath.Base(dir)
}
if len(name) == 0 || len(name) == 1 {
return strings.ToUpper(name)
}
seps := strings.Split(name, "-")
for i, sep := range seps {
seps[i] = strings.ToUpper(sep[:1]) + sep[1:]
}
return strings.Join(seps, " ")
}
func (srv *Server) makeFileLink(dir, name string) string {
name, _ = strings.CutSuffix(name, srv.options.WikiExt)
if name == "index" {
name = ""
}
return path.Clean(dir + "/" + name)
}
func (srv *Server) wikiFilePath(pth string) string {
if pth == "/" || pth == "" {
return filepath.Clean(srv.options.WikiPath + "/index.pmd")
}
if pth[len(pth)-1] == '/' {
pth += "index"
}
return filepath.Clean(filepath.FromSlash(
srv.options.WikiPath + "/" + pth + srv.options.WikiExt,
))
}

View file

@ -15,6 +15,8 @@ var Tool = mtool.T("rwiki").Func(func(flags *mtool.Flags){
flags.StringVar(&addr, "a", ":8080", "address string") flags.StringVar(&addr, "a", ":8080", "address string")
flags.StringVar(&opts.WikiPath, "wiki", "wiki", "path to wiki files") flags.StringVar(&opts.WikiPath, "wiki", "wiki", "path to wiki files")
flags.StringVar(&opts.WebPath, "web", "web", "path to static web files") flags.StringVar(&opts.WebPath, "web", "web", "path to static web files")
flags.StringVar(&opts.WikiExt, "ext", ".pmd", "wiki file exitension")
flags.BoolVar(&opts.AddFileNavigation, "nav", true, "generate navigation")
flags.Parse() flags.Parse()
srv := bond.Server{ srv := bond.Server{

View file

@ -1,4 +1,11 @@
nav {
padding: .5rem;
}
nav a {
padding: 1rem;
}
p { p {
border-style: solid; border-style: solid;
border-width: 0.15rem; border-width: 0.15rem;

1
wiki/some-file-name.pmd Normal file
View file

@ -0,0 +1 @@
shit

3
wiki/sub-dir/check.pmd Normal file
View file

@ -0,0 +1,3 @@
# checking shit
another shit

3
wiki/sub-dir/index.pmd Normal file
View file

@ -0,0 +1,3 @@
# sub dir index
hetaushetusahet