oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1package slides
2
3import (
4 "amadan.net/oficina/estilo.git/sky"
5 "bytes"
6 "embed"
7 "html/template"
8 "io/fs"
9 "net/http"
10 "testing/fstest"
11
12 "amadan.net/oficina/estilo.git/base"
13 "amadan.net/oficina/estilo.git/chrome"
14 "amadan.net/oficina/estilo.git/credits"
15 "amadan.net/oficina/estilo.git/desk"
16 estilomenu "amadan.net/oficina/estilo.git/menu"
17 "amadan.net/oficina/estilo.git/savestatus"
18 "amadan.net/oficina/home/connect"
19 "amadan.net/oficina/slides/internal/slides/icons"
20 "amadan.net/rastrillo/rastrillo"
21 "amadan.net/rastrillo/rastrillo/ui"
22)
23
24//go:embed templates static
25var appFS embed.FS
26
27// LocaleFS carries the catalogs as locales/<code>.toml — the shape
28// rastrillo.Options.LocaleFS (and rastrillo.NewLocales) expects.
29// Separate from appFS because that one is also the fingerprinted asset
30// tree, and a catalog is not an asset anybody fetches.
31//
32//go:embed locales
33var LocaleFS embed.FS
34
35// Locales and DefaultLocale declare this app's locale set.
36// cmd/slides/main.go wires all three of these into rastrillo.Options;
37// without them rastrillo installs no locale middleware and every
38// rastrillo.T call returns its key verbatim, so this is what makes the
39// catalog actually resolve on a real page.
40//
41// English only for now. Twelve locales are the suite's target and every
42// user-visible string is already a key, so adding one is adding a file.
43var Locales = []string{"en"}
44
45// DefaultLocale is the locale an unprefixed request with no stored
46// preference and no usable Accept-Language gets.
47const DefaultLocale = "en"
48
49// assets fingerprints static/: {{asset "static/tokens.css"}} in a
50// template returns a content-hashed URL, served immutable-cacheable by
51// the handler New mounts (app.go).
52//
53// The tree it serves is the app's own static/ plus estilo's shared
54// chrome, under static/estilo/: base.css, menu.css, chrome.css, desk.css
55// and the optional chrome.js. Served from here rather than linked from estilo,
56// so they are fingerprinted like every other asset and a new estilo pin
57// is a new URL.
58var assets = rastrillo.NewAssets(staticFiles())
59
60func staticFiles() fs.FS {
61 files := fstest.MapFS{}
62 err := fs.WalkDir(appFS, "static", func(name string, entry fs.DirEntry, err error) error {
63 if err != nil || entry.IsDir() {
64 return err
65 }
66 data, err := appFS.ReadFile(name)
67 if err != nil {
68 return err
69 }
70 files[name] = &fstest.MapFile{Data: data}
71 return nil
72 })
73 if err != nil {
74 panic(err)
75 }
76 files["static/estilo/base.css"] = &fstest.MapFile{Data: base.CSS()}
77 files["static/estilo/menu.css"] = &fstest.MapFile{Data: estilomenu.CSS()}
78 files["static/estilo/chrome.css"] = &fstest.MapFile{Data: chrome.CSS()}
79 // The desk an editor sits on (--estilo-desk, Docs' grey) and the
80 // floating card; static/desk.css reads the token.
81 files["static/estilo/desk.css"] = &fstest.MapFile{Data: desk.CSS()}
82 // The sky behind the sign-in door (estilo sky); its images are
83 // served by sky.Handler.
84 files["static/estilo/sky.css"] = &fstest.MapFile{Data: sky.CSS()}
85 files["static/estilo/chrome.js"] = &fstest.MapFile{Data: chrome.JS()}
86 files["static/estilo/save-status.css"] = &fstest.MapFile{Data: savestatus.CSS()}
87 files["static/estilo/save-status.mjs"] = &fstest.MapFile{Data: savestatus.JS()}
88 return files
89}
90
91// pages is one *template.Template per page, each combining layout.html
92// with that page's own file — kept separate so every page can define a
93// template named "content" without colliding with the others.
94var pages = map[string]*template.Template{}
95
96// pageFiles is every page and the app partials it is built with. Every
97// page gets the same layout and the same estilo bar; what differs
98// between a signed-in screen, the sign-in page, a share link and a
99// projector is the Frame the handler builds (chrome.go), not which
100// templates were parsed. Before round 2 that choice was made here, in
101// four groups (public, signed in, share link, capability), and the
102// signed-in presenter was shown the anonymous shell's "Account → Sign
103// in" because the group, not the request, decided.
104var pageFiles = map[string][]string{
105 "signin": nil,
106 "signed_out": nil,
107 "errors": nil,
108 "home_link": nil,
109 "credits": nil,
110 "decks": nil,
111 "viewer": nil,
112 "editor": nil,
113 "deck_notes": nil,
114 "deck_new": nil,
115 "deck_settings": nil,
116 "deck_delete": nil,
117 "deck_export": nil,
118 "export_status": nil,
119 "share": nil,
120 "agents": nil,
121 "fonts": nil,
122 "fonts_confirm": nil,
123 // The presenting screens: three share one partial, so the remote
124 // and the presenter view cannot drift on what a button is called or
125 // where it posts.
126 "present": {"templates/present_controls.html"},
127 "present_notes": {"templates/present_controls.html", "templates/present_pairing.html"},
128 // Pairing a phone, from the presenter window's menu; its QR and
129 // code are the old presenter view's (present_pairing.html).
130 "present_pair": {"templates/present_pairing.html"},
131 // The presenter window and its scriptless picker (presenter_window.go).
132 "presenter": nil,
133 "presenter_pick": nil,
134 "remote": {"templates/present_controls.html"},
135 "remote_pair": nil,
136}
137
138// linkPages are names a handler uses for a page built from another
139// page's file. The viewer is ONE file serving the owner, a team member
140// and a stranger holding a share link (viewer.go says why); the Frame is
141// what tells them apart.
142var linkPages = map[string]string{"viewer_link": "viewer"}
143
144// fragmentPages are the templates that are ALSO rendered without the
145// layout: rastrillo's polling shim replaces one element with what it
146// fetches, so the polled endpoint answers a partial and not a page
147// (jobs.Config.RenderFragment — "render a whole page there and the
148// layout nests inside itself on the next poll").
149//
150// One file, two builds, exactly as viewer.html is built twice: the
151// status page and the fragment inside it cannot say different things
152// about the same job if they are one template.
153var fragmentPages = []string{"export_status"}
154
155func init() {
156 for name, shared := range pageFiles {
157 t := buildPage(name, shared...)
158 switch name {
159 case "credits":
160 t = template.Must(t.ParseFS(credits.Templates(), "templates/*.html"))
161 case "home_link":
162 t = template.Must(t.ParseFS(connect.Templates(), "templates/home_link.html"))
163 }
164 pages[name] = t
165 }
166 pages["editor"] = withSaveStatus(pages["editor"])
167 for name, file := range linkPages {
168 pages[name] = pages[file]
169 }
170 for _, name := range fragmentPages {
171 fragments[name] = buildFragment(name)
172 }
173}
174
175// fragments is one *template.Template per template that is also
176// rendered on its own, without the layout around it.
177var fragments = map[string]*template.Template{}
178
179// buildFragment parses a page's file with ui's partials and NOTHING
180// else — no layout, no shell — so executing one of its defines writes
181// the partial by itself.
182func buildFragment(name string) *template.Template {
183 return template.Must(
184 template.Must(template.New("fragment").
185 Funcs(ui.Funcs(ui.WithIcons(icons.Icon, icons.Assets))).
186 Funcs(template.FuncMap{"asset": assets.Path}).
187 ParseFS(ui.Templates(), "*.html")).
188 ParseFS(appFS, "templates/"+name+".html"))
189}
190
191// buildPage parses one page: ui's partials, estilo's menu and chrome
192// templates, then the layout, then any shared app partials, then the
193// page's own file. Order is the whole mechanism — a later definition of
194// a name replaces an earlier one, so a page's "content" wins over the
195// layout's empty default.
196func buildPage(name string, shared ...string) *template.Template {
197 files := append([]string{"templates/layout.html"}, shared...)
198 files = append(files, "templates/"+name+".html")
199 t := template.Must(template.New("layout").
200 // ui's partials and their helpers first, so every page can
201 // call one without registering anything. WithIcons points
202 // ui's two icon seams at the app's own set
203 // (internal/slides/icons, scaffolded by rastrillo new) —
204 // both of them, always: passing only icon would leave
205 // iconAssets on the framework default and silently break
206 // cdn and js delivery. iconAssets is whatever <head> markup
207 // the chosen delivery needs, empty for the vendored-inline
208 // default, so the layout calls it unconditionally and
209 // switching delivery later needs no template edit.
210 Funcs(ui.Funcs(ui.WithIcons(icons.Icon, icons.Assets))).
211 Funcs(template.FuncMap{"asset": assets.Path}).
212 ParseFS(ui.Templates(), "*.html"))
213 t = template.Must(t.ParseFS(estilomenu.Templates(), "templates/*.html"))
214 t = template.Must(t.ParseFS(chrome.Templates(), "templates/*.html"))
215 // Then the app's own: the shell, any shared partial, and this
216 // page's body.
217 return template.Must(t.ParseFS(appFS, files...))
218}
219
220// render executes name's layout into a buffer before anything touches
221// the wire: a template error becomes a clean 500 instead of garbage
222// appended to a half-written page, and headers (flash cookies, say)
223// stay settable until the status line is written.
224func render(w http.ResponseWriter, name string, data any) {
225 var buf bytes.Buffer
226 if err := pages[name].ExecuteTemplate(&buf, "layout", data); err != nil {
227 http.Error(w, "something went wrong", http.StatusInternalServerError)
228 return
229 }
230 buf.WriteTo(w)
231}
232
233// renderFragment executes one define out of a fragment build — the
234// markup a polled endpoint answers with, and nothing around it. Same
235// buffer-first rule as render: a template error is a clean 500 rather
236// than half a fragment swapped into somebody's page.
237func renderFragment(w http.ResponseWriter, name, define string, data any) {
238 var buf bytes.Buffer
239 if err := fragments[name].ExecuteTemplate(&buf, define, data); err != nil {
240 http.Error(w, "something went wrong", http.StatusInternalServerError)
241 return
242 }
243 w.Header().Set("Content-Type", "text/html; charset=utf-8")
244 buf.WriteTo(w)
245}
246