-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
435 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,81 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"io/ioutil" | ||
"regexp" | ||
"strings" | ||
"unicode" | ||
|
||
"github.com/mingrammer/casec" | ||
"github.com/mingrammer/cfmt" | ||
) | ||
|
||
func isCaseSeparator(r rune) bool { | ||
return r == casec.SnakeDelimiter || r == casec.KebabDelimiter | ||
} | ||
|
||
func convertWord(word, from, to string) (string, error) { | ||
if from != "" { | ||
is, err := casec.IsCaseOf(from, word) | ||
if err != nil { | ||
return "", err | ||
} | ||
if !is { | ||
return word, nil | ||
func convertWord(word, src, tgt string) string { | ||
if src != "" { | ||
if is, _ := casec.IsCaseOf(src, word); is { | ||
word, _ = casec.ToCaseFor(tgt, word) | ||
} | ||
} | ||
return casec.ToCaseFor(to, word) | ||
return word | ||
} | ||
|
||
func convertText(text, from, to string, ignore []string) (string, error) { | ||
reExpr := "^$" | ||
if len(ignore) > 0 { | ||
reExpr = strings.Join(ignore, "|") | ||
func convertText(text, src, tgt string, start, end int, ignoreRe *regexp.Regexp) string { | ||
var convLines []string | ||
lines := strings.Split(text, "\n") | ||
if start <= 0 { | ||
start = 1 | ||
} | ||
re, err := regexp.Compile(reExpr) | ||
if err != nil { | ||
return "", errors.New(cfmt.Serrorf("%s is not valid patten", reExpr)) | ||
if end <= 0 { | ||
end = len(lines) | ||
} | ||
out := strings.Builder{} | ||
chunk := strings.Builder{} | ||
size := len(text) | ||
lastLetter := false | ||
for i, r := range text { | ||
if unicode.IsLetter(r) || isCaseSeparator(r) { | ||
chunk.WriteRune(r) | ||
if i < size-1 { | ||
continue | ||
} | ||
lastLetter = true | ||
} | ||
if chunk.Len() > 0 { | ||
word := chunk.String() | ||
if !re.MatchString(word) { | ||
word, err = convertWord(word, from, to) | ||
if err != nil { | ||
return "", err | ||
for i, line := range lines { | ||
if i >= start-1 && i <= end-1 { | ||
conv := strings.Builder{} | ||
chunk := strings.Builder{} | ||
size := len(line) | ||
lastLetter := false | ||
for j, r := range line { | ||
if unicode.IsLetter(r) || isCaseSeparator(r) { | ||
chunk.WriteRune(r) | ||
if j < size-1 { | ||
continue | ||
} | ||
lastLetter = true | ||
} | ||
if chunk.Len() > 0 { | ||
word := chunk.String() | ||
if !ignoreRe.MatchString(word) { | ||
word = convertWord(word, src, tgt) | ||
} | ||
conv.WriteString(word) | ||
if lastLetter { | ||
break | ||
} | ||
chunk.Reset() | ||
} | ||
conv.WriteRune(r) | ||
} | ||
out.WriteString(word) | ||
if lastLetter { | ||
break | ||
} | ||
chunk.Reset() | ||
line = conv.String() | ||
} | ||
out.WriteRune(r) | ||
convLines = append(convLines, line) | ||
} | ||
return out.String(), nil | ||
out := strings.Join(convLines, "\n") | ||
return out | ||
} | ||
|
||
func convertFromText(text, from, to string, ignore []string) (string, string, error) { | ||
converted, err := convertText(text, from, to, ignore) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
return text, converted, nil | ||
func convertFromText(text, src, tgt string, start, end int, ignoreRe *regexp.Regexp) (string, string) { | ||
conv := convertText(text, src, tgt, start, end, ignoreRe) | ||
return text, conv | ||
} | ||
|
||
func convertFromFile(fileName, from, to string, ignore []string) (string, string, error) { | ||
func convertFromFile(fileName, src, tgt string, start, end int, ignoreRe *regexp.Regexp) (string, string, error) { | ||
b, err := ioutil.ReadFile(fileName) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
return convertFromText(string(b), from, to, ignore) | ||
text, conv := convertFromText(string(b), src, tgt, start, end, ignoreRe) | ||
return text, conv, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.