dmitri.shuralyov.com/html/belt/...

Initial commit.
dmitshur committed 6 years ago commit 39adf59a6c4420aec62fcd78a65e1e41c7f57bd5
Collapse all
belt.go
@@ -0,0 +1,57 @@
// Package belt is an opinionated collection of HTML components
// for shared use by multiple web apps.
package belt

import (
	"github.com/shurcooL/htmlg"
	"golang.org/x/net/html"
	"golang.org/x/net/html/atom"
)

// Reference is a component that displays a reference (branch or tag). E.g., "master".
type Reference struct {
	Name          string
	Strikethrough bool
}

func (r Reference) Render() []*html.Node {
	codeStyle := `padding: 2px 6px;
background-color: rgb(232, 241, 246);
border-radius: 3px;`
	if r.Strikethrough {
		codeStyle += `text-decoration: line-through; color: gray;`
	}
	code := &html.Node{
		Type: html.ElementNode, Data: atom.Code.String(),
		Attr:       []html.Attribute{{Key: atom.Style.String(), Val: codeStyle}},
		FirstChild: htmlg.Text(r.Name),
	}
	return []*html.Node{code}
}

// CommitID is a component that displays a commit ID. E.g., "c0de1234".
type CommitID struct {
	SHA     string
	HTMLURL string // Optional.
}

func (c CommitID) Render() []*html.Node {
	sha := &html.Node{
		Type: html.ElementNode, Data: atom.Code.String(),
		Attr: []html.Attribute{
			{Key: atom.Style.String(), Val: "width: 8ch; overflow: hidden; display: inline-grid; white-space: nowrap;"},
			{Key: atom.Title.String(), Val: c.SHA},
		},
		FirstChild: htmlg.Text(c.SHA),
	}
	if c.HTMLURL != "" {
		sha = &html.Node{
			Type: html.ElementNode, Data: atom.A.String(),
			Attr: []html.Attribute{
				{Key: atom.Href.String(), Val: c.HTMLURL},
			},
			FirstChild: sha,
		}
	}
	return []*html.Node{sha}
}