Skip to content
Permalink
Browse files

Testing and godoc

  • Loading branch information...
Depado committed Oct 12, 2017
1 parent 690afbe commit 483fd41ae92f37cf9e908dee3f00cb274340b1ee
Showing with 99 additions and 1 deletion.
  1. +1 −0 README.md
  2. +10 −0 example/main.go
  3. +6 −1 renderer.go
  4. +82 −0 renderer_test.go
@@ -6,6 +6,7 @@
[![Build Status](https://drone.depado.eu/api/badges/Depado/bfchroma/status.svg)](https://drone.depado.eu/Depado/bfchroma)
[![codecov](https://codecov.io/gh/Depado/bfchroma/branch/master/graph/badge.svg)](https://codecov.io/gh/Depado/bfchroma)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Depado/bfchroma/blob/master/LICENSE)
[![Godoc](https://godoc.org/github.com/Depado/bfchroma?status.svg)](https://godoc.org/github.com/Depado/bfchroma)


Integrating [Chroma](https://github.com/alecthomas/chroma) syntax highlighter as a [Blackfriday](https://github.com/russross/blackfriday) renderer
@@ -35,4 +35,14 @@ func main() {
)
h = bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))

md := "```\npackage main\n\nfunc main() {\n}\n```"
r = bfchroma.NewRenderer(bfchroma.WithoutAutodetect())
h = bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))

md = "```go\npackage main\n\nfunc main() {\n}\n```"
r = bfchroma.NewRenderer(bfchroma.ChromaOptions(html.WithLineNumbers()))
h = bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
}
@@ -1,3 +1,5 @@
// Package bfchroma provides an easy and extensible blackfriday renderer that
// uses the chroma syntax highlighter to render code blocks.
package bfchroma

import (
@@ -73,6 +75,7 @@ func NewRenderer(options ...Option) *Renderer {
func (r *Renderer) RenderWithChroma(w io.Writer, text []byte, data bf.CodeBlockData) error {
var lexer chroma.Lexer

// Determining the lexer to use
if len(data.Info) > 0 {
lexer = lexers.Get(string(data.Info))
} else if r.Autodetect {
@@ -81,6 +84,8 @@ func (r *Renderer) RenderWithChroma(w io.Writer, text []byte, data bf.CodeBlockD
if lexer == nil {
lexer = lexers.Fallback
}

// Tokenize the code
iterator, err := lexer.Tokenise(nil, string(text))
if err != nil {
return err
@@ -95,7 +100,7 @@ type Renderer struct {
Autodetect bool
ChromaOptions []html.Option
Style *chroma.Style
Formatter chroma.Formatter
Formatter *html.Formatter
}

// RenderNode satisfies the Renderer interface
@@ -1,8 +1,11 @@
package bfchroma

import (
"fmt"
"testing"

"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/formatters/html"
"github.com/alecthomas/chroma/styles"

"github.com/stretchr/testify/assert"
@@ -19,6 +22,18 @@ func TestExtend(t *testing.T) {
assert.Equal(t, r.Base, b, "should be the same renderer")
}

func ExampleExtend() {
md := "```go\npackage main\n\nfunc main() {\n}\n```"

b := bf.NewHTMLRenderer(bf.HTMLRendererParameters{
Flags: bf.CommonHTMLFlags,
})
r := NewRenderer(Extend(b))

h := bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
}

func TestStyle(t *testing.T) {
var r *Renderer
for k, v := range styles.Registry {
@@ -31,6 +46,15 @@ func TestStyle(t *testing.T) {
}
}

func ExampleStyle() {
md := "```go\npackage main\n\nfunc main() {\n}\n```"

r := NewRenderer(Style("github"))

h := bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
}

func TestChromaStyle(t *testing.T) {
var r *Renderer
for _, v := range styles.Registry {
@@ -39,9 +63,67 @@ func TestChromaStyle(t *testing.T) {
}
}

func ExampleChromaStyle() {
md := "```go\npackage main\n\nfunc main() {\n}\n```"

r := NewRenderer(ChromaStyle(styles.GitHub))

h := bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
}

func TestWithoutAutodetect(t *testing.T) {
r := NewRenderer(WithoutAutodetect())
assert.False(t, r.Autodetect, "Should set Autodetect to false")
r = NewRenderer()
assert.True(t, r.Autodetect, "Not using option should leave Autodetect to true")
}

func ExampleWithoutAutodetect() {
md := "```\npackage main\n\nfunc main() {\n}\n```"

r := NewRenderer(WithoutAutodetect())

h := bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
}

func TestChromaOptions(t *testing.T) {
NewRenderer(ChromaOptions(html.WithClasses()))
}

func ExampleChromaOptions() {
md := "```go\npackage main\n\nfunc main() {\n}\n```"

r := NewRenderer(ChromaOptions(html.WithLineNumbers()))

h := bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
}

func TestRenderWithChroma(t *testing.T) {
md := "```go\npackage main\n\nfunc main() {\n}\n```"
r := NewRenderer()

h := bf.Run([]byte(md), bf.WithRenderer(r))
assert.Contains(t, string(h), r.Style.Get(chroma.NameFunction).Colour.String())
assert.Contains(t, string(h), r.Style.Get(chroma.Background).Background.String())
assert.Contains(t, string(h), "<pre")
}

func ExampleNewRenderer() {
// Complex example on how to initialize the renderer
md := "```go\npackage main\n\nfunc main() {\n}\n```"

r := NewRenderer(
Extend(bf.NewHTMLRenderer(bf.HTMLRendererParameters{
Flags: bf.CommonHTMLFlags,
})),
WithoutAutodetect(),
ChromaStyle(styles.GitHub),
ChromaOptions(html.WithLineNumbers()),
)

h := bf.Run([]byte(md), bf.WithRenderer(r))
fmt.Println(string(h))
}

0 comments on commit 483fd41

Please sign in to comment.
You can’t perform that action at this time.