Skip to content

Commit b0cb95d

Browse files
committed
refactor: removed special handling of qmd cell options, since # | are supported
1 parent 93f3d2a commit b0cb95d

File tree

3 files changed

+16
-58
lines changed

3 files changed

+16
-58
lines changed

nbqa/replace_source.py

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,45 +28,6 @@
2828
SEPARATOR = {True: MARKDOWN_SEPARATOR, False: CODE_SEPARATOR}
2929

3030

31-
def _restore_quarto_cell_options(og_lines: list[str], lines: list[str]) -> list[str]:
32-
"""
33-
Restore the cell option comments #| at the start of the cell.
34-
35-
This comment like is typically changed to '# |' by third party tools.
36-
37-
This is for the cell options in quarto format as described at
38-
https://quarto.org/docs/reference/cells/cells-jupyter.html.
39-
40-
Parameters
41-
----------
42-
og_lines
43-
Original input lines.
44-
lines
45-
Lines after all formatting and magics restored.
46-
47-
Returns
48-
-------
49-
list[str]
50-
Lines with leading '# |' restored to '#|'.
51-
"""
52-
restored_lines = []
53-
i = 0
54-
# iteration logic should be safe since all cell options are at the start
55-
# and single lines so shouldn't suffer multi-line formatting before
56-
# breaking out
57-
for i, line in enumerate(lines):
58-
if not line.startswith("# |") or not og_lines[i].startswith("#|"):
59-
# only leading '#|' are cell options
60-
# if we encounter a line without, then all following are not
61-
# we also skip if the formatting tool hasn't changed '#|' to '# |'
62-
# since that means the formatter is handling qmd lines properly
63-
restored_lines.append(line)
64-
break
65-
restored_lines.append("#|" + line[3:])
66-
restored_lines.extend(lines[i + 1 :])
67-
return restored_lines
68-
69-
7031
def _restore_semicolon(
7132
source: str,
7233
cell_number: int,
@@ -317,8 +278,6 @@ def mutate( # pylint: disable=too-many-locals,too-many-arguments
317278
)
318279
if not new_source:
319280
cells_to_remove.append(cell_number)
320-
if notebook.endswith(".qmd"):
321-
new_source = _restore_quarto_cell_options(cell["source"], new_source)
322281
cell["source"] = new_source
323282

324283
if original_notebook_json == notebook_json:
@@ -418,8 +377,6 @@ def diff( # pylint: disable=too-many-arguments
418377
newlinesbefore[python_file],
419378
newlinesafter[python_file],
420379
)
421-
if notebook.endswith(".qmd"):
422-
new_source = _restore_quarto_cell_options(cell["source"], new_source)
423380
cell["source"][-1] += "\n"
424381
if new_source:
425382
new_source[-1] += "\n"

tests/data/notebook_for_testing.qmd

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ jupyter:
1818

1919
```{python}
2020
#| label: fig-polar
21-
#| fig-cap: A line plot on a polar axis
22-
# Should not be changed back to #|
23-
# This is a comment
24-
# | Looks like a cell option, but treat like
25-
# comment since not at top
21+
# | fig-cap: A line plot on a polar axis
22+
#| Additional content
23+
# This is a comment that stops cell options
24+
#| fig-subcap: A line plot on a polar axis
25+
# | Additional content
2626
2727
import numpy as np
28-
import matplotlib.pyplot as plt
28+
import matplotlib.pyplot as plt
2929
30-
r = np.arange(0,2,0.01)
31-
theta = 2*np.pi*r
32-
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
30+
r = np.arange(0, 2, 0.01)
31+
theta = 2 * np.pi * r
32+
fig, ax = plt.subplots(subplot_kw={"projection": "polar"})
3333
ax.plot(theta, r)
34-
ax.set_rticks([0.5,1,1.5,2])
34+
ax.set_rticks([0.5, 1, 1.5, 2])
3535
ax.grid(True)
3636
plt.show()
3737
```

tests/test_jupytext.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,12 @@ def test_qmd(tmp_test_data: Path) -> None:
234234
"""\n"""
235235
"""```{python}\n"""
236236
"""#| label: fig-polar\n"""
237-
"""#| fig-cap: A line plot on a polar axis\n"""
238-
"""# Should not be changed back to #|\n"""
239-
"""# This is a comment\n"""
240-
"""# | Looks like a cell option, but treat like\n"""
241-
"""# comment since not at top\n"""
237+
"""#| fig-cap: |-\n"""
238+
"""#| A line plot on a polar axis\n"""
239+
"""#| Additional content\n"""
240+
"""# This is a comment that stops cell options\n"""
241+
"""# | fig-subcap: A line plot on a polar axis\n"""
242+
"""# | Additional content\n"""
242243
"""\n"""
243244
"""import numpy as np\n"""
244245
"""import matplotlib.pyplot as plt\n"""

0 commit comments

Comments
 (0)