forked from q191201771/naza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (84 loc) · 2.89 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2019, Chef. All rights reserved.
// https://github.com/cfeeling/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package main
import (
"bytes"
"flag"
"fmt"
"os"
"github.com/cfeeling/naza/pkg/filebatch"
"github.com/cfeeling/naza/pkg/nazalog"
)
//var licenseTmpl = `
//> **原文链接:** [https://pengrl.com/p/%s/](https://pengrl.com/p/%s/)
//> **原文出处:** [yoko blog](https://pengrl.com) (https://pengrl.com)
//> **原文作者:** [yoko](https://github.com/q191201771) (https://github.com/q191201771)
//> **版权声明:** 本文欢迎任何形式转载,转载时完整保留本声明信息(包含原文链接、原文出处、原文作者、版权声明)即可。本文后续所有修改都会第一时间在原始地址更新。
//
//![fccxy](https://pengrl.com/images/fccxy_qccode_and_sys.jpg)`
var licenseTmpl = `
本文完,作者[yoko](https://github.com/q191201771),尊重劳动人民成果,转载请注明原文出处: [https://pengrl.com/p/%s/](https://pengrl.com/p/%s/)`
func main() {
dir := parseFlag()
//linesOfLicense := strings.Split(licenseTmpl, "\n")
//lastLineOfLicense := linesOfLicense[len(linesOfLicense)-1]
var (
skipCount int
modCount int
)
err2 := filebatch.Walk(dir, true, ".md", func(path string, info os.FileInfo, content []byte, err error) []byte {
if err != nil {
nazalog.Warnf("read file failed. file=%s, err=%+v", path, err)
return nil
}
lines := bytes.Split(content, []byte{'\n'})
//if bytes.Index(lines[len(lines)-1], []byte("本文完")) != -1 ||
// res, err := filebatch.DeleteLines(content, filebatch.LineRange{From: -1, To: -1})
// nazalog.Debugf("%s -2", info.Name())
// nazalog.FatalIfErrorNotNil(err)
// return res
//}
//nazalog.Warnf("%s", info.Name())
//return content
// 已添加过声明,不用再添加了
if bytes.Index(lines[len(lines)-1], []byte("本文完,作者")) != -1 ||
bytes.Index(lines[len(lines)-2], []byte("本文完,作者")) != -1 {
nazalog.Debug(info.Name())
skipCount++
return nil
}
// 获取该文章的url的地址
var abbrlink string
for _, line := range lines {
if bytes.Index(line, []byte("abbrlink")) != -1 {
abbrlink = string(bytes.TrimSpace(bytes.Split(line, []byte{':'})[1]))
nazalog.Debug(abbrlink)
break
}
}
if abbrlink == "" {
nazalog.Errorf("abbrlink not exist. path=%s", path)
os.Exit(1)
}
// 构造好license信息,并添加在文章末尾
modCount++
license := fmt.Sprintf(licenseTmpl, abbrlink, abbrlink)
return filebatch.AddTailContent(content, []byte(license))
})
nazalog.Assert(nil, err2)
nazalog.Infof("count. mod=%d, skip=%d", modCount, skipCount)
}
func parseFlag() string {
dir := flag.String("d", "", "dir of posts")
flag.Parse()
if *dir == "" {
flag.Usage()
os.Exit(1)
}
return *dir
}