Skip to content

Commit

Permalink
Allow options for LaTeX minted blocks (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjc committed Jan 27, 2024
1 parent 67a0f76 commit 7c8d70f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -7,6 +7,10 @@ Changelog

Thanks to Joaquim Esteves in `PR #278 <https://github.com/adamchainz/blacken-docs/pull/278>`__.

* Allow options in LaTeX minted blocks.

Thanks to Peter Cock in `PR #313 <https://github.com/adamchainz/blacken-docs/pull/313>`__.

1.16.0 (2023-08-16)
-------------------

Expand Down
4 changes: 2 additions & 2 deletions src/blacken_docs/__init__.py
Expand Up @@ -66,13 +66,13 @@
rf"^{re.escape(PYCON_CONTINUATION_PREFIX)}( |$)",
)
LATEX_RE = re.compile(
r"(?P<before>^(?P<indent> *)\\begin{minted}{python}\n)"
r"(?P<before>^(?P<indent> *)\\begin{minted}(\[.*?\])?{python}\n)"
r"(?P<code>.*?)"
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<before>^(?P<indent> *)\\begin{minted}(\[.*?\])?{pycon}\n)"
r"(?P<code>.*?)"
r"(?P<after>^(?P=indent)\\end{minted}\s*$)",
re.DOTALL | re.MULTILINE,
Expand Down
28 changes: 26 additions & 2 deletions tests/test_blacken_docs.py
Expand Up @@ -178,6 +178,30 @@ def test_format_src_latex_minted():
)


def test_format_src_latex_minted_opt():
before = (
"maths!\n"
"\\begin{minted}[mathescape]{python}\n"
"# Returns $\\sum_{i=1}^{n}i$\n"
"def sum_from_one_to(n):\n"
" r = range(1, n+1)\n"
" return sum(r)\n"
"\\end{minted}\n"
"done"
)
after, _ = blacken_docs.format_str(before, BLACK_MODE)
assert after == (
"maths!\n"
"\\begin{minted}[mathescape]{python}\n"
"# Returns $\\sum_{i=1}^{n}i$\n"
"def sum_from_one_to(n):\n"
" r = range(1, n + 1)\n"
" return sum(r)\n"
"\\end{minted}\n"
"done"
)


def test_format_src_latex_minted_indented():
# Personaly I would have minted python code all flush left,
# with only the Python code's own four space indentation:
Expand All @@ -203,7 +227,7 @@ def test_format_src_latex_minted_indented():
def test_format_src_latex_minted_pycon():
before = (
"Preceeding text\n"
"\\begin{minted}{pycon}\n"
"\\begin{minted}[gobble=2,showspaces]{pycon}\n"
">>> print( 'Hello World' )\n"
"Hello World\n"
"\\end{minted}\n"
Expand All @@ -212,7 +236,7 @@ def test_format_src_latex_minted_pycon():
after, _ = blacken_docs.format_str(before, BLACK_MODE)
assert after == (
"Preceeding text\n"
"\\begin{minted}{pycon}\n"
"\\begin{minted}[gobble=2,showspaces]{pycon}\n"
'>>> print("Hello World")\n'
"Hello World\n"
"\\end{minted}\n"
Expand Down

0 comments on commit 7c8d70f

Please sign in to comment.