rwiki/server/main.go

199 lines
4.1 KiB
Go
Raw Permalink Normal View History

2024-03-01 00:23:26 +03:00
package server
import (
"github.com/gomarkdown/markdown"
2024-03-02 17:33:24 +03:00
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
2024-03-01 00:23:26 +03:00
"vultras.su/core/bond"
"vultras.su/core/bond/contents"
"vultras.su/util/pp"
"path/filepath"
"path"
2024-03-01 00:23:26 +03:00
"os"
"fmt"
//"strings"
"bytes"
2024-03-01 00:23:26 +03:00
)
const htmlHead = `
<!doctype html>
<html><head>
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="/web/main.css">
</head><body>
`
const htmlFooter = `
</body></html>
`
2024-03-01 00:23:26 +03:00
type ServerOptions struct {
WikiPath string
WikiExt string
2024-03-01 00:23:26 +03:00
WebPath string
AddFileNavigation bool
AddDocNavigation bool
2024-03-01 00:23:26 +03:00
}
type Server struct {
handler bond.Handler
options ServerOptions
prep *pp.Preprocessor
}
2024-03-02 17:33:24 +03:00
func (srv *Server) makeMdParser() *parser.Parser {
return parser.NewWithExtensions(
parser.CommonExtensions | parser.AutoHeadingIDs |
2024-03-02 21:30:22 +03:00
parser.NoEmptyLineBeforeBlock | parser.Attributes |
parser.Tables,
2024-03-02 17:33:24 +03:00
)
}
func (srv *Server) makeHtmlRenderer() *html.Renderer {
return html.NewRenderer(
html.RendererOptions{
Flags: html.CommonFlags | html.HrefTargetBlank,
},
)
}
2024-03-01 00:23:26 +03:00
func New(opts ServerOptions) *Server {
srv := &Server{}
srv.handler = makeRootHandler(opts)
srv.options = opts
srv.prep = pp.NewPp(pp.NewTengo())
return srv
}
2024-03-01 00:23:26 +03:00
func (srv *Server) Handle(c *bond.Context) {
c.Data = ContextData{
Server: srv,
}
srv.handler.Handle(c)
}
func (srv *Server) ProcessPmd(pth string, bts []byte) ([]byte, error) {
prep, err := srv.prep.Process(pth, string(bts))
if err != nil {
return nil, err
}
return []byte(prep), nil
}
func (srv *Server) pageHead() string {
return htmlHead
2024-03-01 00:23:26 +03:00
}
func (srv *Server) pageFooter() string {
return htmlFooter
2024-03-01 00:23:26 +03:00
}
func (srv *Server) ProcessToHtml(urlPath, filePath string, bts []byte) ([]byte, error) {
var b bytes.Buffer
2024-03-02 17:33:24 +03:00
doc := srv.makeMdParser().Parse(bts)
main_section := markdown.Render(doc, srv.makeHtmlRenderer())
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, "<header>")
fmt.Fprint(&b, "<nav class=\"base\">")
fmt.Fprintf(
&b, "<a class=\"prev\" href=\"..\">"+
"&lt&lt&lt</a>"+
"<a class=\"home\"href=\".\">%s</a>",
srv.makeFileName(fileDirPath, "index"+srv.options.WikiExt),
)
fmt.Fprint(&b, "</nav>")
fmt.Fprint(&b, "<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>")
fmt.Fprint(&b, "<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>")
headings := getDocumentHeadings(doc)
trees := makeHeadingTrees(headings)
docNav := RenderHeadingTrees(trees, true)
fmt.Fprint(&b, docNav)
fmt.Fprint(&b, "</header>")
}
fmt.Fprint(&b, "<main>")
fmt.Fprintf(&b, string(main_section))
fmt.Fprintf(&b, "</main>")
fmt.Fprint(&b, srv.pageFooter())
return b.Bytes(), nil
}
2024-03-01 00:23:26 +03:00
var makeRootHandler = func(opts ServerOptions) bond.Handler {
return bond.Path().
2024-03-01 00:23:26 +03:00
Case(
"web",
bond.StaticDir(opts.WebPath),
).Case(
"api",
nil,
).Default(
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
}
2024-03-01 00:23:26 +03:00
}
prep, err := c.ProcessPmd(pth, bts)
if err != nil {
c.InternalServerError(err)
return
}
fmt.Println(c.Path(), pth)
bts, err = c.ProcessToHtml(c.Path(), pth, prep)
if err != nil {
c.InternalServerError(err)
return
}
2024-03-01 00:23:26 +03:00
c.SetContentType(contents.Html, contents.Utf8)
c.W.Write(bts)
}),
)
2024-03-01 00:23:26 +03:00
}