html_handlers.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package controller
  2. import (
  3. "html/template"
  4. "net/http"
  5. "git.aetherial.dev/aeth/keiji/pkg/storage"
  6. "github.com/gin-gonic/gin"
  7. "github.com/gomarkdown/markdown"
  8. "github.com/gomarkdown/markdown/html"
  9. "github.com/gomarkdown/markdown/parser"
  10. )
  11. /*
  12. convert markdown to html
  13. :param md: the byte array containing the Markdown to convert
  14. */
  15. func MdToHTML(md []byte) []byte {
  16. extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
  17. p := parser.NewWithExtensions(extensions)
  18. doc := p.Parse(md)
  19. htmlFlags := html.CommonFlags | html.HrefTargetBlank
  20. opts := html.RendererOptions{Flags: htmlFlags}
  21. renderer := html.NewRenderer(opts)
  22. return markdown.Render(doc, renderer)
  23. }
  24. // @Name ServePost
  25. // @Summary serves HTML files out of the HTML directory
  26. // @Tags webpages
  27. // @Router /writing/:id [get]
  28. func (c *Controller) ServePost(ctx *gin.Context) {
  29. post, exist := ctx.Params.Get("id")
  30. if !exist {
  31. ctx.JSON(404, map[string]string{
  32. "Error": "the requested file could not be found",
  33. })
  34. return
  35. }
  36. doc, err := c.database.GetDocument(storage.Identifier(post))
  37. if err != nil {
  38. ctx.JSON(500, map[string]string{
  39. "Error": err.Error(),
  40. })
  41. return
  42. }
  43. if doc.Category == storage.CONFIGURATION {
  44. ctx.Status(404)
  45. return
  46. }
  47. ctx.HTML(http.StatusOK, "blogpost", gin.H{
  48. "navigation": gin.H{
  49. "headers": c.database.GetNavBarLinks(),
  50. },
  51. "Title": doc.Title,
  52. "Ident": doc.Ident,
  53. "Created": doc.Created,
  54. "Body": template.HTML(MdToHTML([]byte(doc.Body))),
  55. "menu": c.database.GetDropdownElements(),
  56. })
  57. }
  58. // @Name ServeBlogHome
  59. // @Summary serves the HTML file for the blog post homepage
  60. // @Tags webpages
  61. // @Router / [get]
  62. func (c *Controller) ServeHome(ctx *gin.Context) {
  63. home := c.database.GetByCategory(storage.HOMEPAGE)
  64. var content storage.Document
  65. if len(home) == 0 {
  66. content = storage.Document{
  67. Body: "Under construction. Sry :(",
  68. }
  69. } else {
  70. content = home[0]
  71. }
  72. ctx.HTML(http.StatusOK, "home", gin.H{
  73. "navigation": gin.H{
  74. "headers": c.database.GetNavBarLinks(),
  75. },
  76. "menu": c.database.GetDropdownElements(),
  77. "default": content,
  78. })
  79. }
  80. // @Name ServeBlog
  81. // @Summary serves the HTML for written post listings
  82. // @Tags webpages
  83. // @Router /blog [get]
  84. func (c *Controller) ServeBlog(ctx *gin.Context) {
  85. ctx.HTML(http.StatusOK, "writing", c.database.GetByCategory(storage.BLOG))
  86. }
  87. // @Name ServeCreative
  88. // @Summary serves the HTML for the creative writing listings
  89. // @Tags webpages
  90. // @Router /creative [get]
  91. func (c *Controller) ServeCreative(ctx *gin.Context) {
  92. ctx.HTML(http.StatusOK, "writing", c.database.GetByCategory(storage.CREATIVE))
  93. }
  94. // @Name ServeDigitalArt
  95. // @Summary serves the HTML file for the digital art homepage
  96. // @Tags webpages
  97. // @Router /digital [get]
  98. func (c *Controller) ServeDigitalArt(ctx *gin.Context) {
  99. images := c.database.GetAllImages()
  100. ctx.HTML(http.StatusOK, "digital_art", gin.H{
  101. "navigation": gin.H{
  102. "headers": c.database.GetNavBarLinks(),
  103. },
  104. "images": images,
  105. "menu": c.database.GetDropdownElements(),
  106. })
  107. }