Skip to content

Commit 67b9619

Browse files
authored
Fix assign_last_line_into_variable (#17)
1 parent f405d29 commit 67b9619

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

sphinx_plotly_directive/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ def save_plotly_figure(fig, path):
1313

1414

1515
def assign_last_line_into_variable(code, variable_name):
16-
*rest, last = code.strip().split("\n")
17-
last = "{} = ".format(variable_name) + last
18-
return "\n".join([*rest, last])
16+
lines = code.split("\n")
17+
for idx in range(len(lines) - 1, -1, -1):
18+
if lines[idx].strip() != "":
19+
lines[idx] = "{} = ".format(variable_name) + lines[idx]
20+
break
21+
return "\n".join(lines)
1922

2023

2124
def create_directive_block(name, arguments, options, content):

tests/test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ def test_assign_last_line_into_variable():
2121
code = """
2222
a = 1
2323
a
24-
""".strip()
24+
"""
2525
expected = """
2626
a = 1
2727
b = a
28-
""".strip()
28+
"""
2929

3030
assert assign_last_line_into_variable(code, "b") == expected
3131

0 commit comments

Comments
 (0)