This commit is contained in:
greeneclipse committed 2026-10-14 19:45:50 +02:00
1 parent 43cfaa0411
commit 3b0d692839
7 files changed
+341 -136

No files matched your search

@@ -97,7 +97,7 @@ func main() {
}
updated++
rewrittenPairs += count
log.Printf("category=%s repeated_pairs_rewritten=%d", c.id, count)
log.Printf("category=%s legacy_sections_normalized=%d", c.id, count)
if !*apply {
continue
}
@@ -117,5 +117,5 @@ func main() {
log.Fatal(fmt.Errorf("category %s changed concurrently; rerun dry-run", c.id))
}
}
log.Printf("A1 cleanup: categories_scanned=%d categories_requiring_cleanup=%d rewritten_pairs=%d applied=%v", len(candidates), updated, rewrittenPairs, *apply)
log.Printf("A1 cleanup: categories_scanned=%d categories_requiring_cleanup=%d normalized_sections=%d applied=%v", len(candidates), updated, rewrittenPairs, *apply)
}
@@ -440,7 +440,7 @@ func main() {
}
}
log.Printf("offline shared default translations: languages=%d templates=%d missing=%d saved=%d eligible=%d updated=%d legacy_upgraded=%d custom_untouched=%d A1_protected=%d A1_duplicate_pairs_rewritten=%d A1_categories_updated=%d failures=%d dry_run=%v", len(targetLangs), len(sources), missing, inserted, eligible, changed, upgraded, custom, protected, a1Removed, a1Updated, failures, *dryRun)
log.Printf("offline shared default translations: languages=%d templates=%d missing=%d saved=%d eligible=%d updated=%d legacy_upgraded=%d custom_untouched=%d A1_protected=%d A1_legacy_sections_normalized=%d A1_categories_updated=%d failures=%d dry_run=%v", len(targetLangs), len(sources), missing, inserted, eligible, changed, upgraded, custom, protected, a1Removed, a1Updated, failures, *dryRun)
if *dryRun && missing > 0 {
log.Printf("dry-run: the full run loads all 25 prelocalized language templates from disk without an LLM")
}
+40 -12
View File
@@ -145,17 +145,24 @@ 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.
// isKnownA1Boilerplate recognizes the exact historical A1-generated
// instructions, even when a previous cleanup left only one to three pairs.
// Generic custom HTML with H2/P sections must never be replaced.
func isKnownA1Boilerplate(body string, blocks int) bool {
if blocks < 4 {
if blocks < 1 {
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"))
lower := strings.ToLower(a1Whitespace.ReplaceAllString(strings.TrimSpace(body), " "))
legacy := strings.Contains(lower, "napiši odstavek, ki je dolg 100 besed")
cableLegacy := strings.Contains(lower, "nadaljuj iz naslova in napiši 100 besed") &&
strings.Contains(lower, "novo_ime_izdelka")
if !legacy && !cableLegacy {
return false
}
return (strings.Contains(lower, "novo ime izdelka") || strings.Contains(lower, "novo_ime_izdelka")) &&
(strings.Contains(lower, "izpostavi en benefit") ||
strings.Contains(lower, "prednost izdelka") ||
strings.Contains(lower, "preverjeno prednost"))
}
func localizedA1SEODescription() string {
@@ -167,6 +174,7 @@ func localizedA1SEODescription() string {
}
var a1SpecTrailing = regexp.MustCompile(`(?is)^\s*<\s*b\s*>.*?<\s*/\s*b\s*>\s*<\s*ul\s*>.*?<\s*/\s*ul\s*>`)
var a1RedundantH1 = regexp.MustCompile(`(?is)<\s*h1\s*>\s*\{\s*zapiši\s+novo_ime_izdelka\s*\}\s*<\s*/\s*h1\s*>\s*`)
// upgradeKnownA1Boilerplate replaces only the recognizable generated legacy
// five-benefit scaffold with short, factual HTML. The title/meta roles and
@@ -182,18 +190,37 @@ func upgradeKnownA1Boilerplate(source string) (string, int) {
}
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]
// Discard only the old generic spec instruction. A category-specific
// technical specification rule must remain intact.
trailing := strings.ToLower(source[end : end+match[1]])
if strings.Contains(trailing, "napiši 3-10 tehničnih specifikacij") ||
strings.Contains(trailing, "napiši 3–10 tehničnih specifikacij") {
end += match[1]
}
}
return source[:start] + html + source[end:], len(blocks)
updated := source[:start] + html + source[end:]
// Only the known imported A1 scaffold generates a redundant page H1.
// The storefront already renders its own H1 from the product title.
updated = a1RedundantH1.ReplaceAllString(updated, "")
return updated, 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) {
// A1 categories frequently have no structured description_template.
// The HTML prompt alone is valid; an empty JSON column isn't an error.
trimmed := strings.TrimSpace(raw)
if trimmed == "" || trimmed == "null" || trimmed == "{}" {
return raw, 0, nil
}
var obj map[string]json.RawMessage
if err := json.Unmarshal([]byte(raw), &obj); err != nil {
return raw, 0, err
}
if len(obj) == 0 || len(obj["sections"]) == 0 || string(obj["sections"]) == "null" {
return raw, 0, nil
}
var sections []DescriptionFormulaSection
if err := json.Unmarshal(obj["sections"], &sections); err != nil {
return raw, 0, nil
@@ -263,8 +290,9 @@ func NormalizeA1RepeatedBenefits(source string) (string, int) {
return out.String(), rewritten
}
// NormalizeA1DescriptionTemplate applies the same non-repeating benefit rules
// to the structured A1 description editor sections. No sections are deleted.
// NormalizeA1DescriptionTemplate upgrades recognizable imported A1 description
// boilerplate to the semantic SEO sections. For unrelated custom descriptions,
// only repeated instructions are rewritten and sections remain intact.
func NormalizeA1DescriptionTemplate(raw string) (string, int, error) {
if cleaned, n, err := upgradeKnownA1Sections(raw); err != nil || n > 0 {
return cleaned, n, err
@@ -143,3 +143,118 @@ func TestNormalizeA1LegacyStructuredSEO(t *testing.T) {
t.Fatal("not repeatable")
}
}
func TestNormalizeA1MissingDescriptionJSON(t *testing.T) {
for _, source := range []string{"", " ", "null", "{}", `{"metaDescription":"140 znakov"}`, `{"sections":null,"metaTitle":"Custom"}`} {
result, changed, err := NormalizeA1DescriptionTemplate(source)
if err != nil || changed != 0 || result != source {
t.Fatalf("source=%q changed=%d err=%v result=%q", source, changed, err, result)
}
}
}
func TestNormalizeA1MixedOldAndNewInstructions(t *testing.T) {
source := `<H2>{Napiši Novo ime izdelka brez barve in izpostavi en benefit}</H2>
<p>{Napiši odstavek, ki je dolg 100 besed, ki NE vsebuje Novo ime izdelka.}</p>
<H2>{Izpostavi en benefit, in NE napiši Novo ime izdelka}</H2>
<p>{Napiši odstavek, ki je dolg 100 besed in VKLJUČI tudi Novo ime izdelka.}</p>
<H2>{Izpostavi 3. drugačno in preverjeno prednost izdelka. Ne ponavljaj prejšnjih prednosti ali celotnega imena izdelka.}</H2>
<p>{Napiši približno 100 besed o 3. samostojni, z dejanskimi podatki podprti prednosti. Ne ponavljaj informacij iz prejšnjih odstavkov. Ne uporabi celotnega novega imena izdelka. Če prednosti ni mogoče potrditi z vhodnimi podatki, je ne izmišljaj.}</p><b>{Tehnične specifikacije}</b><ul><li>{Napiši 3-10 tehničnih specifikacij v alinejah}</li></ul>`
cleaned, replaced := NormalizeA1RepeatedBenefits(source)
if replaced != 3 {
t.Fatalf("expected three old/mixed pairs replaced, got %d: %s", replaced, cleaned)
}
if strings.Contains(cleaned, "100 besed") || strings.Contains(cleaned, "Izpostavi en benefit") || strings.Contains(cleaned, "3. drugačno") {
t.Fatalf("mixed legacy boilerplate survived: %s", cleaned)
}
if strings.Count(cleaned, "<h2>") != 2 || strings.Count(cleaned, "<ul><li>") != 1 || !strings.HasPrefix(cleaned, "<p>{") {
t.Fatalf("expected modern SEO description with 1 intro, 2 sections and one specs list: %s", cleaned)
}
again, changed := NormalizeA1RepeatedBenefits(cleaned)
if changed != 0 || again != cleaned {
t.Fatal("mixed A1 cleanup not idempotent")
}
}
func TestNormalizeA1ShortLegacyAndPreservesOtherRoles(t *testing.T) {
source := `--- Title ---
{Ohranite naslov in pravila iz kategorije}
--- End Title ---
--- Description ---
<H2>{Napiši Novo ime izdelka brez barve in izpostavi en benefit}</H2>
<p>{Napiši odstavek, ki je dolg 100 besed, ki NE vsebuje Novo ime izdelka.}</p>
<H2>{Izpostavi en benefit, in NE napiši Novo ime izdelka}</H2>
<p>{Napiši odstavek, ki je dolg 100 besed in VKLJUČI tudi Novo ime izdelka.}</p>
<b>{Tehnične specifikacije}</b><ul><li>{Napiši 3-10 tehničnih specifikacij v alinejah}</li></ul>
--- End Description ---
--- Meta ---
{Ohranite lasten meta prompt}
--- End Meta ---`
cleaned, n := NormalizeA1RepeatedBenefits(source)
if n != 2 || strings.Count(cleaned, "<h2>") != 2 {
t.Fatalf("expected short template upgraded, changed=%d: %s", n, cleaned)
}
for _, retained := range []string{"{Ohranite naslov in pravila iz kategorije}", "{Ohranite lasten meta prompt}", "--- End Description ---"} {
if !strings.Contains(cleaned, retained) {
t.Fatalf("custom content lost: %s", cleaned)
}
}
if strings.Contains(cleaned, "100 besed") || strings.Count(cleaned, "<ul><li>") != 1 {
t.Fatalf("duplicated spec or old paragraph remains: %s", cleaned)
}
}
func TestNormalizeA1MixedStructuredTemplatePreservesMeta(t *testing.T) {
source := `{"sections":[{"type":"h2","instructions":"Napiši Novo ime izdelka brez barve in izpostavi en benefit"},{"type":"p","instructions":"Napiši odstavek, ki je dolg 100 besed, ki NE vsebuje Novo ime izdelka"},{"type":"h2","instructions":"Izpostavi en benefit, in NE napiši Novo ime izdelka"},{"type":"p","instructions":"Napiši odstavek, ki je dolg 100 besed in VKLJUČI Novo ime izdelka"},{"type":"h2","instructions":"Izpostavi 3. drugačno in preverjeno prednost izdelka. Ne ponavljaj prejšnjih prednosti ali celotnega imena izdelka"},{"type":"p","instructions":"Napiši približno 100 besed o 3. samostojni prednosti"},{"type":"ul","instructions":"Napiši 3-10 tehničnih specifikacij v alinejah"}],"metaTitle":"A1 custom title","metaDescription":"A1 custom meta","otherField":true}`
cleaned, count, err := NormalizeA1DescriptionTemplate(source)
if err != nil || count != 3 {
t.Fatalf("mixed structured template count=%d err=%v result=%s", count, err, cleaned)
}
for _, retained := range []string{`"metaTitle":"A1 custom title"`, `"metaDescription":"A1 custom meta"`, `"otherField":true`} {
if !strings.Contains(cleaned, retained) {
t.Fatalf("lost metadata %s in %s", retained, cleaned)
}
}
if strings.Contains(cleaned, "100 besed") || strings.Count(cleaned, `"type":"h2"`) != 2 || strings.Count(cleaned, `"type":"ul"`) != 1 {
t.Fatalf("did not upgrade description to SEO structure: %s", cleaned)
}
twice, n, err := NormalizeA1DescriptionTemplate(cleaned)
if err != nil || n != 0 || twice != cleaned {
t.Fatalf("structured upgrade not idempotent: n=%d err=%v", n, err)
}
}
func TestNormalizeA1UnrelatedCustomPromptsUntouched(t *testing.T) {
source := `<H2>{Izbira pravega izdelka}</H2><p>{Napiši opis glede na navedena dejstva, brez ugibanja.}</p>`
actual, changed := NormalizeA1RepeatedBenefits(source)
if actual != source || changed != 0 {
t.Fatalf("unexpected change of custom text: %s", actual)
}
}
func TestNormalizeA1LegacyEnDashSpecs(t *testing.T) {
source := `<H2>{Napiši Novo ime izdelka brez barve in izpostavi en benefit}</H2><p>{Napiši odstavek, ki je dolg 100 besed, ki NE vsebuje Novo ime izdelka.}</p>
<b>Tehnične specifikacije</b>
<ul><li>{Napiši 3–10 tehničnih specifikacij v alinejah}</li></ul>`
got, count := NormalizeA1RepeatedBenefits(source)
if count != 1 || strings.Count(got, "<ul><li>") != 1 || strings.Contains(got, "3–10 tehničnih") {
t.Fatalf("duplicated technical specifications: %d %s", count, got)
}
}
func TestNormalizeA1OldCableH1(t *testing.T) {
source := `<name>{Ohrani specifičen naslov kabla}</name>
<metaDescription>{Ohrani meta}</metaDescription>
<H1>{Zapiši Novo_ime_izdelka}</H1>
<H2>{Naj vključuje Novo_ime_izdelka in izpostavi en benefit}</H2>
<p>{Nadaljuj iz naslova in napiši 100 besed o temu benefitu, ne vključuj Novo_ime_izdelka}</p>`
got, count := NormalizeA1RepeatedBenefits(source)
if count != 1 || strings.Contains(strings.ToLower(got), "<h1>") || strings.Contains(got, "napiši 100 besed") {
t.Fatalf("legacy H1 not removed: %d %s", count, got)
}
if !strings.Contains(got, "Ohrani meta") || !strings.Contains(got, "Ohrani specifičen naslov kabla") {
t.Fatal("overwrote category specific metadata")
}
}