From 8c9e5082a951e56666f7865330ae1cdc4cc2bd96 Mon Sep 17 00:00:00 2001 From: Fedorov Vladimir Date: Fri, 2 Oct 2026 23:29:43 +0700 Subject: [PATCH] fix --- internal/app/story_mapper.go | 52 +++++++++++++++---- internal/app/story_mapper_test.go | 16 ++++++ internal/modules/formatter_utils/service.go | 48 ----------------- .../modules/formatter_utils/service_test.go | 40 -------------- 4 files changed, 58 insertions(+), 98 deletions(-) delete mode 100644 internal/modules/formatter_utils/service.go delete mode 100644 internal/modules/formatter_utils/service_test.go diff --git a/internal/app/story_mapper.go b/internal/app/story_mapper.go index 91fafe5..520d88c 100644 --- a/internal/app/story_mapper.go +++ b/internal/app/story_mapper.go @@ -1,7 +1,9 @@ package app import ( - "evening_detective_server/internal/modules/formatter_utils" + "bufio" + "strings" + "evening_detective_server/internal/modules/storytelling" "evening_detective_server/internal/services/scenarios_service" proto "evening_detective_server/proto" @@ -65,7 +67,7 @@ func convertConclusion(o *proto.Conclusion) *storytelling.Conclusion { if o == nil || (o.Text == "" && o.Video == "") { return nil } - return &storytelling.Conclusion{Text: formatter_utils.FormatText(o.Text), Video: o.Video} + return &storytelling.Conclusion{Text: formatText(o.Text), Video: o.Video} } func mapIntroduction(o *storytelling.Introduction) *proto.Introduction { @@ -148,8 +150,8 @@ func mapKey(o *storytelling.Key) *proto.Key { func convertPlace(o *proto.Place) *storytelling.Place { return &storytelling.Place{ Code: o.Code, - Name: formatter_utils.FormatString(o.Name), - Text: formatter_utils.FormatText(o.Text), + Name: formatString(o.Name), + Text: formatText(o.Text), Image: o.Image, Hidden: o.Hidden, Applications: convertApplications(o.Applications), @@ -184,7 +186,7 @@ func convertKeys(o []*proto.Key) []*storytelling.Key { func convertApplication(o *proto.Application) *storytelling.Application { return &storytelling.Application{ - Name: formatter_utils.FormatString(o.Name), + Name: formatString(o.Name), Image: o.Image, FileType: o.FileType, } @@ -193,14 +195,14 @@ func convertApplication(o *proto.Application) *storytelling.Application { func convertDoor(o *proto.Door) *storytelling.Door { return &storytelling.Door{ Code: o.Code, - Name: formatter_utils.FormatString(o.Name), + Name: formatString(o.Name), Keys: convertKeys(o.Keys), } } func convertKey(o *proto.Key) *storytelling.Key { return &storytelling.Key{ - Name: formatter_utils.FormatString(o.Name), + Name: formatString(o.Name), } } @@ -209,7 +211,7 @@ func convertIntroduction(o *proto.Introduction) *storytelling.Introduction { return nil } return &storytelling.Introduction{ - Text: formatter_utils.FormatText(o.Text), + Text: formatText(o.Text), Audio: o.Audio, } } @@ -239,7 +241,37 @@ func convertQuestion(o *proto.Question) *storytelling.Question { } return &storytelling.Question{ Code: o.Code, - Text: formatter_utils.FormatText(o.Text), - Answer: formatter_utils.FormatText(o.Answer), + Text: formatText(o.Text), + Answer: formatText(o.Answer), } } + +func formatText(text string) string { + scanner := bufio.NewScanner(strings.NewReader(text)) + var result strings.Builder + for i := 0; scanner.Scan(); i++ { + line := strings.TrimSpace(scanner.Text()) + if strings.HasPrefix(line, "--") { + line = strings.Replace(line, "--", "—", 1) + } + if i == 0 && strings.HasPrefix(line, "—") { + result.WriteString(" ") + } + if i > 0 { + result.WriteByte('\n') + if line != "" { + result.WriteString(" ") + } + } + result.WriteString(line) + } + return result.String() +} + +func formatString(text string) string { + text = strings.TrimSpace(text) + if strings.HasPrefix(text, "--") { + text = strings.Replace(text, "--", "—", 1) + } + return text +} diff --git a/internal/app/story_mapper_test.go b/internal/app/story_mapper_test.go index 7efdba0..269653a 100644 --- a/internal/app/story_mapper_test.go +++ b/internal/app/story_mapper_test.go @@ -28,3 +28,19 @@ func TestConclusionMapping(t *testing.T) { t.Fatal("empty conclusion must clear it") } } + +func TestFormatText(t *testing.T) { + for _, tc := range []struct { + name, input, want string + }{ + {"plain", "Привет", "Привет"}, + {"paragraph", "Привет\nМир", "Привет\n Мир"}, + {"dash", "-- Привет", " — Привет"}, + } { + t.Run(tc.name, func(t *testing.T) { + if got := formatText(tc.input); got != tc.want { + t.Fatalf("formatText() = %q, want %q", got, tc.want) + } + }) + } +} diff --git a/internal/modules/formatter_utils/service.go b/internal/modules/formatter_utils/service.go deleted file mode 100644 index a473c2c..0000000 --- a/internal/modules/formatter_utils/service.go +++ /dev/null @@ -1,48 +0,0 @@ -package formatter_utils - -import ( - "bufio" - "strings" -) - -// FormatText форматирует многострочный текст: обрезает пробелы по краям -// строк, превращает "--" в тире и добавляет отступ абзаца. -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()) - } - - var res strings.Builder - for i, line := range lines { - l := strings.TrimSpace(line) - if strings.HasPrefix(l, "--") { - l = strings.Replace(l, "--", "—", 1) - } - 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() -} - -// FormatString форматирует одиночную строку: обрезает пробелы и заменяет -// "--" на тире. -func FormatString(text string) string { - l := strings.TrimSpace(text) - if strings.HasPrefix(l, "--") { - l = strings.Replace(l, "--", "—", 1) - } - return l -} diff --git a/internal/modules/formatter_utils/service_test.go b/internal/modules/formatter_utils/service_test.go deleted file mode 100644 index 221af7b..0000000 --- a/internal/modules/formatter_utils/service_test.go +++ /dev/null @@ -1,40 +0,0 @@ -package formatter_utils - -import "testing" - -func Test_service_FormatText(t *testing.T) { - tests := []struct { - name string - text string - want string - }{ - { - name: "Простой текст", - text: "Привет", - want: "Привет", - }, - { - name: "Текст с двумя абзацами", - text: "Привет\nМир", - want: "Привет\n Мир", - }, - { - name: "Прямая речь", - text: "— Привет", - want: " — Привет", - }, - { - name: "Прямая речь через 2 минуса", - text: "-- Привет", - want: " — Привет", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got := FormatText(tt.text) - if got != tt.want { - t.Errorf("FormatText() = %v, want %v", got, tt.want) - } - }) - } -}