Exemplo n.º 1
0
 def create_node(self, output: dict, document, env):
     """Create the output node, give the cell output."""
     # the whole output chunk is deposited and rendered later
     # TODO move these nodes to separate module, to avoid cyclic imports
     output_node = CellOutputBundleNode([output], "default")
     out_node = CellOutputNode(classes=["cell_output"])
     out_node.source, out_node.line = self.source, self.line
     out_node += output_node
     return out_node
Exemplo n.º 2
0
    def render_nb_code_cell(self, token: Token):
        """Render a Jupyter notebook cell."""
        cell = token.meta["cell"]  # type: nbf.NotebookNode

        # TODO logic involving tags should be deferred to a transform
        tags = cell.metadata.get("tags", [])

        # Cell container will wrap whatever is in the cell
        classes = ["cell"]
        for tag in tags:
            classes.append(f"tag_{tag}")
        sphinx_cell = CellNode(classes=classes, cell_type=cell["cell_type"])
        self.current_node += sphinx_cell
        if ("remove_input" not in tags) and ("remove-input" not in tags):
            cell_input = CellInputNode(classes=["cell_input"])
            self.add_line_and_source_path(cell_input, token)
            sphinx_cell += cell_input

            # Input block
            code_block = nodes.literal_block(text=cell["source"])
            if token.meta.get("lexer", None) is not None:
                code_block["language"] = token.meta["lexer"]
            cell_input += code_block

        # ==================
        # Cell output
        # ==================
        if (
            ("remove_output" not in tags)
            and ("remove-output" not in tags)
            and cell["outputs"]
        ):
            cell_output = CellOutputNode(classes=["cell_output"])
            sphinx_cell += cell_output

            outputs = CellOutputBundleNode(
                cell["outputs"], token.meta["renderer"], cell.metadata
            )
            self.add_line_and_source_path(outputs, token)
            cell_output += outputs