|
|
@@ -19,90 +19,183 @@ import (
|
|
|
// - {{.item}}
|
|
|
// 解析模板
|
|
|
func RunTest() {
|
|
|
-
|
|
|
tmpl := ReadFileToString()
|
|
|
- //compileRegex := regexp.MustCompile("{{2} *(for *\\w+ +in +\\.\\w+)|(if +\\.\\w+)|(else)|(else +if +\\.\\w+) *}{2}") // 正则表达式的分组,以括号()表示,每一对括号就是我们匹配到的一个文本,可以把他们提取出来。
|
|
|
- //matchArr := compileRegex.FindStringSubmatch(tmpl) // FindStringSubmatch 方法是提取出匹配的字符串,然后通过[]string返回。我们可以看到,第1个匹配到的是这个字符串本身,从第2个开始,才是我们想要的字符串。
|
|
|
- //if len(matchArr) > 0 {
|
|
|
- // fmt.Println("提取字符串内容:", matchArr[len(matchArr)-1]) // 输出:蜜桃乌龙茶
|
|
|
- //}
|
|
|
- var err error
|
|
|
- AllTagReg, err = regexp.Compile("^{{2} *(for *\\w+ +in +\\.\\w+)|(if +\\.\\w+)|(else)|(else +if +\\.\\w+) *}{2}$")
|
|
|
- if err != nil {
|
|
|
- panic(err)
|
|
|
- }
|
|
|
- ForReg, err = regexp.Compile("^{{2} *for *\\w+ +in +\\.\\w+ *}{2}$")
|
|
|
- if err != nil {
|
|
|
- panic(err)
|
|
|
- }
|
|
|
- IfReg, err = regexp.Compile("^{{2} *if +\\.\\w+ *}{2}$")
|
|
|
- if err != nil {
|
|
|
- panic(err)
|
|
|
- }
|
|
|
- IfElseReg, err = regexp.Compile("^{{2} *else +if +\\.\\w+ *}{2}$")
|
|
|
- if err != nil {
|
|
|
- panic(err)
|
|
|
+ tags, _ := AnalysisTemplateTags(tmpl)
|
|
|
+ //end, i := GetEnd(tags, 15, len(tags))
|
|
|
+ //fmt.Println(end, i)
|
|
|
+ read := Read(tags)
|
|
|
+ fmt.Println(read)
|
|
|
+ //FindDoubleTagEnd(tags)
|
|
|
+ //template, _ := AnalysisTemplate(tmpl, tags)
|
|
|
+ //fmt.Println(template)
|
|
|
+}
|
|
|
+
|
|
|
+func Read(tags []Tag) []TagDouble {
|
|
|
+ tds := make([]TagDouble, 0)
|
|
|
+ var end TagDouble
|
|
|
+ var i = 0
|
|
|
+ //首层
|
|
|
+ for i < len(tags) {
|
|
|
+ end, i = GetEnd(tags, i, len(tags))
|
|
|
+
|
|
|
+ if end.IsDoubleTag && len(end.OtherTag) > 0 {
|
|
|
+ end.SubTags = append(end.SubTags, Read(end.OtherTag)...)
|
|
|
+ fmt.Print("")
|
|
|
+ }
|
|
|
+ tds = append(tds, end)
|
|
|
}
|
|
|
- ElseReg, err = regexp.Compile("^{{2} *else *}{2}$")
|
|
|
- if err != nil {
|
|
|
- panic(err)
|
|
|
+ return tds
|
|
|
+}
|
|
|
+
|
|
|
+// GetEnd 结果,返回数组坐标
|
|
|
+func GetEnd(tags []Tag, startIndex, endIndex int) (TagDouble, int) {
|
|
|
+ fmt.Println(tags, startIndex, endIndex)
|
|
|
+ sign := 0
|
|
|
+ otherTags := make([]Tag, 0)
|
|
|
+ head := tags[startIndex]
|
|
|
+ if head.TagType == "ITEM" {
|
|
|
+ return TagDouble{
|
|
|
+ HeadTag: tags[startIndex],
|
|
|
+ IsDoubleTag: false,
|
|
|
+ EndTag: Tag{},
|
|
|
+ OtherTag: nil,
|
|
|
+ SubTags: nil,
|
|
|
+ }, startIndex + 1
|
|
|
}
|
|
|
- End, err = regexp.Compile("^{{2} *end *}{2}$")
|
|
|
- if err != nil {
|
|
|
- panic(err)
|
|
|
+
|
|
|
+ for i := startIndex + 1; i < endIndex; i++ {
|
|
|
+ tag := tags[i]
|
|
|
+ if tag.TagType == "IF" || tag.TagType == "FOR" {
|
|
|
+ otherTags = append(otherTags, tag)
|
|
|
+ sign += 1
|
|
|
+ } else if tag.TagType == "END" {
|
|
|
+
|
|
|
+ if sign == 0 {
|
|
|
+ return TagDouble{
|
|
|
+ HeadTag: head,
|
|
|
+ IsDoubleTag: true,
|
|
|
+ EndTag: tag,
|
|
|
+ OtherTag: otherTags,
|
|
|
+ SubTags: nil,
|
|
|
+ }, i + 1
|
|
|
+ } else {
|
|
|
+ sign -= 1
|
|
|
+ otherTags = append(otherTags, tag)
|
|
|
+ }
|
|
|
+ } else if tag.TagType == "ITEM" {
|
|
|
+ otherTags = append(otherTags, tag)
|
|
|
+ } else {
|
|
|
+ //panic("其他标签不能作为首标签")
|
|
|
+ }
|
|
|
}
|
|
|
- Item, err = regexp.Compile("^{{2} *\\.\\w+ *}{2}$")
|
|
|
+ panic("在咋回事?3")
|
|
|
+}
|
|
|
|
|
|
- template, index, err := AnalysisTemplate(tmpl, 0)
|
|
|
- if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
+func IsDoubleHeadTag(Tag Tag) bool {
|
|
|
+ switch strings.ToUpper(Tag.TagType) {
|
|
|
+ case "FOR":
|
|
|
+ return true
|
|
|
+ case "IF":
|
|
|
+ return true
|
|
|
+ case "ELSE":
|
|
|
+ return true
|
|
|
+ case "ELSEIF":
|
|
|
+ return true
|
|
|
}
|
|
|
- fmt.Println(template, index)
|
|
|
+ return false
|
|
|
}
|
|
|
|
|
|
-var AllTagReg *regexp.Regexp
|
|
|
-var ForReg *regexp.Regexp
|
|
|
-var IfReg *regexp.Regexp
|
|
|
-var IfElseReg *regexp.Regexp
|
|
|
-var ElseReg *regexp.Regexp
|
|
|
-var End *regexp.Regexp
|
|
|
-var Item *regexp.Regexp
|
|
|
-
|
|
|
-type Template struct {
|
|
|
- Tag string //标签
|
|
|
- TagType string //标签类型 for if
|
|
|
- Content string //
|
|
|
- SubTemplate []Template
|
|
|
+type TagDouble struct {
|
|
|
+ HeadTag Tag //头部标签
|
|
|
+ IsDoubleTag bool //是否为双标签
|
|
|
+ EndTag Tag //结束标签
|
|
|
+ OtherTag []Tag //同级并列标签,用于if标签
|
|
|
+ SubTags []TagDouble //子标签
|
|
|
}
|
|
|
|
|
|
-func (t Template) String() string {
|
|
|
- return fmt.Sprintf("Tag:%s,TagType:%s,Content:%s,SubTemplate:%s", t.Tag, t.TagType, t.Content, t.SubTemplate)
|
|
|
+func (t TagDouble) String() string {
|
|
|
+ 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)
|
|
|
}
|
|
|
|
|
|
-// AnalysisTemplate
|
|
|
-func AnalysisTemplate(tmpl string, startIndex int) (Template, int, error) {
|
|
|
- template := Template{}
|
|
|
- index := IndexOf(tmpl, "{{", startIndex)
|
|
|
+// AnalysisTemplateTags 解析模板中所有的标签
|
|
|
+func AnalysisTemplateTags(tmpl string) ([]Tag, int) {
|
|
|
+ var tags []Tag
|
|
|
+ return FindTag(tmpl, tags, 0)
|
|
|
+}
|
|
|
+
|
|
|
+func FindTag(tmpl string, tags []Tag, startIndex int) ([]Tag, int) {
|
|
|
+ if startIndex == -1 {
|
|
|
+ return tags, -1
|
|
|
+ }
|
|
|
+ index := FindTagSign(tmpl, startIndex, true)
|
|
|
if index == -1 {
|
|
|
//完美
|
|
|
- return template, -1, nil
|
|
|
+ return tags, -1
|
|
|
}
|
|
|
- endIndex := IndexOf(tmpl, "}}", index+1)
|
|
|
+ endIndex := FindTagSign(tmpl, index, false)
|
|
|
if endIndex == -1 {
|
|
|
- return template, -1, nil
|
|
|
+ return tags, -1
|
|
|
}
|
|
|
str := tmpl[index : endIndex+2]
|
|
|
//正则判断
|
|
|
- if AllTagReg.MatchString(str) {
|
|
|
- compileRegex := regexp.MustCompile("\\{\\{ *(for|if|elseif|else|end) +.* *}}")
|
|
|
- matchArr := compileRegex.FindStringSubmatch(str)
|
|
|
- template.TagType = matchArr[1]
|
|
|
- //匹配到
|
|
|
- template.Tag = str
|
|
|
- template.Content = tmpl[index+2 : endIndex]
|
|
|
- return template, endIndex + 2, nil
|
|
|
+ if !AllTagReg.MatchString(str) {
|
|
|
+ return tags, -1
|
|
|
+ }
|
|
|
+ compileRegex := regexp.MustCompile("\\{\\{ *(for|if|elseif|else|end|FOR|IF|ELSEIF|ELSE|END) *.* *}}")
|
|
|
+ matchArr := compileRegex.FindStringSubmatch(str)
|
|
|
+ tag := "ITEM"
|
|
|
+ if len(matchArr) >= 2 {
|
|
|
+ tag = matchArr[1]
|
|
|
+ }
|
|
|
+ tags = append(tags, Tag{
|
|
|
+ Template: nil,
|
|
|
+ Tag: str,
|
|
|
+ StartIndex: startIndex,
|
|
|
+ EndIndex: endIndex + 2,
|
|
|
+ TagType: strings.ToUpper(tag),
|
|
|
+ })
|
|
|
+ return FindTag(tmpl, tags, endIndex+2)
|
|
|
+}
|
|
|
+
|
|
|
+//func AnalysisTemplate(tmpl string, startIndex int) (Template, int) {
|
|
|
+// template := Template{}
|
|
|
+// index := FindTagSign(tmpl, startIndex, true)
|
|
|
+// if index == -1 {
|
|
|
+// //完美
|
|
|
+// return template, -1
|
|
|
+// }
|
|
|
+// endIndex := FindTagSign(tmpl, startIndex, false)
|
|
|
+// if endIndex == -1 {
|
|
|
+// return template, -1
|
|
|
+// }
|
|
|
+// str := tmpl[index : endIndex+2]
|
|
|
+// //正则判断
|
|
|
+// if AllTagReg.MatchString(str) {
|
|
|
+// //提取标签
|
|
|
+// compileRegex := regexp.MustCompile("\\{\\{ *(for|if|elseif|else|end) +.* *}}")
|
|
|
+// matchArr := compileRegex.FindStringSubmatch(str)
|
|
|
+//
|
|
|
+// template.TagType = matchArr[1]
|
|
|
+// //匹配到
|
|
|
+// template.Tag = Tag{
|
|
|
+// Template: &template,
|
|
|
+// tag: "",
|
|
|
+// StartIndex: 0,
|
|
|
+// EndIndex: 0,
|
|
|
+// TagType: "",
|
|
|
+// }
|
|
|
+// template.Content = tmpl[index+2 : endIndex]
|
|
|
+// return template, endIndex + 2
|
|
|
+// }
|
|
|
+// return Template{}, index
|
|
|
+//}
|
|
|
+
|
|
|
+// FindTagSign 查找模板头部 todo 如果后续需要做转义处理,可以在这里完成
|
|
|
+func FindTagSign(tmpl string, startIndex int, isHead bool) int {
|
|
|
+ if isHead {
|
|
|
+ return IndexOf(tmpl, "{{", startIndex)
|
|
|
}
|
|
|
- return Template{}, index, nil
|
|
|
+ return IndexOf(tmpl, "}}", startIndex)
|
|
|
}
|
|
|
|
|
|
func IndexOf(s, subString string, startIndex int) int {
|
|
|
@@ -122,15 +215,16 @@ func ReadFileToString() string {
|
|
|
defer file.Close()
|
|
|
reader := bufio.NewReader(file)
|
|
|
var bytes = make([]byte, 1024)
|
|
|
+ var strBytes = make([]byte, 0)
|
|
|
for {
|
|
|
n, err := reader.Read(bytes)
|
|
|
if err != nil {
|
|
|
panic(err)
|
|
|
}
|
|
|
- bytes = append(bytes, bytes[:n]...)
|
|
|
+ strBytes = append(strBytes, bytes[:n]...)
|
|
|
if n < 1024 {
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
- return string(bytes)
|
|
|
+ return string(strBytes)
|
|
|
}
|