@@ -78,9 +78,10 @@ public Optional<String> parseLanguage() throws IOException {
78
78
79
79
public GeneratedIPythonFile parseNotebook () throws IOException {
80
80
String content = inputFile .wrappedFile ().contents ();
81
+ boolean isCompressed = content .lines ().count () <= 1 ;
81
82
JsonFactory factory = new JsonFactory ();
82
83
try (JsonParser jParser = factory .createParser (content )) {
83
- return parseCells (jParser ).map (notebookData -> {
84
+ return parseCells (jParser , isCompressed ).map (notebookData -> {
84
85
// Account for EOF token
85
86
JsonLocation location = jParser .currentTokenLocation ();
86
87
notebookData .addDefaultLocation (lastPythonLine , location .getLineNr (), location .getColumnNr ());
@@ -90,27 +91,27 @@ public GeneratedIPythonFile parseNotebook() throws IOException {
90
91
91
92
}
92
93
93
- private Optional <NotebookParsingData > parseCells (JsonParser parser ) throws IOException {
94
+ private Optional <NotebookParsingData > parseCells (JsonParser parser , boolean isCompressed ) throws IOException {
94
95
while (!parser .isClosed ()) {
95
96
parser .nextToken ();
96
97
String fieldName = parser .currentName ();
97
98
if ("cells" .equals (fieldName )) {
98
99
// consume array start token
99
100
parser .nextToken ();
100
- Optional <NotebookParsingData > data = parseCellArray (parser );
101
+ Optional <NotebookParsingData > data = parseCellArray (parser , isCompressed );
101
102
parser .close ();
102
103
return data ;
103
104
}
104
105
}
105
106
return Optional .empty ();
106
107
}
107
108
108
- private Optional <NotebookParsingData > parseCellArray (JsonParser jParser ) throws IOException {
109
+ private Optional <NotebookParsingData > parseCellArray (JsonParser jParser , boolean isCompressed ) throws IOException {
109
110
List <NotebookParsingData > cellsData = new ArrayList <>();
110
111
111
112
while (jParser .nextToken () != JsonToken .END_ARRAY ) {
112
113
if (jParser .currentToken () == JsonToken .START_OBJECT ) {
113
- processCodeCell (cellsData , jParser );
114
+ processCodeCell (cellsData , jParser , isCompressed );
114
115
}
115
116
}
116
117
Optional <NotebookParsingData > aggregatedNotebookData = cellsData .stream ().reduce (NotebookParsingData ::combine );
@@ -134,7 +135,7 @@ private static boolean processCodeCellType(JsonParser jParser) throws IOExceptio
134
135
return false ;
135
136
}
136
137
137
- private void processCodeCell (List <NotebookParsingData > accumulator , JsonParser jParser ) throws IOException {
138
+ private void processCodeCell (List <NotebookParsingData > accumulator , JsonParser jParser , boolean isCompressed ) throws IOException {
138
139
boolean isCodeCell = false ;
139
140
Optional <NotebookParsingData > notebookData = Optional .empty ();
140
141
while (jParser .nextToken () != JsonToken .END_OBJECT ) {
@@ -154,7 +155,7 @@ private void processCodeCell(List<NotebookParsingData> accumulator, JsonParser j
154
155
}
155
156
switch (jParser .currentToken ()) {
156
157
case START_ARRAY :
157
- notebookData = Optional .of (parseSourceArray (startLine , jParser ));
158
+ notebookData = Optional .of (parseSourceArray (startLine , jParser , isCompressed ));
158
159
break ;
159
160
case VALUE_STRING :
160
161
notebookData = Optional .of (parseSourceMultilineString (startLine , jParser ));
@@ -172,7 +173,7 @@ private void processCodeCell(List<NotebookParsingData> accumulator, JsonParser j
172
173
}
173
174
}
174
175
175
- private static NotebookParsingData parseSourceArray (int startLine , JsonParser jParser ) throws IOException {
176
+ private static NotebookParsingData parseSourceArray (int startLine , JsonParser jParser , boolean isCompressed ) throws IOException {
176
177
NotebookParsingData cellData = NotebookParsingData .fromLine (startLine );
177
178
JsonLocation tokenLocation = jParser .currentTokenLocation ();
178
179
// In case of an empty cell, we don't add an extra line
@@ -181,7 +182,7 @@ private static NotebookParsingData parseSourceArray(int startLine, JsonParser jP
181
182
String sourceLine = jParser .getValueAsString ();
182
183
var newTokenLocation = jParser .currentTokenLocation ();
183
184
var countEscapedChar = countEscapeCharacters (sourceLine , newTokenLocation .getColumnNr ());
184
- cellData .addLineToSource (sourceLine , newTokenLocation .getLineNr (), newTokenLocation .getColumnNr (), countEscapedChar );
185
+ cellData .addLineToSource (sourceLine , newTokenLocation .getLineNr (), newTokenLocation .getColumnNr (), countEscapedChar , isCompressed );
185
186
lastSourceLine = sourceLine ;
186
187
tokenLocation = newTokenLocation ;
187
188
}
0 commit comments