<commit_before>import os
import sys
import argparse
import pkg_resources

import IPython.nbformat as nbformat

from .notedown import (MarkdownReader,
                       MarkdownWriter,
                       Knitr)


__version__ = pkg_resources.require('notedown')[0].version

markdown_template \
    = pkg_resources.resource_filename('notedown',
                                      'templates/markdown.tpl')
markdown_figure_template \
    = pkg_resources.resource_filename('notedown',
                                      'templates/markdown_outputs.tpl')


def strip(notebook):
    """Remove outputs from a notebook."""
    for cell in notebook.cells:
        cell.outputs = []


def run(notebook):
    """Run a notebook using runipy."""
    try:
        from runipy.notebook_runner import NotebookRunner
    except ImportError:
        raise('You need runipy installed to run notebooks!'
              'try `pip install runipy`')

    runner = NotebookRunner(notebook)
    runner.run_notebook()


examples = """
Example usage of notedown
-------------------------

Convert markdown into notebook:

    notedown input.md > output.ipynb

    notedown input.md --output output.ipynb


Convert a notebook into markdown, with outputs intact:

    notedown input.ipynb --from notebook --to markdown > output_with_outputs.md


Convert a notebook into markdown, stripping all outputs:

    notedown input.ipynb --from notebook --to markdown --strip > output.md


Strip the output cells from markdown:

    notedown with_output_cells.md --to markdown --strip > no_output_cells.md


Convert from markdown and execute using runipy:<commit_msg>v4: execute with ipython ExecutePreprocessor<commit_after>import os
import sys
import argparse
import pkg_resources

import IPython.nbformat as nbformat
from IPython.nbconvert.preprocessors.execute import ExecutePreprocessor

from .notedown import (MarkdownReader,
                       MarkdownWriter,
                       Knitr)


__version__ = pkg_resources.require('notedown')[0].version

markdown_template \
    = pkg_resources.resource_filename('notedown',
                                      'templates/markdown.tpl')
markdown_figure_template \
    = pkg_resources.resource_filename('notedown',
                                      'templates/markdown_outputs.tpl')


def strip(notebook):
    """Remove outputs from a notebook."""
    for cell in notebook.cells:
        cell.outputs = []


def run(notebook):
    executor = ExecutePreprocessor()
    notebook, resources = executor.preprocess(notebook, resources=None)


examples = """
Example usage of notedown
-------------------------

Convert markdown into notebook:

    notedown input.md > output.ipynb

    notedown input.md --output output.ipynb


Convert a notebook into markdown, with outputs intact:

    notedown input.ipynb --from notebook --to markdown > output_with_outputs.md


Convert a notebook into markdown, stripping all outputs:

    notedown input.ipynb --from notebook --to markdown --strip > output.md


Strip the output cells from markdown:

    notedown with_output_cells.md --to markdown --strip > no_output_cells.md


Convert from markdown and execute using runipy:
{'fraction_non_alphanumeric': 0.09159112222569553, 'fraction_numerical': 0.0009377930603313535, 'mean_word_length': 2.7122969837587005, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7082599', 'n_tokens_mistral': 911, 'n_tokens_neox': 871, 'n_words': 251}