Skip to content

Commit 5932017

Browse files
committed
issue2551096 - enable markdown autolink for email and bare url's.
Modify raw markdown adding appropriate link markers '<' and '>' on the fly so bare urls and email addreses are recognized as explicit links by markdown backends. Patch by Cedric Krier.
1 parent 7fc1dfa commit 5932017

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

CHANGES.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Fixed:
4141
Meerwald)
4242
- issue2551092 - fix crash bug by aligning
4343
roundup.anypy.email_.decode_header with stdlib email.header and
44-
convert string to bytes for python 3.
44+
convert string to bytes for python 3. (Cedric Krier)
4545
- issue2551097 - fix underlying bug in use of fenced codeblocks with
4646
markdown2. Fix for issue2551093 to prevent exception trigger.
4747
(patch: Cedric Krier)
@@ -60,6 +60,9 @@ Features:
6060
- issue2551094 - add new markdown config.ini setting to allow embedded
6161
newlines to cause a linebreak same as GitHub Flavored Markdown.
6262
(Patch: Cedric Krier; Doc change/checkin John Rouillard)
63+
- issue2551096 - enable markdown autolink for email and bare url's.
64+
Modify raw markdown adding appropriate link markers on the fly.
65+
(Cedric Krier)
6366

6467
2020-07-13 2.0.0
6568

roundup/cgi/templating.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,23 @@ def _hyper_repl_rst(self, match):
16211621
return match.group(0)
16221622

16231623
def _hyper_repl_markdown(self, match):
1624+
for group in ['url', 'email']:
1625+
if match.group(group):
1626+
start = match.start(group) - 1
1627+
end = match.end(group)
1628+
if start >= 0:
1629+
prefix = match.string[start]
1630+
if end < len(match.string):
1631+
suffix = match.string[end]
1632+
if (prefix, suffix) in {
1633+
('<', '>'),
1634+
('(', ')'),
1635+
}:
1636+
continue
1637+
if prefix == '(' and match.string[end - 1] == ')':
1638+
continue
1639+
s = match.group(group)
1640+
return '<%s>' % s
16241641
if match.group('id') and len(match.group('id')) < 10:
16251642
return self._hyper_repl_item(match,'[%(item)s](%(cls)s%(id)s)')
16261643
else:

test/test_templating.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,33 @@ def test_markdown_break_on_newline(self):
493493
m = p.markdown()
494494
self.assertEqual(0, m.count('<br'))
495495

496+
def test_markdown_hyperlinked_url(self):
497+
# classic markdown does not emit a \n at end of rendered string
498+
# so rstrip \n.
499+
p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'http://example.com/'))
500+
m = p.markdown(hyperlink=1)
501+
self.assertEqual(m.rstrip('\n'), '<p><a href="http://example.com/">http://example.com/</a></p>')
502+
503+
p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'<http://example.com/>'))
504+
m = p.markdown(hyperlink=1)
505+
self.assertEqual(m.rstrip('\n'), '<p><a href="http://example.com/">http://example.com/</a></p>')
506+
507+
p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'![](http://example.com/)'))
508+
m = p.markdown(hyperlink=1)
509+
self.assertIn(m, [
510+
'<p><img src="http://example.com/" alt=""/></p>\n',
511+
'<p><img src="http://example.com/" alt="" /></p>\n',
512+
'<p><img src="http://example.com/" alt=""></p>\n',
513+
'<p><img alt="" src="http://example.com/" /></p>', # markdown
514+
])
515+
516+
p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'An URL http://example.com/ with text'))
517+
m = p.markdown(hyperlink=1)
518+
self.assertEqual(m.rstrip('\n'), '<p>An URL <a href="http://example.com/">http://example.com/</a> with text</p>')
519+
520+
p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'An URL https://example.com/path with text'))
521+
m = p.markdown(hyperlink=1)
522+
self.assertEqual(m.rstrip('\n'), '<p>An URL <a href="https://example.com/path">https://example.com/path</a> with text</p>')
496523

497524
@skip_mistune
498525
class MistuneTestCase(TemplatingTestCase, MarkdownTests) :

0 commit comments

Comments
 (0)