Skip to content

Commit

Permalink
Project init
Browse files Browse the repository at this point in the history
  • Loading branch information
uruba committed Dec 10, 2018
0 parents commit 4ae6b39
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
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
45 changes: 45 additions & 0 deletions config/definitions.go
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
}
1 change: 1 addition & 0 deletions definitions/_global.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

7 changes: 7 additions & 0 deletions definitions/cs_CZ.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
after:
- a
- s
- z
- za
- po
- do
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module nbspify

require gopkg.in/yaml.v2 v2.2.2
3 changes: 3 additions & 0 deletions go.sum
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=
36 changes: 36 additions & 0 deletions input/reader.go
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)
}
46 changes: 46 additions & 0 deletions main.go
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, &nbsperDefinitions.NbsperAround{})
nbspers = append(nbspers, &nbsperDefinitions.NbsperBefore{})
nbspers = append(nbspers, &nbsperDefinitions.NbsperAfter{})

return nbspers
}
6 changes: 6 additions & 0 deletions nbspers/nbsper.go
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
}
21 changes: 21 additions & 0 deletions nbspers/nbsper_after.go
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&nbsp;")
}
21 changes: 21 additions & 0 deletions nbspers/nbsper_around.go
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, "&nbsp;$1&nbsp;")
}
21 changes: 21 additions & 0 deletions nbspers/nbsper_before.go
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, "&nbsp;$1")
}

0 comments on commit 4ae6b39

Please sign in to comment.