diff options
| author | Shulhan <ms@kilabit.info> | 2019-01-04 11:19:00 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2019-01-04 15:15:35 +0700 |
| commit | d7739491bdadf7b936095dcc2ba09f57e2f3a322 (patch) | |
| tree | ccfdcc3e4042f4a976db57569a1037c4b59d4c76 /tagpreprocessor.go | |
| parent | 49e7d14699b299dffa835b2a9240e2f03e8d0dd3 (diff) | |
| download | haminer-d7739491bdadf7b936095dcc2ba09f57e2f3a322.tar.xz | |
Add option to preprocess http_url
Diffstat (limited to 'tagpreprocessor.go')
| -rw-r--r-- | tagpreprocessor.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/tagpreprocessor.go b/tagpreprocessor.go new file mode 100644 index 0000000..2be8846 --- /dev/null +++ b/tagpreprocessor.go @@ -0,0 +1,58 @@ +// Copyright 2019, M. Shulhan (ms@kilabit.info). All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package haminer + +import ( + "errors" + "regexp" + "strings" +) + +type tagPreprocessor struct { + name string + regex *regexp.Regexp + repl string +} + +// +// newTagPreprocessor create and initialize replace tag pre-processing. +// The regex and repl strings must be enclosed with double-quote, except for +// repl it can be empty. +// +func newTagPreprocessor(name, regex, repl string) ( + retag *tagPreprocessor, err error, +) { + name = strings.TrimSpace(name) + regex = strings.TrimSpace(regex) + repl = strings.TrimSpace(repl) + + if len(name) == 0 { + return nil, errors.New("newTagPreprocessor: empty name parameter") + } + if len(regex) == 0 { + return nil, errors.New("newTagPreprocessor: empty regex parameter") + } + + re, err := regexp.Compile(regex) + if err != nil { + return nil, err + } + + retag = &tagPreprocessor{ + name: name, + regex: re, + repl: repl, + } + + return retag, nil +} + +func (tagp *tagPreprocessor) preprocess(name, value string) string { + if tagp.name != name { + return value + } + out := tagp.regex.ReplaceAllString(value, tagp.repl) + return out +} |
