Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for \begin{minted}{pycon} in LaTeX #112

Merged
merged 1 commit into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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