helpers.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package helpers
  2. import (
  3. "time"
  4. "github.com/gomarkdown/markdown"
  5. "github.com/gomarkdown/markdown/html"
  6. "github.com/gomarkdown/markdown/parser"
  7. )
  8. const HEADER_KEY = "header-links"
  9. const MENU_KEY = "menu-config"
  10. const ADMIN_TABLE_KEY = "admin-tables"
  11. const TECHNICAL = "technical"
  12. const CONFIGURATION = "configuration"
  13. const BLOG = "blog"
  14. const CREATIVE = "creative"
  15. const DIGITAL_ART = "digital_art"
  16. var Topics = []string{
  17. TECHNICAL,
  18. BLOG,
  19. CREATIVE,
  20. }
  21. var TopicMap = map[string]string{
  22. TECHNICAL: TECHNICAL,
  23. BLOG: BLOG,
  24. CREATIVE: CREATIVE,
  25. }
  26. type HeaderCollection struct {
  27. Category string `json:"category"`
  28. Elements []HeaderElem `json:"elements"`
  29. }
  30. type HeaderElem struct {
  31. Png string `json:"png"`
  32. Link string `json:"link"`
  33. }
  34. type ImageElement struct {
  35. ImgUrl string `json:"img_url"`
  36. }
  37. type MenuElement struct {
  38. Png string `json:"png"`
  39. Category string `json:"category"`
  40. MenuLinks []MenuLinkPair `json:"menu_links"`
  41. }
  42. type DocumentOld struct {
  43. Ident Identifier`json:"identifier"`
  44. Created string `json:"created"`
  45. Body string `json:"body"`
  46. Category string `json:"category"`
  47. Sample string
  48. }
  49. type AdminPage struct {
  50. Tables map[string][]TableData `json:"tables"`
  51. }
  52. type TableData struct { // TODO: add this to the database io interface
  53. DisplayName string `json:"display_name"`
  54. Link string `json:"link"`
  55. }
  56. func NewDocument(ident string, created *time.Time, body string, category string) Document {
  57. var ts time.Time
  58. if created == nil {
  59. rn := time.Now()
  60. ts = time.Date(rn.Year(), rn.Month(), rn.Day(), rn.Hour(), rn.Minute(),
  61. rn.Second(), rn.Nanosecond(), rn.Location())
  62. } else {
  63. ts = *created
  64. }
  65. return Document{Ident: Identifier(ident), Created: ts.String(), Body: body, Category: category}
  66. }
  67. type DocumentUpload struct {
  68. Name string `json:"name"`
  69. Category string `json:"category"`
  70. Text string `json:"text"`
  71. }
  72. type HeaderIo interface {
  73. GetHeaders() (*HeaderCollection, error)
  74. AddHeaders(HeaderCollection) error
  75. GetMenuLinks() (*MenuElement, error)
  76. }
  77. /*
  78. convert markdown to html
  79. :param md: the byte array containing the Markdown to convert
  80. */
  81. func MdToHTML(md []byte) []byte {
  82. extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
  83. p := parser.NewWithExtensions(extensions)
  84. doc := p.Parse(md)
  85. htmlFlags := html.CommonFlags | html.HrefTargetBlank
  86. opts := html.RendererOptions{Flags: htmlFlags}
  87. renderer := html.NewRenderer(opts)
  88. return markdown.Render(doc, renderer)
  89. }