helpers.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package helpers
  2. import (
  3. "encoding/json"
  4. "time"
  5. "github.com/gomarkdown/markdown"
  6. "github.com/gomarkdown/markdown/html"
  7. "github.com/gomarkdown/markdown/parser"
  8. "github.com/redis/go-redis/v9"
  9. )
  10. const HEADER_KEY = "header-links"
  11. const MENU_KEY = "menu-config"
  12. const ADMIN_TABLE_KEY = "admin-tables"
  13. const TECHNICAL = "technical"
  14. const CONFIGURATION = "configuration"
  15. const BLOG = "blog"
  16. const CREATIVE = "creative"
  17. const DIGITAL_ART = "digital_art"
  18. var Topics = []string{
  19. TECHNICAL,
  20. BLOG,
  21. CREATIVE,
  22. }
  23. var TopicMap = map[string]string{
  24. TECHNICAL: TECHNICAL,
  25. BLOG: BLOG,
  26. CREATIVE: CREATIVE,
  27. }
  28. type HeaderCollection struct {
  29. Category string `json:"category"`
  30. Elements []HeaderElem `json:"elements"`
  31. }
  32. type HeaderElem struct {
  33. Png string `json:"png"`
  34. Link string `json:"link"`
  35. }
  36. type ImageElement struct {
  37. ImgUrl string `json:"img_url"`
  38. }
  39. type MenuElement struct {
  40. Png string `json:"png"`
  41. Category string `json:"category"`
  42. MenuLinks []MenuLinkPair `json:"menu_links"`
  43. }
  44. type DocumentOld struct {
  45. Ident string `json:"identifier"`
  46. Created string `json:"created"`
  47. Body string `json:"body"`
  48. Category string `json:"category"`
  49. Sample string
  50. }
  51. type AdminTables struct {
  52. Tables []Table `json:"tables"`
  53. }
  54. type Table struct {
  55. TableName string `json:"table_name"`
  56. TableData []TableData `json:"table_data"`
  57. }
  58. type TableData struct {
  59. DisplayName string `json:"display_name"`
  60. Link string `json:"link"`
  61. }
  62. func NewDocument(ident string, created *time.Time, body string, category string) Document {
  63. var ts time.Time
  64. if created == nil {
  65. rn := time.Now()
  66. ts = time.Date(rn.Year(), rn.Month(), rn.Day(), rn.Hour(), rn.Minute(),
  67. rn.Second(), rn.Nanosecond(), rn.Location())
  68. } else {
  69. ts = *created
  70. }
  71. return Document{Ident: ident, Created: ts.String(), Body: body, Category: category}
  72. }
  73. type DocumentUpload struct {
  74. Name string `json:"name"`
  75. Category string `json:"category"`
  76. Text string `json:"text"`
  77. }
  78. type HeaderIo interface {
  79. GetHeaders() (*HeaderCollection, error)
  80. AddHeaders(HeaderCollection) error
  81. GetMenuLinks() (*MenuElement, error)
  82. }
  83. /*
  84. Retrieves the header data from the redis database
  85. */
  86. func GetHeaders(redisCfg RedisConf) (*HeaderCollection, error) {
  87. rds := NewRedisClient(redisCfg)
  88. d, err := rds.Client.Get(rds.ctx, HEADER_KEY).Result()
  89. if err != nil {
  90. if err == redis.Nil {
  91. return nil, nil
  92. }
  93. return nil, err
  94. }
  95. header := &HeaderCollection{}
  96. err = json.Unmarshal([]byte(d), header)
  97. if err != nil {
  98. return nil, err
  99. }
  100. return header, nil
  101. }
  102. /*
  103. Retrieves the menu elements from the database
  104. */
  105. func GetMenuLinks(redisCfg RedisConf) (*MenuElement, error) {
  106. rds := NewRedisClient(redisCfg)
  107. d, err := rds.Client.Get(rds.ctx, MENU_KEY).Result()
  108. if err != nil {
  109. if err == redis.Nil {
  110. return nil, nil
  111. }
  112. return nil, err
  113. }
  114. header := &MenuElement{}
  115. err = json.Unmarshal([]byte(d), header)
  116. if err != nil {
  117. return nil, err
  118. }
  119. return header, nil
  120. }
  121. /*
  122. retreives the admin table config from the database
  123. */
  124. func GetAdminTables(redisCfg RedisConf) (*AdminTables, error) {
  125. rds := NewRedisClient(redisCfg)
  126. d, err := rds.Client.Get(rds.ctx, ADMIN_TABLE_KEY).Result()
  127. if err != nil {
  128. if err == redis.Nil {
  129. return nil, nil
  130. }
  131. return nil, err
  132. }
  133. tables := &AdminTables{}
  134. err = json.Unmarshal([]byte(d), tables)
  135. if err != nil {
  136. return nil, err
  137. }
  138. return tables, nil
  139. }
  140. /*
  141. Place holder func to create the header element in redis
  142. */
  143. func AddHeaders(h HeaderCollection, redisCfg RedisConf) error {
  144. rdc := NewRedisClient(redisCfg)
  145. data, err := json.Marshal(&h)
  146. if err != nil {
  147. return err
  148. }
  149. err = rdc.Client.Set(rdc.ctx, HEADER_KEY, data, 0).Err()
  150. if err != nil {
  151. return err
  152. }
  153. return nil
  154. }
  155. /*
  156. Retrieve all documents from the category specified in the argument category
  157. :param category: the category to get documents from
  158. */
  159. func GetAllDocuments(category string, redisCfg RedisConf) ([]*Document, error) {
  160. rdc := NewRedisClient(redisCfg)
  161. ids, err := rdc.AllDocIds()
  162. if err != nil {
  163. return nil, err
  164. }
  165. var docs []*Document
  166. for idx := range ids {
  167. doc, err := rdc.GetItem(ids[idx])
  168. if err != nil {
  169. return nil, err
  170. }
  171. if doc.Category != category {
  172. continue
  173. }
  174. docs = append(docs, &Document{
  175. Ident: doc.Ident,
  176. Created: doc.Created,
  177. Body: doc.Body,
  178. Sample: doc.MakeSample(),
  179. })
  180. }
  181. return docs, nil
  182. }
  183. /*
  184. adds a text post document to the redis database
  185. */
  186. func AddDocument(d Document, redisCfg RedisConf) error {
  187. rdc := NewRedisClient(redisCfg)
  188. return rdc.AddDoc(d)
  189. }
  190. /*
  191. convert markdown to html
  192. :param md: the byte array containing the Markdown to convert
  193. */
  194. func MdToHTML(md []byte) []byte {
  195. extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
  196. p := parser.NewWithExtensions(extensions)
  197. doc := p.Parse(md)
  198. htmlFlags := html.CommonFlags | html.HrefTargetBlank
  199. opts := html.RendererOptions{Flags: htmlFlags}
  200. renderer := html.NewRenderer(opts)
  201. return markdown.Render(doc, renderer)
  202. }