summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Manuel <nilslice@gmail.com>2018-10-09 00:59:07 -0600
committerGitHub <noreply@github.com>2018-10-09 00:59:07 -0600
commit4b6e77b4eb70928531a84448bef26fdc9de70307 (patch)
treee9cd6a0ead23bb92c5a8e8e7b2f81410d129d0af
parent6efa91347dc7e43e61f48d459925b81ae933732e (diff)
parent75d6ae9a1e89ff212e269949a690024e3e362a52 (diff)
Merge pull request #284 from ponzu-cms/ponzu-dev
regex fixes in editor ui + compile optimization
-rw-r--r--system/admin/static/common/js/util.js2
-rw-r--r--system/item/item.go47
2 files changed, 22 insertions, 27 deletions
diff --git a/system/admin/static/common/js/util.js b/system/admin/static/common/js/util.js
index 8d5e74b..b126f2f 100644
--- a/system/admin/static/common/js/util.js
+++ b/system/admin/static/common/js/util.js
@@ -8,8 +8,6 @@ function replaceBadChars(text) {
s = s.replace(/[\u201C\u201D\u201E]/g, "\"");
// ellipsis
s = s.replace(/\u2026/g, "...");
- // dashes
- s = s.replace(/[\u2013\u2014]/g, "-");
// circumflex
s = s.replace(/\u02C6/g, "^");
// open angle bracket
diff --git a/system/item/item.go b/system/item/item.go
index 32d38f7..1108ae0 100644
--- a/system/item/item.go
+++ b/system/item/item.go
@@ -17,6 +17,24 @@ import (
"golang.org/x/text/unicode/norm"
)
+var rxList map[*regexp.Regexp][]byte
+
+func init() {
+ // Compile regex once to use in stringToSlug().
+ // We store the compiled regex as the key
+ // and assign the replacement as the map's value.
+ rxList = map[*regexp.Regexp][]byte{
+ regexp.MustCompile("`[-]+`"): []byte("-"),
+ regexp.MustCompile("[[:space:]]"): []byte("-"),
+ regexp.MustCompile("[[:blank:]]"): []byte(""),
+ regexp.MustCompile("`[^a-z0-9]`i"): []byte("-"),
+ regexp.MustCompile("[!/:-@[-`{-~]"): []byte(""),
+ regexp.MustCompile("/[^\x20-\x7F]/"): []byte(""),
+ regexp.MustCompile("`&(amp;)?#?[a-z0-9]+;`i"): []byte("-"),
+ regexp.MustCompile("`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i"): []byte("\\1"),
+ }
+}
+
// Sluggable makes a struct locatable by URL with it's own path.
// As an Item implementing Sluggable, slugs may overlap. If this is an issue,
// make your content struct (or one which embeds Item) implement Sluggable
@@ -316,31 +334,10 @@ func isMn(r rune) bool {
func stringToSlug(s string) (string, error) {
src := []byte(strings.ToLower(s))
- // convert all spaces to dash
- rx := regexp.MustCompile("[[:space:]]")
- src = rx.ReplaceAll(src, []byte("-"))
-
- // remove all blanks such as tab
- rx = regexp.MustCompile("[[:blank:]]")
- src = rx.ReplaceAll(src, []byte(""))
-
- rx = regexp.MustCompile("[!/:-@[-`{-~]")
- src = rx.ReplaceAll(src, []byte(""))
-
- rx = regexp.MustCompile("/[^\x20-\x7F]/")
- src = rx.ReplaceAll(src, []byte(""))
-
- rx = regexp.MustCompile("`&(amp;)?#?[a-z0-9]+;`i")
- src = rx.ReplaceAll(src, []byte("-"))
-
- rx = regexp.MustCompile("`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i")
- src = rx.ReplaceAll(src, []byte("\\1"))
-
- rx = regexp.MustCompile("`[^a-z0-9]`i")
- src = rx.ReplaceAll(src, []byte("-"))
-
- rx = regexp.MustCompile("`[-]+`")
- src = rx.ReplaceAll(src, []byte("-"))
+ // Range over compiled regex and replacements from init().
+ for rx := range rxList {
+ src = rx.ReplaceAll(src, rxList[rx])
+ }
str := strings.Replace(string(src), "'", "", -1)
str = strings.Replace(str, `"`, "", -1)