This commit is contained in:
2026-10-02 23:29:43 +07:00
parent 7efbaf4754
commit 8c9e5082a9
4 changed files with 58 additions and 98 deletions
+42 -10
View File
@@ -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
}
+16
View File
@@ -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)
}
})
}
}