Skip to content

Commit

Permalink
linter and docs updates
Browse files Browse the repository at this point in the history
  • Loading branch information
stanistan committed Jan 24, 2024
1 parent 65fcaab commit c85019d
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.hermit/
2 changes: 1 addition & 1 deletion common_views.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (r Raw) AsHTML(_ context.Context) (template.HTML, error) {
// Views is a collection of AsView.
type Views []AsView

// Views creates a *View, which is also an AsView.
// View creates a *View, which is also an AsView.
func (vs Views) View(_ context.Context) (*View, error) {
return V(vs), nil
}
Expand Down
1 change: 1 addition & 0 deletions el/attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var (
equalsQuote = []byte(`="`)
)

// Attrs represents an attribute map.
type Attrs map[string]string

func (a Attrs) writeTo(w io.Writer) {
Expand Down
8 changes: 4 additions & 4 deletions el/interface.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package el

type el[T any] interface {
In(*Element) *Element
Class(string) T
Attrs(Attrs) T
Attr(string, string) T
In(parent *Element) *Element
Class(name string) T
Attrs(a Attrs) T
Attr(name string, value string) T
}
2 changes: 2 additions & 0 deletions memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package veun

import "context"

// MustMemo renders a view into another view and will panic
// if there is an unhandled error anywhere in the view tree.
func MustMemo(v AsView) Raw {
out, err := Render(context.Background(), v)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions template/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"html/template"
)

// ErrNilTemplate is an error for when there is no
// template provided to render as HTML.
var ErrNilTemplate = errors.New("nil template")

// HTMLTemplate encapsulates basic html template rendering.
Expand Down
7 changes: 7 additions & 0 deletions template/template.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package template includes [html/template] related
// functions for the veun library.
package template

import (
Expand All @@ -6,12 +8,17 @@ import (
"io/fs"
)

// HTML is a type alias for [template.HTML].
type HTML = template.HTML

// MustParse will panic if it cannot parse the string contents
// of the given template.
func MustParse(name, contents string) *template.Template {
return template.Must(newTpl(name).Parse(contents))
}

// MustParseFS will panic if it cannot create/parse the
// file system given to it.
func MustParseFS(f fs.FS, ps ...string) *template.Template {
return template.Must(newTpl("ROOT").ParseFS(f, ps...))
}
Expand Down
14 changes: 14 additions & 0 deletions template/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,30 @@ import (
"fmt"
"html/template"
tt "text/template"

"github.com/stanistan/veun"
)

// T is an alias for the Template type.
type T = Template

// Template represents a template with slots
// that can be rendered as HTML.
//
// Template fulfills the [veun.HTMLRenderable] interface.
type Template struct {
Tpl *template.Template
Slots Slots
Data any
}

var _ veun.HTMLRenderable = Template{}

// AsHTML fulfills [veun.AsRenderable] for [Template].
//
// It will attempt to extract the template error from
// the golang library to make the error stack a bit
// more parseable when there is a failure.
func (v Template) AsHTML(ctx context.Context) (HTML, error) {
out, err := v.template(ctx).AsHTML(ctx)
if err != nil {
Expand Down
16 changes: 9 additions & 7 deletions veun.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package veun is a small library to enable composition based
// template rendering of go functions and types into HTML.
package veun

import (
Expand All @@ -10,28 +12,28 @@ import (
// AsV is an alias for the AsView interface.
type AsV = AsView

// HTMLRenderable represents anything that can be rendered to HTML.
// HTMLRenderable represents anything that can be rendered to [template.HTML].
type HTMLRenderable interface {
AsHTML(ctx context.Context) (template.HTML, error)
}

// AsView is anything that can be represented as a View.
// AsView is anything that can be represented as a [*View].
type AsView interface {
View(ctx context.Context) (*View, error)
}

// Render takes a context and something that can become a View
// and renders it.
// Render renders a view tree into HTML given a context.
func Render(ctx context.Context, v AsView) (template.HTML, error) {
return V(v).render(ctx)
}

// V is a factory function that transforms any of its
// inputs into a [[View]].
// inputs into a [View].
//
// If this is not view convertible, this call will succeed,
// but any call go [[Render]] this will fail. This is by
// design to allow for error handling during composition.
// but any call to [Render] will fail.
//
// This is by design to allow for error handling during composition.
func V(in any) *View {
if in == nil {
return nil
Expand Down
2 changes: 1 addition & 1 deletion vhttp/handler/with_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ type statusHandler struct {
status int
}

func (h statusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (h statusHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(h.status)
}

0 comments on commit c85019d

Please sign in to comment.