From f4f5d8d70e1cad724d6a7e0babe582435972b117 Mon Sep 17 00:00:00 2001 From: chai2010 Date: Tue, 5 Jan 2016 15:10:36 +0800 Subject: [PATCH] =?UTF-8?q?addlinks=20=E6=94=B9=E7=94=A8=20=E6=AD=A3?= =?UTF-8?q?=E5=88=99=E8=A1=A8=E8=BE=BE=E5=BC=8F=20=E6=9B=BF=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addlinks.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/addlinks.go b/addlinks.go index 580a7df..061dd91 100644 --- a/addlinks.go +++ b/addlinks.go @@ -17,7 +17,6 @@ package main import ( - "bytes" "fmt" "io/ioutil" "log" @@ -101,9 +100,9 @@ func convertFile(path string) (changed bool) { return false } - newData := oldData - for k, v := range _LinksTable { - newData = bytes.Replace(oldData, []byte(k), []byte(v), -1) + newData := append([]byte{}, oldData...) + for re, v := range _RegexpLinksTable { + newData = re.ReplaceAll(newData, []byte(v)) } if string(newData) == string(oldData) { @@ -117,6 +116,15 @@ func convertFile(path string) (changed bool) { return true } -var _LinksTable = map[string]string{ - "[Alan A. A. Donovan]()": "[Alan A. A. Donovan](https://github.com/adonovan)", +var _RegexpLinksTable = func() map[*regexp.Regexp]string { + 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", }