fix: fixed wrong path generation for links.
This commit is contained in:
parent
1aa089ab40
commit
9e69d1bd28
7 changed files with 141 additions and 29 deletions
103
server/main.go
103
server/main.go
|
@ -6,12 +6,30 @@ import (
|
|||
"vultras.su/core/bond/contents"
|
||||
"vultras.su/util/pp"
|
||||
"path/filepath"
|
||||
"path"
|
||||
"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 {
|
||||
WikiPath string
|
||||
WikiExt string
|
||||
WebPath string
|
||||
AddFileNavigation bool
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
|
@ -44,39 +62,65 @@ func (srv *Server) ProcessPmd(pth string, bts []byte) ([]byte, error) {
|
|||
return []byte(prep), nil
|
||||
}
|
||||
|
||||
func (srv *Server) ProcessToHtml(bts []byte) ([]byte, error) {
|
||||
html := markdown.ToHTML(bts, nil, nil)
|
||||
ret := append([]byte(htmlHead), html...)
|
||||
return append(ret, []byte(htmlFooter)...), nil
|
||||
func (srv *Server) pageHead() string {
|
||||
return htmlHead
|
||||
}
|
||||
|
||||
func (srv *Server) wikiFilePath(pth string) string {
|
||||
if pth == "/" || pth == "" {
|
||||
return srv.options.WikiPath + "/index.pmd"
|
||||
func (srv *Server) pageFooter() string {
|
||||
return htmlFooter
|
||||
}
|
||||
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=\"..\"><<<</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 {
|
||||
return bond.Root(bond.Path().
|
||||
|
@ -90,17 +134,26 @@ var makeRootHandler = func(opts ServerOptions) bond.Handler {
|
|||
Func(func(c *Context){
|
||||
pth := c.wikiFilePath(c.Path())
|
||||
bts, err := os.ReadFile(pth)
|
||||
if err != nil {
|
||||
pth = c.wikiFilePath(c.Path()+"/")
|
||||
bts, err = os.ReadFile(pth)
|
||||
if err != nil {
|
||||
c.NotFound()
|
||||
return
|
||||
}
|
||||
}
|
||||
prep, err := c.ProcessPmd(pth, bts)
|
||||
if err != nil {
|
||||
c.InternalServerError(err)
|
||||
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.W.Write(bts)
|
||||
}),
|
||||
|
|
43
server/path.go
Normal file
43
server/path.go
Normal 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,
|
||||
))
|
||||
}
|
2
tool.go
2
tool.go
|
@ -15,6 +15,8 @@ var Tool = mtool.T("rwiki").Func(func(flags *mtool.Flags){
|
|||
flags.StringVar(&addr, "a", ":8080", "address string")
|
||||
flags.StringVar(&opts.WikiPath, "wiki", "wiki", "path to wiki 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()
|
||||
srv := bond.Server{
|
||||
|
|
|
@ -1,4 +1,11 @@
|
|||
|
||||
nav {
|
||||
padding: .5rem;
|
||||
}
|
||||
nav a {
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
p {
|
||||
border-style: solid;
|
||||
border-width: 0.15rem;
|
||||
|
|
1
wiki/some-file-name.pmd
Normal file
1
wiki/some-file-name.pmd
Normal file
|
@ -0,0 +1 @@
|
|||
shit
|
3
wiki/sub-dir/check.pmd
Normal file
3
wiki/sub-dir/check.pmd
Normal file
|
@ -0,0 +1,3 @@
|
|||
# checking shit
|
||||
|
||||
another shit
|
3
wiki/sub-dir/index.pmd
Normal file
3
wiki/sub-dir/index.pmd
Normal file
|
@ -0,0 +1,3 @@
|
|||
# sub dir index
|
||||
|
||||
hetaushetusahet
|
Loading…
Reference in a new issue