-
Notifications
You must be signed in to change notification settings - Fork 0
/
ansi.go
36 lines (33 loc) · 892 Bytes
/
ansi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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) {
// Wrapping in <pre> helps preserve parts of the original
// formatting such as newlines and tabs.
io.WriteString(w, "<pre>")
_, err = w.Write(convert(cell.Text()))
io.WriteString(w, "</pre>")
return
}
}