Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] runbot: add support for attributes on md url #1034

Draft
wants to merge 1 commit into
base: 18.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions runbot/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,23 @@ def code_remove(match):
text = re_icon.sub('<i class="fa fa-\\g<1>"></i>', text)

# links
re_links = re.compile(rf'{escape}\[(.+?){escape}\]{escape}\(((http|/).+?{escape})\)')
text = re_links.sub('<a href="\\g<2>">\\g<1></a>', text)
re_links = re.compile(
rf'{escape}\[(?P<name>.+){escape}?\]{escape}\((?P<url>(?:http|\/).+?{escape})\)(?:\{{(?P<attributes>.+?{escape})}})?'
)
def compile_url(match):
groups = match.groupdict()
attributes = ''
if groups['attributes'] and groups['attributes'].strip():
try:
attributes = ' '.join(
f'{split[0]}="{split[1]}"'
for attribute in groups['attributes'].strip().split(';')
if attribute and (split := attribute.strip().split(' ', 1))
)
except Exception:
_logger.warning('Invalid markdown url attributes; ignored (%s)', groups['attributes'])
return f'<a href="{groups["url"]}" {attributes}>{groups["name"]}</a>'
text = re_links.sub(compile_url, text)

def code_replace(match):
return f'<code>{codes[int(match.group(1))]}</code>'
Expand Down
1 change: 1 addition & 0 deletions runbot/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
from . import test_upgrade
from . import test_dockerfile
from . import test_host
from . import test_utils
55 changes: 55 additions & 0 deletions runbot/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from odoo.tests.common import TransactionCase, mute_logger

from odoo.addons.runbot.common import pseudo_markdown


class TestUtils(TransactionCase):

def test_md_formatting(self):
self.assertEqual(
pseudo_markdown(
"**strong** ~~delete~~ __italic__ \n"
),
"<strong>strong</strong> "
"<del>delete</del> "
"<ins>italic</ins> "
"<br/>\n"
)

def test_md_icons(self):
self.assertEqual(
pseudo_markdown(
"@icon-star"
),
'<i class="fa fa-star"></i>'
)

def test_md_urls(self):
# Basic
self.assertEqual(
pseudo_markdown(
"[name](https://runbot.odoo.com)"
),
'<a href="https://runbot.odoo.com" >name</a>'
)
# Test with attributes
self.assertEqual(
pseudo_markdown(
"[name](https://runbot.odoo.com){target _blank}"
),
'<a href="https://runbot.odoo.com" target="_blank">name</a>'
)

# Invalid input
with mute_logger('odoo.addons.runbot.common'):
pseudo_markdown(
"[name](https://runbot.odoo.com){target}"
)

# Everything at once
self.assertEqual(
pseudo_markdown(
"[@icon-star](https://runbot.odoo.com){target _blank}"
),
'<a href="https://runbot.odoo.com" target="_blank"><i class="fa fa-star"></i></a>'
)