Skip to content

Commit

Permalink
Wrap notebook contents in a <div class="jp-Notebook"> (#14)
Browse files Browse the repository at this point in the history
* Added CSS classes for code cells in in wrapper.go
This removes the need for custom renderers to do that on their own

* fix error while printing diffs when comparing HTML structure in tests

* refactor: close HTML tags automatically
  • Loading branch information
bevzzz authored Mar 5, 2024
1 parent 7525745 commit 557d7eb
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 150 deletions.
1 change: 1 addition & 0 deletions pkg/test/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type fakeWrapper struct{}
var _ render.CellWrapper = (*fakeWrapper)(nil)

func (*fakeWrapper) RegisterFuncs(render.RenderCellFuncRegistry) {}
func (*fakeWrapper) WrapAll(io.Writer, func(io.Writer) error) error { return nil }
func (*fakeWrapper) Wrap(w io.Writer, c schema.Cell, r render.RenderCellFunc) error { return r(w, c) }
func (*fakeWrapper) WrapInput(w io.Writer, c schema.Cell, r render.RenderCellFunc) error {
return r(w, c)
Expand Down
10 changes: 2 additions & 8 deletions render/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type Config struct {

type Option func(*Config)

// WithCSSWriter
// WithCSSWriter registers a writer for CSS stylesheet.
func WithCSSWriter(w io.Writer) Option {
return func(c *Config) {
c.CSSWriter = &WriterOnce{w: w}
c.CSSWriter = w
}
}

Expand Down Expand Up @@ -77,18 +77,12 @@ func (r *Renderer) renderCode(w io.Writer, cell schema.Cell) error {
return nil
}

div.Open(w, attributes{"class": {"cm-editor", "cm-s-jupyter"}}, true)
div.Open(w, attributes{"class": {"highlight", "hl-ipython3"}}, true)

io.WriteString(w, "<pre><code class=\"language-") // TODO: not sure if that's useful here
io.WriteString(w, code.Language())
io.WriteString(w, "\">")
w.Write(code.Text())
io.WriteString(w, "</code></pre>")

div.Close(w)
div.Close(w)

return nil
}

Expand Down
46 changes: 12 additions & 34 deletions render/html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ func TestRenderer(t *testing.T) {
"src": {"data:image/jpeg;base64, base64-encoded-image"},
}},
},
{
name: "image/svg+xml",
cell: test.DisplayData("svg-image", "image/svg+xml"),
want: &node{tag: "img", attr: map[string][]string{
"src": {"data:image/svg+xml;base64, svg-image"},
}},
},
{
name: "code cell",
cell: &test.CodeCell{
Expand All @@ -76,29 +83,14 @@ func TestRenderer(t *testing.T) {
Lang: "python",
},
want: &node{
tag: "div",
attr: map[string][]string{
"class": {"cm-editor", "cm-s-jupyter"},
},
tag: "pre",
children: []*node{
{
tag: "div",
tag: "code",
attr: map[string][]string{
"class": {"highlight"},
},
children: []*node{
{
tag: "pre",
children: []*node{
{
tag: "code",
attr: map[string][]string{
"class": {"language-python"},
},
content: "print('Hi, mom!')",
},
}},
"class": {"language-python"},
},
content: "print('Hi, mom!')",
},
},
},
Expand All @@ -123,20 +115,6 @@ func TestRenderer(t *testing.T) {
}

func TestRenderer_CSSWriter(t *testing.T) {
t.Run("WithCSSWriter wraps in WriterOnce", func(t *testing.T) {
// Arrange
var cfg html.Config
opt := html.WithCSSWriter(io.Discard)

// Act
opt(&cfg)

// Assert
if _, ok := cfg.CSSWriter.(*html.WriterOnce); !ok {
t.Errorf("expected *html.WriterOnce, got %T", cfg.CSSWriter)
}
})

t.Run("captures correct css", func(t *testing.T) {
// Arrange
var css bytes.Buffer
Expand All @@ -149,7 +127,7 @@ func TestRenderer_CSSWriter(t *testing.T) {
}

// Act
err = r.Wrap(io.Discard, test.Markdown(""), noopRender)
err = r.WrapAll(io.Discard, func(w io.Writer) error { return nil })
require.NoError(t, err)

// Assert
Expand Down
Loading

0 comments on commit 557d7eb

Please sign in to comment.