| // Copyright 2024 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| package main |
| |
| import ( |
| "bytes" |
| |
| "github.com/google/safehtml" |
| "github.com/google/safehtml/template" |
| ) |
| |
| // Exec executes the given template on the page. |
| func Exec(tmpl *template.Template, p page) ([]byte, error) { |
| var buf bytes.Buffer |
| if err := tmpl.Execute(&buf, p); err != nil { |
| return nil, err |
| } |
| return buf.Bytes(), nil |
| } |
| |
| // page is a Gaby webpage containing a [CommonPage]. |
| // Any struct that embeds a [CommonPage] implements this interface. |
| type page interface { |
| // do not directly define [isCommonPage]. |
| isCommonPage() |
| } |
| |
| // A CommonPage is a partial representation of a Gaby web page, |
| // used to store data that is common to many pages. |
| // The templates in tmpl/common.tmpl are defined on this type. |
| type CommonPage struct { |
| // The ID of the page. |
| ID pageID |
| // A plain text description of the webpage. |
| Description string |
| // Link to the feedback page for this page. |
| FeedbackURL string |
| // A list of additional stylesheets to use for this webpage. |
| // "/static/style.css" and [pageID.CSS] are always included |
| // without needing to be listed here. |
| Styles []safeURL |
| // The input form. |
| Form Form |
| } |
| |
| // Implements [page.isCommonPage]. |
| func (*CommonPage) isCommonPage() {} |
| |
| // A Form is a representation of an HTML form. |
| type Form struct { |
| // (Optional) Description and/or general tips for filling out the form. |
| Description string |
| // The text to display on the form's submit button. |
| SubmitText string |
| // The form's inputs. |
| Inputs []FormInput |
| } |
| |
| // A FormInput represents an input (or a group of inputs) |
| // to an HTML form. |
| type FormInput struct { |
| Label string // display text |
| Type string // type to display to the user (for tips section) |
| Description string // description of the input and its usage (for tips section) |
| |
| Name safeID // HTML "name" |
| Required bool // whether the input is required |
| |
| // Additional data, based on the type of form input. |
| Typed typedInput |
| } |
| |
| type typedInput interface { |
| InputType() string |
| } |
| |
| // TextInput is a an HTML "text" input. |
| type TextInput struct { |
| ID safeID // HTML "id" |
| Value string // HTML "value" |
| } |
| |
| // Implments [typedInput.InputType]. |
| func (TextInput) InputType() string { |
| return "text" |
| } |
| |
| // RadioInput is a collection of HTML "radio" inputs. |
| type RadioInput struct { |
| Choices []RadioChoice |
| } |
| |
| // Implements [typedInput.InputType]. |
| func (RadioInput) InputType() string { |
| return "radio" |
| } |
| |
| // RadioChoice is a single HTML "radio" input. |
| type RadioChoice struct { |
| Label string // display text |
| ID safeID // HTML "id" |
| Value string // HTML "value" |
| Checked bool // whether the button should be checked |
| } |
| |
| type pageID string |
| |
| func (p pageID) Endpoint() string { |
| return "/" + string(p) |
| } |
| |
| func (p pageID) Title() string { |
| if t, ok := titles[p]; ok { |
| return t |
| } |
| return string(p) |
| } |
| |
| func (p pageID) CSS() safeURL { |
| const cssFmt = "/static/%{p}.css" |
| u, err := safehtml.TrustedResourceURLFormatFromConstant(cssFmt, map[string]string{"p": string(p)}) |
| if err != nil { |
| panic(err) |
| } |
| return u |
| } |
| |
| // Shorthands for safehtml types. |
| type ( |
| safeID = safehtml.Identifier |
| safeURL = safehtml.TrustedResourceURL |
| ) |
| |
| // Shorthands for safehtml functions. |
| var ( |
| toSafeID = safehtml.IdentifierFromConstant |
| toSafeURL = safehtml.TrustedResourceURLFromConstant |
| ) |