diff --git a/server/main.go b/server/main.go index 056c70f..ed45c49 100644 --- a/server/main.go +++ b/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 = ` + + + Title + + +` + +const htmlFooter = ` + +` + 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 +} + +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 + } + + fmt.Fprint(&b, "
") } - if pth[len(pth)-1] == '/' { - pth += "index" - } - return filepath.FromSlash( - srv.options.WikiPath + "/" + pth + ".pmd", - ) + + fmt.Fprint(&b, "
", string(main_section), "
") + fmt.Fprint(&b, srv.pageFooter()) + return b.Bytes(), nil } -const htmlHead = ` - - - Title - - -
-` - -const htmlFooter = ` -
- -` var makeRootHandler = func(opts ServerOptions) bond.Handler { return bond.Root(bond.Path(). @@ -91,8 +135,12 @@ var makeRootHandler = func(opts ServerOptions) bond.Handler { pth := c.wikiFilePath(c.Path()) bts, err := os.ReadFile(pth) if err != nil { - c.NotFound() - return + 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 { @@ -100,7 +148,12 @@ var makeRootHandler = func(opts ServerOptions) bond.Handler { 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) }), diff --git a/server/path.go b/server/path.go new file mode 100644 index 0000000..4b50e29 --- /dev/null +++ b/server/path.go @@ -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, + )) +} diff --git a/tool.go b/tool.go index 116529c..e3f2a42 100644 --- a/tool.go +++ b/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{ diff --git a/web/main.css b/web/main.css index ec37dac..8da5290 100644 --- a/web/main.css +++ b/web/main.css @@ -1,4 +1,11 @@ +nav { + padding: .5rem; +} +nav a { + padding: 1rem; +} + p { border-style: solid; border-width: 0.15rem; diff --git a/wiki/some-file-name.pmd b/wiki/some-file-name.pmd new file mode 100644 index 0000000..9652058 --- /dev/null +++ b/wiki/some-file-name.pmd @@ -0,0 +1 @@ +shit diff --git a/wiki/sub-dir/check.pmd b/wiki/sub-dir/check.pmd new file mode 100644 index 0000000..d574121 --- /dev/null +++ b/wiki/sub-dir/check.pmd @@ -0,0 +1,3 @@ +# checking shit + +another shit diff --git a/wiki/sub-dir/index.pmd b/wiki/sub-dir/index.pmd new file mode 100644 index 0000000..7672933 --- /dev/null +++ b/wiki/sub-dir/index.pmd @@ -0,0 +1,3 @@ +# sub dir index + +hetaushetusahet