html_handlers.go 3.0 KB

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