hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

transform.go (1999B)

    1 // Copyright 2019 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 text
   15 
   16 import (
   17 	"strings"
   18 	"sync"
   19 	"unicode"
   20 
   21 	"golang.org/x/text/runes"
   22 	"golang.org/x/text/transform"
   23 	"golang.org/x/text/unicode/norm"
   24 )
   25 
   26 var accentTransformerPool = &sync.Pool{
   27 	New: func() any {
   28 		return transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
   29 	},
   30 }
   31 
   32 // RemoveAccents removes all accents from b.
   33 func RemoveAccents(b []byte) []byte {
   34 	t := accentTransformerPool.Get().(transform.Transformer)
   35 	b, _, _ = transform.Bytes(t, b)
   36 	t.Reset()
   37 	accentTransformerPool.Put(t)
   38 	return b
   39 }
   40 
   41 // RemoveAccentsString removes all accents from s.
   42 func RemoveAccentsString(s string) string {
   43 	t := accentTransformerPool.Get().(transform.Transformer)
   44 	s, _, _ = transform.String(t, s)
   45 	t.Reset()
   46 	accentTransformerPool.Put(t)
   47 	return s
   48 }
   49 
   50 // Chomp removes trailing newline characters from s.
   51 func Chomp(s string) string {
   52 	return strings.TrimRightFunc(s, func(r rune) bool {
   53 		return r == '\n' || r == '\r'
   54 	})
   55 }
   56 
   57 // Puts adds a trailing \n none found.
   58 func Puts(s string) string {
   59 	if s == "" || s[len(s)-1] == '\n' {
   60 		return s
   61 	}
   62 	return s + "\n"
   63 }
   64 
   65 // VisitLinesAfter calls the given function for each line, including newlines, in the given string.
   66 func VisitLinesAfter(s string, fn func(line string)) {
   67 	high := strings.IndexRune(s, '\n')
   68 	for high != -1 {
   69 		fn(s[:high+1])
   70 		s = s[high+1:]
   71 
   72 		high = strings.IndexRune(s, '\n')
   73 	}
   74 
   75 	if s != "" {
   76 		fn(s)
   77 	}
   78 }