org.testingisdocumenting.znai.jupyter.JupyterParserVer4 Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.testingisdocumenting.znai.jupyter;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static org.testingisdocumenting.znai.jupyter.JupyterCell.CODE_TYPE;
import static org.testingisdocumenting.znai.jupyter.JupyterCell.MARKDOWN_TYPE;
import static java.util.stream.Collectors.toList;
public class JupyterParserVer4 implements JupyterParser {
public JupyterParserVer4() {
}
@Override
public JupyterNotebook parse(Map json) {
List cells = (List) json.get("cells");
return new JupyterNotebook(parseLang(json),
cells.stream().map(this::parseCell).collect(toList()));
}
@SuppressWarnings("unchecked")
private JupyterCell parseCell(Object o) {
Map cellContent = (Map) o;
String type = cellContent.get("cell_type").toString();
switch (type) {
case CODE_TYPE:
return parseCodeCell(cellContent);
case MARKDOWN_TYPE:
return parseMarkdownCell(cellContent);
}
return new JupyterCell("unknown", "", Collections.emptyList());
}
private JupyterCell parseMarkdownCell(Map cellContent) {
return new JupyterCell(MARKDOWN_TYPE, joinLines(cellContent.get("source")), Collections.emptyList());
}
@SuppressWarnings("unchecked")
private JupyterCell parseCodeCell(Map cellContent) {
String input = joinLines(cellContent.get("source"));
List