RunTest.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package util
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "regexp"
  7. "strings"
  8. )
  9. // RunTest 运行测试,检测模板
  10. // 标签对{{~}}开头 ,{{end}}结尾
  11. // - {{for .item in .list}} {{end}}
  12. // - {{if .bool}}~{{end}}
  13. // - {{if .bool}}~{{else}}~{{end}}
  14. // - {{if .bool}}~{{elseif}}~{{else}}~{{end}}
  15. //
  16. // 单标签
  17. // - {{.item}}
  18. // 解析模板
  19. func RunTest() {
  20. tmpl := ReadFileToString()
  21. tags, _ := AnalysisTemplateTags(tmpl)
  22. read := Read(tags)
  23. fmt.Println(read)
  24. }
  25. //func CreateFile(tds []TagDouble, tmpl string, data any) string {
  26. // //将data 转为map
  27. // Decode
  28. //}
  29. func Read(tags []Tag) []TagDouble {
  30. tds := make([]TagDouble, 0)
  31. var end TagDouble
  32. var i = 0
  33. //首层
  34. for i < len(tags) {
  35. end, i = GetEnd(tags, i, len(tags))
  36. if end.IsDoubleTag && len(end.OtherTag) > 0 {
  37. end.SubTags = append(end.SubTags, Read(end.OtherTag)...)
  38. fmt.Print("")
  39. }
  40. tds = append(tds, end)
  41. }
  42. return tds
  43. }
  44. // GetEnd 结果,返回数组坐标
  45. func GetEnd(tags []Tag, startIndex, endIndex int) (TagDouble, int) {
  46. fmt.Println(tags, startIndex, endIndex)
  47. sign := 0
  48. otherTags := make([]Tag, 0)
  49. head := tags[startIndex]
  50. if head.TagType == "ITEM" {
  51. return TagDouble{
  52. HeadTag: tags[startIndex],
  53. IsDoubleTag: false,
  54. EndTag: Tag{},
  55. OtherTag: nil,
  56. SubTags: nil,
  57. }, startIndex + 1
  58. }
  59. for i := startIndex + 1; i < endIndex; i++ {
  60. tag := tags[i]
  61. if tag.TagType == "IF" || tag.TagType == "FOR" {
  62. otherTags = append(otherTags, tag)
  63. sign += 1
  64. } else if tag.TagType == "END" {
  65. if sign == 0 {
  66. return TagDouble{
  67. HeadTag: head,
  68. IsDoubleTag: true,
  69. EndTag: tag,
  70. OtherTag: otherTags,
  71. SubTags: nil,
  72. }, i + 1
  73. } else {
  74. sign -= 1
  75. otherTags = append(otherTags, tag)
  76. }
  77. } else if tag.TagType == "ITEM" {
  78. otherTags = append(otherTags, tag)
  79. } else {
  80. //panic("其他标签不能作为首标签")
  81. }
  82. }
  83. panic("在咋回事?3")
  84. }
  85. func IsDoubleHeadTag(Tag Tag) bool {
  86. switch strings.ToUpper(Tag.TagType) {
  87. case "FOR":
  88. return true
  89. case "IF":
  90. return true
  91. case "ELSE":
  92. return true
  93. case "ELSEIF":
  94. return true
  95. }
  96. return false
  97. }
  98. type TagDouble struct {
  99. HeadTag Tag //头部标签
  100. IsDoubleTag bool //是否为双标签
  101. EndTag Tag //结束标签
  102. OtherTag []Tag //同级并列标签,用于if标签
  103. SubTags []TagDouble //子标签
  104. }
  105. func (t TagDouble) String() string {
  106. return fmt.Sprintf("TagDouble{HeadTag:%s,IsDoubleTag:%t,EndTag:%s,OtherTag:%s,SubTags:%s}", t.HeadTag.String(), t.IsDoubleTag, t.EndTag.String(), t.OtherTag, t.SubTags)
  107. }
  108. // AnalysisTemplateTags 解析模板中所有的标签
  109. func AnalysisTemplateTags(tmpl string) ([]Tag, int) {
  110. var tags []Tag
  111. return FindTag(tmpl, tags, 0)
  112. }
  113. func FindTag(tmpl string, tags []Tag, startIndex int) ([]Tag, int) {
  114. if startIndex == -1 {
  115. return tags, -1
  116. }
  117. index := FindTagSign(tmpl, startIndex, true)
  118. if index == -1 {
  119. //完美
  120. return tags, -1
  121. }
  122. endIndex := FindTagSign(tmpl, index, false)
  123. if endIndex == -1 {
  124. return tags, -1
  125. }
  126. str := tmpl[index : endIndex+2]
  127. //正则判断
  128. if !AllTagReg.MatchString(str) {
  129. return tags, -1
  130. }
  131. compileRegex := regexp.MustCompile("\\{\\{ *(for|if|elseif|else|end|FOR|IF|ELSEIF|ELSE|END) *.* *}}")
  132. matchArr := compileRegex.FindStringSubmatch(str)
  133. tag := "ITEM"
  134. if len(matchArr) >= 2 {
  135. tag = matchArr[1]
  136. }
  137. tags = append(tags, Tag{
  138. Template: nil,
  139. Tag: str,
  140. StartIndex: startIndex,
  141. EndIndex: endIndex + 2,
  142. TagType: strings.ToUpper(tag),
  143. })
  144. return FindTag(tmpl, tags, endIndex+2)
  145. }
  146. //func AnalysisTemplate(tmpl string, startIndex int) (Template, int) {
  147. // template := Template{}
  148. // index := FindTagSign(tmpl, startIndex, true)
  149. // if index == -1 {
  150. // //完美
  151. // return template, -1
  152. // }
  153. // endIndex := FindTagSign(tmpl, startIndex, false)
  154. // if endIndex == -1 {
  155. // return template, -1
  156. // }
  157. // str := tmpl[index : endIndex+2]
  158. // //正则判断
  159. // if AllTagReg.MatchString(str) {
  160. // //提取标签
  161. // compileRegex := regexp.MustCompile("\\{\\{ *(for|if|elseif|else|end) +.* *}}")
  162. // matchArr := compileRegex.FindStringSubmatch(str)
  163. //
  164. // template.TagType = matchArr[1]
  165. // //匹配到
  166. // template.Tag = Tag{
  167. // Template: &template,
  168. // tag: "",
  169. // StartIndex: 0,
  170. // EndIndex: 0,
  171. // TagType: "",
  172. // }
  173. // template.Content = tmpl[index+2 : endIndex]
  174. // return template, endIndex + 2
  175. // }
  176. // return Template{}, index
  177. //}
  178. // FindTagSign 查找模板头部 todo 如果后续需要做转义处理,可以在这里完成
  179. func FindTagSign(tmpl string, startIndex int, isHead bool) int {
  180. if isHead {
  181. return IndexOf(tmpl, "{{", startIndex)
  182. }
  183. return IndexOf(tmpl, "}}", startIndex)
  184. }
  185. func IndexOf(s, subString string, startIndex int) int {
  186. str := s[startIndex:]
  187. index := strings.Index(str, subString)
  188. if index == -1 {
  189. return -1
  190. }
  191. return index + startIndex
  192. }
  193. func ReadFileToString() string {
  194. file, err := os.OpenFile("./service/struct.go.template", os.O_CREATE|os.O_RDWR, 0666)
  195. if err != nil {
  196. panic(err)
  197. }
  198. defer file.Close()
  199. reader := bufio.NewReader(file)
  200. var bytes = make([]byte, 1024)
  201. var strBytes = make([]byte, 0)
  202. for {
  203. n, err := reader.Read(bytes)
  204. if err != nil {
  205. panic(err)
  206. }
  207. strBytes = append(strBytes, bytes[:n]...)
  208. if n < 1024 {
  209. break
  210. }
  211. }
  212. return string(strBytes)
  213. }