Beispiel #1
0
def test_bad_notebook_metadata():
    """Test exception raised if notebook metadata cannot be parsed."""
    with pytest.raises(MystMetadataParsingError):
        myst_to_notebook(
            dedent("""\
            ---
            {{a
            ---
            """))
Beispiel #2
0
def test_bad_markdown_metadata2():
    """Test exception raised if markdown metadata is not a dict."""
    with pytest.raises(MystMetadataParsingError):
        myst_to_notebook(
            dedent(
                """\
            +++ [1, 2]
            """
            )
        )
Beispiel #3
0
def test_bad_markdown_metadata():
    """Test exception raised if markdown metadata cannot be parsed."""
    with pytest.raises(MystMetadataParsingError):
        myst_to_notebook(
            dedent(
                """\
            +++ {{a
            """
            )
        )
Beispiel #4
0
def test_bad_code_metadata():
    """Test exception raised if cell metadata cannot be parsed."""
    with pytest.raises(MystMetadataParsingError):
        myst_to_notebook(
            dedent("""\
            ```{0}
            ---
            {{a
            ---
            ```
            """).format(CODE_DIRECTIVE))
Beispiel #5
0
def make_markdown(template_file_name,
                  tutorial_file_name=None,
                  solution_file_name=None):
    """ Master function to create requested notebooks, in markdown format."""
    # pylint: disable=too-many-branches
    from jupytext.myst import myst_to_notebook
    import nbformat

    if tutorial_file_name is None and solution_file_name is None:
        print("Nothing to do")
        return

    with open(template_file_name) as f:
        template = f.readlines()

    if tutorial_file_name is not None:
        in_solution = False
        tutorial_lines = []
        for i, line in enumerate(template):
            if line.startswith("#TUT_SOLUTION_START"):
                in_solution = True
            elif line.startswith("#TUT_SOLUTION_END"):
                in_solution = False
            elif line.startswith("#TUT_USER"):
                if in_solution:
                    raise Exception(
                        "Line {}: In solution, but found user end/start".
                        format(i))
            elif not in_solution:
                tutorial_lines.append(line)
        with open(tutorial_file_name, 'w') as f:
            f.writelines(tutorial_lines)

    if solution_file_name is not None:
        in_tutorial = False
        solution_lines = []
        for i, line in enumerate(template):
            if line.startswith("#TUT_USER_START"):
                in_tutorial = True
            elif line.startswith("#TUT_USER_END"):
                in_tutorial = False
            elif line.startswith("#TUT_SOLUTION"):
                if in_tutorial:
                    raise Exception(
                        "Line {}: In tutorial, but found solution end/start".
                        format(i))
            elif not in_tutorial:
                solution_lines.append(line)
        # for the solution, since we are not going to parse it in sphinx,
        # we just convert it straight to a notebook
        notebook = myst_to_notebook("".join(solution_lines))
        with open(solution_file_name, 'w') as f:
            nbformat.write(notebook, solution_file_name)
Beispiel #6
0
def test_add_source_map():
    notebook = myst_to_notebook(
        dedent("""\
            ---
            a: 1
            ---
            abc
            +++
            def
            ```{0}
            ---
            b: 2
            ---
            c = 3
            ```
            xyz
            """).format(CODE_DIRECTIVE),
        add_source_map=True,
    )
    assert notebook.metadata.source_map == [3, 5, 7, 12]
def remove_solution(input_myst_str):
    """Removes solution from myst str.

    This is based on solution having "solution" in their cell metadata tags
    """
    nb = myst_to_notebook(input_myst_str)

    cell_tags_list = [c['metadata'].get('tags') for c in nb.cells]
    is_solution_list = [
        tags is not None and 'solution' in tags for tags in cell_tags_list
    ]
    nb.cells = [
        cell for cell, is_solution in zip(nb.cells, is_solution_list)
        if not is_solution
    ]

    myst_nb_str = jupytext.writes(nb, fmt='myst')

    header_pattern = re.compile(r"---\njupytext.+---\s*",
                                re.DOTALL | re.MULTILINE)
    return re.sub(header_pattern, "", myst_nb_str)