You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2013/02/21 21:43:08 UTC
[2/2] git commit: [#5358] Markdown autolink pattern handles escaped
chars now
Updated Branches:
refs/heads/db/5358b [created] a3e82f03e
[#5358] Markdown autolink pattern handles escaped chars now
In contrast with last attempt at this, preceding whitespace
(or beginning of line) is required so it doesn't break regular
link formats.
Added tests for regular link formats, and more tests for the
autolinking.
Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/a3e82f03
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/a3e82f03
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/a3e82f03
Branch: refs/heads/db/5358b
Commit: a3e82f03e944ae4206e01f65790c187d3c8fbef6
Parents: 8b8b483
Author: Dave Brondsema <db...@geek.net>
Authored: Thu Feb 21 20:39:43 2013 +0000
Committer: Dave Brondsema <db...@geek.net>
Committed: Thu Feb 21 20:39:43 2013 +0000
----------------------------------------------------------------------
Allura/allura/lib/markdown_extensions.py | 15 +++++++++-
Allura/allura/tests/test_globals.py | 36 ++++++++++++++++++++-----
2 files changed, 42 insertions(+), 9 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/a3e82f03/Allura/allura/lib/markdown_extensions.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/markdown_extensions.py b/Allura/allura/lib/markdown_extensions.py
index c004db0..2561704 100644
--- a/Allura/allura/lib/markdown_extensions.py
+++ b/Allura/allura/lib/markdown_extensions.py
@@ -38,7 +38,8 @@ class ForgeExtension(markdown.Extension):
md.preprocessors['fenced-code'] = FencedCodeProcessor()
md.preprocessors.add('plain_text_block', PlainTextPreprocessor(md), "_begin")
md.preprocessors.add('macro_include', ForgeMacroIncludePreprocessor(md), '_end')
- md.inlinePatterns['autolink_1'] = AutolinkPattern(r'(http(?:s?)://[a-zA-Z0-9./\-_0%?&=+#;~:]+)')
+ # this has to be before the 'escape' processor, otherwise weird placeholders are inserted for escaped chars within urls, and then the autolink can't match the whole url
+ md.inlinePatterns.add('autolink_without_brackets', AutolinkPattern(r'(http(?:s?)://[a-zA-Z0-9./\-\\_%?&=+#;~:]+)', md), '<escape')
# replace the link pattern with our extended version
md.inlinePatterns['link'] = ForgeLinkPattern(markdown.inlinepatterns.LINK_RE, md, ext=self)
md.inlinePatterns['short_reference'] = ForgeLinkPattern(markdown.inlinepatterns.SHORT_REF_RE, md, ext=self)
@@ -274,12 +275,22 @@ class HTMLSanitizer(markdown.postprocessors.Postprocessor):
return unicode(p.output(), 'utf-8')
-class AutolinkPattern(markdown.inlinepatterns.LinkPattern):
+class AutolinkPattern(markdown.inlinepatterns.Pattern):
+
+ def __init__(self, pattern, markdown_instance=None):
+ markdown.inlinepatterns.Pattern.__init__(self, pattern, markdown_instance)
+ # override the complete regex, requiring the preceding text (.*?) to end
+ # with whitespace or beginning of line "\s|^"
+ self.compiled_re = re.compile("^(.*?\s|^)%s(.*?)$" % pattern,
+ re.DOTALL | re.UNICODE)
def handleMatch(self, mo):
old_link = mo.group(2)
result = markdown.util.etree.Element('a')
result.text = old_link
+ # since this is run before the builtin 'escape' processor, we have to do our own unescaping
+ for char in markdown.Markdown.ESCAPED_CHARS:
+ old_link = old_link.replace('\\' + char, char)
result.set('href', old_link)
return result
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/a3e82f03/Allura/allura/tests/test_globals.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_globals.py b/Allura/allura/tests/test_globals.py
index e7f420d..787dd2e 100644
--- a/Allura/allura/tests/test_globals.py
+++ b/Allura/allura/tests/test_globals.py
@@ -207,7 +207,7 @@ def test_markdown_toc():
@td.with_wiki
-def test_markdown_links():
+def test_wiki_artifact_links():
text = g.markdown.convert('See [18:13:49]')
assert 'See <span>[18:13:49]</span>' in text, text
with h.push_context('test', 'wiki', neighborhood='Projects'):
@@ -218,6 +218,15 @@ def test_markdown_links():
text = g.markdown.convert('See [test:wiki:Home]')
assert '<a class="alink" href="/p/test/wiki/Home/">[test:wiki:Home]</a>' in text, text
+def test_markdown_links():
+ text = g.markdown.convert('Read [here](http://foobar.sf.net/) about our project')
+ assert_in('href="http://foobar.sf.net/">here</a> about', text)
+
+ text = g.markdown.convert('Read [here](/p/foobar/blah) about our project')
+ assert_in('href="/p/foobar/blah">here</a> about', text)
+
+ text = g.markdown.convert('Read <http://foobar.sf.net/> about our project')
+ assert_in('href="http://foobar.sf.net/">http://foobar.sf.net/</a> about', text)
def test_markdown_and_html():
r = g.markdown_wiki.convert('<div style="float:left">blah</div>')
@@ -233,7 +242,6 @@ def test_markdown_within_html():
@td.with_wiki
def test_markdown_basics():
- 'Just a test to get coverage in our markdown extension'
with h.push_context('test', 'wiki', neighborhood='Projects'):
text = g.markdown.convert('# Foo!\n[Home]')
assert '<a class="alink" href=' in text, text
@@ -260,16 +268,30 @@ def foo(): pass
def test_markdown_autolink():
- #with h.set_context('test', 'wiki', neighborhood='Projects')
- text = g.markdown.convert('This is http://sf.net')
- assert '<a href=' in text, text
tgt = 'http://everything2.com/?node=nate+oostendorp'
s = g.markdown.convert('This is %s' % tgt)
assert_equal(
s, '<div class="markdown_content"><p>This is <a href="%s" rel="nofollow">%s</a></p></div>' % (tgt, tgt))
assert '<a href=' in g.markdown.convert('This is http://sf.net')
- assert_not_in('href', g.markdown.convert('literal `http://sf.net` literal'))
- assert_not_in('href', g.markdown.convert(' preformatted http://sf.net'))
+ # beginning of doc
+ assert_in('<a href=', g.markdown.convert('http://sf.net abc'))
+ # beginning of a line
+ assert_in('<br />\n<a href="http://', g.markdown.convert('foobar\nhttp://sf.net abc'))
+ # no conversion of these urls:
+ assert_in('a blahttp://sdf.com z',
+ g.markdown.convert('a blahttp://sdf.com z'))
+ assert_in('literal <code>http://sf.net</code> literal',
+ g.markdown.convert('literal `http://sf.net` literal'))
+ assert_in('<pre>preformatted http://sf.net\n</pre>',
+ g.markdown.convert(' :::text\n'
+ ' preformatted http://sf.net'))
+
+
+def test_markdown_autolink_with_escape():
+ # \_ is unnecessary but valid markdown escaping and should be considered as a regular underscore
+ # (it occurs during html2text conversion during project migrations)
+ r = g.markdown.convert('a http://www.phpmyadmin.net/home\_page/security/\#target b')
+ assert 'href="http://www.phpmyadmin.net/home_page/security/#target"' in r, r
def test_macro_projects():