github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/tpl/strings/regexp.go (about)

     1  // Copyright 2017 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package strings
    15  
    16  import (
    17  	"github.com/gohugoio/hugo/common/hstrings"
    18  	"github.com/spf13/cast"
    19  )
    20  
    21  // FindRE returns a list of strings that match the regular expression. By default all matches
    22  // will be included. The number of matches can be limited with an optional third parameter.
    23  func (ns *Namespace) FindRE(expr string, content any, limit ...any) ([]string, error) {
    24  	re, err := hstrings.GetOrCompileRegexp(expr)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	conv, err := cast.ToStringE(content)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	if len(limit) == 0 {
    35  		return re.FindAllString(conv, -1), nil
    36  	}
    37  
    38  	lim, err := cast.ToIntE(limit[0])
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	return re.FindAllString(conv, lim), nil
    44  }
    45  
    46  // FindRESubmatch returns a slice of all successive matches of the regular
    47  // expression in content. Each element is a slice of strings holding the text
    48  // of the leftmost match of the regular expression and the matches, if any, of
    49  // its subexpressions.
    50  //
    51  // By default all matches will be included. The number of matches can be
    52  // limited with the optional limit parameter. A return value of nil indicates
    53  // no match.
    54  func (ns *Namespace) FindRESubmatch(expr string, content any, limit ...any) ([][]string, error) {
    55  	re, err := hstrings.GetOrCompileRegexp(expr)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	conv, err := cast.ToStringE(content)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	n := -1
    65  	if len(limit) > 0 {
    66  		n, err = cast.ToIntE(limit[0])
    67  		if err != nil {
    68  			return nil, err
    69  		}
    70  	}
    71  
    72  	return re.FindAllStringSubmatch(conv, n), nil
    73  
    74  }
    75  
    76  // ReplaceRE returns a copy of s, replacing all matches of the regular
    77  // expression pattern with the replacement text repl. The number of replacements
    78  // can be limited with an optional fourth parameter.
    79  func (ns *Namespace) ReplaceRE(pattern, repl, s any, n ...any) (_ string, err error) {
    80  	sp, err := cast.ToStringE(pattern)
    81  	if err != nil {
    82  		return
    83  	}
    84  
    85  	sr, err := cast.ToStringE(repl)
    86  	if err != nil {
    87  		return
    88  	}
    89  
    90  	ss, err := cast.ToStringE(s)
    91  	if err != nil {
    92  		return
    93  	}
    94  
    95  	nn := -1
    96  	if len(n) > 0 {
    97  		nn, err = cast.ToIntE(n[0])
    98  		if err != nil {
    99  			return
   100  		}
   101  	}
   102  
   103  	re, err := hstrings.GetOrCompileRegexp(sp)
   104  	if err != nil {
   105  		return "", err
   106  	}
   107  
   108  	return re.ReplaceAllStringFunc(ss, func(str string) string {
   109  		if nn == 0 {
   110  			return str
   111  		}
   112  
   113  		nn -= 1
   114  		return re.ReplaceAllString(str, sr)
   115  	}), nil
   116  }