From d7739491bdadf7b936095dcc2ba09f57e2f3a322 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Fri, 4 Jan 2019 11:19:00 +0700 Subject: Add option to preprocess http_url --- tagpreprocessor.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tagpreprocessor.go (limited to 'tagpreprocessor.go') 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 +} -- cgit v1.3