Skip to content

Commit

Permalink
Working version 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Marek Kowalczyk committed Jun 24, 2020
1 parent 46ceadc commit 446eaf9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 31 deletions.
10 changes: 10 additions & 0 deletions san.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
!/usr/bin/env bash

for oname in "$@"; do
filen=$(echo "${oname%.*}")
exten=$(echo "${oname##*.}")
nfilen=$(sanitize "$filen")
nexten=$(sanitize "$exten")
nname="$nfilen.$nexten"
mv -nv "$oname" "$nname"
done
53 changes: 41 additions & 12 deletions sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"log"
"regexp"
"strings"
"unicode"
"regexp"
"log"
"os"
"fmt"
)
Expand All @@ -20,14 +20,21 @@ func removeIllFormed(input string) (output string) {
output, _, _ = transform.String(runes.ReplaceIllFormed(), input)
return output
}
func replaceNonASCII(input string) (output string) {
replaceNonASCII := runes.Map(func(r rune) rune {
if !(r <= unicode.MaxASCII) {

func toLower(input string) (output string) {

output = strings.ToLower(input)
return output
}

func replaceNonAlphaNum(input string) (output string) {
replaceNonAlphaNum := runes.Map(func(r rune) rune {
if !unicode.IsLetter(r) && !unicode.IsDigit(r) {
return '-'
}
return r
})
output, _, _ = transform.String(replaceNonASCII, input)
output, _, _ = transform.String(replaceNonAlphaNum, input)
return output
}

Expand All @@ -39,7 +46,18 @@ func removeAccents(input string) (output string) {
return output
}

func replaceSpaces(input string) (output string) {
/*func replaceNonASCII(input string) (output string) {
replaceNonASCII := runes.Map(func(r rune) rune {
if !(r <= unicode.MaxASCII) {
return '-'
}
return r
})
output, _, _ = transform.String(replaceNonASCII, input)
return output
}
*/
/*func replaceSpaces(input string) (output string) {
replaceSpaces := runes.Map(func(r rune) rune {
if unicode.Is(unicode.Space, r) {
return '-'
Expand All @@ -48,9 +66,9 @@ func replaceSpaces(input string) (output string) {
})
output, _, _ = transform.String(replaceSpaces, input)
return output
}
}*/

func replacePunct(input string) (output string) {
/*func replacePunct(input string) (output string) {
replacePunct := runes.Map(func(r rune) rune {
if unicode.Is(unicode.Punct, r) {
return '-'
Expand All @@ -60,7 +78,7 @@ func replacePunct(input string) (output string) {
output, _, _ = transform.String(replacePunct, input)
return output
}

*/
func dedupHyp(input string) (output string) {
reg, err := regexp.Compile("-{2,}")
if err != nil {
Expand All @@ -77,7 +95,18 @@ func trimEnds(input string) (output string) {
return output
}

func sanitize(input string) (output string){
output = trimEnds(dedupHyp(replaceNonAlphaNum(removeAccents(toLower(removeIllFormed(input))))))
return output
}

func main() {
input := strings.ToLower(strings.Join(os.Args[1:], " "))
fmt.Println(trimEnds(dedupHyp(replacePunct(replaceSpaces(removeAccents(strings.ToLower(replaceNonASCII(removeIllFormed(input)))))))))

input := strings.Join(os.Args[1:], "-")

/* const input string = "Golang basics - writing unit tests"*/
/* fmt.Println(trimEnds(dedupHyp(replacePunct(replaceSpaces(removeAccents(strings.ToLower(replaceNonASCII(removeIllFormed(input)))))))))*/

fmt.Println(sanitize(input))

}
19 changes: 0 additions & 19 deletions sanitize.sh

This file was deleted.

0 comments on commit 446eaf9

Please sign in to comment.