Skip to content

Commit

Permalink
chore(renderer): update sgml renderer code-gen (#1066)
Browse files Browse the repository at this point in the history
Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
  • Loading branch information
xcoulon authored Jul 7, 2022
1 parent 348d3bc commit 0bbc37e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
24 changes: 20 additions & 4 deletions code-gen/renderer-templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,37 @@ const (
sgmlRenderer = `package sgml
import (
"fmt"
"sync"
text "text/template"
"strings"
texttemplate "text/template"
log "github.com/sirupsen/logrus"
)
type sgmlRenderer struct {
templates Templates
functions text.FuncMap
functions texttemplate.FuncMap
{{ range $i, $tmpl := . }}
{{ once $tmpl}} sync.Once
{{ tmpl $tmpl}} *text.Template
{{ tmpl $tmpl}} *texttemplate.Template
{{ end }}
}
type template func() (*texttemplate.Template, error)
func (r *sgmlRenderer) execute(tmpl template, data interface{}) (string, error) {
result := &strings.Builder{}
t, err := tmpl()
if err != nil {
return "", err
}
if err := t.Execute(result, data); err != nil {
return "", err
}
return result.String(), nil
}
func (r *sgmlRenderer) newTemplate(name string, tmpl string, err error) (*texttemplate.Template, error) {
// NB: if the data is missing below, it will be an empty string.
if err != nil {
Expand All @@ -48,7 +64,7 @@ func (r *sgmlRenderer) newTemplate(name string, tmpl string, err error) (*textte
}
{{ range $i, $tmpl := . }}
func (r *sgmlRenderer) {{ func $tmpl }} (*text.Template, error) {
func (r *sgmlRenderer) {{ func $tmpl }} (*texttemplate.Template, error) {
var err error
r.{{ once $tmpl }}.Do(func() {
r.{{ tmpl $tmpl }}, err = r.newTemplate("{{ $tmpl }}", r.templates.{{ $tmpl }}, err)
Expand Down
26 changes: 12 additions & 14 deletions pkg/renderer/sgml/sgml_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"sync"
texttemplate "text/template"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -245,6 +244,18 @@ type sgmlRenderer struct {

type template func() (*texttemplate.Template, error)

func (r *sgmlRenderer) execute(tmpl template, data interface{}) (string, error) {
result := &strings.Builder{}
t, err := tmpl()
if err != nil {
return "", err
}
if err := t.Execute(result, data); err != nil {
return "", err
}
return result.String(), nil
}

func (r *sgmlRenderer) newTemplate(name string, tmpl string, err error) (*texttemplate.Template, error) {
// NB: if the data is missing below, it will be an empty string.
if err != nil {
Expand All @@ -262,19 +273,6 @@ func (r *sgmlRenderer) newTemplate(name string, tmpl string, err error) (*textte
return t, nil
}

func (s *sgmlRenderer) execute(loadTmpl template, data interface{}) (string, error) {
tmpl, err := loadTmpl()
result := &strings.Builder{}
if err != nil {
return "", errors.Wrap(err, "unable to load template")
}
if err := tmpl.Execute(result, data); err != nil {
return "", err
}
return result.String(), nil

}

func (r *sgmlRenderer) admonitionBlock() (*texttemplate.Template, error) {
var err error
r.admonitionBlockOnce.Do(func() {
Expand Down

0 comments on commit 0bbc37e

Please sign in to comment.