Skip to content

Commit

Permalink
feat(renderer): support elements in titles
Browse files Browse the repository at this point in the history
support elements such as links, etc.
follow-up of bytesparadise#999

Fixes bytesparadise#956

Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
  • Loading branch information
xcoulon committed May 5, 2022
1 parent 868aa5e commit 6a30ed4
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 34 deletions.
4 changes: 2 additions & 2 deletions pkg/renderer/sgml/delimited_block_admonition.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (r *sgmlRenderer) renderAdmonitionBlock(ctx *renderer.Context, b *types.Del
if err != nil {
return "", errors.Wrap(err, "unable to render fenced block content")
}
title, err := r.renderElementTitle(b.Attributes)
title, err := r.renderElementTitle(ctx, b.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func (r *sgmlRenderer) renderAdmonitionParagraph(ctx *renderer.Context, p *types
if err != nil {
return "", errors.Wrap(err, "unable to render fenced block content")
}
title, err := r.renderElementTitle(p.Attributes)
title, err := r.renderElementTitle(ctx, p.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/renderer/sgml/delimited_block_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (r *sgmlRenderer) renderExampleBlock(ctx *renderer.Context, b *types.Delimi
number = ctx.GetAndIncrementExampleBlockCounter()
c = strings.ReplaceAll(c, "{counter:example-number}", strconv.Itoa(number))
}
title, err := r.renderElementTitle(b.Attributes)
title, err := r.renderElementTitle(ctx, b.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down Expand Up @@ -78,7 +78,7 @@ func (r *sgmlRenderer) renderExampleParagraph(ctx *renderer.Context, p *types.Pa
if err != nil {
return "", errors.Wrap(err, "unable to render fenced block content")
}
title, err := r.renderElementTitle(p.Attributes)
title, err := r.renderElementTitle(ctx, p.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/renderer/sgml/delimited_block_fenced.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (r *sgmlRenderer) renderFencedBlock(ctx *renderer.Context, b *types.Delimit
if err != nil {
return "", errors.Wrap(err, "unable to render fenced block roles")
}
title, err := r.renderElementTitle(b.Attributes)
title, err := r.renderElementTitle(ctx, b.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/renderer/sgml/delimited_block_listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (r *sgmlRenderer) renderListingBlock(ctx *renderer.Context, b *types.Delimi
if err != nil {
return "", errors.Wrap(err, "unable to render listing block roles")
}
title, err := r.renderElementTitle(b.Attributes)
title, err := r.renderElementTitle(ctx, b.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down Expand Up @@ -57,7 +57,7 @@ func (r *sgmlRenderer) renderListingParagraph(ctx *renderer.Context, p *types.Pa
if err != nil {
return "", errors.Wrap(err, "unable to render listing block roles")
}
title, err := r.renderElementTitle(p.Attributes)
title, err := r.renderElementTitle(ctx, p.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/renderer/sgml/delimited_block_markdown_quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (r *sgmlRenderer) renderMarkdownQuoteBlock(ctx *renderer.Context, b *types.
if err != nil {
return "", errors.Wrap(err, "unable to render fenced block content")
}
title, err := r.renderElementTitle(b.Attributes)
title, err := r.renderElementTitle(ctx, b.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/renderer/sgml/delimited_block_quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (r *sgmlRenderer) renderQuoteBlock(ctx *renderer.Context, b *types.Delimite
if err != nil {
return "", errors.Wrap(err, "unable to render fenced block content")
}
title, err := r.renderElementTitle(b.Attributes)
title, err := r.renderElementTitle(ctx, b.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down Expand Up @@ -58,7 +58,7 @@ func (r *sgmlRenderer) renderQuoteParagraph(ctx *renderer.Context, p *types.Para
if err != nil {
return "", errors.Wrap(err, "unable to render quote paragraph lines")
}
title, err := r.renderElementTitle(p.Attributes)
title, err := r.renderElementTitle(ctx, p.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/renderer/sgml/delimited_block_sidebar.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (r *sgmlRenderer) renderSidebarBlock(ctx *renderer.Context, b *types.Delimi
if err != nil {
return "", errors.Wrap(err, "unable to render fenced block content")
}
title, err := r.renderElementTitle(b.Attributes)
title, err := r.renderElementTitle(ctx, b.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/renderer/sgml/delimited_block_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (r *sgmlRenderer) renderSourceBlock(ctx *renderer.Context, b *types.Delimit
}
}
}
title, err := r.renderElementTitle(b.Attributes)
title, err := r.renderElementTitle(ctx, b.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/renderer/sgml/delimited_block_verse.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (r *sgmlRenderer) renderVerseBlock(ctx *renderer.Context, b *types.Delimite
if err != nil {
return "", errors.Wrap(err, "unable to render verse block")
}
title, err := r.renderElementTitle(b.Attributes)
title, err := r.renderElementTitle(ctx, b.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down Expand Up @@ -63,7 +63,7 @@ func (r *sgmlRenderer) renderVerseParagraph(ctx *renderer.Context, p *types.Para
if err != nil {
return "", errors.Wrap(err, "unable to render verse block")
}
title, err := r.renderElementTitle(p.Attributes)
title, err := r.renderElementTitle(ctx, p.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/renderer/sgml/html5/delimited_block_listing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,21 @@ import <a>
<div class="paragraph">
<p>&lt;a&gt; an import</p>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})

It("with quoted text and link in title", func() {
source := `.a *link* to https://github.com[GitHub]
----
content
----`
expected := `<div class="listingblock">
<div class="title">a <strong>link</strong> to <a href="https://github.com">GitHub</a></div>
<div class="content">
<pre>content</pre>
</div>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/renderer/sgml/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

func (r *sgmlRenderer) renderImageBlock(ctx *renderer.Context, img *types.ImageBlock) (string, error) {
result := &strings.Builder{}
title, err := r.renderElementTitle(img.Attributes)
title, err := r.renderElementTitle(ctx, img.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render image")
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func (r *sgmlRenderer) renderInlineImage(ctx *renderer.Context, img *types.Inlin
if err != nil {
return "", errors.Wrap(err, "unable to render image")
}
title, err := r.renderElementTitle(img.Attributes)
title, err := r.renderElementTitle(ctx, img.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/renderer/sgml/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (r *sgmlRenderer) renderOrderedList(ctx *renderer.Context, l *types.List) (
if err != nil {
return "", errors.Wrap(err, "unable to render ordered list roles")
}
title, err := r.renderElementTitle(l.Attributes)
title, err := r.renderElementTitle(ctx, l.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down Expand Up @@ -152,7 +152,7 @@ func (r *sgmlRenderer) renderUnorderedList(ctx *renderer.Context, l *types.List)
if err != nil {
return "", errors.Wrap(err, "unable to render unordered list roles")
}
title, err := r.renderElementTitle(l.Attributes)
title, err := r.renderElementTitle(ctx, l.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func (r *sgmlRenderer) renderLabeledList(ctx *renderer.Context, l *types.List) (
if err != nil {
return "", errors.Wrap(err, "unable to render labeled list roles")
}
title, err := r.renderElementTitle(l.Attributes)
title, err := r.renderElementTitle(ctx, l.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render labeled list roles")
}
Expand Down Expand Up @@ -305,7 +305,7 @@ func (r *sgmlRenderer) renderCalloutList(ctx *renderer.Context, l *types.List) (
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
title, err := r.renderElementTitle(l.Attributes)
title, err := r.renderElementTitle(ctx, l.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/renderer/sgml/literal_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (r *sgmlRenderer) renderLiteralBlock(ctx *renderer.Context, b *types.Delimi
if err != nil {
return "", errors.Wrap(err, "unable to render literal block roles")
}
title, err := r.renderElementTitle(b.Attributes)
title, err := r.renderElementTitle(ctx, b.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down Expand Up @@ -59,7 +59,7 @@ func (r *sgmlRenderer) renderLiteralParagraph(ctx *renderer.Context, b *types.Pa
if err != nil {
return "", errors.Wrap(err, "unable to render literal block roles")
}
title, err := r.renderElementTitle(b.Attributes)
title, err := r.renderElementTitle(ctx, b.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render callout list roles")
}
Expand Down
26 changes: 15 additions & 11 deletions pkg/renderer/sgml/paragraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (r *sgmlRenderer) renderRegularParagraph(ctx *renderer.Context, p *types.Pa
if err != nil {
return "", errors.Wrap(err, "unable to render paragraph roles")
}
title, err := r.renderElementTitle(p.Attributes)
title, err := r.renderElementTitle(ctx, p.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render paragraph roles")
}
Expand Down Expand Up @@ -105,7 +105,7 @@ func (r *sgmlRenderer) renderEmbeddedParagraph(ctx *renderer.Context, p *types.P
if err != nil {
return "", errors.Wrap(err, "unable to render delimited block paragraph content")
}
title, err := r.renderElementTitle(p.Attributes)
title, err := r.renderElementTitle(ctx, p.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render delimited block paragraph content")
}
Expand Down Expand Up @@ -142,16 +142,20 @@ func renderCheckStyle(style interface{}) string {
}
}

func (r *sgmlRenderer) renderElementTitle(attrs types.Attributes) (string, error) {
if title, found, err := attrs.GetAsString(types.AttrTitle); err != nil {
return "", err
} else if found {
result := EscapeString(strings.TrimSpace(title))
// log.Debugf("rendered title: '%s'", result)
return result, nil
func (r *sgmlRenderer) renderElementTitle(ctx *renderer.Context, attrs types.Attributes) (string, error) {
title, found := attrs[types.AttrTitle]
if !found {
log.Debug("no title to render")
return "", nil
}
switch title := title.(type) {
case string:
return title, nil
case []interface{}:
return r.renderElements(ctx, title)
default:
return "", errors.New("unable to render title")
}
log.Debug("no title to render")
return "", nil
}

type lineRenderer struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/renderer/sgml/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (r *sgmlRenderer) renderTable(ctx *renderer.Context, t *types.Table) (strin
if err != nil {
return "", errors.Wrap(err, "unable to render table roles")
}
title, err := r.renderElementTitle(t.Attributes)
title, err := r.renderElementTitle(ctx, t.Attributes)
if err != nil {
return "", errors.Wrap(err, "unable to render table title")
}
Expand Down

0 comments on commit 6a30ed4

Please sign in to comment.