oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3import (
4 "amadan.net/oficina/estilo.git/sky"
5 "amadan.net/oficina/home/connect"
6 "bytes"
7 "embed"
8 "html/template"
9 "io/fs"
10 "net/http"
11 "path"
12 "strings"
13 "testing/fstest"
14
15 "amadan.net/oficina/estilo.git/base"
16 "amadan.net/oficina/estilo.git/busy"
17 "amadan.net/oficina/estilo.git/chrome"
18 "amadan.net/oficina/estilo.git/googleimport"
19 estilomenu "amadan.net/oficina/estilo.git/menu"
20 "amadan.net/oficina/estilo.git/theme"
21
22 "amadan.net/rastrillo/rastrillo"
23 "amadan.net/rastrillo/rastrillo/ui"
24)
25
26// templatesFS holds this app's own pages. rastrillo's partials come
27// from ui.Templates() and are parsed alongside them.
28//
29//go:embed templates/*.html
30var templatesFS embed.FS
31
32// staticFS holds everything served under /static: rastrillo's vendored
33// tokens.css (copied by the framework's own scaffold and verified by
34// `rastrillo doctor`), our own CSS and JS, and the vendored grid.
35// rastrillo.js is not on disk: it comes from estilo's busy package, with
36// busy.js and busy.css, below.
37//
38// all: is required, not decorative — the default embed pattern skips
39// files whose names begin with _ or ., and a vendored library that
40// happens to ship one would go missing at run time and nowhere else.
41//
42//go:embed all:static
43var staticFS embed.FS
44
45// assets fingerprints static files so they can be cached forever and
46// still update on an ordinary reload: the URL carries the content hash,
47// so the browser has never cached the new one stale.
48var servedStaticFS = sharedStaticFiles()
49var assets = rastrillo.NewAssets(servedStaticFS)
50
51func sharedStaticFiles() fs.FS {
52 files := fstest.MapFS{}
53 err := fs.WalkDir(staticFS, "static", func(name string, entry fs.DirEntry, err error) error {
54 if err != nil {
55 return err
56 }
57 if entry.IsDir() {
58 return nil
59 }
60 data, err := staticFS.ReadFile(name)
61 if err != nil {
62 return err
63 }
64 files[name] = &fstest.MapFile{Data: data}
65 return nil
66 })
67 if err != nil {
68 panic(err)
69 }
70 // Calma, with Sheets' own accent from estilo's colour ring (estilo
71 // AGENTS.md, decisions table, row 8).
72 files["static/theme.css"] = &fstest.MapFile{Data: sheetsTheme}
73 // estilo's shared chrome, served under the app's own fingerprinted
74 // /static like the theme, in the suite's order: base, menu, chrome.
75 files["static/estilo/base.css"] = &fstest.MapFile{Data: base.CSS()}
76 files["static/estilo/menu.css"] = &fstest.MapFile{Data: estilomenu.CSS()}
77 files["static/estilo/chrome.css"] = &fstest.MapFile{Data: chrome.CSS()}
78 // The sky behind the sign-in door (estilo sky); its images are
79 // served by sky.Handler.
80 files["static/estilo/sky.css"] = &fstest.MapFile{Data: sky.CSS()}
81 files["static/estilo/chrome.js"] = &fstest.MapFile{Data: chrome.JS()}
82 // rastrillo's busy rule (Paul, 2026-09-29): a submit button's label
83 // gives way to a spinner for at least 650ms, the button disables, and
84 // Back resets it. estilo carries rastrillo's shim without the old busy
85 // rule, busy.js, and the spinner-in-place CSS; see estilo's busy.
86 files["static/rastrillo.js"] = &fstest.MapFile{Data: busy.ShimJS()}
87 files["static/estilo/busy.js"] = &fstest.MapFile{Data: busy.JS()}
88 files["static/estilo/busy.css"] = &fstest.MapFile{Data: busy.CSS()}
89 return files
90}
91
92// sheetsTheme is estilo's theme for Sheets. "sheets" is on the ring, so
93// ForApp cannot refuse it; a panic here is a broken estilo pin, found at
94// boot rather than on a page.
95var sheetsTheme = func() []byte {
96 css, ok := theme.ForApp("sheets")
97 if !ok {
98 panic("estilo's theme ring has no sheets")
99 }
100 return css
101}()
102
103// pages is one parsed template tree per page.
104//
105// One tree each, rather than one tree holding everything, because every
106// page defines a block called "content" and a single tree would keep
107// only the last one parsed. This is the shape rastrillo's SKILL.md
108// documents for exactly that reason.
109var pages = parsePages()
110
111// layoutFile is the one template every page is wrapped in, and the one
112// file under templates/ that is not itself a page.
113const layoutFile = "layout.html"
114
115// parsePages builds one tree per page, discovered from the embedded
116// directory rather than from a list kept in this file.
117//
118// Discovered, because a hand-kept list is a second place to remember:
119// adding templates/foo.html and forgetting the list gives a page that
120// silently never renders, and the mistake is invisible in the diff that
121// causes it. It runs once, at startup, and panics on a broken template
122// — which is right, since a template that cannot parse cannot serve any
123// request and failing at boot is louder than failing per request.
124func parsePages() map[string]*template.Template {
125 funcs := ui.Funcs()
126 funcs["asset"] = assets.Path
127
128 files, err := fs.Glob(templatesFS, "templates/*.html")
129 if err != nil {
130 panic("sheets: reading templates: " + err.Error())
131 }
132 out := make(map[string]*template.Template, len(files))
133 for _, f := range files {
134 name := strings.TrimSuffix(path.Base(f), ".html")
135 if path.Base(f) == layoutFile {
136 continue
137 }
138 t := template.Must(template.New(name).Funcs(funcs).
139 ParseFS(ui.Templates(), "*.html"))
140 t = template.Must(t.ParseFS(connect.Templates(), "templates/*.html"))
141 t = template.Must(t.ParseFS(estilomenu.Templates(), "templates/*.html"))
142 t = template.Must(t.ParseFS(chrome.Templates(), "templates/*.html"))
143 t = template.Must(t.ParseFS(googleimport.Templates(), "templates/*.html"))
144 t = template.Must(t.ParseFS(templatesFS, "templates/"+layoutFile, f))
145 out[name] = t
146 }
147 return out
148}
149
150// PageData is what every template receives.
151//
152// Frame is the chrome, built once per request in chrome.go: the bar, the
153// <title> parts and the light/dark choice. The choice is resolved there,
154// server-side, from a cookie — never fetched. AGENTS.md: it must resolve
155// before first paint, because cell fills resolve against the theme and a
156// late preference repaints every coloured cell. A page carrying it in
157// its first byte cannot flash; one that asks for it can.
158type PageData struct {
159 Frame Frame
160 // Data is the page's own payload. Named rather than embedded so a
161 // template always says .Data.Sheets and never has to guess whether
162 // a field belongs to the page or the shell.
163 Data any
164}
165
166// render writes a page. f describes its chrome: the <title> parts, the
167// open sheet, the bar's actions (see chrome.go).
168//
169// The template executes into a buffer first, and only a complete render
170// reaches the client. Executing straight to the ResponseWriter means a
171// template error halfway through has already sent a 200 and half a
172// page, and the browser shows a truncated document with no indication
173// anything went wrong.
174func (a *App) render(w http.ResponseWriter, r *http.Request, page string, f frame, status int, data any) {
175 if nativePage(w, r, page, status, data) {
176 return
177 }
178 t, ok := pages[page]
179 if !ok {
180 a.fail(w, r, errNoTemplate)
181 return
182 }
183 var buf bytes.Buffer
184 if err := t.ExecuteTemplate(&buf, "layout", PageData{Frame: a.frameFor(r, f), Data: data}); err != nil {
185 // No cell content, sheet title or tab name reaches this — the
186 // error is dropped, because an html/template error can quote the
187 // data it was rendering (AGENTS.md / Mechanical rules). The 500
188 // page is a template too; if the layout is what failed, it falls
189 // back to plain text itself (errorPage).
190 a.fail(w, r, err)
191 return
192 }
193 pageHeaders(w)
194 w.Header().Set("Content-Type", "text/html; charset=utf-8")
195 w.WriteHeader(status)
196 w.Write(buf.Bytes())
197}
198
199// staticHandler serves /static/* with a long cache lifetime for
200// fingerprinted URLs. Mounted at the FS's own path, with no prefix
201// stripped: rastrillo's Assets uses http.FileServerFS semantics, where
202// the URL path is "/" plus the FS path.
203func staticHandler() http.Handler { return assets.Handler() }
204
205// staticFiles is the embedded tree, exposed for the guard test that
206// walks our JavaScript for the line cap.
207func staticFiles() fs.FS { return servedStaticFS }
208