From 9a76e4da12ef6f2c4e778bf0fe2b86a5d321ae4c Mon Sep 17 00:00:00 2001 From: Peter Cock
.*?)'
+ 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'