-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4ae6b39
Showing
12 changed files
with
226 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# IDE | ||
.idea | ||
*.iml | ||
|
||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, build with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package config | ||
|
||
import ( | ||
"gopkg.in/yaml.v2" | ||
"io/ioutil" | ||
"log" | ||
"strings" | ||
) | ||
|
||
const DefinitionDir = "./definitions/" | ||
const DefinitionFileExt = "yaml" | ||
|
||
func LoadDefinitions() map[string][]string { | ||
fileList, err := ioutil.ReadDir(DefinitionDir) | ||
if err != nil { | ||
log.Fatalf("Could not list the definition folder: %v", err) | ||
} | ||
|
||
definitionMap := make(map[string][]string) | ||
|
||
for _, file := range fileList { | ||
fileName := file.Name() | ||
|
||
extension := fileName[strings.IndexByte(fileName, '.')+1:] | ||
if extension == DefinitionFileExt { | ||
fileContent, err := ioutil.ReadFile(DefinitionDir + fileName) | ||
if err != nil { | ||
log.Fatalf("Could not read a definition file: %v", err) | ||
} | ||
|
||
fileDefinitionChunk := make(map[string][]string) | ||
err = yaml.Unmarshal(fileContent, &fileDefinitionChunk) | ||
if err != nil { | ||
log.Fatalf("Could not parse a definition file: %v", err) | ||
} | ||
|
||
for definitionKey, definitionValues := range fileDefinitionChunk { | ||
// TODO - deduplication | ||
definitionMap[definitionKey] = append(definitionMap[definitionKey], definitionValues...) | ||
} | ||
} | ||
} | ||
|
||
return definitionMap | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
|
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
after: | ||
- a | ||
- s | ||
- z | ||
- za | ||
- po | ||
- do |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module nbspify | ||
|
||
require gopkg.in/yaml.v2 v2.2.2 |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= | ||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package input | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"log" | ||
"os" | ||
) | ||
|
||
func ReadInput() string { | ||
fileInfo, err := os.Stdin.Stat() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if fileInfo.Mode()&os.ModeCharDevice != 0 { | ||
log.Fatal("No input data, exiting...") | ||
} | ||
|
||
reader := bufio.NewReader(os.Stdin) | ||
|
||
var inputRead []rune | ||
for { | ||
input, _, err := reader.ReadRune() | ||
if err != nil && err == io.EOF { | ||
break | ||
} | ||
inputRead = append(inputRead, input) | ||
} | ||
|
||
if len(inputRead) < 0 { | ||
log.Fatal("No input data, exiting...") | ||
} | ||
|
||
return string(inputRead) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"nbspify/config" | ||
"nbspify/input" | ||
nbsperDefinitions "nbspify/nbspers" | ||
) | ||
|
||
func main() { | ||
programInput := input.ReadInput() | ||
if len(programInput) == 0 { | ||
log.Fatalf("The input is empty.") | ||
} | ||
|
||
processedText := programInput | ||
|
||
definitions := config.LoadDefinitions() | ||
|
||
nbspers := getNbspers() | ||
|
||
if nbspers != nil { | ||
for _, nbsper := range nbspers { | ||
definition := definitions[nbsper.GetCode()] | ||
|
||
if len(definition) > 0 { | ||
processedText = nbsper.Apply(processedText, definition) | ||
} | ||
} | ||
} | ||
|
||
// STDOUT | ||
fmt.Print(processedText) | ||
} | ||
|
||
func getNbspers() []nbsperDefinitions.Nbsper { | ||
nbspers := make([]nbsperDefinitions.Nbsper, 0) | ||
|
||
// The order matters as the output is modified in-place... | ||
nbspers = append(nbspers,  erDefinitions.NbsperAround{}) | ||
nbspers = append(nbspers,  erDefinitions.NbsperBefore{}) | ||
nbspers = append(nbspers,  erDefinitions.NbsperAfter{}) | ||
|
||
return nbspers | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package nbspers | ||
|
||
type Nbsper interface { | ||
GetCode() string | ||
Apply(input string, matchSegments []string) string | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package nbspers | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type NbsperAfter struct { | ||
} | ||
|
||
func (nbsperAfter *NbsperAfter) GetCode() string { | ||
return "after" | ||
} | ||
|
||
func (nbsperAfter *NbsperAfter) Apply(input string, matchSegments []string) string { | ||
words := strings.Join(matchSegments, "|") | ||
expression := regexp.MustCompile(fmt.Sprintf("(%s) ", words)) | ||
|
||
return expression.ReplaceAllString(input, "$1 ") | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package nbspers | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type NbsperAround struct { | ||
} | ||
|
||
func (nbsperAround *NbsperAround) GetCode() string { | ||
return "around" | ||
} | ||
|
||
func (nbsperAround *NbsperAround) Apply(input string, matchSegments []string) string { | ||
words := strings.Join(matchSegments, "|") | ||
expression := regexp.MustCompile(fmt.Sprintf(" (%s) ", words)) | ||
|
||
return expression.ReplaceAllString(input, " $1 ") | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package nbspers | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type NbsperBefore struct { | ||
} | ||
|
||
func (nbsperBefore *NbsperBefore) GetCode() string { | ||
return "before" | ||
} | ||
|
||
func (nbsperBefore *NbsperBefore) Apply(input string, matchSegments []string) string { | ||
words := strings.Join(matchSegments, "|") | ||
expression := regexp.MustCompile(fmt.Sprintf(" (%s)", words)) | ||
|
||
return expression.ReplaceAllString(input, " $1") | ||
} |