Properly handle blockquote blocks

This commit is contained in:
oobabooga 2023-04-16 18:00:12 -03:00
parent 27f3a78834
commit 5342f72968

View File

@ -33,15 +33,20 @@ def fix_newlines(string):
string = string.strip() string = string.strip()
return string return string
# This could probably be generalized and improved def replace_blockquote(m):
return m.group().replace('\n', '\n> ').replace('\\begin{blockquote}', '').replace('\\end{blockquote}', '')
def convert_to_markdown(string): def convert_to_markdown(string):
# Blockquote
pattern = re.compile(r'\\begin{blockquote}(.*?)\\end{blockquote}', re.DOTALL)
string = pattern.sub(replace_blockquote, string)
# Code
string = string.replace('\\begin{code}', '```') string = string.replace('\\begin{code}', '```')
string = string.replace('\\end{code}', '```') string = string.replace('\\end{code}', '```')
string = string.replace('\\begin{blockquote}', '> ')
string = string.replace('\\end{blockquote}', '')
string = re.sub(r"(.)```", r"\1\n```", string) string = re.sub(r"(.)```", r"\1\n```", string)
string = fix_newlines(string) string = fix_newlines(string)
return markdown.markdown(string, extensions=['fenced_code']) return markdown.markdown(string, extensions=['fenced_code'])