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)
}
})
}
}
@@ -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
}
@@ -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)
}
})
}
}