Files
supermarket/internal/web/sitesection.go
T
davidandClaude Opus 5.5 fdfbcd3d3d Phase 7 part 5: add a section to an existing page
Sites → Sections and pages → Add a section, on any page built from
sections: choose one of the site's section types and where it goes; the
AI writes it from the page, its searches, the profile and the Inventory,
with the same rules as a new page. It's reviewed like a page (claims to
confirm, to-dos), and the pull request inserts it into the page's file as
text, so only that section is added; the build checks it. What to write
next suggests a FAQ for a section page found with questions, and a
comparison for one searched against something else.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 07:25:19 -07:00

134 lines
4.1 KiB
Go

package web
import (
"context"
"errors"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"supermarket/internal/seo"
"supermarket/internal/sites"
)
// Adding a section to a page that's already on the site (Sites → Sections
// and pages → Add a section).
// pageNow reads a page's file from the site's working copy, as published.
func (s *Server) pageNow(ctx context.Context, cur siteRepo, path string) (raw []byte, title string, err error) {
ap := s.applier(seoSite{Index: cur.Index, URL: cur.URL})
wc, _, _, err := ap.WorkingCopy(ctx)
if err != nil {
return nil, "", err
}
defer wc.Release()
file, err := wc.FindContent(path)
if err != nil {
return nil, "", errors.New("the site has no page at " + path)
}
raw, err = os.ReadFile(filepath.Join(wc.Dir, filepath.FromSlash(file)))
if err != nil {
return nil, "", err
}
return raw, wc.FrontMatterValue(file, "title"), nil
}
// composeSection writes the section a site page of KindSection adds.
func (s *Server) composeSection(ctx context.Context, cur siteRepo, p *sites.SitePage) error {
client, prof, err := s.draftClient()
if err != nil {
return err
}
if prof == nil || !prof.IsSet() {
return errors.New("fill in the company profile under Settings → Profile first")
}
cat, err := s.catalog(ctx, cur, true)
if err != nil {
return err
}
raw, title, err := s.pageNow(ctx, cur, p.Path)
if err != nil {
return err
}
existing, _, err := sites.PageSections(raw)
if err != nil {
return err
}
in := sites.SectionInputs{Catalog: cat, PagePath: p.Path, PageTitle: title, Existing: existing, Type: p.Source.Type,
At: p.Source.At, Notes: p.Notes, SiteURL: cur.URL}
for _, st := range s.seoSites() {
if st.URL == cur.URL && st.Property != "" {
in.Searches, _ = seo.PageSearches(s.DB, s.scope(st), p.Path, 10)
}
}
out, err := (&sites.Composer{AI: client, Profile: prof}).ComposeSection(ctx, in)
if err != nil {
return err
}
p.Title, p.Needs, p.Sections = or(title, p.Path), out.Needs, out.Sections
return s.checkPage(cat, p, s.campaignOf(p))
}
// sitesAddPage is the form: which section, where, and what it's for.
func (s *Server) sitesAddPage(w http.ResponseWriter, r *http.Request) {
s.renderSitesAdd(w, r, "")
}
func (s *Server) renderSitesAdd(w http.ResponseWriter, r *http.Request, errMsg string) {
_, cur := s.currentSite(r)
path := sites.CleanPath(r.FormValue("page"))
data := map[string]any{"Current": cur, "Path": path, "Type": r.FormValue("type"), "Notes": r.FormValue("notes")}
if cur == nil || cur.Repo == "" {
errMsg = or(errMsg, "Set this website's Hugo repository in Settings → Profile first.")
} else {
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Minute)
defer cancel()
cat, err := s.catalog(ctx, *cur, false)
if err != nil {
errMsg = or(errMsg, err.Error())
} else {
data["Types"] = cat.Types
for _, pg := range cat.Pages {
if pg.Path == path {
data["Page"] = pg
}
}
if data["Page"] == nil {
errMsg = or(errMsg, "The site has no page built from sections at "+path+".")
}
}
}
s.render(w, r, "sites-add", page{Title: "Add a section", Nav: "sites/site", Error: errMsg, Data: data})
}
// sitesAdd writes the section and opens it for review, like a new page.
func (s *Server) sitesAdd(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
_, cur := s.currentSite(r)
if cur == nil || cur.Repo == "" {
s.renderSitesAdd(w, r, "")
return
}
at, _ := strconv.Atoi(r.PostFormValue("at"))
p := &sites.SitePage{Site: cur.URL, Repo: cur.Repo, Kind: sites.KindSection, Path: sites.CleanPath(r.PostFormValue("page")),
Notes: strings.TrimSpace(r.PostFormValue("notes"))}
p.Source.Type, p.Source.At = r.PostFormValue("type"), at
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Minute)
defer cancel()
if err := s.composeSection(ctx, *cur, p); err != nil {
s.renderSitesAdd(w, r, err.Error())
return
}
if err := sites.SavePage(s.DB, p); err != nil {
s.serverError(w, err)
return
}
http.Redirect(w, r, "/sites/pages/"+strconv.FormatInt(p.ID, 10), http.StatusSeeOther)
}