-
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.
feat(ext): add adapter for ansihtml.ConvertToHTML
Collected all adapters in a single extension/adapter package, because there is a lot of shared logic and structure and no naming clashes.
- Loading branch information
Showing
8 changed files
with
209 additions
and
63 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
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/internal/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
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
This file was deleted.
Oops, something went wrong.
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,44 @@ | ||
package extension | ||
|
||
import ( | ||
"github.com/bevzzz/nb" | ||
"github.com/bevzzz/nb/render" | ||
"github.com/bevzzz/nb/schema" | ||
"github.com/bevzzz/nb/schema/common" | ||
) | ||
|
||
// NewStream overrides the default rendering function for "stream" and "error" output cells. | ||
// These will often be formatted with ANSI-color codes, which you may want to replace with | ||
// styled HTML tags or strip from the output completely. | ||
// | ||
// For example, use [ansihtml] with a dedicated adapter: | ||
// | ||
// extension.NewStream( | ||
// adapter.AnsiHtml(ansihtml.ConvertToHTML) | ||
// ) | ||
// | ||
// [ansihtml]: https://github.com/robert-nix/ansihtml | ||
func NewStream(f render.RenderCellFunc) nb.Extension { | ||
return &stream{ | ||
render: f, | ||
} | ||
} | ||
|
||
type stream struct { | ||
render render.RenderCellFunc | ||
} | ||
|
||
var _ nb.Extension = (*stream)(nil) | ||
var _ render.CellRenderer = (*stream)(nil) | ||
|
||
// RegisterFuncs registers a new RenderCellFunc for stream output cells. | ||
func (s *stream) RegisterFuncs(reg render.RenderCellFuncRegistry) { | ||
reg.Register(render.Pref{Type: schema.Stream, MimeType: common.Stdout}, s.render) | ||
reg.Register(render.Pref{Type: schema.Stream, MimeType: common.Stderr}, s.render) | ||
reg.Register(render.Pref{Type: schema.Error, MimeType: common.Stderr}, s.render) | ||
} | ||
|
||
// Extend adds stream as a cell renderer. | ||
func (s *stream) Extend(n *nb.Notebook) { | ||
n.Renderer().AddOptions(render.WithCellRenderers(s)) | ||
} |