Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate all HTML elements and attributes #2

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions attributes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package main

type attribute struct {
Name string
IsURL bool
}

var attributes = []attribute{
{Name: "accept"},
{Name: "accept-charset"},
{Name: "accesskey"},
{Name: "action"},
{Name: "align"},
{Name: "allow"},
{Name: "alt"},
{Name: "async"},
{Name: "autocapitalize"},
{Name: "autocomplete"},
{Name: "autofocus"},
{Name: "autoplay"},
{Name: "background"},
{Name: "bgcolor"},
{Name: "border"},
{Name: "buffered"},
{Name: "capture"},
{Name: "challenge"},
{Name: "charset"},
{Name: "checked"},
{Name: "cite"},
{Name: "class"},
{Name: "code"},
{Name: "codebase"},
{Name: "color"},
{Name: "cols"},
{Name: "colspan"},
{Name: "content"},
{Name: "contenteditable"},
{Name: "contextmenu"},
{Name: "controls"},
{Name: "coords"},
{Name: "crossorigin"},
{Name: "data"},
// {Name: "data-*"}, // special case
{Name: "datetime"},
{Name: "decoding"},
{Name: "default"},
{Name: "defer"},
{Name: "dir"},
{Name: "dirname"},
{Name: "disabled"},
{Name: "download"},
{Name: "draggable"},
{Name: "enctype"},
{Name: "for"},
{Name: "form"},
{Name: "formaction"},
{Name: "formenctype"},
{Name: "formmethod"},
{Name: "formnovalidate"},
{Name: "formtarget"},
{Name: "headers"},
{Name: "height"},
{Name: "hidden"},
{Name: "high"},
{Name: "href"},
{Name: "hreflang"},
{Name: "http-equiv"},
{Name: "id"},
{Name: "integrity"},
{Name: "inputmode"},
{Name: "ismap"},
{Name: "itemprop"},
{Name: "keytype"},
{Name: "kind"},
{Name: "label"},
{Name: "lang"},
{Name: "list"},
{Name: "loop"},
{Name: "low"},
{Name: "max"},
{Name: "maxlength"},
{Name: "minlength"},
{Name: "media"},
{Name: "method"},
{Name: "min"},
{Name: "multiple"},
{Name: "muted"},
{Name: "name"},
{Name: "novalidate"},
{Name: "open"},
{Name: "optimum"},
{Name: "pattern"},
{Name: "ping"},
{Name: "placeholder"},
{Name: "playsinline"},
{Name: "poster"},
{Name: "preload"},
{Name: "readonly"},
{Name: "referrerpolicy"},
{Name: "rel"},
{Name: "required"},
{Name: "reversed"},
{Name: "role"},
{Name: "rows"},
{Name: "rowspan"},
{Name: "sandbox"},
{Name: "scope"},
{Name: "selected"},
{Name: "shape"},
{Name: "size"},
{Name: "sizes"},
{Name: "slot"},
{Name: "span"},
{Name: "spellcheck"},
{Name: "src"},
{Name: "srcdoc"},
{Name: "srclang"},
{Name: "srcset"},
{Name: "start"},
{Name: "step"},
{Name: "style"},
{Name: "tabindex"},
{Name: "target"},
{Name: "title"},
{Name: "translate"},
{Name: "type"},
{Name: "usemap"},
{Name: "value"},
{Name: "width"},
{Name: "wrap"},
}

var attributesFileTemplate = `// Code generated by blocks; DO NOT EDIT.
package attr

{{range . -}}
func {{ .FuncName }}(value any) Attributes {
return Attributes{AttrPair{Key: "{{ .AttrName }}", Value: value}}
}
func (a Attributes) {{ .FuncName }}(value any) Attributes {
return append(a, AttrPair{Key: "{{ .AttrName }}", Value: value})
}
{{ end }}`
9 changes: 0 additions & 9 deletions blocks.go

This file was deleted.

141 changes: 141 additions & 0 deletions elements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package main

import "github.com/mbertschler/blocks/html"

type element struct {
Name string
Option html.ElementOption
}

// Source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element
// taken from the sidebar, deprecated elements removed
var elements = []element{
{Name: "a"},
{Name: "abbr"},
{Name: "address"},
{Name: "area"},
{Name: "article"},
{Name: "aside"},
{Name: "audio"},
{Name: "b"},
{Name: "base"},
{Name: "bdi"},
{Name: "bdo"},
{Name: "blockquote"},
{Name: "body"},
{Name: "br", Option: html.Void},
{Name: "button"},
{Name: "canvas"},
{Name: "caption"},
{Name: "cite"},
{Name: "code"},
{Name: "col"},
{Name: "colgroup"},
{Name: "data"},
{Name: "datalist"},
{Name: "dd"},
{Name: "del"},
{Name: "details"},
{Name: "dfn"},
{Name: "dialog"},
{Name: "div"},
{Name: "dl"},
{Name: "dt"},
{Name: "em"},
{Name: "embed"},
{Name: "fieldset"},
{Name: "figcaption"},
{Name: "figure"},
{Name: "footer"},
{Name: "form"},
{Name: "h1"},
{Name: "h2"},
{Name: "h3"},
{Name: "h4"},
{Name: "h5"},
{Name: "h6"},
{Name: "head"},
{Name: "header"},
{Name: "hgroup"},
{Name: "hr", Option: html.Void},
{Name: "html"},
{Name: "i"},
{Name: "iframe"},
{Name: "img", Option: html.Void},
{Name: "input", Option: html.Void},
{Name: "ins"},
{Name: "kbd"},
{Name: "label"},
{Name: "legend"},
{Name: "li"},
{Name: "link", Option: html.Void},
{Name: "main"},
{Name: "map"},
{Name: "mark"},
{Name: "menu"},
{Name: "meta", Option: html.Void},
{Name: "meter"},
{Name: "nav"},
{Name: "noscript"},
{Name: "object"},
{Name: "ol"},
{Name: "optgroup"},
{Name: "option"},
{Name: "output"},
{Name: "p"},
{Name: "picture"},
{Name: "portal"},
{Name: "pre", Option: html.NoWhitespace},
{Name: "progress"},
{Name: "q"},
{Name: "rp"},
{Name: "rt"},
{Name: "ruby"},
{Name: "s"},
{Name: "samp"},
{Name: "script", Option: html.JSElement},
{Name: "section"},
{Name: "select"},
{Name: "slot"},
{Name: "small"},
{Name: "source"},
{Name: "span"},
{Name: "strong"},
{Name: "style", Option: html.CSSElement},
{Name: "sub"},
{Name: "summary"},
{Name: "sup"},
{Name: "table"},
{Name: "tbody"},
{Name: "td"},
{Name: "template"},
{Name: "textarea", Option: html.NoWhitespace},
{Name: "tfoot"},
{Name: "th"},
{Name: "thead"},
{Name: "time"},
{Name: "title"},
{Name: "tr"},
{Name: "track"},
{Name: "u"},
{Name: "ul"},
{Name: "var"},
{Name: "video"},
{Name: "wbr"},
}

var elementsFileTemplate = `// Code generated by blocks. DO NOT EDIT.
package html

import "github.com/mbertschler/blocks/html/attr"

{{range . -}}
{{ if .NoChildren -}}
func {{ .FuncName }}(attr attr.Attributes) Block {
return newElement("{{ .TagName }}", attr, nil, Void)
}
{{ else -}}
func {{ .FuncName }}(attr attr.Attributes, children ...Block) Block {
return newElement("{{ .TagName }}", attr, children, {{ .Option }})
}
{{ end }}{{ end }}`
20 changes: 11 additions & 9 deletions examples/components/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"fmt"

"github.com/gin-gonic/gin"

"github.com/mbertschler/blocks/html"
"github.com/mbertschler/blocks/html/attr"
)

type Counter struct {
Expand All @@ -27,15 +29,15 @@ func counterLayout(main html.Block) html.Block {
html.Doctype("html"),
html.Html(nil,
html.Head(nil,
html.Meta(html.Charset("utf-8")),
html.Meta(attr.Charset("utf-8")),
html.Title(nil, html.Text("Blocks")),
html.Link(html.Rel("stylesheet").Href("https://cdn.jsdelivr.net/npm/simpledotcss@2.2.0/simple.min.css")),
html.Link(html.Rel("stylesheet").Href("/dist/bundle.css")),
html.Link(attr.Rel("stylesheet").Href("https://cdn.jsdelivr.net/npm/simpledotcss@2.2.0/simple.min.css")),
html.Link(attr.Rel("stylesheet").Href("/dist/bundle.css")),
),
html.Body(nil,
main,
html.A(html.Href("/"), html.Text("TodoMVC Example")),
html.Script(html.Src("/dist/bundle.js")),
html.A(attr.Href("/"), html.Text("TodoMVC Example")),
html.Script(attr.Src("/dist/bundle.js")),
),
),
}
Expand All @@ -60,12 +62,12 @@ func (c *Counter) RenderBlock(ctx *gin.Context) (html.Block, error) {
if err != nil {
return nil, err
}
block := html.Div(html.Id("counter"),
block := html.Div(attr.Id("counter"),
html.H3(nil, html.Text("Counter")),
html.P(html.Id("count"), html.Text(fmt.Sprintf("Current count: %d", counter.Count))),
html.Button(html.Class("ga").Attr("ga-on", "click").Attr("ga-action", "Counter.Decrease"), html.Text("-")),
html.P(attr.Id("count"), html.Text(fmt.Sprintf("Current count: %d", counter.Count))),
html.Button(attr.Class("ga").Attr("ga-on", "click").Attr("ga-action", "Counter.Decrease"), html.Text("-")),
html.Text(" "),
html.Button(html.Class("ga").Attr("ga-on", "click").Attr("ga-action", "Counter.Increase"), html.Text("+")),
html.Button(attr.Class("ga").Attr("ga-on", "click").Attr("ga-action", "Counter.Increase"), html.Text("+")),
)
return block, nil
}
Expand Down
4 changes: 2 additions & 2 deletions examples/components/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/mbertschler/blocks/components
module github.com/mbertschler/blocks/examples/components

go 1.20
go 1.18

require (
github.com/evanw/esbuild v0.17.16
Expand Down
Loading