generated from VLADIMIR/template
clear
This commit is contained in:
parent
caaed14ebc
commit
5ab7ae0fcd
@ -5,6 +5,8 @@ import (
|
|||||||
"embed"
|
"embed"
|
||||||
"evening_detective/internal/app"
|
"evening_detective/internal/app"
|
||||||
"evening_detective/internal/config"
|
"evening_detective/internal/config"
|
||||||
|
"evening_detective/internal/modules/cleaner"
|
||||||
|
"evening_detective/internal/modules/formatter"
|
||||||
"evening_detective/internal/modules/link"
|
"evening_detective/internal/modules/link"
|
||||||
"evening_detective/internal/modules/password"
|
"evening_detective/internal/modules/password"
|
||||||
"evening_detective/internal/modules/pdf"
|
"evening_detective/internal/modules/pdf"
|
||||||
@ -44,8 +46,13 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cleaner := cleaner.NewCleaner()
|
||||||
|
|
||||||
|
formatter := formatter.NewFormatter()
|
||||||
|
|
||||||
storyFilepath := config.GetStoryFilepath()
|
storyFilepath := config.GetStoryFilepath()
|
||||||
storyService, err := story_service.NewStoryService(storyFilepath)
|
|
||||||
|
storyService, err := story_service.NewStoryService(cleaner, formatter, storyFilepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|||||||
6
internal/modules/cleaner/interface.go
Normal file
6
internal/modules/cleaner/interface.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package cleaner
|
||||||
|
|
||||||
|
type ICleaner interface {
|
||||||
|
// ([Ы-1]) -> ы1, Ы-1 -> ы1
|
||||||
|
ClearCode(code string) string
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package story_service
|
package cleaner
|
||||||
|
|
||||||
import "strings"
|
import "strings"
|
||||||
|
|
||||||
@ -20,15 +20,17 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func clearMatch(s string) string {
|
type service struct{}
|
||||||
s = strings.TrimPrefix(s, "(")
|
|
||||||
s = strings.TrimPrefix(s, "[")
|
func NewCleaner() ICleaner {
|
||||||
s = strings.TrimSuffix(s, ")")
|
return &service{}
|
||||||
s = strings.TrimSuffix(s, "]")
|
|
||||||
return clearCode(s)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func clearCode(code string) string {
|
func (s *service) ClearCode(code string) string {
|
||||||
|
code = strings.TrimPrefix(code, "(")
|
||||||
|
code = strings.TrimPrefix(code, "[")
|
||||||
|
code = strings.TrimSuffix(code, ")")
|
||||||
|
code = strings.TrimSuffix(code, "]")
|
||||||
code = strings.ToLower(code)
|
code = strings.ToLower(code)
|
||||||
code = strings.TrimSpace(code)
|
code = strings.TrimSpace(code)
|
||||||
code = strings.ReplaceAll(code, "-", "")
|
code = strings.ReplaceAll(code, "-", "")
|
||||||
5
internal/modules/formatter/interface.go
Normal file
5
internal/modules/formatter/interface.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package formatter
|
||||||
|
|
||||||
|
type IFormatter interface {
|
||||||
|
FormatText(text string) string
|
||||||
|
}
|
||||||
39
internal/modules/formatter/service.go
Normal file
39
internal/modules/formatter/service.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package formatter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type service struct{}
|
||||||
|
|
||||||
|
func NewFormatter() IFormatter {
|
||||||
|
return &service{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *service) FormatText(text string) string {
|
||||||
|
scanner := bufio.NewScanner(strings.NewReader(text))
|
||||||
|
|
||||||
|
scanner.Split(bufio.ScanLines)
|
||||||
|
|
||||||
|
lines := []string{}
|
||||||
|
for scanner.Scan() {
|
||||||
|
lines = append(lines, scanner.Text())
|
||||||
|
}
|
||||||
|
|
||||||
|
var res strings.Builder
|
||||||
|
for i, line := range lines {
|
||||||
|
l := strings.TrimSpace(line)
|
||||||
|
if i == 0 && strings.HasPrefix(l, "—") {
|
||||||
|
res.WriteString(" ")
|
||||||
|
}
|
||||||
|
if i > 0 {
|
||||||
|
res.WriteString("\n")
|
||||||
|
if len(l) > 0 {
|
||||||
|
res.WriteString(" ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res.WriteString(l)
|
||||||
|
}
|
||||||
|
return res.String()
|
||||||
|
}
|
||||||
@ -1,9 +1,10 @@
|
|||||||
package story_service
|
package story_service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"evening_detective/internal/modules/cleaner"
|
||||||
|
"evening_detective/internal/modules/formatter"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@ -12,12 +13,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type StoryService struct {
|
type StoryService struct {
|
||||||
|
cleaner cleaner.ICleaner
|
||||||
|
formatter formatter.IFormatter
|
||||||
filepath string
|
filepath string
|
||||||
story *Story
|
story *Story
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStoryService(filepath string) (*StoryService, error) {
|
func NewStoryService(
|
||||||
s := &StoryService{filepath: filepath}
|
cleaner cleaner.ICleaner,
|
||||||
|
formatter formatter.IFormatter,
|
||||||
|
filepath string,
|
||||||
|
) (*StoryService, error) {
|
||||||
|
s := &StoryService{
|
||||||
|
cleaner: cleaner,
|
||||||
|
formatter: formatter,
|
||||||
|
filepath: filepath,
|
||||||
|
}
|
||||||
if err := s.Load(); err != nil {
|
if err := s.Load(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -66,9 +77,9 @@ func (s *StoryService) GetPlace(code string) *Place {
|
|||||||
Text: "Уважаемые детективы внимательно прочитайте правила.",
|
Text: "Уважаемые детективы внимательно прочитайте правила.",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
code = clearCode(code)
|
clearCode := s.cleaner.ClearCode(code)
|
||||||
for _, place := range s.story.Places {
|
for _, place := range s.story.Places {
|
||||||
if clearCode(place.Code) == code {
|
if s.cleaner.ClearCode(place.Code) == clearCode {
|
||||||
re := regexp.MustCompile(`\(\[[a-zA-Zа-яА-Я\d-]+\]\)`)
|
re := regexp.MustCompile(`\(\[[a-zA-Zа-яА-Я\d-]+\]\)`)
|
||||||
applications := make([]*Application, 0, len(place.Applications))
|
applications := make([]*Application, 0, len(place.Applications))
|
||||||
for _, application := range place.Applications {
|
for _, application := range place.Applications {
|
||||||
@ -122,7 +133,7 @@ func (s *StoryService) UpdatePlace(code string, node *GraphNode) error {
|
|||||||
&Place{
|
&Place{
|
||||||
Code: node.Code,
|
Code: node.Code,
|
||||||
Name: node.Name,
|
Name: node.Name,
|
||||||
Text: formatText(node.Text),
|
Text: s.formatter.FormatText(node.Text),
|
||||||
Applications: nodeApplications,
|
Applications: nodeApplications,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -138,7 +149,7 @@ func (s *StoryService) UpdatePlace(code string, node *GraphNode) error {
|
|||||||
s.story.Places[i] = &Place{
|
s.story.Places[i] = &Place{
|
||||||
Code: node.Code,
|
Code: node.Code,
|
||||||
Name: node.Name,
|
Name: node.Name,
|
||||||
Text: formatText(node.Text),
|
Text: s.formatter.FormatText(node.Text),
|
||||||
Applications: nodeApplications,
|
Applications: nodeApplications,
|
||||||
}
|
}
|
||||||
update = true
|
update = true
|
||||||
@ -151,7 +162,7 @@ func (s *StoryService) UpdatePlace(code string, node *GraphNode) error {
|
|||||||
s.story.Places[i] = &Place{
|
s.story.Places[i] = &Place{
|
||||||
Code: code,
|
Code: code,
|
||||||
Name: node.Name,
|
Name: node.Name,
|
||||||
Text: formatText(node.Text),
|
Text: s.formatter.FormatText(node.Text),
|
||||||
Applications: nodeApplications,
|
Applications: nodeApplications,
|
||||||
}
|
}
|
||||||
update = true
|
update = true
|
||||||
@ -167,7 +178,7 @@ func (s *StoryService) GetGraph(ctx context.Context) *Graph {
|
|||||||
m := make(map[string]string, len(s.story.Places))
|
m := make(map[string]string, len(s.story.Places))
|
||||||
nodes := make([]*GraphNode, 0, len(s.story.Places))
|
nodes := make([]*GraphNode, 0, len(s.story.Places))
|
||||||
for _, place := range s.story.Places {
|
for _, place := range s.story.Places {
|
||||||
m[clearCode(place.Code)] = place.Code
|
m[s.cleaner.ClearCode(place.Code)] = place.Code
|
||||||
applications := make([]*GraphApplication, 0, len(place.Applications))
|
applications := make([]*GraphApplication, 0, len(place.Applications))
|
||||||
for _, application := range place.Applications {
|
for _, application := range place.Applications {
|
||||||
applications = append(
|
applications = append(
|
||||||
@ -196,8 +207,8 @@ func (s *StoryService) GetGraph(ctx context.Context) *Graph {
|
|||||||
edges = append(
|
edges = append(
|
||||||
edges,
|
edges,
|
||||||
&GraphEdge{
|
&GraphEdge{
|
||||||
From: m[clearCode(place.Code)],
|
From: m[s.cleaner.ClearCode(place.Code)],
|
||||||
To: m[clearMatch(match)],
|
To: m[s.cleaner.ClearCode(match)],
|
||||||
Type: "node",
|
Type: "node",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -209,8 +220,8 @@ func (s *StoryService) GetGraph(ctx context.Context) *Graph {
|
|||||||
edges = append(
|
edges = append(
|
||||||
edges,
|
edges,
|
||||||
&GraphEdge{
|
&GraphEdge{
|
||||||
From: m[clearCode(place.Code)],
|
From: m[s.cleaner.ClearCode(place.Code)],
|
||||||
To: m[clearMatch(match)],
|
To: m[s.cleaner.ClearCode(match)],
|
||||||
Type: "application",
|
Type: "application",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -223,30 +234,3 @@ func (s *StoryService) GetGraph(ctx context.Context) *Graph {
|
|||||||
Edges: edges,
|
Edges: edges,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatText(text string) string {
|
|
||||||
scanner := bufio.NewScanner(strings.NewReader(text))
|
|
||||||
|
|
||||||
scanner.Split(bufio.ScanLines)
|
|
||||||
|
|
||||||
lines := []string{}
|
|
||||||
for scanner.Scan() {
|
|
||||||
lines = append(lines, scanner.Text())
|
|
||||||
}
|
|
||||||
|
|
||||||
res := ""
|
|
||||||
for i, line := range lines {
|
|
||||||
l := strings.TrimSpace(line)
|
|
||||||
if i == 0 && strings.HasPrefix(l, "—") {
|
|
||||||
res += " "
|
|
||||||
}
|
|
||||||
if i > 0 {
|
|
||||||
res += "\n"
|
|
||||||
if len(l) > 0 {
|
|
||||||
res += " "
|
|
||||||
}
|
|
||||||
}
|
|
||||||
res += l
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user