Skip to content

Commit

Permalink
refactor: easier to let tempfile delete the file
Browse files Browse the repository at this point in the history
I'm not sure why I set delete=False and then handled deletion myself...
  • Loading branch information
Ned Batchelder committed Oct 12, 2023
1 parent 435456e commit 402c30c
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions src/scriv/format_rst.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""reStructuredText knowledge for scriv."""

import os
import re
import tempfile
from typing import Optional
Expand Down Expand Up @@ -145,24 +144,20 @@ def format_sections(
def convert_to_markdown(
self, text: str
) -> str: # noqa: D102 (inherited docstring)
rst_file = None
try:
with tempfile.NamedTemporaryFile(
mode="w", prefix="scriv_rst_", delete=False
) as rst_file:
rst_file.write(text)
rst_file.flush()
ok, output = run_command(
"pandoc -frst -tmarkdown_strict "
+ "--markdown-headings=atx --wrap=none "
+ "--fail-if-warnings "
+ rst_file.name
with tempfile.NamedTemporaryFile(
mode="w",
prefix="scriv_rst_",
) as rst_file:
rst_file.write(text)
rst_file.flush()
ok, output = run_command(
"pandoc -frst -tmarkdown_strict "
+ "--markdown-headings=atx --wrap=none "
+ "--fail-if-warnings "
+ rst_file.name
)
if not ok:
raise ScrivException(
f"Couldn't convert ReST to Markdown: {output!r}"
)
if not ok:
raise ScrivException(
f"Couldn't convert ReST to Markdown: {output!r}"
)
return output.replace("\r\n", "\n")
finally:
if rst_file is not None:
os.unlink(rst_file.name)
return output.replace("\r\n", "\n")

0 comments on commit 402c30c

Please sign in to comment.