This commit is contained in:
greeneclipse committed 2026-10-14 19:23:16 +02:00
1 parent ff6c2df618
commit 43cfaa0411
18 files changed
+683 -299

No files matched your search

+88
View File
@@ -145,7 +145,92 @@ func a1ReplaceTagContent(source string, expression *regexp.Regexp, newInstructio
return source[:matches[4]] + newInstruction + source[matches[5]:]
}
// isKnownA1Boilerplate only targets the old generated five-benefit formula.
// User-authored copy (even if it contains multiple H2s) is never replaced.
func isKnownA1Boilerplate(body string, blocks int) bool {
if blocks < 4 {
return false
}
lower := strings.ToLower(body)
return (strings.Contains(lower, "napiši odstavek, ki je dolg 100 besed") ||
strings.Contains(lower, "drugačno in preverjeno prednost") ||
strings.Contains(lower, "napiši približno 100 besed o")) &&
(strings.Contains(lower, "novo ime izdelka") || strings.Contains(lower, "prednost izdelka"))
}
func localizedA1SEODescription() string {
prompt, err := OfflineDefaultPrompt("sl")
if err != nil {
return ""
}
return DescriptionSectionInstructions(prompt)
}
var a1SpecTrailing = regexp.MustCompile(`(?is)^\s*<\s*b\s*>.*?<\s*/\s*b\s*>\s*<\s*ul\s*>.*?<\s*/\s*ul\s*>`)
// upgradeKnownA1Boilerplate replaces only the recognizable generated legacy
// five-benefit scaffold with short, factual HTML. The title/meta roles and
// surrounding category-specific instructions are kept untouched.
func upgradeKnownA1Boilerplate(source string) (string, int) {
blocks := a1HeadingParagraph.FindAllStringIndex(source, -1)
if !isKnownA1Boilerplate(source, len(blocks)) {
return source, 0
}
html := localizedA1SEODescription()
if html == "" {
return source, 0
}
start, end := blocks[0][0], blocks[len(blocks)-1][1]
if match := a1SpecTrailing.FindStringIndex(source[end:]); len(match) == 2 && match[0] == 0 {
end += match[1]
}
return source[:start] + html + source[end:], len(blocks)
}
// upgradeKnownA1Sections is equally narrow for JSON description_template.
// It retains the existing metaTitle/metaDescription and unknown JSON fields.
func upgradeKnownA1Sections(raw string) (string, int, error) {
var obj map[string]json.RawMessage
if err := json.Unmarshal([]byte(raw), &obj); err != nil {
return raw, 0, err
}
var sections []DescriptionFormulaSection
if err := json.Unmarshal(obj["sections"], &sections); err != nil {
return raw, 0, nil
}
var joined strings.Builder
pairs := 0
for i := 0; i+1 < len(sections); i++ {
if sections[i].Type == "h2" && sections[i+1].Type == "p" {
joined.WriteString(sections[i].Instructions + " " + sections[i+1].Instructions + " ")
pairs++
i++
}
}
if !isKnownA1Boilerplate(joined.String(), pairs) {
return raw, 0, nil
}
seo := localizedA1SEODescription()
parsed := DeriveDescriptionFormulaFromLegacyParts(LegacyEnhanceParts{DescriptionRules: seo, WasLegacy: true})
if len(parsed.Sections) != 5 {
return raw, 0, fmt.Errorf("invalid bundled Slovenian description")
}
encoded, err := json.Marshal(parsed.Sections)
if err != nil {
return raw, 0, err
}
obj["sections"] = encoded
out, err := json.Marshal(obj)
if err != nil {
return raw, 0, err
}
return string(out), pairs, nil
}
func NormalizeA1RepeatedBenefits(source string) (string, int) {
if cleaned, n := upgradeKnownA1Boilerplate(source); n > 0 {
return cleaned, n
}
blocks := a1HeadingParagraph.FindAllStringIndex(source, -1)
if len(blocks) < 2 {
return source, 0
@@ -181,6 +266,9 @@ func NormalizeA1RepeatedBenefits(source string) (string, int) {
// NormalizeA1DescriptionTemplate applies the same non-repeating benefit rules
// to the structured A1 description editor sections. No sections are deleted.
func NormalizeA1DescriptionTemplate(raw string) (string, int, error) {
if cleaned, n, err := upgradeKnownA1Sections(raw); err != nil || n > 0 {
return cleaned, n, err
}
if strings.TrimSpace(raw) == "" || strings.TrimSpace(raw) == "null" {
return raw, 0, nil
}