-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.go
94 lines (78 loc) · 2.14 KB
/
convert.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package nb
import (
"io"
"github.com/bevzzz/nb/decode"
"github.com/bevzzz/nb/render"
"github.com/bevzzz/nb/render/html"
)
// Convert a Jupyter notebook using default converter.
func Convert(w io.Writer, source []byte) error {
return defaultNotebook.Convert(w, source)
}
var defaultNotebook = New()
// Converter converts raw Jupyter Notebook JSON to the selected format.
type Converter interface {
Convert(w io.Writer, source []byte) error
}
// WithExtensions adds extensions.
func WithExtensions(exts ...Extension) Option {
return func(n *Notebook) {
n.extensions = append(n.extensions, exts...)
}
}
// WithRenderer sets a new notebook renderer.
// Set this option before passing any WithRenderOptions.
func WithRenderer(r render.Renderer) Option {
return func(n *Notebook) {
n.renderer = r
}
}
// WithRendererOptions adds configuration to the current notebook renderer.
func WithRenderOptions(opts ...render.Option) Option {
return func(n *Notebook) {
n.renderer.AddOptions(opts...)
}
}
// Notebook is an extensible Converter implementation.
type Notebook struct {
renderer render.Renderer
extensions []Extension
}
var _ Converter = (*Notebook)(nil)
type Option func(*Notebook)
// New returns a converter with default HTML renderer and extensions.
func New(opts ...Option) *Notebook {
nb := Notebook{
renderer: DefaultRenderer(),
}
for _, opt := range opts {
opt(&nb)
}
for _, ext := range nb.extensions {
ext.Extend(&nb)
}
return &nb
}
// DefaultRenderer configures an HTML renderer.
func DefaultRenderer() render.Renderer {
return render.NewRenderer(
render.WithCellRenderers(html.NewRenderer()),
)
}
// Сonvert raw Jupyter Notebook JSON and write the output.
func (n *Notebook) Convert(w io.Writer, source []byte) error {
// nb, err := decode.Decode(source)
nb, err := decode.Bytes(source)
if err != nil {
return err
}
return n.renderer.Render(w, nb)
}
// Renderer exposes current renderer, allowing it to be further configured and/or extended.
func (n *Notebook) Renderer() render.Renderer {
return n.renderer
}
// Extension adds new capabilities to the base Notebook.
type Extension interface {
Extend(n *Notebook)
}