Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(renderer): render external links #48

Merged
merged 1 commit into from
Jan 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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