Skip to content

Commit

Permalink
feat(renderer): render external links (#48)
Browse files Browse the repository at this point in the history
Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
  • Loading branch information
xcoulon authored Jan 7, 2018
1 parent 65f9c9c commit 1154a87
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
28 changes: 28 additions & 0 deletions renderer/html5/external_link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package html5

import (
"bytes"
"html/template"

"github.com/bytesparadise/libasciidoc/renderer"
"github.com/bytesparadise/libasciidoc/types"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

var externalLinkTmpl *template.Template

// initializes the templates
func init() {
externalLinkTmpl = newHTMLTemplate("external link", `<a href="{{ .URL }}">{{ .Text }}</a>`)
}

func renderExternalLink(ctx *renderer.Context, l *types.ExternalLink) ([]byte, error) {
result := bytes.NewBuffer(nil)
err := externalLinkTmpl.Execute(result, l)
if err != nil {
return nil, errors.Wrapf(err, "unable to render external link")
}
log.Debugf("rendered external link: %s", result.Bytes())
return result.Bytes(), nil
}
17 changes: 17 additions & 0 deletions renderer/html5/external_link_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package html5_test

import . "github.com/onsi/ginkgo"

var _ = Describe("Links", func() {
Context("External links", func() {

It("External link alone", func() {

actualContent := "https://foo.com[the website]"
expected := `<div class="paragraph">
<p><a href="https://foo.com">the website</a></p>
</div>`
verify(GinkgoT(), expected, actualContent)
})
})
})
20 changes: 11 additions & 9 deletions renderer/html5/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,27 @@ func renderElement(ctx *renderer.Context, element types.DocElement) ([]byte, err
case *types.Passthrough:
return renderPassthrough(ctx, e)
case *types.BlockImage:
return renderBlockImage(ctx, element.(*types.BlockImage))
return renderBlockImage(ctx, e)
case *types.InlineImage:
return renderInlineImage(ctx, element.(*types.InlineImage))
return renderInlineImage(ctx, e)
case *types.DelimitedBlock:
return renderDelimitedBlock(ctx, element.(*types.DelimitedBlock))
return renderDelimitedBlock(ctx, e)
case *types.LiteralBlock:
return renderLiteralBlock(ctx, element.(*types.LiteralBlock))
return renderLiteralBlock(ctx, e)
case *types.InlineContent:
return renderInlineContent(ctx, element.(*types.InlineContent))
return renderInlineContent(ctx, e)
case *types.ExternalLink:
return renderExternalLink(ctx, e)
case *types.StringElement:
return renderStringElement(ctx, element.(*types.StringElement))
return renderStringElement(ctx, e)
case *types.DocumentAttributeDeclaration:
// 'process' function do not return any rendered content, but may return an error
return nil, processAttributeDeclaration(ctx, element.(*types.DocumentAttributeDeclaration))
return nil, processAttributeDeclaration(ctx, e)
case *types.DocumentAttributeReset:
// 'process' function do not return any rendered content, but may return an error
return nil, processAttributeReset(ctx, element.(*types.DocumentAttributeReset))
return nil, processAttributeReset(ctx, e)
case *types.DocumentAttributeSubstitution:
return renderAttributeSubstitution(ctx, element.(*types.DocumentAttributeSubstitution))
return renderAttributeSubstitution(ctx, e)
default:
return nil, errors.Errorf("unsupported type of element: %T", element)
}
Expand Down

0 comments on commit 1154a87

Please sign in to comment.