Skip to content

Commit

Permalink
refactor(renderer): Simplify templates (#634)
Browse files Browse the repository at this point in the history
This eliminates the `ContextualPipeline`, and simplifies the templates
a bit, making them hopefully much easier to read and maintain, and
also to build derivatives.

The templates no longer require the use of functions, for the most
part. In particular the detailed rendering functions for nesting
are completely hidden from the templates, instead there is just a
generic `.Content` field which represents the rendered content for
nested content.

While here, I've added `.Roles` support in the renderer for top level
block.  These won't be used until the parser is updated to provide
Role attributes for them though.

A lot of the templates use the "sanitized" type to track the fact that
they can be trusted for HTML safety.

None of the tests were modified, in spite of the size of this change.

This is foundation work both for future backends (ePUB, docbook), as
well as further improvements to the existing rendering (for example
adding support for table styling.)
  • Loading branch information
gdamore authored Jun 23, 2020
1 parent 3fc5e81 commit f2931da
Show file tree
Hide file tree
Showing 39 changed files with 1,310 additions and 962 deletions.
56 changes: 44 additions & 12 deletions pkg/renderer/sgml/callout_list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sgml

import (
"io"
"strings"

"github.com/bytesparadise/libasciidoc/pkg/renderer"
Expand All @@ -10,22 +11,53 @@ import (

func (r *sgmlRenderer) renderCalloutList(ctx *renderer.Context, l types.CalloutList) (string, error) {
result := &strings.Builder{}
err := r.calloutList.Execute(result, ContextualPipeline{
content := &strings.Builder{}

for _, item := range l.Items {

err := r.renderCalloutListItem(ctx, content, item)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list item")
}
}
err := r.calloutList.Execute(result, struct {
Context *renderer.Context
ID sanitized
Title string
Roles sanitized
Content sanitized
Items []types.CalloutListItem
}{
Context: ctx,
Data: struct {
ID string
Title string
Role string
Items []types.CalloutListItem
}{
ID: r.renderElementID(l.Attributes),
Title: l.Attributes.GetAsStringWithDefault(types.AttrTitle, ""),
Role: l.Attributes.GetAsStringWithDefault(types.AttrRole, ""),
Items: l.Items,
},
ID: r.renderElementID(l.Attributes),
Title: r.renderElementTitle(l.Attributes),
Roles: r.renderElementRoles(l.Attributes),
Content: sanitized(content.String()),
Items: l.Items,
})
if err != nil {
return "", errors.Wrap(err, "unable to render callout list")
}
return result.String(), nil
}

func (r *sgmlRenderer) renderCalloutListItem(ctx *renderer.Context, w io.Writer, item types.CalloutListItem) error {

content, err := r.renderListElements(ctx, item.Elements)
if err != nil {
return errors.Wrap(err, "unable to render callout list item content")
}
err = r.calloutListItem.Execute(w, struct {
Context *renderer.Context
Ref int
Content sanitized
}{
Context: ctx,
Ref: item.Ref,
Content: sanitized(content),
})
if err != nil {
return errors.Wrap(err, "unable to render callout list")
}
return nil
}
11 changes: 0 additions & 11 deletions pkg/renderer/sgml/contextual_pipeline.go

This file was deleted.

Loading

0 comments on commit f2931da

Please sign in to comment.