From d31089234b7a9f0fbdf2b1776639ce5eb7d7e7e6 Mon Sep 17 00:00:00 2001 From: Samuel Meuli Date: Wed, 18 Mar 2020 18:29:30 +0700 Subject: [PATCH] Make structs private --- convert.go | 24 ++++++++++----------- convert_test.go | 6 +++--- parse.go | 38 ++++++++++++++++----------------- test_data.go | 56 ++++++++++++++++++++++++------------------------- 4 files changed, 62 insertions(+), 62 deletions(-) diff --git a/convert.go b/convert.go index 9f60698..df390ae 100644 --- a/convert.go +++ b/convert.go @@ -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 { @@ -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 "
An unknown error occurred
" @@ -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 "" @@ -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) @@ -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( `
%s
`, html.EscapeString(strings.Join(cell.Source, "")), @@ -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) @@ -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) @@ -216,8 +216,8 @@ func ConvertString(writer io.Writer, notebookString string) error { }) t, err = t.Parse(`
- {{ $fileExtension := .FileExtension }} - {{ range .Notebook.Cells }} + {{ $fileExtension := .fileExtension }} + {{ range .notebook.Cells }}
@@ -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) } diff --git a/convert_test.go b/convert_test.go index 9edc819..8624b74 100644 --- a/convert_test.go +++ b/convert_test.go @@ -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) } @@ -90,7 +90,7 @@ data`) 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) } @@ -103,7 +103,7 @@ With ANSI colors`) func TestConvertErrorOutputMissingKey(t *testing.T) { expected := template.HTML("
An unknown error occurred
") - actual := convertErrorOutput(Output{OutputType: "error"}) + actual := convertErrorOutput(output{OutputType: "error"}) assert.Equal(t, expected, actual) } diff --git a/parse.go b/parse.go index af23757..284dba0 100644 --- a/parse.go +++ b/parse.go @@ -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"` @@ -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), ¬ebookParsed) return notebookParsed, err } diff --git a/test_data.go b/test_data.go index 1f7b4b4..375a33f 100644 --- a/test_data.go +++ b/test_data.go @@ -154,7 +154,7 @@ const testNotebookString = `{ ] }` -var testMarkdownCell = Cell{ +var testMarkdownCell = cell{ CellType: "markdown", Source: []string{ "# Hello World\n", @@ -163,9 +163,9 @@ var testMarkdownCell = Cell{ }, } -var testDisplayDataHTMLOutput = Output{ +var testDisplayDataHTMLOutput = output{ OutputType: "display_data", - Data: OutputData{ + Data: outputData{ TextHTML: []string{ "
\n", "

Hello world

\n", @@ -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{ "\n", "\n", @@ -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", @@ -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", @@ -239,7 +239,7 @@ var testDisplayDataPlainTextOutput = Output{ }, } -var testErrorOutput = Output{ +var testErrorOutput = output{ OutputType: "error", Traceback: []string{ "Error message", @@ -247,7 +247,7 @@ var testErrorOutput = Output{ }, } -var testStreamOutput = Output{ +var testStreamOutput = output{ OutputType: "stream", Text: []string{ "multiline\n", @@ -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", @@ -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, @@ -292,7 +292,7 @@ var testCodeCell = Cell{ }, } -var testRawCell = Cell{ +var testRawCell = cell{ CellType: "raw", Source: []string{ "This is a raw section, without formatting.\n", @@ -300,14 +300,14 @@ var testRawCell = Cell{ }, } -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,