Skip to content

Commit

Permalink
Support for \begin{minted}{pycon} in LaTeX
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjc authored and asottile committed Aug 27, 2021
1 parent dc9c151 commit 9a76e4d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
12 changes: 12 additions & 0 deletions blacken_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
r'(?P<after>^(?P=indent)\\end{minted}\s*$)',
re.DOTALL | re.MULTILINE,
)
LATEX_PYCON_RE = re.compile(
r'(?P<before>^(?P<indent> *)\\begin{minted}{pycon}\n)'
r'(?P<code>.*?)'
r'(?P<after>^(?P=indent)\\end{minted}\s*$)',
re.DOTALL | re.MULTILINE,
)
PYTHONTEX_LANG = r'(?P<lang>pyblock|pycode|pyconsole|pyverbatim)'
PYTHONTEX_RE = re.compile(
rf'(?P<before>^(?P<indent> *)\\begin{{{PYTHONTEX_LANG}}}\n)'
Expand Down Expand Up @@ -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

Expand Down
44 changes: 42 additions & 2 deletions tests/blacken_docs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 9a76e4d

Please sign in to comment.