addlinks 改用 正则表达式 替换

This commit is contained in:
chai2010 2016-01-05 15:10:36 +08:00
parent a31f9355e0
commit f4f5d8d70e

View File

@ -17,7 +17,6 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
@ -101,9 +100,9 @@ func convertFile(path string) (changed bool) {
return false return false
} }
newData := oldData newData := append([]byte{}, oldData...)
for k, v := range _LinksTable { for re, v := range _RegexpLinksTable {
newData = bytes.Replace(oldData, []byte(k), []byte(v), -1) newData = re.ReplaceAll(newData, []byte(v))
} }
if string(newData) == string(oldData) { if string(newData) == string(oldData) {
@ -117,6 +116,15 @@ func convertFile(path string) (changed bool) {
return true return true
} }
var _LinksTable = map[string]string{ var _RegexpLinksTable = func() map[*regexp.Regexp]string {
"[Alan A. A. Donovan]()": "[Alan A. A. Donovan](https://github.com/adonovan)", m := make(map[*regexp.Regexp]string)
for k, v := range _LinkTable {
reKey := regexp.MustCompile(regexp.QuoteMeta(`[`+k+`]`) + `\(\S*\)`)
m[reKey] = fmt.Sprintf("[%s](%s)", k, v)
}
return m
}()
var _LinkTable = map[string]string{
"Alan A. A. Donovan": "https://github.com/adonovan",
} }