예제 #1
0
파일: _notebook.py 프로젝트: podoc/podoc
 def write(self, ast, context=None):
     self.execution_count = 1
     self._md = MarkdownPlugin()
     # Add code cells in the AST.
     ccw = CodeCellWrapper()
     ast = ccw.wrap(ast)
     # Find the directory containing the notebook file.
     doc_path = (context or {}).get('path', None)
     if doc_path:
         self._dir_path = op.dirname(op.realpath(doc_path))
     else:
         logger.warn("No input path, unable to resolve the image relative paths.")
         self._dir_path = None
     # Create the notebook.
     # new_output, new_code_cell, new_markdown_cell
     # TODO: kernelspect
     nb = new_notebook()
     # Go through all top-level blocks.
     for index, node in enumerate(ast.children):
         # Determine the block type.
         if node.name == 'CodeCell':
             node_type = 'code'
         else:
             node_type = 'markdown'
         # Create the notebook cell.
         cell = getattr(self, 'new_{}_cell'.format(node_type))(node, index)
         # Add it to the notebook.
         nb.cells.append(cell)
     nbformat.validate(nb)
     return nb
예제 #2
0
def test_notebook_reader_hello():
    # Open a test notebook with just 1 Markdown cell.
    path = get_test_file_path('notebook', 'hello.ipynb')
    notebook = open_notebook(path)
    # Convert it to an AST.
    ast = NotebookReader().read(notebook)
    ast.show()
    # Check that the AST is equal to the one of a simple Mardown line.
    ast_1 = MarkdownPlugin().read('hello *world*')
    assert ast == ast_1
예제 #3
0
파일: _notebook.py 프로젝트: podoc/podoc
 def read_markdown(self, cell, cell_index=None):
     if self._markdown_tree:
         cell_tree = self._markdown_tree.pop(0)
         self.tree.children.extend(cell_tree.children)
     else:
         logger.warn("Isolated read_markdown() call: slow because of pandoc call overhead.")
         ast = MarkdownPlugin().read(cell.source)
         if not ast.children:
             logger.debug("Skipping empty node.")
             return
         self.tree.children.append(ast)  # pragma: no cover
예제 #4
0
파일: _notebook.py 프로젝트: podoc/podoc
 def _read_all_markdown(self, cells):
     sources = [cell.source for cell in cells if cell.cell_type == 'markdown']
     contents = ('\n\n%s\n\n' % self._NEW_CELL_DELIMITER).join(sources)
     ast = MarkdownPlugin().read(contents)
     if not ast.children:
         logger.debug("Skipping empty node.")
         return
     curtree = ASTNode('root')
     for child in ast.children:
         curtree.children.append(child)
         # Create a new tree at every cell delimiter.
         if child.children and child.children[0] == self._NEW_CELL_DELIMITER:
             # Remove the delimiter node.
             curtree.children.pop()
             # Append the current cell tree and create the next one.
             self._markdown_tree.append(curtree)
             curtree = ASTNode('root')
     # Append the last cell tree if not empty.
     if curtree.children:
         self._markdown_tree.append(curtree)
예제 #5
0
def test_notebook_reader_notebook():
    # Open a test notebook with a code cell.
    path = get_test_file_path('notebook', 'simplenb.ipynb')
    notebook = open_notebook(path)
    # Convert it to an AST.
    reader = NotebookReader()
    ast = reader.read(notebook)
    ast.show()

    # Compare with the markdown version.
    path = get_test_file_path('markdown', 'simplenb.md')
    markdown_expected = load_text(path)
    markdown_converted = MarkdownPlugin().write(ast)
    markdown_converted = re.sub(r'\{resource:([^\}]+)\}', r'simplenb_files/\1',
                                markdown_converted)
    # The test file has a trailing new line, but not the AST.
    markdown_converted += '\n'
    # Replace the image filename because the conversion is done without output path.
    markdown_expected = markdown_expected.replace('simplenb_4_1.png',
                                                  'output_4_1.png')
    assert markdown_converted == markdown_expected

    assert 'output_4_1.png' in reader.resources