html_handlers.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package controller
  2. import (
  3. "html/template"
  4. "log"
  5. "math/rand"
  6. "net/http"
  7. "git.aetherial.dev/aeth/keiji/pkg/storage"
  8. "github.com/gin-gonic/gin"
  9. "github.com/gomarkdown/markdown"
  10. "github.com/gomarkdown/markdown/html"
  11. "github.com/gomarkdown/markdown/parser"
  12. )
  13. /*
  14. convert markdown to html
  15. :param md: the byte array containing the Markdown to convert
  16. */
  17. func MdToHTML(md []byte) []byte {
  18. extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
  19. p := parser.NewWithExtensions(extensions)
  20. doc := p.Parse(md)
  21. htmlFlags := html.CommonFlags | html.HrefTargetBlank
  22. opts := html.RendererOptions{Flags: htmlFlags}
  23. renderer := html.NewRenderer(opts)
  24. return markdown.Render(doc, renderer)
  25. }
  26. // These helpers retain compatibility with older DocumentIO implementations while
  27. // allowing repositories that implement DocumentListReader to report failures.
  28. func (c *Controller) htmlDocuments(category string) ([]storage.Document, error) {
  29. if reader, ok := c.database.(storage.DocumentListReader); ok {
  30. return reader.GetByCategoryWithError(category)
  31. }
  32. return c.database.GetByCategory(category), nil
  33. }
  34. func (c *Controller) htmlImages(category string) ([]storage.Image, error) {
  35. if reader, ok := c.database.(storage.DocumentListReader); ok {
  36. return reader.GetImagesByCategoryWithError(category)
  37. }
  38. return c.database.GetImagesByCategory(category), nil
  39. }
  40. func (c *Controller) htmlNavigation() ([]storage.NavBarItem, []storage.LinkPair, error) {
  41. if reader, ok := c.database.(storage.DocumentListReader); ok {
  42. headers, err := reader.GetNavBarLinksWithError()
  43. if err != nil {
  44. return nil, nil, err
  45. }
  46. menu, err := reader.GetDropdownElementsWithError()
  47. return headers, menu, err
  48. }
  49. return c.database.GetNavBarLinks(), c.database.GetDropdownElements(), nil
  50. }
  51. func htmlListError(ctx *gin.Context, err error) {
  52. log.Printf("list query failed: %v", err)
  53. ctx.JSON(http.StatusInternalServerError, gin.H{"Error": "Could not load content"})
  54. }
  55. // @Name ServePost
  56. // @Summary serves HTML files out of the HTML directory
  57. // @Tags webpages
  58. // @Router /writing/:id [get]
  59. func (c *Controller) ServePost(ctx *gin.Context) {
  60. post, exist := ctx.Params.Get("id")
  61. if !exist {
  62. ctx.JSON(404, map[string]string{
  63. "Error": "the requested file could not be found",
  64. })
  65. return
  66. }
  67. doc, err := c.database.GetDocument(storage.Identifier(post))
  68. if err != nil {
  69. ctx.JSON(500, map[string]string{
  70. "Error": err.Error(),
  71. })
  72. return
  73. }
  74. if doc.Category == storage.CONFIGURATION {
  75. ctx.Status(404)
  76. return
  77. }
  78. headers, menu, err := c.htmlNavigation()
  79. if err != nil {
  80. htmlListError(ctx, err)
  81. return
  82. }
  83. ctx.HTML(http.StatusOK, "blogpost", gin.H{
  84. "navigation": gin.H{
  85. "headers": headers,
  86. },
  87. "Title": doc.Title,
  88. "Ident": doc.Ident,
  89. "Created": doc.Created,
  90. "Body": template.HTML(MdToHTML([]byte(doc.Body))),
  91. "menu": menu,
  92. })
  93. }
  94. // @Name ServeBlogHome
  95. // @Summary serves the HTML file for the blog post homepage
  96. // @Tags webpages
  97. // @Router / [get]
  98. func (c *Controller) ServeHome(ctx *gin.Context) {
  99. home, err := c.htmlDocuments(storage.HOMEPAGE)
  100. if err != nil {
  101. htmlListError(ctx, err)
  102. return
  103. }
  104. headers, menu, err := c.htmlNavigation()
  105. if err != nil {
  106. htmlListError(ctx, err)
  107. return
  108. }
  109. var content storage.Document
  110. if len(home) == 0 {
  111. content = storage.Document{
  112. Body: "Under construction. Sry :(",
  113. }
  114. } else {
  115. index := rand.Intn(len(home))
  116. content = home[index]
  117. }
  118. ctx.HTML(http.StatusOK, "home", gin.H{
  119. "navigation": gin.H{
  120. "headers": headers,
  121. },
  122. "menu": menu,
  123. "default": content,
  124. })
  125. }
  126. // @Name ServeBlog
  127. // @Summary serves the HTML for written post listings
  128. // @Tags webpages
  129. // @Router /blog [get]
  130. func (c *Controller) ServeBlog(ctx *gin.Context) {
  131. docs, err := c.htmlDocuments(storage.BLOG)
  132. if err != nil {
  133. htmlListError(ctx, err)
  134. return
  135. }
  136. ctx.HTML(http.StatusOK, "writing", docs)
  137. }
  138. // @Name ServeCreative
  139. // @Summary serves the HTML for the creative writing listings
  140. // @Tags webpages
  141. // @Router /creative [get]
  142. func (c *Controller) ServeCreative(ctx *gin.Context) {
  143. docs, err := c.htmlDocuments(storage.CREATIVE)
  144. if err != nil {
  145. htmlListError(ctx, err)
  146. return
  147. }
  148. ctx.HTML(http.StatusOK, "writing", docs)
  149. }
  150. // @Name ServeDigitalArt
  151. // @Summary serves the HTML file for the digital art homepage
  152. // @Tags webpages
  153. // @Router /digital [get]
  154. func (c *Controller) ServeDigitalArt(ctx *gin.Context) {
  155. images, err := c.htmlImages(storage.DIGITAL_ART)
  156. if err != nil {
  157. htmlListError(ctx, err)
  158. return
  159. }
  160. headers, menu, err := c.htmlNavigation()
  161. if err != nil {
  162. htmlListError(ctx, err)
  163. return
  164. }
  165. ctx.HTML(http.StatusOK, "digital_art", gin.H{
  166. "navigation": gin.H{
  167. "headers": headers,
  168. },
  169. "images": images,
  170. "menu": menu,
  171. })
  172. }