RunTest.go 5.3 KB

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