-
Notifications
You must be signed in to change notification settings - Fork 1
/
changeset_writer.go
54 lines (46 loc) · 1.25 KB
/
changeset_writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package godiff
import (
"io"
"regexp"
"strings"
"text/template"
"github.com/seletskiy/tplutil"
)
var changesetTpl = template.New("changeset")
func init() {
commentsTpl := template.New("comment")
reBeginningOfLine := regexp.MustCompile(`(?m)^`)
reNewLine := regexp.MustCompile(`^|\n`)
reDanglingSpace := regexp.MustCompile(`(?m)\s+$`)
funcs := template.FuncMap{
"indent": func(input string) string {
return reBeginningOfLine.ReplaceAllString(input, " ")
},
"writeComments": func(input CommentsTree) string {
res, _ := tplutil.ExecuteToString(commentsTpl, input)
return res
},
"writeNote": func(input string) string {
return Note(input)
},
"trimWhitespace": func(input string) string {
return strings.TrimSpace(input)
},
"comment": func(input string) string {
//log.Printf("%#v", input)
return reDanglingSpace.ReplaceAllString(
reNewLine.ReplaceAllString(input, `$0# `),
``,
)
},
}
template.Must(
commentsTpl.Funcs(funcs).Funcs(tplutil.Last).Parse(
tplutil.Strip(commentsTplText)))
template.Must(
changesetTpl.Funcs(funcs).Funcs(tplutil.Last).Parse(
tplutil.Strip(changesetTplText)))
}
func WriteChangeset(changeset Changeset, to io.Writer) error {
return changesetTpl.Execute(to, changeset)
}