Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
feat: Inserts newlines automatically if not the first child to improv…
Browse files Browse the repository at this point in the history
…e formatting
  • Loading branch information
pojntfx committed Oct 4, 2021
1 parent 0c7504c commit 2d79b27
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 46 deletions.
127 changes: 86 additions & 41 deletions example/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,90 @@ type PF4Tabs struct {
}

func (c *PF4Tabs) Render() app.UI {
return app.Div().Body(
app.Div().Class("pf-c-tabs pf-m-fill").DataSet("ouia-component-type", "PF4/Tabs").DataSet(
"ouia-safe", true).DataSet("ouia-component-id", "OUIA-Generated-Tabs-12").Body(
app.Button().Class("pf-c-tabs__scroll-button").Aria("label", "Scroll left").Aria("hidden",
true).Disabled(true).Body(), app.Ul().Class("pf-c-tabs__list").Body(
app.Li().Class("pf-c-tabs__item pf-m-current").Body(
app.Button().DataSet("ouia-component-type", "PF4/TabButton").DataSet("ouia-safe", true).Class(
"pf-c-tabs__link").ID("pf-tab-0-pf-1633041198065oegccuf7ng").Aria("controls",
"pf-tab-section-0-pf-1633041198065oegccuf7ng").Body(
app.Span().Class("pf-c-tabs__item-icon"), app.Span().Class("pf-c-tabs__item-text").Body(
app.Text("Users")))), app.Li().Class("pf-c-tabs__item").Body(
app.Button().DataSet("ouia-component-type", "PF4/TabButton").DataSet("ouia-safe", true).Class(
"pf-c-tabs__link").ID("pf-tab-1-pf-1633041198065oegccuf7ng").Aria("controls",
"pf-tab-section-1-pf-1633041198065oegccuf7ng").Body(
app.Span().Class("pf-c-tabs__item-icon"), app.Span().Class("pf-c-tabs__item-text").Body(
app.Text("Containers")))), app.Li().Class("pf-c-tabs__item").Body(
app.Button().DataSet("ouia-component-type", "PF4/TabButton").DataSet("ouia-safe", true).Class(
"pf-c-tabs__link").ID("pf-tab-2-pf-1633041198065oegccuf7ng").Aria("controls",
"pf-tab-section-2-pf-1633041198065oegccuf7ng").Body(
app.Span().Class("pf-c-tabs__item-icon"), app.Span().Class("pf-c-tabs__item-text").Body(
app.Text("Database"))))), app.Button().Class("pf-c-tabs__scroll-button").Aria("label",
"Scroll right").Aria("hidden", true).Disabled(true).Body()), app.Section().Class(
"pf-c-tab-content").ID("pf-tab-section-0-pf-1633041198065oegccuf7ng").Aria("labelledby",
"pf-tab-0-pf-1633041198065oegccuf7ng").Aria("role", "tabpanel").TabIndex(0).DataSet(
"ouia-component-type", "PF4/TabContent").DataSet("ouia-safe", true).Body(
app.Text("\n Users\n ")), app.Section().Class("pf-c-tab-content").ID(
"pf-tab-section-1-pf-1633041198065oegccuf7ng").Aria("labelledby",
"pf-tab-1-pf-1633041198065oegccuf7ng").Aria("role", "tabpanel").TabIndex(0).DataSet(
"ouia-component-type", "PF4/TabContent").DataSet("ouia-safe", true).Hidden(true).Body(
app.Text("\n Containers\n ")), app.Section().Hidden(true).Class("pf-c-tab-content").ID(
"pf-tab-section-2-pf-1633041198065oegccuf7ng").Aria("labelledby",
"pf-tab-2-pf-1633041198065oegccuf7ng").Aria("role", "tabpanel").TabIndex(0).DataSet(
"ouia-component-type", "PF4/TabContent").DataSet("ouia-safe", true).Body(
app.Text("\n Database\n ")), app.Div().Style("margin-top", " 20px").Body(
app.Div().Class("pf-c-check").Body(
app.Input().ID("toggle-box-filled-icon").Name("toggle-box-filled-icon").Class(
"pf-c-check__input").Type("checkbox").Aria("invalid", "false").Aria("label",
"show box variation checkbox with filled icon tabs").DataSet("ouia-component-type",
"PF4/Checkbox").DataSet("ouia-safe", true).DataSet("ouia-component-id",
"OUIA-Generated-Checkbox-9"), app.Label().Class("pf-c-check__label").For("toggle-box-filled-icon").Body(
app.Text("isBox")))))
return app.Div().
Body(
app.Div().Class("pf-c-tabs pf-m-fill").DataSet("ouia-component-type", "PF4/Tabs").DataSet(
"ouia-safe", true).DataSet("ouia-component-id", "OUIA-Generated-Tabs-12").
Body(
app.Button().Class("pf-c-tabs__scroll-button").Aria("label", "Scroll left").Aria("hidden", true).Disabled(true).
Body(),
app.Ul().Class("pf-c-tabs__list").
Body(
app.Li().Class("pf-c-tabs__item pf-m-current").
Body(
app.Button().DataSet("ouia-component-type", "PF4/TabButton").DataSet("ouia-safe",
true).Class("pf-c-tabs__link").ID("pf-tab-0-pf-1633041198065oegccuf7ng").Aria("controls",
"pf-tab-section-0-pf-1633041198065oegccuf7ng").
Body(
app.Span().Class("pf-c-tabs__item-icon"),
app.Span().Class("pf-c-tabs__item-text").
Body(
app.Text("Users"),
),
),
),
app.Li().Class("pf-c-tabs__item").
Body(
app.Button().DataSet("ouia-component-type", "PF4/TabButton").DataSet("ouia-safe",
true).Class("pf-c-tabs__link").ID("pf-tab-1-pf-1633041198065oegccuf7ng").Aria("controls",
"pf-tab-section-1-pf-1633041198065oegccuf7ng").
Body(
app.Span().Class("pf-c-tabs__item-icon"),
app.Span().Class("pf-c-tabs__item-text").
Body(
app.Text("Containers"),
),
),
),
app.Li().Class("pf-c-tabs__item").
Body(
app.Button().DataSet("ouia-component-type", "PF4/TabButton").DataSet("ouia-safe",
true).Class("pf-c-tabs__link").ID("pf-tab-2-pf-1633041198065oegccuf7ng").Aria("controls",
"pf-tab-section-2-pf-1633041198065oegccuf7ng").
Body(
app.Span().Class("pf-c-tabs__item-icon"),
app.Span().Class("pf-c-tabs__item-text").
Body(
app.Text("Database"),
),
),
),
),
app.Button().Class("pf-c-tabs__scroll-button").Aria("label", "Scroll right").Aria("hidden", true).Disabled(true).
Body(),
),
app.Section().Class("pf-c-tab-content").ID("pf-tab-section-0-pf-1633041198065oegccuf7ng").Aria(
"labelledby", "pf-tab-0-pf-1633041198065oegccuf7ng").Aria("role", "tabpanel").TabIndex(0).DataSet(
"ouia-component-type", "PF4/TabContent").DataSet("ouia-safe", true).
Body(
app.Text("\n Users\n "),
),
app.Section().Class("pf-c-tab-content").ID("pf-tab-section-1-pf-1633041198065oegccuf7ng").Aria(
"labelledby", "pf-tab-1-pf-1633041198065oegccuf7ng").Aria("role", "tabpanel").TabIndex(0).DataSet(
"ouia-component-type", "PF4/TabContent").DataSet("ouia-safe", true).Hidden(true).
Body(
app.Text("\n Containers\n "),
),
app.Section().Hidden(true).Class("pf-c-tab-content").ID(
"pf-tab-section-2-pf-1633041198065oegccuf7ng").Aria("labelledby",
"pf-tab-2-pf-1633041198065oegccuf7ng").Aria("role", "tabpanel").TabIndex(0).DataSet(
"ouia-component-type", "PF4/TabContent").DataSet("ouia-safe", true).
Body(
app.Text("\n Database\n "),
),
app.Div().Style("margin-top", " 20px").
Body(
app.Div().Class("pf-c-check").
Body(
app.Input().ID("toggle-box-filled-icon").Name("toggle-box-filled-icon").Class(
"pf-c-check__input").Type("checkbox").Aria("invalid", "false").Aria("label",
"show box variation checkbox with filled icon tabs").DataSet("ouia-component-type",
"PF4/Checkbox").DataSet("ouia-safe", true).DataSet("ouia-component-id", "OUIA-Generated-Checkbox-9"),
app.Label().Class("pf-c-check__label").For("toggle-box-filled-icon").
Body(
app.Text("isBox"),
),
),
),
)
}
17 changes: 12 additions & 5 deletions pkg/converter/goapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

func convertHTMLToStatements(doc *html.Node, goAppPkg string) (*Statement, error) {
var crawler func(*html.Node) (*Statement, error)
crawler = func(node *html.Node) (*Statement, error) {
var crawler func(node *html.Node, nthChild int) (*Statement, error)
crawler = func(node *html.Node, nthChild int) (*Statement, error) {
el := Null()

if node.Type == html.TextNode && strings.TrimSpace(node.Data) != "" {
Expand All @@ -22,6 +22,10 @@ func convertHTMLToStatements(doc *html.Node, goAppPkg string) (*Statement, error
} else if node.Type == html.ElementNode && node.DataAtom.String() != "" {
// Handle complex node
el = Qual(goAppPkg, formatTag(node.DataAtom.String())).Call()
if nthChild >= 2 {
el = Line().Qual(goAppPkg, formatTag(node.DataAtom.String())).Call()
}

for _, attr := range node.Attr {
// Attributes to ignore
if attr.Key == "gutter" || attr.Key == "onload" {
Expand Down Expand Up @@ -127,26 +131,29 @@ func convertHTMLToStatements(doc *html.Node, goAppPkg string) (*Statement, error
}

children := []Code{}
i := 0
for child := node.FirstChild; child != nil; child = child.NextSibling {
// Tags to ignore
if child.DataAtom.String() != "svg" {
child, err := crawler(child)
child, err := crawler(child, i)
if err != nil {
return nil, err
}

children = append(children, child)

i++
}
}

if len(children) > 0 {
el.Dot("Body").Call(Line().List(children...))
el.Dot("").Line().Id("Body").Call(Line().List(append(children, Line())...))
}

return el, nil
}

return crawler(doc)
return crawler(doc, 0)
}

func formatTag(tag string) string {
Expand Down

0 comments on commit 2d79b27

Please sign in to comment.