Phase 6 part 5, step 2: start a new site
Sites → New site fills an empty Gitea repository with the theme, a hugo.toml in the site's brand and a starter home page: checked that the repository is empty and builds with Hugo, then the first commit is pushed to its default branch. The site joins the profile, and a First pages card builds its Home, About, Contact and product pages with the page builder; About and Contact join the top menu. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,209 @@
|
||||
package sites
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"supermarket/internal/api/gitea"
|
||||
"supermarket/internal/hugorepo"
|
||||
"supermarket/internal/theme"
|
||||
)
|
||||
|
||||
// NewSite is what a new site starts from.
|
||||
type NewSite struct {
|
||||
Repo string // owner/name: an empty repository the user made
|
||||
URL string // where it will be published, https://…
|
||||
Title string
|
||||
Tagline string
|
||||
Email string // where lead forms send while they aren't connected
|
||||
Dark string // #rrggbb
|
||||
Primary string
|
||||
Accent string
|
||||
Font string // a Google Fonts family
|
||||
Logo string // an image's address, or ""
|
||||
}
|
||||
|
||||
var (
|
||||
hexColor = regexp.MustCompile(`^#[0-9a-fA-F]{6}$`)
|
||||
fontName = regexp.MustCompile(`^[A-Za-z0-9 ]{2,40}$`)
|
||||
)
|
||||
|
||||
// Fonts are offered for a new site (any Google Fonts family works).
|
||||
var Fonts = []string{"Inter", "IBM Plex Sans", "Manrope", "DM Sans", "Source Sans 3", "Work Sans", "Lato", "Merriweather Sans"}
|
||||
|
||||
// Defaults are the theme's own brand: Federated Computer's.
|
||||
func Defaults() NewSite {
|
||||
return NewSite{Dark: "#001a2b", Primary: "#e0ff4f", Accent: "#614efa", Font: "Inter"}
|
||||
}
|
||||
|
||||
// Check checks a new site's settings and tidies them.
|
||||
func (n *NewSite) Check() error {
|
||||
n.Repo, n.URL, n.Title = strings.TrimSpace(n.Repo), strings.TrimRight(strings.TrimSpace(n.URL), "/"), strings.TrimSpace(n.Title)
|
||||
n.Tagline, n.Email, n.Logo = strings.TrimSpace(n.Tagline), strings.TrimSpace(n.Email), strings.TrimSpace(n.Logo)
|
||||
if _, full, err := gitea.ParseRepo(n.Repo); err != nil {
|
||||
return fmt.Errorf("the repository: %v", err)
|
||||
} else {
|
||||
n.Repo = full
|
||||
}
|
||||
if u, err := url.Parse(n.URL); err != nil || u.Scheme != "https" || u.Host == "" || (u.Path != "" && u.Path != "/") {
|
||||
return errors.New("the site's address must be like https://www.example.com")
|
||||
}
|
||||
if n.Title == "" {
|
||||
return errors.New("give the site a name")
|
||||
}
|
||||
for _, v := range []string{n.Title, n.Tagline, n.Email, n.Logo} {
|
||||
if strings.ContainsFunc(v, unicode.IsControl) {
|
||||
return errors.New("the name, tagline, email and logo can't contain line breaks or tabs")
|
||||
}
|
||||
}
|
||||
if n.Email != "" && !strings.Contains(n.Email, "@") {
|
||||
return errors.New("that isn't an email address")
|
||||
}
|
||||
for _, c := range []*string{&n.Dark, &n.Primary, &n.Accent} {
|
||||
*c = strings.TrimSpace(*c)
|
||||
if !hexColor.MatchString(*c) {
|
||||
return fmt.Errorf("%q isn't a color like #1d1238", *c)
|
||||
}
|
||||
}
|
||||
n.Font = strings.TrimSpace(n.Font)
|
||||
if !fontName.MatchString(n.Font) {
|
||||
return errors.New("the font must be a Google Fonts family, like Inter")
|
||||
}
|
||||
if n.Logo != "" {
|
||||
if u, err := url.Parse(n.Logo); err != nil || (u.Scheme != "https" && !strings.HasPrefix(n.Logo, "/")) {
|
||||
return errors.New("the logo must be an https:// address, or a path on the site like /images/logo.svg")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func q(s string) string { return strconv.Quote(s) }
|
||||
|
||||
// Files are the new site's first commit: the theme, hugo.toml, a starter
|
||||
// home page, a README and a .gitignore.
|
||||
func (n NewSite) Files() map[string]string {
|
||||
files := map[string]string{}
|
||||
for p, body := range theme.Files() {
|
||||
files[theme.Dir+"/"+p] = body
|
||||
}
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "baseURL = %s\nlanguageCode = \"en-us\"\ntitle = %s\ntheme = %s\n", q(n.URL+"/"), q(n.Title), q(theme.Name))
|
||||
b.WriteString("disableKinds = [\"taxonomy\", \"term\"]\n\n[params]\n")
|
||||
fmt.Fprintf(&b, " tagline = %s\n\n[params.brand]\n", q(n.Tagline))
|
||||
fmt.Fprintf(&b, " dark = %s\n primary = %s\n accent = %s\n font = %s\n", q(n.Dark), q(n.Primary), q(n.Accent), q(n.Font))
|
||||
if n.Logo != "" {
|
||||
fmt.Fprintf(&b, " logo = %s\n", q(n.Logo))
|
||||
}
|
||||
b.WriteString("\n[params.header]\n ctas = [{ label = \"Contact us\", url = \"/contact/\", style = \"primary\" }]\n")
|
||||
fmt.Fprintf(&b, "\n[params.footer]\n tagline = %s\n groups = [{ title = \"Company\", menu = \"footer_company\" }]\n", q(n.Tagline))
|
||||
b.WriteString("\n# Where the lead forms post: EspoCRM's Lead Capture (Supermarket → Sites → Lead forms).\n")
|
||||
fmt.Fprintf(&b, "[params.leads]\n action = \"\"\n email = %s\n", q(n.Email))
|
||||
b.WriteString("\n[[menu.main]]\n name = \"Home\"\n url = \"/\"\n weight = 1\n")
|
||||
files["hugo.toml"] = b.String()
|
||||
|
||||
tagline := n.Tagline
|
||||
if tagline == "" {
|
||||
tagline = "Welcome."
|
||||
}
|
||||
files["content/_index.md"] = fmt.Sprintf(`---
|
||||
title: %s
|
||||
description: %s
|
||||
sections:
|
||||
- type: hero
|
||||
title: %s
|
||||
subhead: %s
|
||||
ctas:
|
||||
- label: "Contact us"
|
||||
url: "/contact/"
|
||||
style: primary
|
||||
- type: final-cta
|
||||
headline: "Talk to us"
|
||||
body: "Tell us what you need, and we'll get back to you."
|
||||
ctas:
|
||||
- label: "Contact us"
|
||||
url: "/contact/"
|
||||
style: primary
|
||||
---
|
||||
`, q(n.Title), q(tagline), q(n.Title), q(tagline))
|
||||
files["README.md"] = fmt.Sprintf(`# %s
|
||||
|
||||
The website at %s, a [Hugo](https://gohugo.io/) site made with
|
||||
Supermarket's theme (`+"`themes/supermarket`"+`; its README lists the section
|
||||
types and the settings in `+"`hugo.toml`"+`).
|
||||
|
||||
Pages are in `+"`content/`"+`: each page's front matter has a `+"`sections:`"+` list.
|
||||
Supermarket changes the site through pull requests; merging publishes.
|
||||
Run `+"`hugo server`"+` to see it locally.
|
||||
`, n.Title, n.URL)
|
||||
files[".gitignore"] = "/public/\n/resources/_gen/\n.hugo_build.lock\n"
|
||||
return files
|
||||
}
|
||||
|
||||
// Create makes the site: it checks the repository is empty and Supermarket
|
||||
// may push to it, builds the files with Hugo (when there is one) so nothing
|
||||
// broken is pushed, then makes the first commit on the default branch and
|
||||
// leaves the working copy at dir. It returns the branch.
|
||||
func Create(ctx context.Context, g *gitea.Client, n NewSite, dir, author string) (string, error) {
|
||||
if err := n.Check(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if g == nil {
|
||||
return "", errors.New("save a Gitea token under Settings → Search engines first")
|
||||
}
|
||||
repo, err := g.GetRepo(ctx, n.Repo)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("couldn't read %s on Gitea (create it first, empty): %w", n.Repo, err)
|
||||
}
|
||||
if !repo.Permissions.Push {
|
||||
return "", fmt.Errorf("the Gitea token can't push to %s", n.Repo)
|
||||
}
|
||||
if !repo.Empty {
|
||||
return "", fmt.Errorf("%s isn't empty: Supermarket only starts sites in an empty repository (create it without a README or license)", n.Repo)
|
||||
}
|
||||
branch := repo.DefaultBranch
|
||||
if branch == "" {
|
||||
branch = "main"
|
||||
}
|
||||
files := n.Files()
|
||||
if hugorepo.HugoVersion(ctx) != "" {
|
||||
tmp, err := os.MkdirTemp("", "supermarket-newsite-")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer os.RemoveAll(tmp)
|
||||
for rel, body := range files {
|
||||
p := filepath.Join(tmp, rel)
|
||||
os.MkdirAll(filepath.Dir(p), 0o755)
|
||||
if err := os.WriteFile(p, []byte(body), 0o644); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
out, err := hugorepo.BuildDir(ctx, tmp)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("the new site wouldn't build: %w", err)
|
||||
}
|
||||
os.RemoveAll(out)
|
||||
}
|
||||
wc := &hugorepo.Repo{Dir: dir, CloneURL: repo.CloneURL, Token: g.Token(), Branch: branch, Author: author}
|
||||
wc.Acquire()
|
||||
defer wc.Release()
|
||||
if _, err := os.Stat(dir); err == nil {
|
||||
// A copy left from before (an earlier attempt): the repository is
|
||||
// empty, so there's nothing in it to keep.
|
||||
if err := os.RemoveAll(dir); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
if err := hugorepo.InitPush(ctx, wc, files, "Start the site with Supermarket's theme"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return branch, nil
|
||||
}
|
||||
Reference in New Issue
Block a user