From 514de022f44fef7071aaa894f70774a7f1ea6ba0 Mon Sep 17 00:00:00 2001 From: Xavier-Do Date: Mon, 10 May 2021 14:58:26 +0200 Subject: [PATCH] [FIX] runbot: fix markdown code When code blocks were containing markdown like text, the inside of the code block was also formated. This commit removes the code blocks before applying other formating and place them back at the end. closes #481 --- runbot/common.py | 26 +++++++++++++++++++------- runbot/tests/test_event.py | 13 +++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/runbot/common.py b/runbot/common.py index d6a485f0..27a1b5bc 100644 --- a/runbot/common.py +++ b/runbot/common.py @@ -122,12 +122,19 @@ def list_local_dbs(additionnal_conditions=None): def pseudo_markdown(text): text = utils.escape(text) + + # first, extract code blocs: + codes = [] + def code_remove(match): + codes.append(match.group(1)) + return f'{len(codes)-1}' + patterns = { - r'\*\*(.+?)\*\*': '\g<1>', - r'~~(.+?)~~': '\g<1>', # it's not official markdown but who cares - r'__(.+?)__': '\g<1>', # same here, maybe we should change the method name - r'`(.+?)`': '\g<1>', - r'\r?\n': '
', + r'`(.+?)`': code_remove, + r'\*\*(.+?)\*\*': '\\g<1>', + r'~~(.+?)~~': '\\g<1>', # it's not official markdown but who cares + r'__(.+?)__': '\\g<1>', # same here, maybe we should change the method name + r'\r?\n': '
', } for p, b in patterns.items(): @@ -135,9 +142,14 @@ def pseudo_markdown(text): # icons re_icon = re.compile(r'@icon-([a-z0-9-]+)') - text = re_icon.sub('', text) + text = re_icon.sub('', text) # links re_links = re.compile(r'\[(.+?)\]\((.+?)\)') - text = re_links.sub('\g<1>', text) + text = re_links.sub('\\g<1>', text) + + def code_replace(match): + return f'{codes[int(match.group(1))]}' + + text = re.sub(r'(\d+)', code_replace, text, flags=re.DOTALL) return text diff --git a/runbot/tests/test_event.py b/runbot/tests/test_event.py index c7def2ff..1bb0ebf3 100644 --- a/runbot/tests/test_event.py +++ b/runbot/tests/test_event.py @@ -91,6 +91,19 @@ class TestIrLogging(RunbotCase): 'Hello ' ) + log.message = 'a bit of code :\n`print(__name__)`' + self.assertEqual( + log._markdown(), + 'a bit of code :
print(__name__)' + ) + + log.message = 'a bit of __code__ :\n`print(__name__)` **but also** `print(__name__)`' + self.assertEqual( + log._markdown(), + 'a bit of code :
print(__name__) but also print(__name__)' + ) + + # test links log.message = 'This [link](https://wwww.somewhere.com) goes to somewhere and [this one](http://www.nowhere.com) to nowhere.' self.assertEqual(