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(