2022-03-18 13:26:13 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-03-23 00:25:58 +01:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2022-03-28 17:14:01 +02:00
|
|
|
"github.com/oscarmlage/wikingo/model"
|
|
|
|
|
2022-03-18 13:26:13 +01:00
|
|
|
"github.com/labstack/echo/v4"
|
2022-03-22 22:06:41 +01:00
|
|
|
"github.com/microcosm-cc/bluemonday"
|
|
|
|
"github.com/russross/blackfriday/v2"
|
2022-03-18 13:26:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func WikiHome(c echo.Context) error {
|
2022-03-23 00:24:40 +01:00
|
|
|
return c.Redirect(http.StatusMovedPermanently, "/WikiHome")
|
2022-03-22 16:36:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func WikiPage(c echo.Context) error {
|
2022-03-28 17:14:01 +02:00
|
|
|
var page model.Page
|
2022-03-22 16:36:18 +01:00
|
|
|
var err error
|
2022-03-28 17:14:01 +02:00
|
|
|
page_id := c.Param("page")
|
|
|
|
version := c.Param("version")
|
|
|
|
if version != "" {
|
2022-03-28 21:11:10 +02:00
|
|
|
Debug.Printf("version: %s", version)
|
2022-03-28 17:14:01 +02:00
|
|
|
page, err = store.GetPageVersion(page_id, version)
|
|
|
|
if err != nil {
|
|
|
|
// If record not found, show 404
|
|
|
|
if err.Error() == "record not found" {
|
2022-03-28 21:11:10 +02:00
|
|
|
Debug.Printf("Page not found: %s/%s", page_id, version)
|
2022-03-28 21:09:07 +02:00
|
|
|
return c.Render(http.StatusNotFound, "404.html", map[string]interface{}{
|
2022-03-28 17:14:01 +02:00
|
|
|
"name": page_id,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
page, err = store.GetPage(page_id)
|
|
|
|
if err != nil {
|
|
|
|
// If record not found, show the create
|
|
|
|
if err.Error() == "record not found" {
|
2022-03-28 21:11:10 +02:00
|
|
|
Debug.Printf("Page not found: %s", page_id)
|
2022-03-28 17:14:01 +02:00
|
|
|
return c.Render(http.StatusOK, "notfound.html", map[string]interface{}{
|
|
|
|
"name": page_id,
|
|
|
|
})
|
|
|
|
}
|
2022-03-22 16:36:18 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-28 21:11:10 +02:00
|
|
|
Debug.Printf("res: %T\n", page)
|
2022-03-23 00:25:18 +01:00
|
|
|
body := strings.ReplaceAll(page.Body, "\r\n", "\n")
|
|
|
|
unsafe := blackfriday.Run([]byte(body))
|
2022-03-22 22:06:41 +01:00
|
|
|
html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
|
2022-03-22 16:36:18 +01:00
|
|
|
return c.Render(http.StatusOK, "page.html", map[string]interface{}{
|
|
|
|
"page": page,
|
2022-03-22 22:06:41 +01:00
|
|
|
"html": string(html[:]),
|
2022-03-22 16:36:18 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func WikiPageEdit(c echo.Context) error {
|
2022-03-28 17:14:01 +02:00
|
|
|
var page model.Page
|
2022-03-22 16:36:18 +01:00
|
|
|
var err error
|
2022-03-28 17:14:01 +02:00
|
|
|
page_id := c.Param("page")
|
|
|
|
version := c.Param("version")
|
|
|
|
if version != "" {
|
2022-03-28 21:11:10 +02:00
|
|
|
Debug.Printf("version; %s\n", version)
|
2022-03-28 17:14:01 +02:00
|
|
|
page, err = store.GetPageVersion(page_id, version)
|
|
|
|
} else {
|
2022-03-28 21:11:10 +02:00
|
|
|
Debug.Println("No version")
|
2022-03-28 17:14:01 +02:00
|
|
|
page, err = store.GetPage(page_id)
|
|
|
|
}
|
2022-03-22 16:36:18 +01:00
|
|
|
if err != nil {
|
|
|
|
// If record not found, create and show the form
|
|
|
|
if err.Error() == "record not found" {
|
|
|
|
err := store.AddPage(page_id, "# "+page_id)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err.Error())
|
|
|
|
}
|
2022-03-28 21:11:10 +02:00
|
|
|
Debug.Printf("Page %s created", page_id)
|
2022-03-22 16:36:18 +01:00
|
|
|
page, err = store.GetPage(page_id)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-28 21:11:10 +02:00
|
|
|
Debug.Printf("res: %T\n", page)
|
2022-03-22 16:36:18 +01:00
|
|
|
return c.Render(http.StatusOK, "edit.html", map[string]interface{}{
|
|
|
|
"page": page,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func WikiPagePostEdit(c echo.Context) error {
|
|
|
|
page_id := c.Param("page")
|
|
|
|
var err error
|
|
|
|
page, err := store.GetPage(page_id)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err.Error())
|
|
|
|
}
|
|
|
|
page.Body = c.FormValue("html")
|
|
|
|
store.CreatePage(page)
|
|
|
|
return c.Redirect(http.StatusMovedPermanently, "/"+page.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func WikiList(c echo.Context) error {
|
|
|
|
pages, err := store.GetAllPages()
|
2022-03-18 13:26:13 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
2022-03-28 21:11:10 +02:00
|
|
|
Debug.Printf("res: %T\n", pages)
|
2022-03-22 16:36:18 +01:00
|
|
|
return c.Render(http.StatusOK, "list.html", map[string]interface{}{
|
|
|
|
"pages": pages,
|
2022-03-19 00:20:31 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func WikiAbout(c echo.Context) error {
|
2022-03-22 16:36:18 +01:00
|
|
|
return c.Render(http.StatusOK, "about.html", map[string]interface{}{})
|
2022-03-18 13:26:13 +01:00
|
|
|
}
|