diff --git a/README.md b/README.md index dc2a598..2f6fac5 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,15 @@ def hello(): \end{minted} ``` +(latex `pycon`) +```latex +\begin{minted}{pycon} +>>> def hello(): +... print("hello world") +... +\end{minted} +``` + (latex with pythontex) ```latex \begin{pycode} diff --git a/blacken_docs.py b/blacken_docs.py index ce112ad..1d96cd7 100644 --- a/blacken_docs.py +++ b/blacken_docs.py @@ -61,6 +61,12 @@ r'(?P^(?P=indent)\\end{minted}\s*$)', re.DOTALL | re.MULTILINE, ) +LATEX_PYCON_RE = re.compile( + r'(?P^(?P *)\\begin{minted}{pycon}\n)' + r'(?P.*?)' + r'(?P^(?P=indent)\\end{minted}\s*$)', + re.DOTALL | re.MULTILINE, +) PYTHONTEX_LANG = r'(?Ppyblock|pycode|pyconsole|pyverbatim)' PYTHONTEX_RE = re.compile( rf'(?P^(?P *)\\begin{{{PYTHONTEX_LANG}}}\n)' @@ -170,11 +176,17 @@ def _latex_match(match: Match[str]) -> str: code = textwrap.indent(code, match['indent']) return f'{match["before"]}{code}{match["after"]}' + def _latex_pycon_match(match: Match[str]) -> str: + code = _pycon_match(match) + code = textwrap.indent(code, match['indent']) + return f'{match["before"]}{code}{match["after"]}' + src = MD_RE.sub(_md_match, src) src = MD_PYCON_RE.sub(_md_pycon_match, src) src = RST_RE.sub(_rst_match, src) src = RST_PYCON_RE.sub(_rst_pycon_match, src) src = LATEX_RE.sub(_latex_match, src) + src = LATEX_PYCON_RE.sub(_latex_pycon_match, src) src = PYTHONTEX_RE.sub(_latex_match, src) return src, errors diff --git a/tests/blacken_docs_test.py b/tests/blacken_docs_test.py index 0c4d8e3..db516e3 100644 --- a/tests/blacken_docs_test.py +++ b/tests/blacken_docs_test.py @@ -72,8 +72,6 @@ def test_format_src_indented_markdown(): def test_format_src_latex_minted(): - # Nicer style to put the \begin and \end on new lines, - # but not actually required for the begin line before = ( 'hello\n' '\\begin{minted}{python}\n' @@ -113,6 +111,48 @@ def test_format_src_latex_minted_indented(): ) +def test_format_src_latex_minted_pycon(): + before = ( + 'Preceeding text\n' + '\\begin{minted}{pycon}\n' + ">>> print( 'Hello World' )\n" + 'Hello World\n' + '\\end{minted}\n' + 'Following text.' + ) + after, _ = blacken_docs.format_str(before, BLACK_MODE) + assert after == ( + 'Preceeding text\n' + '\\begin{minted}{pycon}\n' + '>>> print("Hello World")\n' + 'Hello World\n' + '\\end{minted}\n' + 'Following text.' + ) + + +def test_format_src_latex_minted_pycon_indented(): + # Nicer style to put the \begin and \end on new lines, + # but not actually required for the begin line + before = ( + 'Preceeding text\n' + ' \\begin{minted}{pycon}\n' + " >>> print( 'Hello World' )\n" + ' Hello World\n' + ' \\end{minted}\n' + 'Following text.' + ) + after, _ = blacken_docs.format_str(before, BLACK_MODE) + assert after == ( + 'Preceeding text\n' + ' \\begin{minted}{pycon}\n' + ' >>> print("Hello World")\n' + ' Hello World\n' + ' \\end{minted}\n' + 'Following text.' + ) + + def test_src_pythontex(tmpdir): before = ( 'hello\n'