{{ render $ctx .Elements | printf "%s" }}
+{{ render $ctx .Elements }}
diff --git a/pkg/renderer/sgml/blank_line.go b/pkg/renderer/sgml/blank_line.go index d6e405d4..560e5216 100644 --- a/pkg/renderer/sgml/blank_line.go +++ b/pkg/renderer/sgml/blank_line.go @@ -1,29 +1,29 @@ package sgml import ( - "bytes" + "strings" "github.com/bytesparadise/libasciidoc/pkg/types" log "github.com/sirupsen/logrus" ) -func (r *sgmlRenderer) renderBlankLine(ctx *Context, _ types.BlankLine) ([]byte, error) { +func (r *sgmlRenderer) renderBlankLine(ctx *Context, _ types.BlankLine) (string, error) { if ctx.IncludeBlankLine { - buf := &bytes.Buffer{} + buf := &strings.Builder{} if err := r.blankLine.Execute(buf, nil); err != nil { - return nil, err + return "", err } log.Debug("rendering blank line") - return buf.Bytes(), nil + return buf.String(), nil } - return []byte{}, nil + return "", nil } -func (r *sgmlRenderer) renderLineBreak() ([]byte, error) { - buf := &bytes.Buffer{} +func (r *sgmlRenderer) renderLineBreak() (string, error) { + buf := &strings.Builder{} if err := r.lineBreak.Execute(buf, nil); err != nil { - return nil, err + return "", err } - return buf.Bytes(), nil + return buf.String(), nil } diff --git a/pkg/renderer/sgml/callout_list.go b/pkg/renderer/sgml/callout_list.go index ceffc44b..53561e05 100644 --- a/pkg/renderer/sgml/callout_list.go +++ b/pkg/renderer/sgml/callout_list.go @@ -1,15 +1,15 @@ package sgml import ( - "bytes" + "strings" "github.com/bytesparadise/libasciidoc/pkg/renderer" "github.com/bytesparadise/libasciidoc/pkg/types" "github.com/pkg/errors" ) -func (r *sgmlRenderer) renderCalloutList(ctx *renderer.Context, l types.CalloutList) ([]byte, error) { - result := &bytes.Buffer{} +func (r *sgmlRenderer) renderCalloutList(ctx *renderer.Context, l types.CalloutList) (string, error) { + result := &strings.Builder{} err := r.calloutList.Execute(result, ContextualPipeline{ Context: ctx, Data: struct { @@ -25,7 +25,7 @@ func (r *sgmlRenderer) renderCalloutList(ctx *renderer.Context, l types.CalloutL }, }) if err != nil { - return nil, errors.Wrapf(err, "unable to render callout list") + return "", errors.Wrap(err, "unable to render callout list") } - return result.Bytes(), nil + return result.String(), nil } diff --git a/pkg/renderer/sgml/cross_reference.go b/pkg/renderer/sgml/cross_reference.go index f28d668a..15802d80 100644 --- a/pkg/renderer/sgml/cross_reference.go +++ b/pkg/renderer/sgml/cross_reference.go @@ -1,8 +1,8 @@ package sgml import ( - "bytes" "path/filepath" + "strings" "github.com/bytesparadise/libasciidoc/pkg/renderer" "github.com/bytesparadise/libasciidoc/pkg/types" @@ -10,9 +10,9 @@ import ( log "github.com/sirupsen/logrus" ) -func (r *sgmlRenderer) renderInternalCrossReference(ctx *renderer.Context, xref types.InternalCrossReference) ([]byte, error) { +func (r *sgmlRenderer) renderInternalCrossReference(ctx *renderer.Context, xref types.InternalCrossReference) (string, error) { log.Debugf("rendering cross reference with ID: %s", xref.ID) - result := &bytes.Buffer{} + result := &strings.Builder{} var label string if xref.Label != "" { label = xref.Label @@ -20,11 +20,11 @@ func (r *sgmlRenderer) renderInternalCrossReference(ctx *renderer.Context, xref if t, ok := target.([]interface{}); ok { renderedContent, err := r.renderElement(ctx, t) if err != nil { - return nil, errors.Wrapf(err, "error while rendering internal cross reference") + return "", errors.Wrap(err, "error while rendering internal cross reference") } - label = string(renderedContent) + label = renderedContent } else { - return nil, errors.Errorf("unable to process internal cross reference to element of type %T", target) + return "", errors.Errorf("unable to process internal cross reference to element of type %T", target) } } else { label = "[" + xref.ID + "]" @@ -37,29 +37,29 @@ func (r *sgmlRenderer) renderInternalCrossReference(ctx *renderer.Context, xref Label: label, }) if err != nil { - return nil, errors.Wrapf(err, "unable to render internal cross reference") + return "", errors.Wrapf(err, "unable to render internal cross reference") } - return result.Bytes(), nil + return result.String(), nil } -func (r *sgmlRenderer) renderExternalCrossReference(ctx *renderer.Context, xref types.ExternalCrossReference) ([]byte, error) { +func (r *sgmlRenderer) renderExternalCrossReference(ctx *renderer.Context, xref types.ExternalCrossReference) (string, error) { log.Debugf("rendering cross reference with ID: %s", xref.Location) - result := &bytes.Buffer{} + result := &strings.Builder{} label, err := r.renderInlineElements(ctx, xref.Label) if err != nil { - return nil, errors.Wrapf(err, "unable to render external cross reference") + return "", errors.Wrap(err, "unable to render external cross reference") } err = r.externalCrossReference.Execute(result, struct { Href string Label string }{ Href: getCrossReferenceLocation(xref), - Label: string(label), + Label: label, }) if err != nil { - return nil, errors.Wrapf(err, "unable to render external cross reference") + return "", errors.Wrap(err, "unable to render external cross reference") } - return result.Bytes(), nil + return result.String(), nil } func getCrossReferenceLocation(xref types.ExternalCrossReference) string { diff --git a/pkg/renderer/sgml/delimited_block.go b/pkg/renderer/sgml/delimited_block.go index 4d00d4a9..5110a78d 100644 --- a/pkg/renderer/sgml/delimited_block.go +++ b/pkg/renderer/sgml/delimited_block.go @@ -3,6 +3,7 @@ package sgml import ( "bytes" "strconv" + "strings" "github.com/alecthomas/chroma" "github.com/alecthomas/chroma/formatters/html" @@ -14,7 +15,7 @@ import ( log "github.com/sirupsen/logrus" ) -func (r *sgmlRenderer) renderDelimitedBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, error) { +func (r *sgmlRenderer) renderDelimitedBlock(ctx *renderer.Context, b types.DelimitedBlock) (string, error) { log.Debugf("rendering delimited block of kind '%v'", b.Attributes[types.AttrKind]) var err error kind := b.Kind @@ -36,11 +37,11 @@ func (r *sgmlRenderer) renderDelimitedBlock(ctx *renderer.Context, b types.Delim case types.Passthrough: return r.renderPassthrough(ctx, b) default: - return nil, errors.Wrapf(err, "unable to render delimited block") + return "", errors.Wrap(err, "unable to render delimited block") } } -func (r *sgmlRenderer) renderFencedBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, error) { +func (r *sgmlRenderer) renderFencedBlock(ctx *renderer.Context, b types.DelimitedBlock) (string, error) { previousWithinDelimitedBlock := ctx.WithinDelimitedBlock previousIncludeBlankLine := ctx.IncludeBlankLine defer func() { @@ -49,7 +50,7 @@ func (r *sgmlRenderer) renderFencedBlock(ctx *renderer.Context, b types.Delimite }() ctx.WithinDelimitedBlock = true ctx.IncludeBlankLine = true - result := &bytes.Buffer{} + result := &strings.Builder{} err := r.fencedBlock.Execute(result, ContextualPipeline{ Context: ctx, Data: struct { @@ -62,10 +63,10 @@ func (r *sgmlRenderer) renderFencedBlock(ctx *renderer.Context, b types.Delimite Elements: discardTrailingBlankLines(b.Elements), }, }) - return result.Bytes(), err + return result.String(), err } -func (r *sgmlRenderer) renderListingBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, error) { +func (r *sgmlRenderer) renderListingBlock(ctx *renderer.Context, b types.DelimitedBlock) (string, error) { previousWithinDelimitedBlock := ctx.WithinDelimitedBlock previousIncludeBlankLine := ctx.IncludeBlankLine defer func() { @@ -74,7 +75,7 @@ func (r *sgmlRenderer) renderListingBlock(ctx *renderer.Context, b types.Delimit }() ctx.WithinDelimitedBlock = true ctx.IncludeBlankLine = true - result := &bytes.Buffer{} + result := &strings.Builder{} err := r.listingBlock.Execute(result, ContextualPipeline{ Context: ctx, Data: struct { @@ -87,10 +88,10 @@ func (r *sgmlRenderer) renderListingBlock(ctx *renderer.Context, b types.Delimit Elements: discardTrailingBlankLines(b.Elements), }, }) - return result.Bytes(), err + return result.String(), err } -func (r *sgmlRenderer) renderSourceBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, error) { +func (r *sgmlRenderer) renderSourceBlock(ctx *renderer.Context, b types.DelimitedBlock) (string, error) { previousWithinDelimitedBlock := ctx.WithinDelimitedBlock previousIncludeBlankLine := ctx.IncludeBlankLine defer func() { @@ -100,7 +101,7 @@ func (r *sgmlRenderer) renderSourceBlock(ctx *renderer.Context, b types.Delimite ctx.WithinDelimitedBlock = true ctx.IncludeBlankLine = true // first, render the content - contentBuf := &bytes.Buffer{} + contentBuf := &strings.Builder{} err := r.sourceBlockContent.Execute(contentBuf, ContextualPipeline{ Context: ctx, Data: struct { @@ -109,7 +110,7 @@ func (r *sgmlRenderer) renderSourceBlock(ctx *renderer.Context, b types.Delimite Elements: discardTrailingBlankLines(b.Elements), }}) if err != nil { - return []byte{}, err + return "", err } content := contentBuf.String() @@ -117,7 +118,7 @@ func (r *sgmlRenderer) renderSourceBlock(ctx *renderer.Context, b types.Delimite language, found := b.Attributes.GetAsString(types.AttrLanguage) if found && highlighter == "pygments" { // using github.com/alecthomas/chroma to highlight the content - contentBuf = &bytes.Buffer{} + contentBuf = &strings.Builder{} lexer := lexers.Get(language) lexer = chroma.Coalesce(lexer) style := styles.Fallback @@ -126,7 +127,7 @@ func (r *sgmlRenderer) renderSourceBlock(ctx *renderer.Context, b types.Delimite } iterator, err := lexer.Tokenise(nil, content) if err != nil { - return []byte{}, err + return "", err } options := []html.Option{ html.ClassPrefix("tok-"), @@ -144,7 +145,7 @@ func (r *sgmlRenderer) renderSourceBlock(ctx *renderer.Context, b types.Delimite } err = html.New(options...).Format(contentBuf, style, iterator) if err != nil { - return []byte{}, err + return "", err } content = contentBuf.String() } @@ -163,15 +164,15 @@ func (r *sgmlRenderer) renderSourceBlock(ctx *renderer.Context, b types.Delimite Language: language, Content: content, }) - return result.Bytes(), err + return result.String(), err } -func (r *sgmlRenderer) renderExampleBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, error) { - result := &bytes.Buffer{} +func (r *sgmlRenderer) renderExampleBlock(ctx *renderer.Context, b types.DelimitedBlock) (string, error) { + result := &strings.Builder{} if k, ok := b.Attributes[types.AttrAdmonitionKind].(types.AdmonitionKind); ok { icon, err := r.renderIcon(ctx, types.Icon{Class: string(k)}, true) if err != nil { - return nil, err + return "", err } err = r.admonitionBlock.Execute(result, ContextualPipeline{ Context: ctx, @@ -189,7 +190,7 @@ func (r *sgmlRenderer) renderExampleBlock(ctx *renderer.Context, b types.Delimit Elements: discardTrailingBlankLines(b.Elements), }, }) - return result.Bytes(), err + return result.String(), err } // default, example block var title string @@ -208,11 +209,11 @@ func (r *sgmlRenderer) renderExampleBlock(ctx *renderer.Context, b types.Delimit Elements: discardTrailingBlankLines(b.Elements), }, }) - return result.Bytes(), err + return result.String(), err } -func (r *sgmlRenderer) renderQuoteBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, error) { - result := &bytes.Buffer{} +func (r *sgmlRenderer) renderQuoteBlock(ctx *renderer.Context, b types.DelimitedBlock) (string, error) { + result := &strings.Builder{} err := r.quoteBlock.Execute(result, ContextualPipeline{ Context: ctx, Data: struct { @@ -227,11 +228,11 @@ func (r *sgmlRenderer) renderQuoteBlock(ctx *renderer.Context, b types.Delimited Elements: b.Elements, }, }) - return result.Bytes(), err + return result.String(), err } -func (r *sgmlRenderer) renderVerseBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, error) { - result := &bytes.Buffer{} +func (r *sgmlRenderer) renderVerseBlock(ctx *renderer.Context, b types.DelimitedBlock) (string, error) { + result := &strings.Builder{} err := r.verseBlock.Execute(result, ContextualPipeline{ Context: ctx, Data: struct { @@ -246,10 +247,10 @@ func (r *sgmlRenderer) renderVerseBlock(ctx *renderer.Context, b types.Delimited Elements: discardTrailingBlankLines(b.Elements), }, }) - return result.Bytes(), err + return result.String(), err } -func (r *sgmlRenderer) renderVerseBlockElement(ctx *renderer.Context, element interface{}) ([]byte, error) { +func (r *sgmlRenderer) renderVerseBlockElement(ctx *renderer.Context, element interface{}) (string, error) { previousIncludeBlankLine := ctx.IncludeBlankLine defer func() { ctx.IncludeBlankLine = previousIncludeBlankLine @@ -261,13 +262,13 @@ func (r *sgmlRenderer) renderVerseBlockElement(ctx *renderer.Context, element in case types.BlankLine: return r.renderBlankLine(ctx, e) default: - return nil, errors.Errorf("unexpected type of element to include in verse block: %T", element) + return "", errors.Errorf("unexpected type of element to include in verse block: %T", element) } } -func (r *sgmlRenderer) renderVerseBlockParagraph(ctx *renderer.Context, p types.Paragraph) ([]byte, error) { +func (r *sgmlRenderer) renderVerseBlockParagraph(ctx *renderer.Context, p types.Paragraph) (string, error) { log.Debugf("rendering paragraph with %d line(s) within a delimited block or a list", len(p.Lines)) - result := &bytes.Buffer{} + result := &strings.Builder{} err := r.verseBlockParagraph.Execute(result, ContextualPipeline{ Context: ctx, Data: struct { @@ -276,11 +277,11 @@ func (r *sgmlRenderer) renderVerseBlockParagraph(ctx *renderer.Context, p types. Lines: p.Lines, }, }) - return result.Bytes(), err + return result.String(), err } -func (r *sgmlRenderer) renderSidebarBlock(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, error) { - result := &bytes.Buffer{} +func (r *sgmlRenderer) renderSidebarBlock(ctx *renderer.Context, b types.DelimitedBlock) (string, error) { + result := &strings.Builder{} err := r.sidebarBlock.Execute(result, ContextualPipeline{ Context: ctx, Data: struct { @@ -293,11 +294,11 @@ func (r *sgmlRenderer) renderSidebarBlock(ctx *renderer.Context, b types.Delimit Elements: discardTrailingBlankLines(b.Elements), }, }) - return result.Bytes(), err + return result.String(), err } -func (r *sgmlRenderer) renderPassthrough(ctx *renderer.Context, b types.DelimitedBlock) ([]byte, error) { - result := &bytes.Buffer{} +func (r *sgmlRenderer) renderPassthrough(ctx *renderer.Context, b types.DelimitedBlock) (string, error) { + result := &strings.Builder{} err := r.passthroughBlock.Execute(result, ContextualPipeline{ Context: ctx, Data: struct { @@ -308,7 +309,7 @@ func (r *sgmlRenderer) renderPassthrough(ctx *renderer.Context, b types.Delimite Elements: discardTrailingBlankLines(b.Elements), }, }) - return result.Bytes(), err + return result.String(), err } func discardTrailingBlankLines(elements []interface{}) []interface{} { diff --git a/pkg/renderer/sgml/document_details.go b/pkg/renderer/sgml/document_details.go index 4550dda1..2a49b595 100644 --- a/pkg/renderer/sgml/document_details.go +++ b/pkg/renderer/sgml/document_details.go @@ -3,6 +3,7 @@ package sgml import ( "bytes" "strconv" + "strings" "github.com/bytesparadise/libasciidoc/pkg/renderer" "github.com/bytesparadise/libasciidoc/pkg/types" @@ -40,7 +41,7 @@ func (r *sgmlRenderer) renderDocumentDetails(ctx *renderer.Context) (*sanitized, } func (r *sgmlRenderer) renderDocumentAuthorsDetails(ctx *renderer.Context) (*sanitized, error) { // TODO: use `types.DocumentAuthor` attribute in context - authorsDetailsBuff := &bytes.Buffer{} + authorsDetailsBuff := &strings.Builder{} i := 1 for { var authorKey string @@ -57,9 +58,11 @@ func (r *sgmlRenderer) renderDocumentAuthorsDetails(ctx *renderer.Context) (*san } // having at least one author is the minimal requirement for document details if author, ok := ctx.Attributes.GetAsString(authorKey); ok { - authorDetailsBuff := &bytes.Buffer{} + if i > 1 { + authorsDetailsBuff.WriteString("\n") + } email, _ := ctx.Attributes.GetAsString(emailKey) - err := r.documentAuthorDetails.Execute(authorDetailsBuff, struct { + err := r.documentAuthorDetails.Execute(authorsDetailsBuff, struct { Index string Name string Email string @@ -72,10 +75,6 @@ func (r *sgmlRenderer) renderDocumentAuthorsDetails(ctx *renderer.Context) (*san return nil, errors.Wrap(err, "error while rendering the document author") } // if there were authors before, need to insert a `\n` - if i > 1 { - authorsDetailsBuff.WriteString("\n") - } - authorsDetailsBuff.Write(authorDetailsBuff.Bytes()) i++ } else { break diff --git a/pkg/renderer/sgml/elements.go b/pkg/renderer/sgml/elements.go index 1ec96a51..7fc5614c 100644 --- a/pkg/renderer/sgml/elements.go +++ b/pkg/renderer/sgml/elements.go @@ -1,8 +1,8 @@ package sgml import ( - "bytes" "reflect" + "strings" "github.com/bytesparadise/libasciidoc/pkg/renderer" "github.com/bytesparadise/libasciidoc/pkg/types" @@ -11,9 +11,9 @@ import ( log "github.com/sirupsen/logrus" ) -func (r *sgmlRenderer) renderElements(ctx *renderer.Context, elements []interface{}) ([]byte, error) { +func (r *sgmlRenderer) renderElements(ctx *renderer.Context, elements []interface{}) (string, error) { log.Debugf("rendering %d elements(s)...", len(elements)) - buff := &bytes.Buffer{} + buff := &strings.Builder{} hasContent := false if !ctx.Config.IncludeHeaderFooter && len(elements) > 0 { if s, ok := elements[0].(types.Section); ok && s.Level == 0 { @@ -28,7 +28,7 @@ func (r *sgmlRenderer) renderElements(ctx *renderer.Context, elements []interfac for _, element := range elements { renderedElement, err := r.renderElement(ctx, element) if err != nil { - return nil, err // no need to wrap the error here + return "", err // no need to wrap the error here } // insert new line if there's already some content (except for BlankLine) _, isBlankline := element.(types.BlankLine) @@ -36,20 +36,20 @@ func (r *sgmlRenderer) renderElements(ctx *renderer.Context, elements []interfac if hasContent && (isVerbatimLine || (!isBlankline && len(renderedElement) > 0)) { buff.WriteString("\n") } - buff.Write(renderedElement) + buff.WriteString(renderedElement) if len(renderedElement) > 0 { hasContent = true } } // log.Debugf("rendered elements: '%s'", buff.String()) - return buff.Bytes(), nil + return buff.String(), nil } // renderListElements is similar to the `renderElements` func above, // but it sets the `withinList` context flag to true for the first element only -func (r *sgmlRenderer) renderListElements(ctx *renderer.Context, elements []interface{}) ([]byte, error) { +func (r *sgmlRenderer) renderListElements(ctx *renderer.Context, elements []interface{}) (string, error) { log.Debugf("rendering list with %d element(s)...", len(elements)) - buff := &bytes.Buffer{} + buff := &strings.Builder{} hasContent := false for i, element := range elements { if i == 0 { @@ -60,23 +60,23 @@ func (r *sgmlRenderer) renderListElements(ctx *renderer.Context, elements []inte ctx.WithinList-- } if err != nil { - return nil, errors.Wrapf(err, "unable to render a list block") + return "", errors.Wrap(err, "unable to render a list block") } // insert new line if there's already some content if hasContent && len(renderedElement) > 0 { buff.WriteString("\n") } - buff.Write(renderedElement) + buff.WriteString(renderedElement) if len(renderedElement) > 0 { hasContent = true } } // log.Debugf("rendered elements: '%s'", buff.String()) - return buff.Bytes(), nil + return buff.String(), nil } // nolint: gocyclo -func (r *sgmlRenderer) renderElement(ctx *renderer.Context, element interface{}) ([]byte, error) { +func (r *sgmlRenderer) renderElement(ctx *renderer.Context, element interface{}) (string, error) { log.Debugf("rendering element of type `%T`", element) switch e := element.(type) { case []interface{}: @@ -138,12 +138,12 @@ func (r *sgmlRenderer) renderElement(ctx *renderer.Context, element interface{}) case types.QuotedString: return r.renderQuotedString(ctx, e) default: - return nil, errors.Errorf("unsupported type of element: %T", element) + return "", errors.Errorf("unsupported type of element: %T", element) } } // nolint: gocyclo -func (r *sgmlRenderer) renderPlainText(ctx *renderer.Context, element interface{}) ([]byte, error) { +func (r *sgmlRenderer) renderPlainText(ctx *renderer.Context, element interface{}) (string, error) { log.Debugf("rendering plain string for element of type %T", element) switch element := element.(type) { case []interface{}: @@ -153,18 +153,18 @@ func (r *sgmlRenderer) renderPlainText(ctx *renderer.Context, element interface{ case types.QuotedText: return r.renderPlainText(ctx, element.Elements) case types.Icon: - return []byte(element.Attributes.GetAsStringWithDefault(types.AttrImageAlt, "")), nil + return element.Attributes.GetAsStringWithDefault(types.AttrImageAlt, ""), nil case types.InlineImage: - return []byte(element.Attributes.GetAsStringWithDefault(types.AttrImageAlt, "")), nil + return element.Attributes.GetAsStringWithDefault(types.AttrImageAlt, ""), nil case types.InlineLink: if alt, ok := element.Attributes[types.AttrInlineLinkText].([]interface{}); ok { return r.renderPlainText(ctx, alt) } - return []byte(element.Location.String()), nil + return element.Location.String(), nil case types.BlankLine: - return []byte("\n\n"), nil + return "\n\n", nil case types.StringElement: - return []byte(element.Content), nil + return element.Content, nil case types.QuotedString: return r.renderQuotedStringPlain(ctx, element) case types.Paragraph: @@ -173,7 +173,7 @@ func (r *sgmlRenderer) renderPlainText(ctx *renderer.Context, element interface{ // footnotes are rendered in HTML so they can appear as such in the table of contents return r.renderFootnoteReferencePlainText(element) default: - return nil, errors.Errorf("unable to render plain string for element of type '%T'", element) + return "", errors.Errorf("unable to render plain string for element of type '%T'", element) } } diff --git a/pkg/renderer/sgml/footnote_reference.go b/pkg/renderer/sgml/footnote_reference.go index 1d2bc498..ec70c51c 100644 --- a/pkg/renderer/sgml/footnote_reference.go +++ b/pkg/renderer/sgml/footnote_reference.go @@ -1,7 +1,6 @@ package sgml import ( - "bytes" "fmt" "strings" @@ -18,8 +17,8 @@ func (r *sgmlRenderer) renderFootnote(ctx *renderer.Context, elements []interfac return strings.TrimSpace(string(result)), nil } -func (r *sgmlRenderer) renderFootnoteReference(note types.FootnoteReference) ([]byte, error) { - result := &bytes.Buffer{} +func (r *sgmlRenderer) renderFootnoteReference(note types.FootnoteReference) (string, error) { + result := &strings.Builder{} if note.ID != types.InvalidFootnoteReference && !note.Duplicate { // valid case for a footnote with content, with our without an explicit reference err := r.footnote.Execute(result, struct { @@ -30,7 +29,7 @@ func (r *sgmlRenderer) renderFootnoteReference(note types.FootnoteReference) ([] Ref: note.Ref, }) if err != nil { - return nil, errors.Wrapf(err, "unable to render footnote") + return "", errors.Wrap(err, "unable to render footnote") } } else if note.Duplicate { // valid case for a footnote with content, with our without an explicit reference @@ -42,7 +41,7 @@ func (r *sgmlRenderer) renderFootnoteReference(note types.FootnoteReference) ([] Ref: note.Ref, }) if err != nil { - return nil, errors.Wrapf(err, "unable to render footnote") + return "", errors.Wrap(err, "unable to render footnote") } } else { // invalid footnote @@ -52,14 +51,14 @@ func (r *sgmlRenderer) renderFootnoteReference(note types.FootnoteReference) ([] Ref: note.Ref, }) if err != nil { - return nil, errors.Wrapf(err, "unable to render missing footnote") + return "", errors.Wrap(err, "unable to render missing footnote") } } - return result.Bytes(), nil + return result.String(), nil } -func (r *sgmlRenderer) renderFootnoteReferencePlainText(note types.FootnoteReference) ([]byte, error) { - result := &bytes.Buffer{} +func (r *sgmlRenderer) renderFootnoteReferencePlainText(note types.FootnoteReference) (string, error) { + result := &strings.Builder{} if note.ID != types.InvalidFootnoteReference { // valid case for a footnote with content, with our without an explicit reference err := r.footnoteRefPlain.Execute(result, struct { @@ -70,20 +69,20 @@ func (r *sgmlRenderer) renderFootnoteReferencePlainText(note types.FootnoteRefer Class: "footnote", }) if err != nil { - return nil, errors.Wrapf(err, "unable to render footnote") + return "", errors.Wrap(err, "unable to render footnote") } } else { - return nil, fmt.Errorf("unable to render missing footnote") + return "", fmt.Errorf("unable to render missing footnote") } - return result.Bytes(), nil + return result.String(), nil } -func (r *sgmlRenderer) renderFootnotes(ctx *renderer.Context, notes []types.Footnote) ([]byte, error) { +func (r *sgmlRenderer) renderFootnotes(ctx *renderer.Context, notes []types.Footnote) (string, error) { // skip if there's no foot note in the doc if len(notes) == 0 { - return []byte{}, nil + return "", nil } - result := &bytes.Buffer{} + result := &strings.Builder{} err := r.footnotes.Execute(result, ContextualPipeline{ Context: ctx, @@ -94,7 +93,7 @@ func (r *sgmlRenderer) renderFootnotes(ctx *renderer.Context, notes []types.Foot }, }) if err != nil { - return []byte{}, errors.Wrapf(err, "failed to render footnotes") + return "", errors.Wrap(err, "failed to render footnotes") } - return result.Bytes(), nil + return result.String(), nil } diff --git a/pkg/renderer/sgml/html5/callout_list.go b/pkg/renderer/sgml/html5/callout_list.go index 46bc9f5a..94cae305 100644 --- a/pkg/renderer/sgml/html5/callout_list.go +++ b/pkg/renderer/sgml/html5/callout_list.go @@ -6,7 +6,7 @@ const ( {{ if .Title }}
{{ render $ctx .Elements | printf "%s" }}
+{{ render $ctx .Elements }}
{{ renderElements $ctx .Elements | printf "%s" }}+
{{ renderElements $ctx .Elements }}
-{{ renderElements $ctx .Elements | printf "%s" }} +{{ renderElements $ctx .Elements }}{{ if .Attribution.First }}
{{ range $index, $element := .Elements }}{{ renderVerse $ctx $element | printf "%s" }}{{ end }}{{ if .Attribution.First }} +
{{ range $index, $element := .Elements }}{{ renderVerse $ctx $element }}{{ end }}{{ if .Attribution.First }}
{{ if .Title }} {{ escape .Title }}
-{{ end }}{{ renderElements $ctx .Elements | printf "%s" }}
+{{ end }}{{ renderElements $ctx .Elements }}
|
{{ $items := .Items }}{{ range $itemIndex, $item := $items }} -{{ renderInline $ctx $item.Term | printf "%s" }} +{{ renderInline $ctx $item.Term }} {{ if $item.Elements }} | -{{ renderList $ctx $item.Elements | printf "%s" }} +{{ renderList $ctx $item.Elements }} {{ if includeNewline $ctx $itemIndex $items }} | |||
@@ -20,17 +20,17 @@ const ( |
{{ .CheckStyle }}{{ renderLines $ctx .Lines | printf "%s" }}
{{ end }}` + delimitedBlockParagraphTmpl = `{{ $ctx := .Context }}{{ with .Data }}{{ .CheckStyle }}{{ renderLines $ctx .Lines }}
{{ end }}` sourceParagraphTmpl = `{{ $ctx := .Context }}{{ with .Data }}{{ if .Language }}{{ else }}{{ end }}{{ renderLines $ctx .Lines | printf "%s" }}
+{{ if .Language }}{{ else }}{{ end }}{{ renderLines $ctx .Lines }}
{{ renderLines $ctx .Lines plainText | printf "%s" }}{{ if .Attribution.First }} +
{{ renderLines $ctx .Lines plainText }}{{ if .Attribution.First }}
-{{ renderLines $ctx .Lines | printf "%s" }} +{{ renderLines $ctx .Lines }}{{ if .Attribution.First }}
{{ $renderedLines }}
{{ end }}` + manpageNameParagraphTmpl = `{{ $ctx := .Context }}{{ with .Data }}{{ $renderedLines := renderLines $ctx .Lines }}{{ $renderedLines }}
{{ end }}` ) diff --git a/pkg/renderer/sgml/html5/section.go b/pkg/renderer/sgml/html5/section.go index fb745319..6e2e9989 100644 --- a/pkg/renderer/sgml/html5/section.go +++ b/pkg/renderer/sgml/html5/section.go @@ -4,19 +4,19 @@ package html5 const ( preambleTmpl = `{{ $ctx := .Context }}{{ with .Data }}{{ if .Wrapper }}{{ renderInline $ctx $cell | printf "%s" }}
{{ renderInline $ctx $cell }}
-{{ renderElements $ctx .Elements | printf "%s" }} +{{ renderElements $ctx .Elements }}{{ if .Attribution.First }}
{{ range $index, $element := .Elements }}{{ renderVerse $ctx $element | printf "%s" }}{{ end }}{{ if .Attribution.First }} +
{{ range $index, $element := .Elements }}{{ renderVerse $ctx $element }}{{ end }}{{ if .Attribution.First }}
{{ $items := .Items }}{{ range $itemIndex, $item := $items }} -{{ renderInline $ctx $item.Term | printf "%s" }} +{{ renderInline $ctx $item.Term }} {{ if $item.Elements }} | -{{ renderList $ctx $item.Elements | printf "%s" }} +{{ renderList $ctx $item.Elements }} {{ if includeNewline $ctx $itemIndex $items }} | ||
{{ renderInline $ctx $cell | printf "%s" }} | {{ includeNewline $ctx $index $headerCells }}{{ end }} +{{ $headerCells := .Header.Cells }}{{ range $index, $cell := $headerCells }}{{ renderInline $ctx $cell }} | {{ includeNewline $ctx $index $headerCells }}{{ end }}||
---|---|---|---|
{{ renderInline $ctx $cell | printf "%s" }} | {{ includeNewline $ctx $indexCells $line.Cells }}{{ end }}
+{{ range $indexCells, $cell := $line.Cells }}{{ renderInline $ctx $cell }} | {{ includeNewline $ctx $indexCells $line.Cells }}{{ end }}