You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

renderconfig.go 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package markdown
  5. import (
  6. "fmt"
  7. "strings"
  8. "github.com/yuin/goldmark/ast"
  9. east "github.com/yuin/goldmark/extension/ast"
  10. "gopkg.in/yaml.v2"
  11. )
  12. // RenderConfig represents rendering configuration for this file
  13. type RenderConfig struct {
  14. Meta string
  15. Icon string
  16. TOC bool
  17. Lang string
  18. }
  19. // ToRenderConfig converts a yaml.MapSlice to a RenderConfig
  20. func (rc *RenderConfig) ToRenderConfig(meta yaml.MapSlice) {
  21. if meta == nil {
  22. return
  23. }
  24. found := false
  25. var giteaMetaControl yaml.MapItem
  26. for _, item := range meta {
  27. strKey, ok := item.Key.(string)
  28. if !ok {
  29. continue
  30. }
  31. strKey = strings.TrimSpace(strings.ToLower(strKey))
  32. switch strKey {
  33. case "gitea":
  34. giteaMetaControl = item
  35. found = true
  36. case "include_toc":
  37. val, ok := item.Value.(bool)
  38. if !ok {
  39. continue
  40. }
  41. rc.TOC = val
  42. case "lang":
  43. val, ok := item.Value.(string)
  44. if !ok {
  45. continue
  46. }
  47. val = strings.TrimSpace(val)
  48. if len(val) == 0 {
  49. continue
  50. }
  51. rc.Lang = val
  52. }
  53. }
  54. if found {
  55. switch v := giteaMetaControl.Value.(type) {
  56. case string:
  57. switch v {
  58. case "none":
  59. rc.Meta = "none"
  60. case "table":
  61. rc.Meta = "table"
  62. default: // "details"
  63. rc.Meta = "details"
  64. }
  65. case yaml.MapSlice:
  66. for _, item := range v {
  67. strKey, ok := item.Key.(string)
  68. if !ok {
  69. continue
  70. }
  71. strKey = strings.TrimSpace(strings.ToLower(strKey))
  72. switch strKey {
  73. case "meta":
  74. val, ok := item.Value.(string)
  75. if !ok {
  76. continue
  77. }
  78. switch strings.TrimSpace(strings.ToLower(val)) {
  79. case "none":
  80. rc.Meta = "none"
  81. case "table":
  82. rc.Meta = "table"
  83. default: // "details"
  84. rc.Meta = "details"
  85. }
  86. case "details_icon":
  87. val, ok := item.Value.(string)
  88. if !ok {
  89. continue
  90. }
  91. rc.Icon = strings.TrimSpace(strings.ToLower(val))
  92. case "include_toc":
  93. val, ok := item.Value.(bool)
  94. if !ok {
  95. continue
  96. }
  97. rc.TOC = val
  98. case "lang":
  99. val, ok := item.Value.(string)
  100. if !ok {
  101. continue
  102. }
  103. val = strings.TrimSpace(val)
  104. if len(val) == 0 {
  105. continue
  106. }
  107. rc.Lang = val
  108. }
  109. }
  110. }
  111. }
  112. }
  113. func (rc *RenderConfig) toMetaNode(meta yaml.MapSlice) ast.Node {
  114. switch rc.Meta {
  115. case "table":
  116. return metaToTable(meta)
  117. case "details":
  118. return metaToDetails(meta, rc.Icon)
  119. default:
  120. return nil
  121. }
  122. }
  123. func metaToTable(meta yaml.MapSlice) ast.Node {
  124. table := east.NewTable()
  125. alignments := []east.Alignment{}
  126. for range meta {
  127. alignments = append(alignments, east.AlignNone)
  128. }
  129. row := east.NewTableRow(alignments)
  130. for _, item := range meta {
  131. cell := east.NewTableCell()
  132. cell.AppendChild(cell, ast.NewString([]byte(fmt.Sprintf("%v", item.Key))))
  133. row.AppendChild(row, cell)
  134. }
  135. table.AppendChild(table, east.NewTableHeader(row))
  136. row = east.NewTableRow(alignments)
  137. for _, item := range meta {
  138. cell := east.NewTableCell()
  139. cell.AppendChild(cell, ast.NewString([]byte(fmt.Sprintf("%v", item.Value))))
  140. row.AppendChild(row, cell)
  141. }
  142. table.AppendChild(table, row)
  143. return table
  144. }
  145. func metaToDetails(meta yaml.MapSlice, icon string) ast.Node {
  146. details := NewDetails()
  147. summary := NewSummary()
  148. summary.AppendChild(summary, NewIcon(icon))
  149. details.AppendChild(details, summary)
  150. details.AppendChild(details, metaToTable(meta))
  151. return details
  152. }