forked from huanglizhuo/kotlin-in-chinese
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addSpace2Sharp.go
61 lines (57 loc) · 1.3 KB
/
addSpace2Sharp.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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
var listFile []string
func main() {
filepath.Walk(".", listFun)
// readLine("/Users/lizhuohuang/Workspace/b.md")
for _, name := range listFile {
addSpace2Sharp(name)
}
}
func addSpace2Sharp(filename string) {
modify := false
fmt.Println(filename)
tmpname := filename + ".tmp"
inFile, err := os.Open(filename)
fmt.Println("open:", err)
defer inFile.Close()
// reader := bufio.NewReader(inFile)
outfile, _ := os.Create(tmpname)
writer := bufio.NewWriter(outfile)
scanner := bufio.NewScanner(inFile)
scanner.Split(bufio.ScanLines)
reg := regexp.MustCompile(`^[#]{1,4}`)
for scanner.Scan() {
if len(reg.FindAllString(scanner.Text(), -1)) != 0 {
src := reg.FindString(scanner.Text())
modify = true
writer.WriteString(reg.ReplaceAllString(scanner.Text(), src+" "))
fmt.Println(src)
} else {
writer.WriteString(scanner.Text() + "\n")
}
}
if modify {
writer.Flush()
exec.Command("rm", filename).Run()
exec.Command("mv", tmpname, filename).Run()
} else {
exec.Command("rm", tmpname).Run()
}
}
func listFun(path string, f os.FileInfo, err error) error {
strRet, _ := os.Getwd()
strRet += "/" + path
if strings.HasSuffix(strRet, ".md") {
listFile = append(listFile, strRet)
}
return nil
}