mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-28 17:19:06 +00:00
addlinks 改用 正则表达式 替换
This commit is contained in:
parent
a31f9355e0
commit
f4f5d8d70e
20
addlinks.go
20
addlinks.go
@ -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",
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user