Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
Make structs private
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmeuli committed Mar 18, 2020
1 parent 6440d7b commit d310892
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 62 deletions.
24 changes: 12 additions & 12 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func renderMarkdown(markdownLines []string) string {
// Output renderers

// convertDataOutput converts data output (e.g. a base64-encoded plot image) to HTML.
func convertDataOutput(output Output) template.HTML {
func convertDataOutput(output output) template.HTML {
htmlString := ""

switch {
Expand Down Expand Up @@ -86,7 +86,7 @@ func convertDataOutput(output Output) template.HTML {
}

// convertErrorOutput converts error output (e.g. generated by a Python exception) to HTML.
func convertErrorOutput(output Output) template.HTML {
func convertErrorOutput(output output) template.HTML {
if output.Traceback == nil {
fmt.Printf("missing `traceback` key in output of type `error`\n")
return "<pre>An unknown error occurred</pre>"
Expand All @@ -103,7 +103,7 @@ func convertErrorOutput(output Output) template.HTML {
}

// convertStreamOutput converts stream output (e.g. stdout written by a Python program) to HTML.
func convertStreamOutput(output Output) template.HTML {
func convertStreamOutput(output output) template.HTML {
if output.Text == nil {
fmt.Printf("missing `text` key in output of type `stream`\n")
return ""
Expand All @@ -116,12 +116,12 @@ func convertStreamOutput(output Output) template.HTML {
// Cell renderers

// convertMarkdownCell converts a Markdown cell to HTML.
func convertMarkdownCell(cell Cell) template.HTML {
func convertMarkdownCell(cell cell) template.HTML {
return template.HTML(renderMarkdown(cell.Source))
}

// convertCodeCell converts a code cell to HTML with classes for syntax highlighting.
func convertCodeCell(cell Cell, fileExtension string) template.HTML {
func convertCodeCell(cell cell, fileExtension string) template.HTML {
codeString := strings.Join(cell.Source, "")
codeBuffer := new(bytes.Buffer)
err := highlightCode(codeBuffer, codeString, fileExtension)
Expand All @@ -133,7 +133,7 @@ func convertCodeCell(cell Cell, fileExtension string) template.HTML {
}

// convertRawCell returns a simple HTML element for the raw notebook cell.
func convertRawCell(cell Cell) template.HTML {
func convertRawCell(cell cell) template.HTML {
htmlString := fmt.Sprintf(
`<pre>%s</pre>`,
html.EscapeString(strings.Join(cell.Source, "")),
Expand All @@ -152,7 +152,7 @@ func convertPrompt(executionCount *int) template.HTML {
}

// convertOutput converts the provided cell input to HTML.
func convertInput(fileExtension string, cell Cell) template.HTML {
func convertInput(fileExtension string, cell cell) template.HTML {
switch cell.CellType {
case "markdown":
return convertMarkdownCell(cell)
Expand All @@ -167,7 +167,7 @@ func convertInput(fileExtension string, cell Cell) template.HTML {
}

// convertOutput converts the provided output of a cell execution to HTML.
func convertOutput(output Output) template.HTML {
func convertOutput(output output) template.HTML {
switch output.OutputType {
case "display_data":
return convertDataOutput(output)
Expand Down Expand Up @@ -216,8 +216,8 @@ func ConvertString(writer io.Writer, notebookString string) error {
})
t, err = t.Parse(`
<div class="notebook">
{{ $fileExtension := .FileExtension }}
{{ range .Notebook.Cells }}
{{ $fileExtension := .fileExtension }}
{{ range .notebook.Cells }}
<div class="cell cell-{{ .CellType }}">
<div class="input-wrapper">
<div class="input-prompt">
Expand Down Expand Up @@ -246,8 +246,8 @@ func ConvertString(writer io.Writer, notebookString string) error {
}

templateVars := map[string]interface{}{
"FileExtension": fileExtension,
"Notebook": notebook,
"fileExtension": fileExtension,
"notebook": notebook,
}
return t.Execute(writer, templateVars)
}
6 changes: 3 additions & 3 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ text

func TestConvertStreamOutputMissingKey(t *testing.T) {
expected := template.HTML("")
actual := convertStreamOutput(Output{OutputType: "stream"})
actual := convertStreamOutput(output{OutputType: "stream"})
assert.Equal(t, expected, actual)
}

Expand Down Expand Up @@ -90,7 +90,7 @@ data</pre>`)

func TestConvertDataOutputMissingKey(t *testing.T) {
expected := template.HTML("")
actual := convertDataOutput(Output{OutputType: "display_data"})
actual := convertDataOutput(output{OutputType: "display_data"})
assert.Equal(t, expected, actual)
}

Expand All @@ -103,7 +103,7 @@ With <span class="term-fg31">ANSI colors</span></pre>`)

func TestConvertErrorOutputMissingKey(t *testing.T) {
expected := template.HTML("<pre>An unknown error occurred</pre>")
actual := convertErrorOutput(Output{OutputType: "error"})
actual := convertErrorOutput(output{OutputType: "error"})
assert.Equal(t, expected, actual)
}

Expand Down
38 changes: 19 additions & 19 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
// Documentation of the Jupyter Notebook JSON format: https://ipython.org/ipython-doc/3/notebook/nbformat.html
// (VCS: https://github.com/ipython/ipython-doc/blob/e9c83570cf3dea6d7a6b178ee59869b4f441220f/3/notebook/nbformat.html)

// OutputData can contain the cell output in various data types.
// outputData can contain the cell output in various data types.
// Source: https://github.com/jupyter/nbconvert/blob/c837a22d44d98f6a58d1934bd85af1506df48f21/nbconvert/utils/base.py#L16
type OutputData struct {
type outputData struct {
TextHTML []string `json:"text/html,omitempty"`
ApplicationPDF *string `json:"application/pdf,omitempty"`
TextLaTeX *string `json:"text/latex,omitempty"`
Expand All @@ -20,49 +20,49 @@ type OutputData struct {
TextPlain []string `json:"text/plain,omitempty"`
}

// Output is the result of a code cell's execution in a Jupyter Notebook.
type Output struct {
// output is the result of a code cell's execution in a Jupyter Notebook.
type output struct {
OutputType string `json:"output_type"`
ExecutionCount *int `json:"execution_count,omitempty"`
Text []string `json:"text,omitempty"`
Data OutputData `json:"data,omitempty"`
Data outputData `json:"data,omitempty"`
Traceback []string `json:"traceback,omitempty"`
// Omitted fields: "ename", "evalue", "name"
}

// Cell is a single Jupyter Notebook cell.
type Cell struct {
// cell is a single Jupyter Notebook cell.
type cell struct {
CellType string `json:"cell_type"`
ExecutionCount *int `json:"execution_count,omitempty"`
Source []string `json:"source"`
Outputs []Output `json:"outputs,omitempty"`
Outputs []output `json:"outputs,omitempty"`
// Omitted fields: "metadata"
}

// LanguageInfo provides details about the programming language of the Jupyter Notebook kernel.
type LanguageInfo struct {
// languageInfo provides details about the programming language of the Jupyter Notebook kernel.
type languageInfo struct {
FileExtension string `json:"file_extension"`
// Omitted fields: codemirror_mode", "mimetype", "name", "nbconvert_exporter", "pygments_lexer",
// "version"
}

// Metadata contains additional information about the Jupyter Notebook.
type Metadata struct {
LanguageInfo LanguageInfo `json:"language_info"`
// metadata contains additional information about the Jupyter Notebook.
type metadata struct {
LanguageInfo languageInfo `json:"language_info"`
// Omitted fields: "kernelspec"
}

// Notebook represents the JSON data structure in which a Jupyter Notebook is stored.
type Notebook struct {
Cells []Cell `json:"cells"`
Metadata Metadata `json:"metadata"`
// notebook represents the JSON data structure in which a Jupyter Notebook is stored.
type notebook struct {
Cells []cell `json:"cells"`
Metadata metadata `json:"metadata"`
// Omitted fields: "nbformat", "nbformat_minor"
}

// parseNotebook takes the provided Jupyter Notebook JSON string and parses it into the
// corresponding structs.
func parseNotebook(notebookString string) (Notebook, error) {
var notebookParsed Notebook
func parseNotebook(notebookString string) (notebook, error) {
var notebookParsed notebook
err := json.Unmarshal([]byte(notebookString), &notebookParsed)
return notebookParsed, err
}
56 changes: 28 additions & 28 deletions test_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ const testNotebookString = `{
]
}`

var testMarkdownCell = Cell{
var testMarkdownCell = cell{
CellType: "markdown",
Source: []string{
"# Hello World\n",
Expand All @@ -163,9 +163,9 @@ var testMarkdownCell = Cell{
},
}

var testDisplayDataHTMLOutput = Output{
var testDisplayDataHTMLOutput = output{
OutputType: "display_data",
Data: OutputData{
Data: outputData{
TextHTML: []string{
"<div>\n",
"<p>Hello world</p>\n",
Expand All @@ -175,24 +175,24 @@ var testDisplayDataHTMLOutput = Output{
}

var testPDFString = "base64-encoded-pdf-data"
var testDisplayDataPDFOutput = Output{
var testDisplayDataPDFOutput = output{
OutputType: "display_data",
Data: OutputData{
Data: outputData{
ApplicationPDF: &testPDFString,
},
}

var testLaTeXString = "latex-data"
var testDisplayDataLaTeXOutput = Output{
var testDisplayDataLaTeXOutput = output{
OutputType: "display_data",
Data: OutputData{
Data: outputData{
TextLaTeX: &testLaTeXString,
},
}

var testDisplayDataSVGOutput = Output{
var testDisplayDataSVGOutput = output{
OutputType: "display_data",
Data: OutputData{
Data: outputData{
ImageSVGXML: []string{
"<svg id=\"star\" xmlns=\"http://www.w3.org/2000/svg\" width=\"255\" height=\"240\" viewBox=\"0 0 51 48\">\n",
"<path d=\"M25 1l6 17h18L35 29l5 17-15-10-15 10 5-17L1 18h18z\"/>\n",
Expand All @@ -202,24 +202,24 @@ var testDisplayDataSVGOutput = Output{
}

var testPNGString = "base64-encoded-png-data"
var testDisplayDataPNGOutput = Output{
var testDisplayDataPNGOutput = output{
OutputType: "display_data",
Data: OutputData{
Data: outputData{
ImagePNG: &testPNGString,
},
}

var testJPEGString = "base64-encoded-jpeg-data"
var testDisplayDataJPEGOutput = Output{
var testDisplayDataJPEGOutput = output{
OutputType: "display_data",
Data: OutputData{
Data: outputData{
ImageJPEG: &testJPEGString,
},
}

var testDisplayDataMarkdownOutput = Output{
var testDisplayDataMarkdownOutput = output{
OutputType: "display_data",
Data: OutputData{
Data: outputData{
TextMarkdown: []string{
"# Hello World\n",
"\n",
Expand All @@ -228,9 +228,9 @@ var testDisplayDataMarkdownOutput = Output{
},
}

var testDisplayDataPlainTextOutput = Output{
var testDisplayDataPlainTextOutput = output{
OutputType: "display_data",
Data: OutputData{
Data: outputData{
TextPlain: []string{
"multiline\n",
"text\n",
Expand All @@ -239,15 +239,15 @@ var testDisplayDataPlainTextOutput = Output{
},
}

var testErrorOutput = Output{
var testErrorOutput = output{
OutputType: "error",
Traceback: []string{
"Error message",
"With \u001b[0;31mANSI colors\u001b[0m",
},
}

var testStreamOutput = Output{
var testStreamOutput = output{
OutputType: "stream",
Text: []string{
"multiline\n",
Expand All @@ -257,10 +257,10 @@ var testStreamOutput = Output{
}

var testExecutionCount1 = 1
var testExecuteResultOutput = Output{
var testExecuteResultOutput = output{
OutputType: "execute_result",
ExecutionCount: &testExecutionCount1,
Data: OutputData{
Data: outputData{
TextPlain: []string{
"multiline\n",
"text\n",
Expand All @@ -270,14 +270,14 @@ var testExecuteResultOutput = Output{
}

var testExecutionCount2 = 2
var testCodeCell = Cell{
var testCodeCell = cell{
CellType: "code",
ExecutionCount: &testExecutionCount2,
Source: []string{
"print(\"Hello\")\n",
"print(\"World\")",
},
Outputs: []Output{
Outputs: []output{
testDisplayDataHTMLOutput,
testDisplayDataPDFOutput,
testDisplayDataLaTeXOutput,
Expand All @@ -292,22 +292,22 @@ var testCodeCell = Cell{
},
}

var testRawCell = Cell{
var testRawCell = cell{
CellType: "raw",
Source: []string{
"This is a raw section, without formatting.\n",
"This is the second line.",
},
}

var testMetadata = Metadata{
LanguageInfo: LanguageInfo{
var testMetadata = metadata{
LanguageInfo: languageInfo{
FileExtension: ".py",
},
}

var testParsedNotebook = Notebook{
Cells: []Cell{
var testParsedNotebook = notebook{
Cells: []cell{
testMarkdownCell,
testCodeCell,
testRawCell,
Expand Down

0 comments on commit d310892

Please sign in to comment.