-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Preference API allows renderers to target specific cell/mime- types * Extension API + convenient adapters for popular packages: * markdown: goldmark and blackfriday * stream outputs: ansihtml * internal/test -> pkg/test: handy test utils now available for usage in external packages * NoWrapper render option * decode cell attachments for v4 notebooks.
- Loading branch information
Showing
17 changed files
with
592 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package adapter_test | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/bevzzz/nb/extension/adapter" | ||
"github.com/bevzzz/nb/pkg/test" | ||
"github.com/bevzzz/nb/render" | ||
"github.com/bevzzz/nb/schema" | ||
) | ||
|
||
func TestAdapter(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
render render.RenderCellFunc | ||
cell schema.Cell | ||
want string | ||
}{ | ||
{ | ||
name: "Goldmark", | ||
render: adapter.Goldmark(func(b []byte, w io.Writer) error { | ||
w.Write(b) | ||
return nil | ||
}), | ||
cell: test.Markdown("Hi, mom!"), | ||
want: "Hi, mom!", | ||
}, | ||
{ | ||
name: "Blackfriday", | ||
render: adapter.Blackfriday(func(b []byte) []byte { return b }), | ||
cell: test.Markdown("Hi, mom!"), | ||
want: "Hi, mom!", | ||
}, | ||
{ | ||
name: "AnsiHtml", | ||
render: adapter.AnsiHtml(func(b []byte) []byte { return b }), | ||
cell: test.Stdout("Hi, mom!"), | ||
want: "Hi, mom!", | ||
}, | ||
{ | ||
name: "AnsiHtml", | ||
render: adapter.AnsiHtml(func(b []byte) []byte { return b }), | ||
cell: test.Stderr("Hi, mom!"), | ||
want: "Hi, mom!", | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// Arrange | ||
var sb strings.Builder | ||
|
||
// Act | ||
tt.render(&sb, tt.cell) | ||
|
||
// Assert | ||
if got := sb.String(); got != tt.want { | ||
t.Errorf("wrong content: want %q, got %q", tt.want, got) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package adapter | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/bevzzz/nb/render" | ||
"github.com/bevzzz/nb/schema" | ||
) | ||
|
||
// AnsiHtml wraps [ansihtml]-style function in RenderCellFunc. | ||
// | ||
// Usage: | ||
// | ||
// extension.NewStream( | ||
// adapter.AnsiHtml(ansihtml.ConvertToHTML) | ||
// ) | ||
// | ||
// To force ansihtml to use classes instead of inline styles, pass an anonymous function intead: | ||
// | ||
// extension.NewStream( | ||
// adapter.AnsiHtml(func([]byte) []byte) { | ||
// ansihtml.ConvertToHTMLWithClasses(b, "class-", false) | ||
// }) | ||
// ) | ||
// | ||
// [ansihtml]: https://github.com/robert-nix/ansihtml | ||
func AnsiHtml(convert func([]byte) []byte) render.RenderCellFunc { | ||
return func(w io.Writer, cell schema.Cell) (err error) { | ||
_, err = w.Write(convert(cell.Text())) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Package adapter provides convenient adapters for other popular packages | ||
// making it simple to use those as nb extensions. | ||
// | ||
// - Markdown: [goldmark] and [blackfriday] | ||
// - ANSI to HTML conversion: [ansihtml] | ||
// | ||
// [goldmark]: https://github.com/yuin/goldmark | ||
// [blackfriday]: https://github.com/russross/blackfriday | ||
// [ansihtml]: https://github.com/robert-nix/ansihtml | ||
package adapter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package adapter | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/bevzzz/nb/render" | ||
"github.com/bevzzz/nb/schema" | ||
) | ||
|
||
// Blackfriday wraps [blackfriday]-style function in RenderCellFunc. | ||
// | ||
// Usage: | ||
// | ||
// extension.NewMarkdown( | ||
// adapter.Blackfriday(blackfriday.MarkdownCommon) | ||
// ) | ||
// | ||
// [blackfriday]: https://github.com/russross/blackfriday | ||
func Blackfriday(convert func([]byte) []byte) render.RenderCellFunc { | ||
return func(w io.Writer, cell schema.Cell) (err error) { | ||
_, err = w.Write(convert(cell.Text())) | ||
return | ||
} | ||
} | ||
|
||
// Goldmark wraps [goldmark]-style function in RenderCellFunc. | ||
// | ||
// Usage: | ||
// | ||
// extension.NewMarkdown( | ||
// adapter.Goldmark(func(b []byte, w io.Writer) error { | ||
// return goldmark.Convert(b, w, parseOptions...) | ||
// }) | ||
// ) | ||
// | ||
// Notice, how Goldmark is a bit more verbose compared to Blackfriday: | ||
// this is because goldmark.Convert accepts variadic parser.ParseOptions, which | ||
// is a dependency the client should capture in the closure and pass manually. | ||
// | ||
// [goldmark]: https://github.com/yuin/goldmark | ||
func Goldmark(write func([]byte, io.Writer) error) render.RenderCellFunc { | ||
return func(w io.Writer, cell schema.Cell) error { | ||
return write(cell.Text(), w) | ||
} | ||
} |
Oops, something went wrong.