-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
70 lines (59 loc) · 1.62 KB
/
main.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* Copyright (c) 2024. LGBT-CN & KevinZonda
* This file is part of LGBT-CN Signature Counting.
*/
package main
import (
"fmt"
"github.com/KevinZonda/GoX/pkg/iox"
"github.com/KevinZonda/GoX/pkg/panicx"
"github.com/KevinZonda/GoX/pkg/stringx"
"os"
"strings"
)
const SIGN_BEGIN = "<!-- BEGIN LGBT-CN SIGNATURE -->"
const SIGN_END = "<!-- END LGBT-CN SIGNATURE -->"
const COUNT_BEGIN = "<!-- BEGIN LGBT-CN COUNT -->"
const COUNT_END = "<!-- END LGBT-CN COUNT -->"
func main() {
fileName := os.Args[1]
txt, err := iox.ReadAllText(fileName)
panicx.PanicIfNotNil(err, err)
count, txt := GetSigCount(txt)
txt = SetSigCount(txt, count)
err = iox.WriteAllText(fileName, txt)
}
func Split(content string, beginToken string, endToken string) (header string, body string, footer string) {
begins := strings.SplitN(content, beginToken, 2)
header = strings.TrimSpace(begins[0])
content = begins[1]
ends := strings.SplitN(content, endToken, 2)
body = strings.TrimSpace(ends[0])
footer = strings.TrimSpace(ends[1])
return
}
func GetSigCount(txt string) (int, string) {
header, content, footer := Split(txt, SIGN_BEGIN, SIGN_END)
bodyLines := strings.Split(content, "\n")
bodyLines = stringx.TrimAllAndClean(bodyLines)
content = strings.Join(bodyLines, "\n")
txt = strings.Join([]string{
header,
SIGN_BEGIN,
content,
SIGN_END,
footer,
}, "\n")
return len(bodyLines), txt
}
func SetSigCount(txt string, count int) string {
header, _, footer := Split(txt, COUNT_BEGIN, COUNT_END)
return strings.Join(
[]string{
header,
COUNT_BEGIN,
fmt.Sprintf("已有 %d 人签署!", count),
COUNT_END,
footer,
}, "\n")
}