strip the correct fence

This commit is contained in:
Paul Gauthier 2023-07-13 15:54:54 -07:00
parent 12c3c1ed68
commit 14ce1d43c7

View file

@ -185,7 +185,7 @@ def replace_most_similar_chunk(whole, part, replace):
return modified_whole return modified_whole
def strip_quoted_wrapping(res, fname=None): def strip_quoted_wrapping(res, fname=None, fence=None):
""" """
Given an input string which may have extra "wrapping" around it, remove the wrapping. Given an input string which may have extra "wrapping" around it, remove the wrapping.
For example: For example:
@ -199,12 +199,15 @@ def strip_quoted_wrapping(res, fname=None):
if not res: if not res:
return res return res
if not fence:
fence = ("```", "```")
res = res.splitlines() res = res.splitlines()
if fname and res[0].strip().endswith(Path(fname).name): if fname and res[0].strip().endswith(Path(fname).name):
res = res[1:] res = res[1:]
if res[0].startswith("```") and res[-1].startswith("```"): if res[0].startswith(fence[0]) and res[-1].startswith(fence[1]):
res = res[1:-1] res = res[1:-1]
res = "\n".join(res) res = "\n".join(res)
@ -214,9 +217,9 @@ def strip_quoted_wrapping(res, fname=None):
return res return res
def do_replace(fname, content, before_text, after_text): def do_replace(fname, content, before_text, after_text, fence=None):
before_text = strip_quoted_wrapping(before_text, fname) before_text = strip_quoted_wrapping(before_text, fname, fence)
after_text = strip_quoted_wrapping(after_text, fname) after_text = strip_quoted_wrapping(after_text, fname, fence)
fname = Path(fname) fname = Path(fname)
# does it want to make a new file? # does it want to make a new file?